diff py/tempserver.py @ 145:6517ddee3187

few more bits
author Matt Johnston <matt@ucc.asn.au>
date Thu, 29 Nov 2012 23:50:40 +0800
parents 482d7852b511
children b32e5a11a4cb
line wrap: on
line diff
--- a/py/tempserver.py	Mon Nov 26 23:21:03 2012 +0800
+++ b/py/tempserver.py	Thu Nov 29 23:50:40 2012 +0800
@@ -3,28 +3,47 @@
 import sys
 import os
 import gevent
+import gevent.monkey
 
 import utils
+import fridge
+import config
+import sensor_ds18b20
+import params
+
 
 class Tempserver(object):
     def __init__(self):
         self.readings = []
-        self.current = (None, None, None)
+        self.current = (None, None)
+
+        # don't patch os, fork() is used by daemonize
+        gevent.monkey.patch_all(os=False, thread=False)
 
-        self.start_time = utils.monotonic_time()
+        self.start_time = self.now()
+
+        self.params = params.Params()
+        self.params.load()
 
         self.fridge = fridge.Fridge(self)
         self.fridge.start()
 
         self.set_sensors(sensor_ds18b20.DS18B20s(self))
 
+    def run(self):
+        # won't return.
+        while True:
+            gevent.sleep(60)
+
+    def now(self):
+        return utils.monotonic_time()
+
     def set_sensors(self, sensors):
         if self.hasattr(self, 'sensors'):
             self.sensors.kill()
         self.sensors = sensors
         self.wort_name = sensors.wort_name()
         self.fridge_name = sensors.fridge_name()
-        self.sensor_names = sensors.sensor_names()
 
     def take_readings(self):
         ret = self.readings
@@ -39,10 +58,26 @@
     # are float degrees
     def add_reading(self, reading):
         """ adds a reading at the current time """
-        self.readings.append( (reading, utils.monotonic_time()))
+        self.readings.append( (reading, self.now()))
         self.current = (reading.get(self.wort_name, None),
                     reading.get(self.fridge_name, None))
 
     def current_temps(self):
         """ returns (wort_temp, fridge_temp) tuple """
         return current
+
+def setup_logging():
+    logging.basicConfig(format='%(asctime)s %(message)s', 
+            datefmt='%m/%d/%Y %I:%M:%S %p',
+            level=logging.INFO)
+
+def main():
+    server = Tempserver()
+
+    if '--daemon' in sys.argv:
+        utils.cheap_daemon()
+
+    server.run()
+
+if __name__ == '__main__':
+    main()