REFACTOR: Use new /latest/ API endpoint
This commit is contained in:
parent
64f7e9b27e
commit
0a76fbdcd6
|
@ -10,12 +10,12 @@ import os
|
|||
from mopidy import config, ext
|
||||
|
||||
from .frontend import PummeluffFrontend
|
||||
from .web import CardRequestHandler
|
||||
from .web import LatestScanHandler
|
||||
|
||||
|
||||
def app_factory(config, core):
|
||||
return [
|
||||
('/card/', CardRequestHandler, {'core': core})
|
||||
('/latest/', LatestScanHandler, {'core': core})
|
||||
]
|
||||
|
||||
|
||||
|
|
|
@ -13,21 +13,27 @@ import pykka
|
|||
from mopidy import core
|
||||
|
||||
from .rfid_reader import RFIDReader, ReadError
|
||||
from .cards import Card
|
||||
|
||||
LOGGER = getLogger(__name__)
|
||||
|
||||
LAST_UID = ''
|
||||
|
||||
|
||||
class CardReader(Thread):
|
||||
'''
|
||||
Thread class which reads RFID cards from the RFID reader.
|
||||
'''
|
||||
def __init__(self, stop_event):
|
||||
latest = {}
|
||||
|
||||
def __init__(self, core, stop_event):
|
||||
'''
|
||||
Class constructor.
|
||||
|
||||
:param threading.Event stop_event: The stop event
|
||||
'''
|
||||
super(CardReader, self).__init__()
|
||||
self.core = core
|
||||
self.stop_event = stop_event
|
||||
|
||||
def run(self):
|
||||
|
@ -47,15 +53,36 @@ class CardReader(Thread):
|
|||
|
||||
if now - prev_time > 1 or uid != prev_uid:
|
||||
LOGGER.info('Card with UID %s read', uid)
|
||||
self.handle_uid(uid)
|
||||
|
||||
prev_uid = uid
|
||||
prev_time = now
|
||||
prev_uid = uid
|
||||
|
||||
except ReadError:
|
||||
pass
|
||||
|
||||
reader.cleanup()
|
||||
|
||||
def handle_uid(self, uid):
|
||||
'''
|
||||
Handle the scanned card / retreived UID.
|
||||
|
||||
:param str uid: The UID
|
||||
'''
|
||||
card = Card(uid)
|
||||
|
||||
if card.registered:
|
||||
LOGGER.info('Card is registered, triggering action')
|
||||
card.action(mopidy_core=self.core)
|
||||
else:
|
||||
LOGGER.info('Card is not registered, doing nothing')
|
||||
|
||||
CardReader.latest = {
|
||||
'time': time(),
|
||||
'uid': card.uid,
|
||||
'card': str(card)
|
||||
}
|
||||
|
||||
|
||||
class PummeluffFrontend(pykka.ThreadingActor, core.CoreListener):
|
||||
'''
|
||||
|
@ -66,7 +93,7 @@ class PummeluffFrontend(pykka.ThreadingActor, core.CoreListener):
|
|||
super(PummeluffFrontend, self).__init__()
|
||||
self.core = core
|
||||
self.stop_event = Event()
|
||||
self.card_reader = CardReader(self.stop_event)
|
||||
self.card_reader = CardReader(core=core, stop_event=self.stop_event)
|
||||
|
||||
def on_start(self):
|
||||
'''
|
||||
|
|
|
@ -6,24 +6,22 @@ Python module for Mopidy Pummeluff web classes.
|
|||
from __future__ import absolute_import, unicode_literals
|
||||
|
||||
__all__ = (
|
||||
'Card',
|
||||
'LatestScanHandler',
|
||||
)
|
||||
|
||||
from time import time
|
||||
from json import dumps
|
||||
from logging import getLogger
|
||||
|
||||
from tornado.web import RequestHandler
|
||||
|
||||
from .cards import Card
|
||||
|
||||
from .frontend import CardReader
|
||||
|
||||
LOGGER = getLogger(__name__)
|
||||
|
||||
|
||||
class CardRequestHandler(RequestHandler):
|
||||
class LatestScanHandler(RequestHandler):
|
||||
'''
|
||||
Request handler for the card API endpoint.
|
||||
Request handler which returns the latest scanned card.
|
||||
'''
|
||||
last_scan = {}
|
||||
|
||||
|
@ -35,54 +33,14 @@ class CardRequestHandler(RequestHandler):
|
|||
'''
|
||||
self.core = core
|
||||
|
||||
def write_response(self, data={}, error=None):
|
||||
self.set_header('Content-type', 'application/json')
|
||||
|
||||
if error:
|
||||
self.set_status(400)
|
||||
|
||||
data.update({
|
||||
'success': not error,
|
||||
'message': error or 'Request successful'
|
||||
})
|
||||
|
||||
self.write(dumps(data))
|
||||
|
||||
def get(self):
|
||||
'''
|
||||
Handle GET request, which will simply respond with the last scanned
|
||||
Handle GET request, which will simply respond with the latest scanned
|
||||
card.
|
||||
'''
|
||||
last_scan = CardRequestHandler.last_scan
|
||||
LOGGER.debug('Returning last scanned card with UID %s', last_scan.get('uid'))
|
||||
self.write_response(data=last_scan)
|
||||
latest = CardReader.latest
|
||||
|
||||
def post(self):
|
||||
'''
|
||||
Handle POST request, which will do two things:
|
||||
LOGGER.debug('Returning latest card with UID %s', latest.get('uid'))
|
||||
|
||||
- Store the card for later use (e.g. a GET method)
|
||||
- Run the action linked to the card in case it's registered
|
||||
'''
|
||||
uid = self.request.body.strip()
|
||||
LOGGER.info('Scanned card with UID "%s"', uid)
|
||||
|
||||
if uid:
|
||||
card = Card(uid)
|
||||
|
||||
if card.registered:
|
||||
LOGGER.info('Card is registered, triggering action')
|
||||
card.action(mopidy_core=self.core)
|
||||
else:
|
||||
LOGGER.info('Card is not registered, doing nothing')
|
||||
|
||||
CardRequestHandler.last_scan = {
|
||||
'time': time(),
|
||||
'uid': card.uid,
|
||||
'card': str(card)
|
||||
}
|
||||
|
||||
self.write_response(data=CardRequestHandler.last_scan)
|
||||
|
||||
else:
|
||||
self.write_response(error='No UID in body found')
|
||||
self.set_header('Content-type', 'application/json')
|
||||
self.write(dumps(latest))
|
||||
|
|
Loading…
Reference in a new issue