Mercurial > hg > Applications > levistone
comparison levistone.py @ 0:099871090d7a draft default tip
Add edyReader scripts
py HG: added pifacedigitalio.py
author | innparusu |
---|---|
date | Wed, 13 Dec 2017 20:11:51 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:099871090d7a |
---|---|
1 #!/usr/bin/env python | |
2 | |
3 import nfc | |
4 import nfc.clf | |
5 import nfc.ndef | |
6 import binascii | |
7 | |
8 import os | |
9 import os.path | |
10 import sys | |
11 import pifacedigitalio | |
12 import threading | |
13 import time | |
14 from threading import Timer | |
15 | |
16 import ldapaccess | |
17 import logging | |
18 import logging.handlers | |
19 | |
20 logging.basicConfig(level=logging.INFO) | |
21 log = logging.getLogger('main') | |
22 path = os.path.join(os.environ['LOG_DIR'], 'scan_card.log') | |
23 handler = logging.handlers.TimedRotatingFileHandler( | |
24 path, | |
25 when="D", | |
26 interval=1, | |
27 backupCount=5 | |
28 ) | |
29 handler.setLevel(logging.INFO) | |
30 | |
31 formatter = logging.Formatter('%(levelname)s(%(name)s): %(asctime)s - %(message)s') | |
32 handler.setFormatter(formatter) | |
33 log.addHandler(handler) | |
34 | |
35 | |
36 class Reader: | |
37 def __init__(self): | |
38 # nfc library | |
39 self.system_code = 0xFE00 | |
40 self.service_code = { | |
41 "suica": 0x090f, | |
42 "univ": 0x50CB, | |
43 "edy": 0x110B, | |
44 "waon": 0x67CF, | |
45 "nanaco": 0x558B | |
46 } | |
47 | |
48 # piface | |
49 self.piface = pifacedigitalio.PiFaceDigital() | |
50 | |
51 # thread | |
52 self.event = threading.Event() | |
53 | |
54 self.ldapAccess = ldapaccess.LdapAccess() | |
55 | |
56 def turn_off_event(self): | |
57 self.turn_off() | |
58 | |
59 def balus(self, nfcId): | |
60 print(nfcId + '\n') | |
61 if self.ldapAccess.searchEdyId(nfcId): | |
62 self.turn_on() | |
63 | |
64 def card_connected(self, tag): | |
65 if tag.type != "Type3Tag": | |
66 return False | |
67 | |
68 try: | |
69 tag.idm, tag.pmm = tag.polling(self.system_code) | |
70 except nfc.tag.tt3.Type3TagCommandError as err: | |
71 log.error("polling error: " + str(err)) | |
72 return False | |
73 except Exception as e: | |
74 log.error("card read error: " + str(e)) | |
75 | |
76 for key in sorted(self.service_code.keys()): | |
77 | |
78 service_code = self.service_code[key] | |
79 sc_list = [nfc.tag.tt3.ServiceCode(service_code >> 6, service_code & 0x3f)] | |
80 try: | |
81 if tag.request_service(sc_list) == [0xFFFF]: | |
82 continue | |
83 except nfc.tag.tt3.Type3TagCommandError as err: | |
84 continue | |
85 except Exception as e: | |
86 log.error("card read error: " + str(e)) | |
87 continue | |
88 | |
89 if isinstance(tag, nfc.tag.tt3_sony.FelicaStandard) or isinstance(tag, nfc.tag.tt3_sony.FelicaMobile): | |
90 try: | |
91 bc = nfc.tag.tt3.BlockCode(0x00, service=0) | |
92 data = tag.read_without_encryption(sc_list, [bc]) | |
93 log.info("block: " + binascii.hexlify(data)) | |
94 except Exception as e: | |
95 log.error("card read error: " + str(e)) | |
96 | |
97 if key == "suica": | |
98 print("suica balance: (little endian)%s" % binascii.hexlify(data[10:12])) | |
99 elif key == "univ": | |
100 self.balus(binascii.hexlify(data[0:6])) | |
101 elif key == "edy": | |
102 self.balus(binascii.hexlify(data[2:10])) | |
103 elif key == "waon": | |
104 # self.balus(binascii.hexlify(data[2:10])) | |
105 print("waon balance: %s" % binascii.hexlify(data[0:24])) | |
106 elif key == "nanaco": | |
107 self.balus(binascii.hexlify(data[0:8])) | |
108 else: | |
109 log.error("error: tag isn't Type3Tag") | |
110 break | |
111 | |
112 return True | |
113 | |
114 def turn_on(self): | |
115 self.piface.output_pins[1].turn_on() | |
116 Timer(10, self.turn_off_event, ()).start() | |
117 | |
118 def turn_off(self): | |
119 self.piface.output_pins[1].turn_off() | |
120 | |
121 def run(self): | |
122 try: | |
123 clf = nfc.ContactlessFrontend('usb') | |
124 except IOError as error: | |
125 raise SystemExit(1) | |
126 try: | |
127 return clf.connect(rdwr={'on-connect': self.card_connected}) | |
128 except Exception as e: | |
129 log.error("card read error: " + str(e)) | |
130 return True | |
131 finally: | |
132 clf.close() | |
133 | |
134 | |
135 if __name__ == '__main__': | |
136 reader = Reader() | |
137 # reader.socket_connect() | |
138 | |
139 while reader.run(): | |
140 log.info("*** RESTART ***") |