comparison py/tempserver.py @ 144:482d7852b511

a bit more, and some tests
author Matt Johnston <matt@ucc.asn.au>
date Mon, 26 Nov 2012 23:21:03 +0800
parents 0895f5ad7731
children 6517ddee3187
comparison
equal deleted inserted replaced
143:0895f5ad7731 144:482d7852b511
11 self.readings = [] 11 self.readings = []
12 self.current = (None, None, None) 12 self.current = (None, None, None)
13 13
14 self.start_time = utils.monotonic_time() 14 self.start_time = utils.monotonic_time()
15 15
16 self.fridge = fridge.Fridge() 16 self.fridge = fridge.Fridge(self)
17 self.fridge.run(self) 17 self.fridge.start()
18 18
19 self.set_sensors(sensor_ds18b20.DS18B20s(self))
20
21 def set_sensors(self, sensors):
22 if self.hasattr(self, 'sensors'):
23 self.sensors.kill()
24 self.sensors = sensors
25 self.wort_name = sensors.wort_name()
26 self.fridge_name = sensors.fridge_name()
27 self.sensor_names = sensors.sensor_names()
19 28
20 def take_readings(self): 29 def take_readings(self):
21 ret = self.readings 30 ret = self.readings
22 self.readings = [] 31 self.readings = []
23 return ret 32 return ret
24 33
25 def pushfront(self, readings): 34 def pushfront(self, readings):
26 """ used if a caller of take_readings() fails """ 35 """ used if a caller of take_readings() fails """
27 self.readings = pushback + self.readings 36 self.readings = pushback + self.readings
28 37
38 # a reading is a map of {sensorname: value}. temperatures
39 # are float degrees
29 def add_reading(self, reading): 40 def add_reading(self, reading):
30 """ adds a reading at the current time """ 41 """ adds a reading at the current time """
31 self.readings.append( (reading, utils.monotonic_time())) 42 self.readings.append( (reading, utils.monotonic_time()))
43 self.current = (reading.get(self.wort_name, None),
44 reading.get(self.fridge_name, None))
32 45
33 def current_temps(self): 46 def current_temps(self):
34 """ returns (wort_temp, fridge_temp, ambient_temp) tuple """ 47 """ returns (wort_temp, fridge_temp) tuple """
35 return current 48 return current
36
37 def set_current(self, wort, fridge, ambient):
38 current = (wort, fridge, ambient)
39
40 def now(self):
41 return utils.monotonic_time()
42
43 def spawn_timer(seconds, fn, *args, **kwargs):
44 def loop():
45 while True:
46 fn(*args, **kwargs)
47 gevent.sleep(seconds)
48 return gevent.spawn(loop)
49
50 def setup():
51 pass
52
53 def main():
54 setup()
55
56 def p(x):
57 print "hello %s" % x
58
59 spawn_timer(2, p, 'one')
60
61 gevent.sleep(20)
62
63
64 if __name__ == '__main__':
65 main()