diff --git a/plugins/collectd.py b/plugins/collectd.py index 18ae0d6ad6cf145be473a74135908fe55f218f09..349ab00573ff44ac7769e1d4988fb5db5e8ed942 100644 --- a/plugins/collectd.py +++ b/plugins/collectd.py @@ -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) diff --git a/plugins/mail.py b/plugins/mail.py index 68bf81a36a8c0afc4358d8e60547c4e13014d733..f856488bd3233dd0197e83fda670056ed5c5adbd 100644 --- a/plugins/mail.py +++ b/plugins/mail.py @@ -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): diff --git a/ssn_tempermonitor.service b/ssn_tempermonitor.service index 745c7bae400c5db289f7c6c1eabfb27f487d5f7d..b5fc6840d0d6e2b22b163fd6054d94559d3321bb 100644 --- a/ssn_tempermonitor.service +++ b/ssn_tempermonitor.service @@ -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] diff --git a/tempermonitor.py b/tempermonitor.py index d61b4477e9dfa4712ddd46688d1ee794aebc939f..439bf5f21beb4b5d15b9e57edb0947dcc770df8f 100755 --- a/tempermonitor.py +++ b/tempermonitor.py @@ -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",