1 | # -*- coding: utf-8 -*- |
||
2 | # |
||
3 | # Configuration |
||
4 | # ------------- |
||
5 | # |
||
6 | # 1. Define a new keyboard of type 'pn532' |
||
7 | # 2. Define inputPins section for that keyboard |
||
8 | # |
||
9 | # Sample: |
||
10 | # |
||
11 | # [keyboards] |
||
12 | # nfcreader = pn532 |
||
13 | # |
||
14 | # [nfcreader_keyboard] |
||
15 | # TODO |
||
16 | # device = tty:AMA0:pn532 |
||
17 | # |
||
18 | # [nfcreader_InputPins] |
||
19 | # 1234 = **623 |
||
20 | # #calls **623 when tag-ID 1234 is scanned, logs user1 for this action (NOT the ID itself unless you are using debug mode) |
||
21 | # |
||
22 | # [EVENT_OnKeyPressed_nfcreader.1234] |
||
23 | # 00 = call:**622 |
||
24 | # |
||
25 | # /TODO |
||
26 | # That's all... |
||
27 | # |
||
28 | # Hardware Connections |
||
29 | # -------------------- |
||
30 | # |
||
31 | # For this keyboard we are using UART connection |
||
32 | # Connect pn532 and Raspberry Pi as follows: |
||
33 | # PN532 -- Raspberry Pi |
||
34 | # VCC -- 5V |
||
35 | # GND -- GND |
||
36 | # RX -- GPIO 14 (TX) |
||
37 | # TX -- GPIO 15 (RX) |
||
38 | # |
||
39 | # IMPORTANT --- IMPORTANT ---- IMPORTANT --- IMPORTANT ---- IMPORTANT --- IMPORTANT |
||
40 | # TODO |
||
41 | # you will also need the libnfc and nfcpy for this to work |
||
42 | # |
||
43 | # 1) libnfc: |
||
44 | # sudo apt-get install autoconf libtool libpcsclite-dev libusb-dev |
||
45 | # cd /home/pi |
||
46 | # mkdir src |
||
47 | # cd src |
||
48 | # wget https://github.com/nfc-tools/libnfc/archive/master.zip |
||
49 | # rename master.zip libnfc.zip |
||
50 | # unzip libnfc.zip |
||
51 | # cd libnfc --> ANPASSEN!!!! |
||
52 | # sudo mkdir /etc/nfc |
||
53 | # sudo mkdir /etc/nfc/devices.d |
||
54 | # sudo cp contrib/libnfc/pn532_uart_on_rpi.conf.sample /etc/nfc/devices.d/pn532_uart_on_rpi.conf |
||
55 | # add the line |
||
56 | # allow_intrusive_scan = true <-- checken, ob das wirklich gebraucht wird und was das macht |
||
57 | # to file /etc/nfc/devices.d/pn532_uart_on_rpi.conf |
||
58 | # cd /home/pi/src/libnfc <-- anpassen |
||
59 | # touch NEWS |
||
60 | # touch README |
||
61 | # autoreconf -vis |
||
62 | # ./configure --with-drivers=pn532_uart --sysconfdir=/etc --prefix=/usr |
||
63 | # sudo make clean |
||
64 | # sudo make install all |
||
65 | # cd examples |
||
66 | # sudo ./nfc-poll |
||
67 | # hold a tag near the reader and you should get an output similar to this: |
||
68 | # SAMPLE-OUTPUT einfügen |
||
69 | # 2) nfcpy |
||
70 | # install following this: http://nfcpy.org/latest/topics/get-started.html#installation |
||
71 | # be sure that serial console is deactivated in kernel (cmdline.txt or raspi-config) |
||
72 | # test nfcpy using |
||
73 | # python tagtool.py --device tty:AMA0:pn532 |
||
74 | # you should see output similar to |
||
75 | # SAMPLE-OUTPUT einfügen |
||
76 | # copy source-dir to pythons libdir: |
||
77 | # sudo cp -r nfc /usr/local/lib/python2.7/dist-packages/nfc <--- CHECKEN! |
||
78 | # /TODO |
||
79 | |||
80 | import logging |
||
81 | logger = logging.getLogger(__name__) |
||
82 | logger.debug("%s loaded", __name__) |
||
83 | |||
84 | import threading |
||
85 | from doorpi.keyboard.AbstractBaseClass import KeyboardAbstractBaseClass, HIGH_LEVEL, LOW_LEVEL |
||
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
86 | import doorpi |
||
87 | import nfc |
||
88 | import time |
||
89 | |||
90 | def get(**kwargs): return pn532(**kwargs) |
||
91 | |||
92 | |||
93 | class pn532(KeyboardAbstractBaseClass): |
||
94 | name = 'pn532 nfc keyboard' |
||
95 | |||
96 | @property |
||
97 | def current_millisecond_timestamp(self): |
||
98 | return int(round(time.time() * 1000)) |
||
99 | |||
100 | @property |
||
101 | def in_bouncetime(self): |
||
102 | return self.last_key_time + self.bouncetime >= self.current_millisecond_timestamp |
||
103 | |||
104 | def pn532_recognized(self, tag): |
||
105 | try: |
||
106 | if self.in_bouncetime: |
||
107 | logger.debug('founded tag while bouncetime -> skip') |
||
108 | return |
||
109 | self.last_key_time = self.current_millisecond_timestamp |
||
110 | logger.debug("tag: %s", tag) |
||
111 | hmm = str(tag) |
||
112 | ID = str(hmm.split('ID=')[-1:])[2:-2] |
||
113 | logger.debug("ID: %s", ID) |
||
114 | if ID in self._InputPins: |
||
115 | logger.debug("ID gefunden: %s", ID) |
||
116 | self.last_key = ID |
||
117 | self._fire_OnKeyDown(self.last_key, __name__) |
||
118 | self._fire_OnKeyPressed(self.last_key, __name__) |
||
119 | self._fire_OnKeyUp(self.last_key, __name__) |
||
120 | doorpi.DoorPi().event_handler('OnFoundKnownTag', __name__) |
||
121 | logger.debug("last_key is %s", self.last_key) |
||
122 | except Exception as ex: |
||
123 | logger.exception(ex) |
||
124 | finally: |
||
125 | logger.debug("pn532_recognized thread ended") |
||
126 | |||
127 | def pn532_read(self): |
||
128 | try: |
||
129 | while not self._shutdown: |
||
130 | self.__clf.connect(rdwr={'on-connect': self.pn532_recognized}) |
||
131 | except Exception as ex: |
||
132 | logger.exception(ex) |
||
133 | finally: |
||
134 | logger.debug("pn532 thread ended") |
||
135 | |||
136 | def __init__(self, input_pins, output_pins, keyboard_name, conf_pre, conf_post, bouncetime, *args, **kwargs): |
||
0 ignored issues
–
show
|
|||
137 | self.keyboard_name = keyboard_name |
||
138 | self.last_key = "" |
||
139 | self.bouncetime = bouncetime |
||
140 | self.last_key_time = self.current_millisecond_timestamp |
||
141 | # auslesen aus ini: |
||
142 | section_name = conf_pre+'keyboard'+conf_post |
||
143 | self._device = doorpi.DoorPi().config.get_string_parsed(section_name, 'device', 'tty:AMA0:pn532') |
||
144 | self._InputPins = map(str.upper, input_pins) |
||
145 | self._InputPairs = {} |
||
146 | self.__clf = nfc.ContactlessFrontend(self._device) #init nfc-reader |
||
147 | for input_pin in self._InputPins: |
||
148 | self._register_EVENTS_for_pin(input_pin, __name__) |
||
149 | logger.debug("__init__ (input_pin = %s)", input_pin) |
||
150 | |||
151 | #doorpi.DoorPi().event_handler.register_event('OnFoundKnownTag', __name__) |
||
152 | self._shutdown = False |
||
153 | self._thread = threading.Thread(target = self.pn532_read) |
||
154 | self._thread.daemon = True |
||
155 | self._thread.start() |
||
156 | self.register_destroy_action() |
||
157 | |||
158 | def destroy(self): |
||
159 | if self.is_destroyed: return |
||
160 | logger.debug("destroy") |
||
161 | self.__clf.close() |
||
162 | self.__clf = None |
||
163 | self._shutdown = True |
||
164 | doorpi.DoorPi().event_handler.unregister_source(__name__, True) |
||
165 | self.__destroyed = True |
||
166 | |||
167 | def status_input(self, tag): |
||
168 | logger.debug("status_input for tag %s", tag) |
||
169 | if tag == self.last_key: |
||
170 | return True |
||
171 | else: |
||
172 | return False |
||
173 |