diff --git a/rattail_tempmon/client.py b/rattail_tempmon/client.py index abefb7d..c3d8b63 100644 --- a/rattail_tempmon/client.py +++ b/rattail_tempmon/client.py @@ -72,46 +72,60 @@ class TempmonClient(Daemon): # main loop: take readings, pause, repeat while True: + self.take_readings(client_uuid) + time.sleep(self.delay) - session = Session() + def take_readings(self, client_uuid): + """ + Take new readings for all enabled probes on this client. + """ + session = Session() + try: + client = session.query(tempmon.Client).get(client_uuid) + self.delay = client.delay or 60 + if client.enabled: + for probe in client.enabled_probes(): + self.take_reading(session, probe) + + except: + log.exception("Failed to read/record temperature data (but will keep trying)") + session.rollback() + + else: + # make sure we show as being online + if not client.online: + client.online = True try: - client = session.query(tempmon.Client).get(client_uuid) - self.delay = client.delay or 60 - if client.enabled: - for probe in client.enabled_probes(): - self.take_reading(session, probe) - + session.commit() except: log.exception("Failed to read/record temperature data (but will keep trying)") session.rollback() - else: - # make sure we show as being online - if not client.online: - client.online = True - try: - session.commit() - except: - log.exception("Failed to read/record temperature data (but will keep trying)") - session.rollback() - - finally: - session.close() - - time.sleep(self.delay) + finally: + session.close() def take_reading(self, session, probe): """ Take a single reading and add to Rattail database. """ reading = tempmon.Reading() - reading.client = probe.client - reading.probe = probe reading.degrees_f = self.read_temp(probe) - reading.taken = datetime.datetime.utcnow() - session.add(reading) - return reading + + # a reading of 185.0 °F indicates some sort of power issue. when this + # happens we log an error (which sends basic email) but do not record + # the temperature. that way the server doesn't see the 185.0 reading + # and send out a "false alarm" about the temperature being too high. + # https://www.controlbyweb.com/support/faq/temp-sensor-reading-error.html + if reading.degrees_f == 185.0: + log.error("got reading of 185.0 from probe: %s", probe.description) + + else: # we have a good reading + reading.client = probe.client + reading.probe = probe + reading.taken = datetime.datetime.utcnow() + session.add(reading) + return reading def read_temp(self, probe): """ @@ -126,7 +140,9 @@ class TempmonClient(Daemon): equals_pos = lines[1].find('t=') if equals_pos != -1: temp_string = lines[1][equals_pos+2:] + # temperature data comes in as celsius temp_c = float(temp_string) / 1000.0 + # convert celsius to fahrenheit temp_f = temp_c * 9.0 / 5.0 + 32.0 return round(temp_f,4)