forked from zewelor/bt-mqtt-gateway
-
Notifications
You must be signed in to change notification settings - Fork 5
/
gateway.py
executable file
·91 lines (76 loc) · 2.23 KB
/
gateway.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python3
import sys
from exceptions import WorkerTimeoutError, DeviceTimeoutError
if sys.version_info < (3, 5):
print("To use this script you need python 3.5 or newer! got %s" % sys.version_info)
sys.exit(1)
import logger
logger.setup()
import logging
import argparse
import queue
from workers_queue import _WORKERS_QUEUE
from config import settings
from mqtt import MqttClient
from workers_manager import WorkersManager
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group()
group.add_argument(
"-d",
"--debug",
action="store_true",
default=False,
help="Set logging to output debug information",
)
group.add_argument(
"-q",
"--quiet",
action="store_true",
default=False,
help="Set logging to just output warnings and errors",
)
parser.add_argument(
"-s",
"--suppress-update-failures",
dest="suppress",
action="store_true",
default=False,
help="Suppress any errors regarding failed device updates",
)
parsed = parser.parse_args()
_LOGGER = logger.get()
if parsed.quiet:
_LOGGER.setLevel(logging.WARNING)
elif parsed.debug:
_LOGGER.setLevel(logging.DEBUG)
logger.enable_debug_formatter()
else:
_LOGGER.setLevel(logging.INFO)
logger.suppress_update_failures(parsed.suppress)
_LOGGER.info("Starting")
global_topic_prefix = settings["mqtt"].get("topic_prefix")
mqtt = MqttClient(settings["mqtt"])
manager = WorkersManager(settings["manager"])
manager.register_workers(global_topic_prefix).start(mqtt)
running = True
while running:
try:
mqtt.publish(_WORKERS_QUEUE.get(timeout=10).execute())
except queue.Empty: # Allow for SIGINT processing
pass
except (WorkerTimeoutError, DeviceTimeoutError) as e:
logger.log_exception(
_LOGGER,
str(e) if str(e) else "Timeout while executing worker command",
suppress=True,
)
except (KeyboardInterrupt, SystemExit):
running = False
_LOGGER.info(
"Finish current jobs and shut down. If you need force exit use kill"
)
except Exception as e:
logger.log_exception(
_LOGGER, "Fatal error while executing worker command: %s", type(e).__name__
)
raise e