changeset 461:1eb68df9f8ab

Add daemon mode with locking, add "disabled" parameter
author Matt Johnston <matt@ucc.asn.au>
date Fri, 11 Jan 2013 23:41:56 +0800
parents bd01dca5f5b4
children a91adc95543d
files py/config.py py/fridge.py py/params.py py/tempserver.py requirements.txt
diffstat 5 files changed, 30 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- 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'
--- 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()
--- 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):
--- 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()
--- 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