-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensormonitor.py
283 lines (231 loc) · 8.28 KB
/
sensormonitor.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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import errno
import json
import logging
import os
import statistics
import threading
import time
import traceback
import requests
from paho.mqtt import client as mqtt_client
from paho.mqtt.client import MQTT_ERR_SUCCESS
from config import Config
from sensor import Sensor
class SensorMonitor:
mqtt_client = None
latest_data = None
sensors = None
lock = None
sender_session: requests.Session = None
@staticmethod
def send_data(data, url=None) -> bool:
if SensorMonitor.sender_session is None:
SensorMonitor.sender_session = requests.Session()
session = SensorMonitor.sender_session
if url is None:
url = Config.get("Server.Address")
if url is None:
return False
auth = None
if (
Config.get("Server.Username") is not None
and Config.get("Server.Password") is not None
):
auth = (
Config.get("Server.Username", None),
Config.get("Server.Password", None),
)
headers = Config.get("Server.Headers")
try:
response = session.post(url=url, json=data, auth=auth, headers=headers)
except requests.exceptions.RequestException as e:
logging.error("Exception while trying to send data")
logging.error(e)
return False
if response.status_code < 200 or response.status_code > 299:
logging.error(
"Received status code "
+ str(response.status_code)
+ " with body: "
+ response.text
)
return False
try:
json.loads(response.text)
except ValueError:
logging.error("Response could not be decoded: " + response.text)
return False
return True
@staticmethod
def aggregate_data(data, decimals):
value = round(statistics.mean(data), decimals)
if decimals == 0:
value = int(value)
return value
@staticmethod
def send_aggregated_data_mqtt(entries):
client = SensorMonitor.get_mqtt_client()
if client is None:
return False
for key in entries:
entry = entries[key]
if "precision" in entry:
precision = entry["precision"]
else:
precision = 0
topic = entry["topic"]
message = str(
SensorMonitor.aggregate_data(entry["values"].values(), precision)
)
if client is None:
logging.error("MQTT client is none!")
result = client.publish(topic, message)
status = result[0]
if status != 0:
logging.error("Failed to send mqtt message for " + topic)
result = client.loop()
if result != MQTT_ERR_SUCCESS:
logging.error("MQTT publishing error: " + str(result))
SensorMonitor.mqtt_client = None
@staticmethod
def get_mqtt_client():
client = SensorMonitor.mqtt_client
if isinstance(client, mqtt_client.Client) and client.is_connected():
can_loop = SensorMonitor.mqtt_client.loop()
if can_loop != MQTT_ERR_SUCCESS:
logging.error("MQTT looping error: " + str(can_loop))
client = None
if client is None:
client_id = "sensormonitor-mqtt-" + os.uname()[1] + "-"
os.getpid()
client = mqtt_client.Client(client_id)
SensorMonitor.mqtt_client = client
if isinstance(client, mqtt_client.Client) and not client.is_connected():
broker = Config.get("MQTT.Broker")
port = Config.get("MQTT.Port", 1883)
username = Config.get("MQTT.Username")
password = Config.get("MQTT.Password")
client.username_pw_set(username, password)
client.on_connect = SensorMonitor.on_mqtt_connect
client.connect(broker, port)
return SensorMonitor.mqtt_client
# noinspection PyUnusedLocal
@staticmethod
def on_mqtt_connect(client, userdata, flags, rc):
if rc == 0:
logging.info("Connected to MQTT broker!")
else:
logging.error("Failed to connect to MQTT broker, return code %d\n", rc)
SensorMonitor.mqtt_client = None
@staticmethod
def read_sensors():
data = []
for sensor in SensorMonitor.sensors:
if not sensor.should_read_now():
continue
value = sensor.read()
if value is None:
continue
data += value
with SensorMonitor.lock:
for entry in data:
key = entry["group"] + "/" + entry["name"]
if key not in SensorMonitor.latest_data:
SensorMonitor.latest_data[key] = entry
else:
for t in entry["values"]:
SensorMonitor.latest_data[key]["values"][t] = entry["values"][t]
return data
@staticmethod
def setup_sensors():
SensorMonitor.sensors = []
for data in Config.get("Sensors"):
sensor = Sensor(data)
if sensor.get_config("Active", True):
SensorMonitor.sensors.append(sensor)
@staticmethod
def get_process_lock_file():
return os.path.dirname(__file__) + "/.sensors.lock"
@staticmethod
def pid_exists(pid):
if pid < 0:
return False
if pid == 0:
raise ValueError("invalid PID 0")
try:
os.kill(pid, 0)
except OSError as err:
if err.errno == errno.ESRCH:
return False
elif err.errno == errno.EPERM:
return True
else:
raise
else:
return True
@staticmethod
def is_already_running():
filename = SensorMonitor.get_process_lock_file()
if not os.path.exists(filename):
return False
file = open(filename, "r")
pid = file.read()
file.close()
result = False
if pid.isnumeric():
procfile = f"/proc/{pid}/comm"
if os.path.exists(procfile):
file = open(procfile, "r")
name = file.read()
file.close()
result = name == "main.py"
if not result:
os.remove(filename)
return result
@staticmethod
def save_process_lock():
logging.info("Saving process lock")
file = open(SensorMonitor.get_process_lock_file(), "w")
file.write(str(os.getpid()))
file.close()
@staticmethod
def send_data_loop():
interval = Config.get_interval()
while True:
loop_started = time.time()
with SensorMonitor.lock:
entries = SensorMonitor.latest_data.copy()
SensorMonitor.latest_data = {}
try:
SensorMonitor.send_aggregated_data_mqtt(entries)
except Exception as e:
logging.error(e)
logging.error(traceback.format_exc())
pass
sleep_time = max([0, interval - time.time() + loop_started])
if sleep_time < 0.7:
logging.info("Sleeping for " + str(sleep_time))
time.sleep(sleep_time)
@staticmethod
def run():
if SensorMonitor.is_already_running():
logging.info("Aborting startup due to existing process")
return
SensorMonitor.save_process_lock()
SensorMonitor.latest_data = {}
SensorMonitor.lock = threading.Lock()
logging.info("Starting sensors")
SensorMonitor.setup_sensors()
logging.info("Starting sender thread")
SensorMonitor.sender_thread = threading.Thread(
target=SensorMonitor.send_data_loop
)
SensorMonitor.sender_thread.start()
logging.info("Started sender thread, proceeding to main loop")
while True:
loop_started = time.time()
SensorMonitor.read_sensors()
sleep_time = max([0, 1 - time.time() + loop_started])
if sleep_time < 0.7:
logging.info("Sleeping for " + str(sleep_time))
time.sleep(sleep_time)