# HG changeset patch # User Matt Johnston # Date 1357918916 -28800 # Node ID d73077e8cd67b9428c432171e9c0c38d952c7a36 # Parent ad22db765ba1602ec902bcdcc35abc724674cca7 Add daemon mode with locking, add "disabled" parameter diff -r ad22db765ba1 -r d73077e8cd67 py/config.py --- a/py/config.py Thu Jan 10 19:46:32 2013 +0800 +++ b/py/config.py Fri Jan 11 23:41:56 2013 +0800 @@ -1,3 +1,4 @@ +import os.path FRIDGE_SLEEP = 60 SENSOR_SLEEP = 15 @@ -9,7 +10,7 @@ # 12 hours MAX_READINGS = 12*60*60 / SENSOR_SLEEP -PARAMS_FILE = './tempserver.conf' +PARAMS_FILE = os.path.join(os.path.dirname(__file__), 'tempserver.conf') SENSOR_BASE_DIR = '/sys/devices/w1_bus_master1' FRIDGE_GPIO = '/sys/devices/virtual/gpio/gpio17' diff -r ad22db765ba1 -r d73077e8cd67 py/fridge.py --- a/py/fridge.py Thu Jan 10 19:46:32 2013 +0800 +++ b/py/fridge.py Fri Jan 11 23:41:56 2013 +0800 @@ -50,6 +50,8 @@ # greenlet subclassed def _run(self): + if self.server.params.disabled: + L("Fridge is disabled") while True: self.do() gevent.sleep(config.FRIDGE_SLEEP) @@ -71,6 +73,12 @@ L("fridge skipping, too early") return + if params.disabled: + if self.is_on(): + L("Disabled, turning fridge off") + self.off() + return + # handle broken wort sensor if wort is not None: self.wort_valid_clock = self.server.now() diff -r ad22db765ba1 -r d73077e8cd67 py/params.py --- a/py/params.py Thu Jan 10 19:46:32 2013 +0800 +++ b/py/params.py Fri Jan 11 23:41:56 2013 +0800 @@ -14,6 +14,7 @@ 'fridge_difference': 0.2, 'overshoot_delay': 720, # 12 minutes 'overshoot_factor': 1, # ÂșC + 'disabled': False, } class Params(dict): diff -r ad22db765ba1 -r d73077e8cd67 py/tempserver.py --- a/py/tempserver.py Thu Jan 10 19:46:32 2013 +0800 +++ b/py/tempserver.py Fri Jan 11 23:41:56 2013 +0800 @@ -6,6 +6,8 @@ import gevent import gevent.monkey +import lockfile +import daemon import utils from utils import L,D,EX,W @@ -94,13 +96,25 @@ datefmt='%m/%d/%Y %I:%M:%S %p', level=logging.DEBUG) +def start(): + with Tempserver() as server: + server.run() + def main(): setup_logging() + pidpath = os.path.join(os.path.dirname(__file__), 'tempserver-lock') + pidf = lockfile.FileLock(pidpath, threaded=False) + pidf.acquire(0) + if '--daemon' in sys.argv: - utils.cheap_daemon() - with Tempserver() as server: - server.run() + logpath = os.path.join(os.path.dirname(__file__), 'tempserver.log') + logf = open(logpath, 'a+') + with daemon.DaemonContext(pidfile=pidf, stdout=logf, stderr = logf): + start() + else: + with pidf: + start() if __name__ == '__main__': main() diff -r ad22db765ba1 -r d73077e8cd67 requirements.txt --- a/requirements.txt Thu Jan 10 19:46:32 2013 +0800 +++ b/requirements.txt Fri Jan 11 23:41:56 2013 +0800 @@ -1,5 +1,7 @@ argparse==1.2.1 gevent==1.0rc2 greenlet==0.4.0 +lockfile==0.9.1 +python-daemon==1.6 smbus==1.1 wsgiref==0.1.2