{{ message }}
{% end %}diff --git a/minigrid/device_interface.py b/minigrid/device_interface.py index d938fa8..7c46c6e 100644 --- a/minigrid/device_interface.py +++ b/minigrid/device_interface.py @@ -21,6 +21,42 @@ def _wrap_binary(binary): return b'qS' + binary.hex().encode('ascii') + b'EL' +def erase_card(session, cache): + """Erase card.""" + sector_1 = b''.join(( + bytes(45), + )) + sector_2 = b''.join(( + bytes(32), + )) + naive_payload = sector_1 + sector_2 + actual_payload = b''.join(( + naive_payload[:15], + (sum(naive_payload[:15]) & 0xFF).to_bytes(1, 'big'), + naive_payload[15:30], + (sum(naive_payload[15:30]) & 0xFF).to_bytes(1, 'big'), + naive_payload[30:45], + (sum(naive_payload[30:45]) & 0xFF).to_bytes(1, 'big'), + naive_payload[45:60], + (sum(naive_payload[45:60]) & 0xFF).to_bytes(1, 'big'), + naive_payload[60:75], + (sum(naive_payload[60:75]) & 0xFF).to_bytes(1, 'big'), + naive_payload[75:77], + bytes(13), + (sum(naive_payload[75:77]) & 0xFF).to_bytes(1, 'big'), + )) + cache.set('device_info', _wrap_binary(actual_payload), 30) + write_result = OrderedDict() + write_result['card_type'] = 'Blank Card' + write_result['future_time'] = str(int(time.time())+30) + cache.set('write_info', json_encode(write_result), 30) + notify = OrderedDict() + notify['notification'] = 'Erasing Card...' + notify['type'] = 'alert-warning' + cache.set('notification', json_encode(notify), 30) + cache.delete('received_info') + + def write_vendor_card(session, cache, key, minigrid_id, payment_id, vendor): """Write information to a vendor ID card.""" card_produce_time = int(time.time()) diff --git a/minigrid/handlers.py b/minigrid/handlers.py index 15f4db1..34d6a7f 100644 --- a/minigrid/handlers.py +++ b/minigrid/handlers.py @@ -1,4 +1,5 @@ """Handlers for the URL endpoints.""" +import time from binascii import unhexlify from collections import OrderedDict from datetime import datetime, timedelta @@ -26,7 +27,7 @@ import tornado.ioloop from minigrid.device_interface import ( - write_maintenance_card_card, + write_maintenance_card_card, erase_card, write_vendor_card, write_customer_card, write_credit_card) import minigrid.error import minigrid.models as models @@ -52,6 +53,7 @@ 'B': 'Customer ID Card', 'C': 'Credit Card', 'D': 'Maintenance Card', + 'E': 'Blank Card', } @@ -60,6 +62,7 @@ 'B': 'Customer User ID', 'C': 'Credit Amount', 'D': 'Maintenance Card ID', + 'E': 'Card Type', } @@ -321,6 +324,12 @@ def get(self): http_protocol = 'https' if options.minigrid_https else 'http' self.render('cards.html', http_protocol=http_protocol) + @tornado.web.authenticated + def post(self): + """Erase card.""" + erase_card(self.session, cache) + self.redirect(f'/cards') + class MinigridHandler(BaseHandler): """Handlers for a minigrid view.""" @@ -807,12 +816,19 @@ def _pack_into_dict(session, binary): if system_id == 'up': logging.info(f'Operator Box is {system_id}') return json_encode(result) + else: + cache.set('device_active', 1, 10) + cache.set('received_info', json_encode(result), 10) card_type = sector_1[4:5].decode('ascii') logging.info(f'Card Type: {card_type}') try: result['Card Type'] = _card_type_dict[card_type] except KeyError: if card_type == '\x00': + found = OrderedDict() + found['Card Type'] = 'Blank Card' + found['Connected Device'] = device_address.hex() + cache.set('received_info', json_encode(found), 10) raise minigrid.error.CardReadError('This card appears blank') raise minigrid.error.CardReadError( f'This card appears to have the invalid card type {card_type}') @@ -1033,6 +1049,15 @@ def _verify_written_card(session): mc_maintenance_card_card_id=mcid, )) cache.delete('write_info') + elif type == 'E': # Blank card + future = write_result['future_time'] + logging.info(f'future: {future}') + if int(future) > int(time.time()): + notify['notification'] = 'Card Blank' + notify['type'] = 'alert-success' + cache.set('notification', json_encode(notify), 10) + logging.info('Card Erased') + cache.delete('write_info') else: # Error case for card type notify['notification'] = 'Invalid Card Type' diff --git a/minigrid/templates/cards.html b/minigrid/templates/cards.html index a9fb7d7..0510ecd 100644 --- a/minigrid/templates/cards.html +++ b/minigrid/templates/cards.html @@ -1,17 +1,36 @@ {% extends 'base.html' %} {% block body %} +
{{ message }}
{% end %}