comparison py/tempserver.py @ 444:c5629d79b4ac

few more bits
author Matt Johnston <matt@ucc.asn.au>
date Thu, 29 Nov 2012 23:50:40 +0800
parents bca470d153fd
children b32e5a11a4cb
comparison
equal deleted inserted replaced
443:bca470d153fd 444:c5629d79b4ac
1 #!/usr/bin/env python2.7 1 #!/usr/bin/env python2.7
2 2
3 import sys 3 import sys
4 import os 4 import os
5 import gevent 5 import gevent
6 import gevent.monkey
6 7
7 import utils 8 import utils
9 import fridge
10 import config
11 import sensor_ds18b20
12 import params
13
8 14
9 class Tempserver(object): 15 class Tempserver(object):
10 def __init__(self): 16 def __init__(self):
11 self.readings = [] 17 self.readings = []
12 self.current = (None, None, None) 18 self.current = (None, None)
13 19
14 self.start_time = utils.monotonic_time() 20 # don't patch os, fork() is used by daemonize
21 gevent.monkey.patch_all(os=False, thread=False)
22
23 self.start_time = self.now()
24
25 self.params = params.Params()
26 self.params.load()
15 27
16 self.fridge = fridge.Fridge(self) 28 self.fridge = fridge.Fridge(self)
17 self.fridge.start() 29 self.fridge.start()
18 30
19 self.set_sensors(sensor_ds18b20.DS18B20s(self)) 31 self.set_sensors(sensor_ds18b20.DS18B20s(self))
32
33 def run(self):
34 # won't return.
35 while True:
36 gevent.sleep(60)
37
38 def now(self):
39 return utils.monotonic_time()
20 40
21 def set_sensors(self, sensors): 41 def set_sensors(self, sensors):
22 if self.hasattr(self, 'sensors'): 42 if self.hasattr(self, 'sensors'):
23 self.sensors.kill() 43 self.sensors.kill()
24 self.sensors = sensors 44 self.sensors = sensors
25 self.wort_name = sensors.wort_name() 45 self.wort_name = sensors.wort_name()
26 self.fridge_name = sensors.fridge_name() 46 self.fridge_name = sensors.fridge_name()
27 self.sensor_names = sensors.sensor_names()
28 47
29 def take_readings(self): 48 def take_readings(self):
30 ret = self.readings 49 ret = self.readings
31 self.readings = [] 50 self.readings = []
32 return ret 51 return ret
37 56
38 # a reading is a map of {sensorname: value}. temperatures 57 # a reading is a map of {sensorname: value}. temperatures
39 # are float degrees 58 # are float degrees
40 def add_reading(self, reading): 59 def add_reading(self, reading):
41 """ adds a reading at the current time """ 60 """ adds a reading at the current time """
42 self.readings.append( (reading, utils.monotonic_time())) 61 self.readings.append( (reading, self.now()))
43 self.current = (reading.get(self.wort_name, None), 62 self.current = (reading.get(self.wort_name, None),
44 reading.get(self.fridge_name, None)) 63 reading.get(self.fridge_name, None))
45 64
46 def current_temps(self): 65 def current_temps(self):
47 """ returns (wort_temp, fridge_temp) tuple """ 66 """ returns (wort_temp, fridge_temp) tuple """
48 return current 67 return current
68
69 def setup_logging():
70 logging.basicConfig(format='%(asctime)s %(message)s',
71 datefmt='%m/%d/%Y %I:%M:%S %p',
72 level=logging.INFO)
73
74 def main():
75 server = Tempserver()
76
77 if '--daemon' in sys.argv:
78 utils.cheap_daemon()
79
80 server.run()
81
82 if __name__ == '__main__':
83 main()