This repository was archived by the owner on Apr 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.py
More file actions
123 lines (100 loc) · 4.52 KB
/
Program.py
File metadata and controls
123 lines (100 loc) · 4.52 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/python -tt
from daemonpy.daemon import Daemon
import Inventum as Inventum
import logging
import configparser
import sys
import json
import paho.mqtt.client as mqtt
class InventumProcessor(object):
def __init__(self):
self.client = None
self.inventum = None
self.mqtttopic = ''
def logging_setup(self, level, log_file, foreground):
# If we are running in the foreground we use stderr for logging, if running as forking daemon we use the logfile
if foreground:
logging.basicConfig(format='%(asctime)-15s %(funcName)s(%(lineno)d) - %(levelname)s: %(message)s',
stream=sys.stderr, level=level)
else:
logging.basicConfig(format='%(asctime)-15s %(funcName)s(%(lineno)d) - %(levelname)s: %(message)s',
filename=log_file, level=level)
def on_message(self, client, userdata, message):
payload = str(message.payload.decode("utf-8"))
logging.info('Received command: %s', payload)
if payload == 'FAN=1':
self.inventum.set_command_fan_high()
elif payload == 'FAN=0':
self.inventum.set_command_fan_auto()
elif payload == 'DATA=1':
self.inventum.set_command_data_start()
elif payload == 'DATA=0':
self.inventum.set_command_data_stop()
elif payload == 'QUIT':
self.inventum.interrupt()
else:
logging.error('Unknown command received: %s', payload)
def on_data(self, data):
json_data = json.dumps(data)
combinedtopic = self.mqtttopic + '/data'
logging.debug('Publishing data to MQTT on channel %s', combinedtopic)
self.client.publish(combinedtopic, json_data)
def run_process(self, foreground):
config = configparser.RawConfigParser()
config.read('/etc/inventumusb.conf')
mqttserver = config.get("mqtt", "server", fallback="localhost")
mqttport = config.getint("mqtt", "port", fallback=1883)
self.mqtttopic = config.get("mqtt", "topic", fallback="ventilation/inventum")
mqttclientid = config.get("mqtt", "clientid", fallback="inventum-usb")
mqttusername = config.get("mqtt", "username", fallback="")
mqttpasswd = config.get("mqtt", "password", fallback=None)
loglevel = config.get("inventum", "loglevel", fallback="INFO")
logfile = config.get("inventum", "logfile", fallback="/var/log/inventum.log")
device = config.get("inventum", "device", fallback="/dev/ttyACM0")
reset_after = config.getint("inventum", "reset", fallback=20)
numeric_level = getattr(logging, loglevel.upper(), None)
if not isinstance(numeric_level, int):
raise ValueError('Invalid log level: %s' % loglevel)
self.logging_setup(numeric_level, logfile, foreground)
try:
self.client = mqtt.Client(mqttclientid)
if mqttusername != "":
self.client.username_pw_set(mqttusername, mqttpasswd);
logging.debug("Set username -%s-, password -%s-", mqttusername, mqttpasswd)
self.client.connect(mqttserver, port=mqttport)
logging.info('Connected to MQTT %s:%s', mqttserver, mqttport)
self.client.on_message = self.on_message
logging.info('Waiting for commands on MQTT channel %s/commands', self.mqtttopic)
self.client.subscribe(self.mqtttopic + '/commands')
self.client.loop_start()
except Exception as e:
logging.error("%s:%s: %s", mqttserver, mqttport, e)
return 3
self.inventum = Inventum.Inventum(logging, device, reset_after)
self.inventum.on_data = self.on_data
self.inventum.start()
self.client.loop_stop()
return 0
class MyDaemon(Daemon):
def run(self):
_processor = InventumProcessor()
_processor.run_process(foreground=False)
if __name__ == "__main__":
if len(sys.argv) != 2:
print("usage: %s start|stop|restart|foreground" % sys.argv[0])
sys.exit(2)
if 'foreground' == sys.argv[1]:
processor = InventumProcessor()
ret_val = processor.run_process(foreground=True)
sys.exit(ret_val)
daemon = MyDaemon('/var/run/inventum.pid', '/dev/null', '/dev/null', '/dev/null')
if 'start' == sys.argv[1]:
daemon.start()
elif 'stop' == sys.argv[1]:
daemon.stop()
elif 'restart' == sys.argv[1]:
daemon.restart()
else:
print("Unknown command")
sys.exit(2)
sys.exit(0)