-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevldaemon.py
93 lines (69 loc) · 2.68 KB
/
evldaemon.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
92
93
import argparse
import asyncio
import logging
import logging.config
import socket
import evl.config as conf
import evl.connection as conn
import evl.event as ev
logger = logging.getLogger("evl")
class EvlDaemon:
def __init__(self, host: str, password: str, port: int, config: conf.ConfigSchema):
self.host = host
self.password = password
self.port = port
self.connection = None
if config is None:
raise ValueError("Invalid config value!")
self.config = config
self.event_queue = asyncio.Queue()
self.status = ev.Status()
# Assign zone and partition names as read from configuration file.
ev.EventManager.zones = self.config.zones
ev.EventManager.partitions = self.config.partitions
# TODO: Read command name, priority, login name, etc. overrides from config.
self.event_manager = ev.EventManager(self.event_queue, status=self.status)
self.event_manager.add_notifiers(self.config.notifiers)
self.event_manager.add_storages(self.config.storage)
self.heartbeats = self.config.heartbeats
self.listeners = conf.load_listeners(self.config.listeners, self.event_manager)
self.status.listeners = self.listeners
async def start(self):
logger.debug("Starting daemon...")
resolved = socket.gethostbyname(self.host)
self.connection = conn.Connection(
event_manager=self.event_manager, host=resolved, password=self.password
)
self.status.connection = {"hostname": resolved, "port": self.connection.port}
await asyncio.gather(
self.connection.start(),
self.event_manager.wait(),
*[listener.listen() for listener in self.listeners],
*[heartbeat.start() for heartbeat in self.heartbeats]
)
def stop(self):
logger.debug("Stopping daemon...")
self.connection.stop()
logger.debug("Daemon stopped.")
def main():
print("Welcome to EvlDaemon.")
parser = argparse.ArgumentParser()
parser.add_argument(
"-c", "--config", required=False, default="~/.config/evl_daemon/config.json"
)
options = parser.parse_args()
config = conf.read(options.config)
if config is None:
print("Unable to read configuration file. Exiting.")
exit(1)
logging.config.dictConfig(config.logging)
host = str(config.ip)
ed = EvlDaemon(host, config.password, config.port, config)
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(ed.start())
except KeyboardInterrupt:
logger.debug("Ctrl+C pressed, stopping...")
ed.stop()
if __name__ == "__main__":
main()