REFACTOR: Better handling of latest card

This commit is contained in:
Dominique Barton 2019-02-21 15:04:06 +01:00
parent 85fe590fff
commit 031ad44e88
3 changed files with 24 additions and 13 deletions

View file

@ -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):

View file

@ -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):

View file

@ -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)
})