# HG changeset patch # User Matt Johnston # Date 1355667286 -28800 # Node ID fe729664a5e68ee1c50ac614868ae30c7d9ecf73 # Parent 8fdf86ea41e7a529acd7c7b386001294c218c9c2 working better. logging works properly, cleanup fridge.off() happens. diff -r 8fdf86ea41e7 -r fe729664a5e6 py/config.py --- a/py/config.py Sat Dec 15 23:49:08 2012 +0800 +++ b/py/config.py Sun Dec 16 22:14:46 2012 +0800 @@ -1,17 +1,14 @@ -FRIDGE_GPIO = '/sys/devices/virtual/gpio/gpio17' -FRIDGE_SLEEP = 60 - -FRIDGE_DELAY = 600 # 10 mins -FRIDGE_WORT_INVALID_TIME = 300 # 5 mins +FRIDGE_SLEEP = 10 +SENSOR_SLEEP = 15 +UPLOAD_SLEEP = 300 -SENSOR_SLEEP = 120 - -UPLOAD_SLEEP = 300 +FRIDGE_DELAY = 3 # 10 mins +FRIDGE_WORT_INVALID_TIME = 300 # 5 mins PARAMS_FILE = './tempserver.conf' SENSOR_BASE_DIR = '/sys/devices/w1_bus_master1' - -WORT_NAME = 'aa' +FRIDGE_GPIO = '/sys/devices/virtual/gpio/gpio17' +WORT_NAME = '28-0000031abc49' FRIDGE_NAME = 'bb' diff -r 8fdf86ea41e7 -r fe729664a5e6 py/fridge.py --- a/py/fridge.py Sat Dec 15 23:49:08 2012 +0800 +++ b/py/fridge.py Sun Dec 16 22:14:46 2012 +0800 @@ -15,7 +15,7 @@ self.setup_gpio() self.wort_valid_clock = 0 self.fridge_on_clock = 0 - self.fridge_off_clock = 0 + self.fridge_off_clock = server.now() def setup_gpio(self): dir_fn = '%s/direction' % config.FRIDGE_GPIO diff -r 8fdf86ea41e7 -r fe729664a5e6 py/sensor_ds18b20.py --- a/py/sensor_ds18b20.py Sat Dec 15 23:49:08 2012 +0800 +++ b/py/sensor_ds18b20.py Sun Dec 16 22:14:46 2012 +0800 @@ -1,10 +1,13 @@ #!/usr/bin/env python2.7 +import os +import re + import gevent import gevent.threadpool + import config -import re -from utils import L,W,E,EX +from utils import D,L,W,E,EX class DS18B20s(gevent.Greenlet): @@ -27,20 +30,24 @@ # greenlets keep running. # the ds18b20 takes ~750ms to read, which is noticable # interactively. - return self.readthread.apply(lambda: f.read) + return self.readthread.apply(f.read) def do_sensor(self, s, contents = None): """ contents can be set by the caller for testing """ + D("dosensor %s" % s) try: if contents is None: fn = os.path.join(self.master_dir, s, 'w1_slave') f = open(fn, 'r') contents = self.read_wait(f) + match = self.THERM_RE.match(contents) if match is None: + D("no match") return None - temp = int(match.groups(1)[0]) - return temp / 1000.0 + temp = int(match.groups(1)[0]) / 1000.0 + D("returning %f" % temp) + return temp except Exception, e: EX("Problem reading sensor '%s': %s" % (s, str(e))) return None @@ -48,7 +55,7 @@ def do(self): vals = {} for n in self.sensor_names(): - value = do_sensor(n) + value = self.do_sensor(n) if value is not None: vals[n] = value @@ -57,7 +64,9 @@ def sensor_names(self): """ Returns a sequence of sensorname """ slaves_path = os.path.join(self.master_dir, "w1_master_slaves") - return open(slaves_path, 'r').split() + names = open(slaves_path, 'r').read().split() + D("returning names %s" % names) + return names def wort_name(self): return config.WORT_NAME diff -r 8fdf86ea41e7 -r fe729664a5e6 py/tempserver.py --- a/py/tempserver.py Sat Dec 15 23:49:08 2012 +0800 +++ b/py/tempserver.py Sun Dec 16 22:14:46 2012 +0800 @@ -2,10 +2,13 @@ import sys import os +import logging + import gevent import gevent.monkey import utils +from utils import L,D,EX,W import fridge import config import sensor_ds18b20 @@ -16,21 +19,32 @@ def __init__(self): self.readings = [] self.current = (None, None) + self.fridge = None # don't patch os, fork() is used by daemonize gevent.monkey.patch_all(os=False, thread=False) - self.start_time = self.now() - + def __enter__(self): self.params = params.Params() + self.fridge = fridge.Fridge(self) self.params.load() + self.set_sensors(sensor_ds18b20.DS18B20s(self)) + return self - self.fridge = fridge.Fridge(self) - self.fridge.start() - - self.set_sensors(sensor_ds18b20.DS18B20s(self)) + def __exit__(self, exc_type, exc_value, traceback): + L("Exiting, cleanup handler"); + self.fridge.off() def run(self): + + if self.fridge is None: + raise Exception("Tempserver.run() must be within 'with Tempserver() as server'") + + # XXX do these go here or in __enter_() ? + self.start_time = self.now() + self.fridge.start() + self.sensors.start() + # won't return. while True: gevent.sleep(60) @@ -69,15 +83,15 @@ def setup_logging(): logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p', - level=logging.INFO) + level=logging.DEBUG) def main(): - server = Tempserver() + setup_logging() if '--daemon' in sys.argv: utils.cheap_daemon() - - server.run() + with Tempserver() as server: + server.run() if __name__ == '__main__': main() diff -r 8fdf86ea41e7 -r fe729664a5e6 py/utils.py --- a/py/utils.py Sat Dec 15 23:49:08 2012 +0800 +++ b/py/utils.py Sun Dec 16 22:14:46 2012 +0800 @@ -5,6 +5,7 @@ import select import logging +D = logging.debug L = logging.info W = logging.warning E = logging.error