Completed
Push — master ( cac84e...bf8d57 )
by Thomas
8s
created

doorpi/keyboard/from_pn532.py (5 issues)

Severity
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 LOW_LEVEL imported from doorpi.keyboard.AbstractBaseClass
Loading history...
Unused HIGH_LEVEL imported from doorpi.keyboard.AbstractBaseClass
Loading history...
86
import doorpi
87
import nfc
88
89
90
def get(**kwargs): return pn532(**kwargs)
91
92
93
class pn532(KeyboardAbstractBaseClass):
94
    name = 'pn532 nfc keyboard'
95
96
    def pn532_recognized(self, tag):
97
        try:
98
            logger.debug("tag: %s", tag)
99
            hmm = str(tag)
100
            ID = str(hmm.split('ID=')[-1:])[2:-2]
101
            logger.debug("ID: %s", ID)
102
            if ID in self._InputPins:
103
                logger.debug("ID gefunden: %s", ID)
104
                self.last_key = ID
105
                self._fire_OnKeyDown(self.last_key, __name__)
106
                self._fire_OnKeyPressed(self.last_key, __name__)
107
                self._fire_OnKeyUp(self.last_key, __name__)
108
                doorpi.DoorPi().event_handler('OnFoundKnownTag', __name__)
109
                logger.debug("last_key is %s", self.last_key)
110
        except Exception as ex:
111
            logger.exception(ex)
112
        finally:
113
            logger.debug("pn532_recognized thread ended")
114
115
    def pn532_read(self):
116
        try:
117
            while not self._shutdown:
118
                self.__clf.connect(rdwr={'on-connect': self.pn532_recognized})
119
        except Exception as ex:
120
            logger.exception(ex)
121
        finally:
122
            logger.debug("pn532 thread ended")
123
124
    def __init__(self, input_pins, output_pins, keyboard_name, conf_pre, conf_post, *args, **kwargs):
0 ignored issues
show
The argument args seems to be unused.
Loading history...
The argument output_pins seems to be unused.
Loading history...
The argument kwargs seems to be unused.
Loading history...
125
        self.keyboard_name = keyboard_name
126
        self.last_key = ""
127
        self.last_key_time = 0
128
        # auslesen aus ini:
129
        section_name = conf_pre+'keyboard'+conf_post
130
        self._device = doorpi.DoorPi().config.get_string_parsed(section_name, 'device', 'tty:AMA0:pn532')
131
        self._InputPins = map(str.upper, input_pins)
132
        self._InputPairs = {}
133
        self.__clf = nfc.ContactlessFrontend(self._device) #init nfc-reader
134
        for input_pin in self._InputPins:
135
            self._register_EVENTS_for_pin(input_pin, __name__)
136
            logger.debug("__init__ (input_pin = %s)", input_pin)
137
138
        #doorpi.DoorPi().event_handler.register_event('OnFoundKnownTag', __name__)
139
        self._shutdown = False
140
        self._thread = threading.Thread(target = self.pn532_read)
141
        self._thread.daemon = True
142
        self._thread.start()
143
        self.register_destroy_action()
144
145
    def destroy(self):
146
        if self.is_destroyed: return
147
        logger.debug("destroy")
148
        self.__clf.close()
149
        self.__clf = None
150
        self._shutdown = True
151
        doorpi.DoorPi().event_handler.unregister_source(__name__, True)
152
        self.__destroyed = True
153
154
    def status_input(self, tag):
155
        logger.debug("status_input for tag %s", tag)
156
        if tag == self.last_key:
157
            return True
158
        else:
159
            return False
160