REFACTOR: Better handling of latest card
This commit is contained in:
parent
85fe590fff
commit
031ad44e88
|
@ -72,8 +72,8 @@ class Card(object):
|
|||
except KeyError:
|
||||
raise InvalidCardType('Card class for type "{}" does\'t exist.'.format(card_type))
|
||||
|
||||
@staticmethod
|
||||
def get_type(card_class):
|
||||
@classmethod
|
||||
def get_type(cls, card_class=None):
|
||||
'''
|
||||
Return the type for a specific card class.
|
||||
|
||||
|
@ -82,7 +82,7 @@ class Card(object):
|
|||
:return: The card type
|
||||
:rtype: str
|
||||
'''
|
||||
return card_class.__name__[0:-4].lower()
|
||||
return (card_class or cls).__name__[0:-4].lower()
|
||||
|
||||
@classmethod
|
||||
def all(cls):
|
||||
|
|
|
@ -24,7 +24,7 @@ class CardReader(Thread):
|
|||
'''
|
||||
Thread class which reads RFID cards from the RFID reader.
|
||||
'''
|
||||
latest = {}
|
||||
latest = None
|
||||
|
||||
def __init__(self, core, stop_event):
|
||||
'''
|
||||
|
@ -77,11 +77,8 @@ class CardReader(Thread):
|
|||
else:
|
||||
LOGGER.info('Card is not registered, thus doing nothing')
|
||||
|
||||
CardReader.latest = {
|
||||
'time': time(),
|
||||
'uid': card.uid,
|
||||
'card': str(card)
|
||||
}
|
||||
card.scanned = time()
|
||||
CardReader.latest = card
|
||||
|
||||
|
||||
class PummeluffFrontend(pykka.ThreadingActor, core.CoreListener):
|
||||
|
|
|
@ -39,12 +39,25 @@ class LatestHandler(RequestHandler):
|
|||
'''
|
||||
Handle GET request.
|
||||
'''
|
||||
latest = CardReader.latest
|
||||
card = CardReader.latest
|
||||
|
||||
LOGGER.debug('Returning latest card with UID %s', latest.get('uid'))
|
||||
LOGGER.debug('Returning latest card %s', card)
|
||||
|
||||
data = {'success': True}
|
||||
data.update(latest)
|
||||
if card is None:
|
||||
data = {
|
||||
'success': False,
|
||||
'message': 'No card scanned yet'
|
||||
}
|
||||
else:
|
||||
data = {
|
||||
'success': True,
|
||||
'message': 'Scanned card found',
|
||||
'scanned': card.scanned,
|
||||
'uid': card.uid,
|
||||
'alias': card.alias,
|
||||
'type': card.get_type(),
|
||||
'parameter': card.parameter,
|
||||
}
|
||||
|
||||
self.set_header('Content-type', 'application/json')
|
||||
self.write(dumps(data))
|
||||
|
@ -78,6 +91,7 @@ class RegisterHandler(RequestHandler):
|
|||
self.set_header('Content-type', 'application/json')
|
||||
self.write({
|
||||
'success': True,
|
||||
'message': 'Card successfully registered',
|
||||
'card': str(card)
|
||||
})
|
||||
|
||||
|
|
Loading…
Reference in a new issue