summaryrefslogtreecommitdiff
path: root/barcode_scanner_daemon.py
blob: 09139f7b0a2a03c0087d315f48f1fd6a081aefc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/python3


"""Executable to read the Key Events from the Barcode Scanner (root Priviledges necessary)"""


import evdev
import os

import iot_barcode_scanner.config as config


def main():

    try:
        # read config
        cfg = config.get_config()
        fifo_path = cfg["scanner"]["fifo_path"]
        event_device_path = cfg["scanner"]["evdev_path"]

        # prepare fifo
        os.makedirs(os.path.dirname(fifo_path), exist_ok=True)
        os.mkfifo(fifo_path)

        barcode_scanner = evdev.InputDevice(event_device_path)
        with barcode_scanner.grab_context():
            for event in barcode_scanner.read_loop():
                if event.type == evdev.ecodes.EV_KEY:
                    eventdata = evdev.categorize(event)
                    if eventdata.keystate:
                        with open(fifo_path, "w") as fifo:
                            fifo.write(eventdata.keycode)
                            fifo.flush()

    except KeyboardInterrupt:
        pass

    finally:
        os.remove(fifo_path)


if __name__ == "__main__":
    main()