diff py/tempserver.py @ 443:bca470d153fd

a bit more, and some tests
author Matt Johnston <matt@ucc.asn.au>
date Mon, 26 Nov 2012 23:21:03 +0800
parents 02318c9660cd
children 6517ddee3187
line wrap: on
line diff
--- a/py/tempserver.py	Tue Nov 20 22:03:10 2012 +0800
+++ b/py/tempserver.py	Mon Nov 26 23:21:03 2012 +0800
@@ -13,9 +13,18 @@
 
         self.start_time = utils.monotonic_time()
 
-        self.fridge = fridge.Fridge()
-        self.fridge.run(self)
+        self.fridge = fridge.Fridge(self)
+        self.fridge.start()
+
+        self.set_sensors(sensor_ds18b20.DS18B20s(self))
 
+    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
@@ -26,40 +35,14 @@
         """ used if a caller of take_readings() fails """
         self.readings = pushback + self.readings
 
+    # a reading is a map of {sensorname: value}. temperatures
+    # are float degrees
     def add_reading(self, reading):
         """ adds a reading at the current time """
         self.readings.append( (reading, utils.monotonic_time()))
+        self.current = (reading.get(self.wort_name, None),
+                    reading.get(self.fridge_name, None))
 
     def current_temps(self):
-        """ returns (wort_temp, fridge_temp, ambient_temp) tuple """
+        """ returns (wort_temp, fridge_temp) tuple """
         return current
-
-    def set_current(self, wort, fridge, ambient):
-        current = (wort, fridge, ambient)
-
-    def now(self):
-        return utils.monotonic_time()
-
-def spawn_timer(seconds, fn, *args, **kwargs):
-    def loop():
-        while True:
-            fn(*args, **kwargs)
-            gevent.sleep(seconds)
-    return gevent.spawn(loop)
-
-def setup():
-    pass
-
-def main():
-    setup()
-
-    def p(x):
-        print "hello %s" % x
-
-    spawn_timer(2, p, 'one')
-
-    gevent.sleep(20)
-
-
-if __name__ == '__main__':
-    main()