comparison py/tempserver.py @ 459:c34083c078db

uploader works
author Matt Johnston <matt@ucc.asn.au>
date Thu, 03 Jan 2013 22:37:33 +0800
parents 3db3665af2e2
children d73077e8cd67
comparison
equal deleted inserted replaced
458:94932e7051e5 459:c34083c078db
11 from utils import L,D,EX,W 11 from utils import L,D,EX,W
12 import fridge 12 import fridge
13 import config 13 import config
14 import sensor_ds18b20 14 import sensor_ds18b20
15 import params 15 import params
16 import uploader
16 17
17 18
18 class Tempserver(object): 19 class Tempserver(object):
19 def __init__(self): 20 def __init__(self):
20 self.readings = [] 21 self.readings = []
25 gevent.monkey.patch_all(os=False, thread=False) 26 gevent.monkey.patch_all(os=False, thread=False)
26 27
27 def __enter__(self): 28 def __enter__(self):
28 self.params = params.Params() 29 self.params = params.Params()
29 self.fridge = fridge.Fridge(self) 30 self.fridge = fridge.Fridge(self)
31 self.uploader = uploader.Uploader(self)
30 self.params.load() 32 self.params.load()
31 self.set_sensors(sensor_ds18b20.DS18B20s(self)) 33 self.set_sensors(sensor_ds18b20.DS18B20s(self))
32 return self 34 return self
33 35
34 def __exit__(self, exc_type, exc_value, traceback): 36 def __exit__(self, exc_type, exc_value, traceback):
42 44
43 # XXX do these go here or in __enter_() ? 45 # XXX do these go here or in __enter_() ?
44 self.start_time = self.now() 46 self.start_time = self.now()
45 self.fridge.start() 47 self.fridge.start()
46 self.sensors.start() 48 self.sensors.start()
49 self.uploader.start()
47 50
48 # won't return. 51 # won't return.
49 while True: 52 while True:
50 try: 53 try:
51 gevent.sleep(60) 54 gevent.sleep(60)
67 self.readings = [] 70 self.readings = []
68 return ret 71 return ret
69 72
70 def pushfront(self, readings): 73 def pushfront(self, readings):
71 """ used if a caller of take_readings() fails """ 74 """ used if a caller of take_readings() fails """
72 self.readings = pushback + self.readings 75 self.readings = readings + self.readings
73 76
74 # a reading is a map of {sensorname: value}. temperatures 77 # a reading is a map of {sensorname: value}. temperatures
75 # are float degrees 78 # are float degrees
76 def add_reading(self, reading): 79 def add_reading(self, reading):
77 """ adds a reading at the current time """ 80 """ adds a reading at the current time """
78 D("add_reading(%s)" % str(reading)) 81 D("add_reading(%s)" % str(reading))
79 self.readings.append( (reading, self.now())) 82 self.readings.append( (reading, self.now()))
80 self.current = (reading.get(self.wort_name, None), 83 self.current = (reading.get(self.wort_name, None),
81 reading.get(self.fridge_name, None)) 84 reading.get(self.fridge_name, None))
85 if len(self.readings) > config.MAX_READINGS:
86 self.readings = self.readings[-config.MAX_READINGS:]
82 87
83 def current_temps(self): 88 def current_temps(self):
84 """ returns (wort_temp, fridge_temp) tuple """ 89 """ returns (wort_temp, fridge_temp) tuple """
85 return self.current 90 return self.current
86 91