-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdomd.py
executable file
·74 lines (56 loc) · 1.78 KB
/
domd.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
#!/usr/bin/env python
###
# Domotica monitoring daemon.
# responsible for all features which require regular polling of the PLC (i.e.
# everything we can't do from the web interface)
###
import daemon
import optparse
import time
import logging
import s7
import domotica.settings as settings
from pollers.alarmpoller import AlarmPoller
from pollers.temperaturepoller import TemperaturePoller
POLL_INTERVAL = 5
def poll(s7conn, pollers):
for poller in pollers:
poller.poll(s7conn)
def connect():
return s7.S7Comm(settings.PLC_IP, 1)
def setup_logger(options):
lvl = logging.INFO
if options.debug:
lvl = logging.DEBUG
fmt = "%(asctime)s:%(levelname)s:%(module)s:%(message)s"
logging.basicConfig(level=lvl, format=fmt)
def main():
parser = optparse.OptionParser()
parser.add_option("-f", "--foreground", action="store_true",
dest="foreground", help="Do not daemonize", default=False)
parser.add_option("-d", "--debug", action="store_true",
dest="debug", help="Debug logging", default=False)
(options, args) = parser.parse_args()
if not options.foreground:
d = daemon.DaemonContext(prevent_core=False)
d.open()
setup_logger(options)
logging.info("Starting domd...")
pollers = [
AlarmPoller(),
TemperaturePoller()
]
s7conn = connect()
while True:
try:
poll(s7conn, pollers)
except s7.S7Exception as e:
if e.errno() == s7.S7Exception.ERR_CONNECTION_CLOSED:
logging.warn("Connection lost. Reconnecting...")
s7conn = connect()
else:
logging.error("Unknown error: %s", e)
raise
time.sleep(POLL_INTERVAL)
if __name__ == "__main__":
main()