Skip to content
Snippets Groups Projects
Commit 2df22ce4 authored by Johannes Walcher's avatar Johannes Walcher
Browse files

added check for valid data (not only any data)

parent 66fd254e
No related branches found
No related tags found
No related merge requests found
......@@ -3,7 +3,7 @@ from email.mime.text import MIMEText
from email.utils import formatdate
import smtplib
UNKNOWN_SENSOR_SUBJECT = "WARNING: Unconfigured Sensor ID"
UNKNOWN_SENSOR_SUBJECT = "WARNING: Unconfigured Sensor ID: {owid}"
UNKNOWN_SENSOR_BODY = """Hello Guys,
An unknown sensor has been connected to the temperature monitoring service.
......@@ -54,6 +54,20 @@ This is unlikely - please come and check
Regards, Temperature
"""
NO_VALID_DATA_SUBJECT = "WARNING: Garbage data"
NO_VALID_DATA_BODY = """Helly guys,
We have data on the line - but it fails even the most simple verification.
The last received line was:
{last_line}
please check if the controller is going haywire.
Regards, Temperature
"""
def init(monitor):
......@@ -80,9 +94,10 @@ class PluginMail:
msg['Subject'] = subject
msg['From'] = self.config['mail']['from']
if urgent:
msg['To'] = self.config['mail']['to_urgent']
recipients = self.config['mail']['to_urgent'].split(',')
else:
msg['To'] = self.config['mail']['to']
recipients = self.config['mail']['to'].split(',')
msg['To'] = ",".join([s.strip() for s in recipients])
msg['Date'] = formatdate(localtime=True)
print("Notification: {}".format(subject))
......@@ -97,15 +112,18 @@ class PluginMail:
self._mail_rate_limit[subject] = time.time()
smtp = smtplib.SMTP("mail.stusta.mhn.de")
#smtp.sendmail(msg['From'], msg['To'], msg.as_string())
#smtp.sendmail(msg['From'], recipients, msg.as_string())
smtp.quit()
async def err_nodata(self, **kwargs):
await self.send_mail(NO_DATA_SUBJECT, NO_DATA_BODY)
async def err_no_valid_data(self, **lwargs):
await self.send_mail(NO_VALID_DATA_SUBJECT, NO_VALID_DATA_BODY)
async def err_unknown_sensor(self, **kwargs):
await self.send_mail(
UNKNOWN_SENSOR_SUBJECT,
UNKNOWN_SENSOR_SUBJECT.format(**kwargs),
UNKNOWN_SENSOR_BODY.format(**kwargs))
async def err_problem_sensor(self, **kwargs):
......
......@@ -34,7 +34,7 @@ class Sensor:
self.temperature = None
self.last_update = 0
self.calibration = 0
self.valid = False
self.valid = True
try:
if owid in config:
......@@ -125,9 +125,14 @@ class TempMonitor:
Read the protocol, update the sensors or trigger a collectd update
"""
await self.reconnect()
last_valid_data_received = time.time()
line = ""
while True:
# Wait for the next line
if time.time() - last_valid_data_received > 1800:
self.call_plugin("err_no_valid_data", last_line=line)
try:
line = await asyncio.wait_for(
self._reader.readline(),
......@@ -146,6 +151,7 @@ class TempMonitor:
continue
#print("recv:", line)
if line == '':
# Block has ended
await self.store_sensors()
......@@ -158,6 +164,9 @@ class TempMonitor:
print("Invaid line received: {}\n{}".format(line, exc))
continue
## we have at least a valid line
last_valid_data_received = time.time()
sensor = self.sensors.get(owid, None)
if not sensor:
# If the sensor is new - notify the operators
......
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