-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
executable file
·79 lines (54 loc) · 1.84 KB
/
run.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
#!/usr/bin/env python3
"""Run the HomeAgent as a service"""
import sys
import threading
import traceback
import logging
from service.states import ThreadSafeDict
from service.scheduler import Scheduler
from service.agent_args import parse_args
from service.agent import LOG_PREFIX, HomeAgent
from service.log import LOGGER
from service.const import DEBUG
from config import APP_NAME, Config, load_config
LOG_PREFIX = r"[HomeAgent]"
#########################################
def run_service(config: Config, _sensors=None):
"""Run Home Agent Service"""
LOGGER.info("%s is starting", LOG_PREFIX)
_state = ThreadSafeDict()
_running = threading.Event()
_running.set()
sched = Scheduler(_state, _running)
agent = HomeAgent(config, _running, sched, _sensors)
sched.run(agent.start)
sched.queue(agent.collector, 10)
sched.start()
LOGGER.info("%s Stopping", LOG_PREFIX)
# agent.stop()
if _running:
_running.clear()
LOGGER.info("%s has stopped", LOG_PREFIX)
#########################################
def main():
"""Main run function"""
LOGGER.info("Starting %s", APP_NAME)
_args = parse_args(sys.argv[1:], APP_NAME)
if _args.get(DEBUG):
level = logging.getLevelName("DEBUG")
LOGGER.setLevel(level)
LOGGER.debug("Debug enabled")
if _args.get("service") is False:
LOGGER.error("Must use -s argument to run as a service")
sys.exit(2)
try:
LOGGER.info("%s Loading config file: %s", LOG_PREFIX, _args.get("config"))
_config = Config(load_config(_args))
run_service(_config)
except Exception as err: # pylint: disable=broad-except
LOGGER.error(err)
LOGGER.error(traceback.format_exc())
LOGGER.info("Quit %s", APP_NAME)
#########################################
if __name__ == "__main__":
main()