Skip to content
Snippets Groups Projects
Commit 23ff6264 authored by Michael Loipführer's avatar Michael Loipführer
Browse files

Merge branch 'esptemper' of gitlab.stusta.de:stustanet/temperature-daemon into esptemper

parents f1c13e56 890ee77a
Branches esptemper
No related tags found
No related merge requests found
......@@ -51,9 +51,13 @@ class PluginCollectd:
interval,
timestamp,
value)
# print("Sending data:", data.strip())
self._writer.write(data.encode('utf-8'))
await self._writer.drain()
try:
#print("Sending data:", data.strip())
self._writer.write(data.encode('utf-8'))
await self._writer.drain()
except:
await self.reconnect()
return
try:
line = await asyncio.wait_for(self._reader.readline(), 1)
......
......@@ -126,14 +126,14 @@ class PluginMail:
# Ratelimit the emails
time_since_last_mail = time.time() - self._mail_rate_limit.get(subject, 0)
if time_since_last_mail < int(self.config['mail']['min_delay_between_messages']):
print("Not sending due to ratelimiting")
print("Not sending due to ratelimiting: %i", time_since_last_mail)
return
print("Body: {}".format(body))
self._mail_rate_limit[subject] = time.time()
smtp = smtplib.SMTP("mail.stusta.mhn.de")
# smtp.sendmail(msg['From'], recipients, msg.as_string())
smtp.sendmail(msg['From'], recipients, msg.as_string())
smtp.quit()
async def err_nodata(self, **kwargs):
......
......@@ -30,11 +30,13 @@ class PluginPrometheus:
addr=self.config["prometheus"].get('address', 'localhost'),
port=int(self.config["prometheus"]["port"])
)
print("started prometheus http server")
async def update_sensor_values(self, sensor):
"""
update
"""
print("updating prometheus metrics")
self.sensor_metrics.labels(sensor=sensor.name).set(sensor.temperature)
async def send_stats_graph(self, graph, stattype, stattime, statval):
......
......@@ -4,7 +4,7 @@ After=network.target
[Service]
WorkingDirectory=/usr/local/bin/temperature-daemonv2/
ExecStart=/usr/local/bin/temperature-daemonv2/tempermonitor.py
ExecStart=/usr/local/bin/temperature-daemonv2/venv/bin/python /usr/local/bin/temperature-daemonv2/tempermonitor.py tempermon_hugin.ini
Restart=on-failure
[Install]
......
......@@ -110,10 +110,16 @@ class TempMonitor:
"""
Connect to the ESP chip
"""
self._reader, self._writer = await serial_asyncio.open_serial_connection(
url=self.config['serial']['port'],
baudrate=self.config['serial']['baudrate'],
loop=self.loop)
try:
self._reader, self._writer = await serial_asyncio.open_serial_connection(
url=self.config['serial']['port'],
baudrate=self.config['serial']['baudrate'],
loop=self.loop)
except serial.SerialException:
print("Connection failed!")
self.loop.stop()
raise
# upon startup we only see garbage. (micropython starting up),
# also it will produce warnings if the recording is started in the middle
# of a message, so wait until the end of a message block to start the game
......@@ -133,17 +139,23 @@ class TempMonitor:
await self.reconnect()
last_valid_data_received = time.time()
line = ""
reconnected_on_error = False
while True:
# Wait for the next line
if time.time() - last_valid_data_received > 1800:
if time.time() - last_valid_data_received > 10:
await self.call_plugin("err_no_valid_data", last_line=line)
if not reconnected_on_error:
reconnected_on_error = True
await self.reconnect()
try:
line = await asyncio.wait_for(
self._reader.readline(),
timeout=int(self.config['serial']['timeout']))
print("Received: ", line)
except asyncio.TimeoutError:
print("No Data")
await self.call_plugin("err_nodata")
continue
except serial.SerialException as exc:
......@@ -154,13 +166,17 @@ class TempMonitor:
try:
line = line.decode('ascii').strip()
except UnicodeError:
print("Unicode error")
continue
# print("recv:", line)
if line == '':
# Block has ended
print("Done block, storing sensors")
await self.store_sensors()
print("Done")
continue
# Try to parse the line
try:
owid, temp = line.split(' ')
......@@ -171,15 +187,18 @@ class TempMonitor:
## we have at least a valid line
last_valid_data_received = time.time()
reconnected_on_error = False
sensor = self.sensors.get(owid, None)
if not sensor:
# If the sensor is new - notify the operators
print("Unknown sensor")
await self.call_plugin("err_unknown_sensor",
config=self._configname,
owid=owid,
temp=temp)
elif temp > 1000 or temp < -1000:
print("Sensor invalid")
sensor.valid = False
# if the sensor is giving bullshit data - notify the operators
await self.call_plugin("err_problem_sensor",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment