-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
69 lines (57 loc) · 1.9 KB
/
main.py
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import rpi_funcs as rpi
import io_funcs
from classes import AllowList, CoffeeMachine
import sys
import typing as tp
import logging
# set up logging
logging.basicConfig(
level=logging.DEBUG,
filename="daemon.log",
format="%(asctime)s %(levelname)s: %(message)s"
)
# load up configuration
config: tp.Dict[str, tp.Any] = io_funcs.read_config()
if not config:
logging.critical("Config load error, exiting")
sys.exit()
else:
logging.info("Config load successful")
# load up allow list
allow_list_data: tp.List[str] = io_funcs.read_allow_list()
if not allow_list_data:
logging.critical("Allow list load error, exiting")
sys.exit()
else:
logging.info("Allow list load successful")
# initialize an instance of AllowList object
allow_list = AllowList(allow_list_data, config["use_allow_list"])
# initialize an instance of CoffeeMachine object
coffee_machine = CoffeeMachine(
gpio_outputs=[0, 18, 0, 0, 0, 0, 0]
)
# entry point
if __name__ == "__main__":
logging.info("Daemon started")
# start the daemon
while True:
# poll NFC reader until input received
logging.info("Start polling NFC reader")
while True:
tag_id: str = rpi.poll_nfc_reader()
if tag_id:
logging.info(f"Found NFC tag with UID {tag_id}. Stopped polling")
break
# check if user is allowed to use the machine
if not allow_list.usage_allowed(tag_id):
logging.warning(f"UID {tag_id} is not in the allow list. Usage declined.")
continue
else:
logging.info(f"UID {tag_id} allowed. Continuing.")
# make a coffee if usage allowed
operation = coffee_machine.make_a_coffee()
if operation["success"]:
logging.info("Operation successful.")
else:
logging.error(f"Operation FAILED: {operation['message']}")
logging.info("Session over")