diff py/tempserver.py @ 465:a40f30fad8f6

Merge
author Matt Johnston <matt@ucc.asn.au>
date Mon, 21 Jan 2013 07:25:52 +0800
parents a91adc95543d
children 5821c5fab919
line wrap: on
line diff
--- a/py/tempserver.py	Mon Jan 21 07:25:29 2013 +0800
+++ b/py/tempserver.py	Mon Jan 21 07:25:52 2013 +0800
@@ -6,6 +6,8 @@
 
 import gevent
 import gevent.monkey
+import lockfile
+import daemon
 
 import utils
 from utils import L,D,EX,W
@@ -13,6 +15,7 @@
 import config
 import sensor_ds18b20
 import params
+import uploader
 
 
 class Tempserver(object):
@@ -27,6 +30,7 @@
     def __enter__(self):
         self.params = params.Params()
         self.fridge = fridge.Fridge(self)
+        self.uploader = uploader.Uploader(self)
         self.params.load()
         self.set_sensors(sensor_ds18b20.DS18B20s(self))
         return self
@@ -44,6 +48,7 @@
         self.start_time = self.now()
         self.fridge.start()
         self.sensors.start()
+        self.uploader.start()
 
         # won't return.
         while True:
@@ -69,7 +74,7 @@
 
     def pushfront(self, readings):
         """ used if a caller of take_readings() fails """
-        self.readings = pushback + self.readings
+        self.readings = readings + self.readings
 
     # a reading is a map of {sensorname: value}. temperatures
     # are float degrees
@@ -79,6 +84,8 @@
         self.readings.append( (reading, self.now()))
         self.current = (reading.get(self.wort_name, None),
                     reading.get(self.fridge_name, None))
+        if len(self.readings) > config.MAX_READINGS:
+            self.readings = self.readings[-config.MAX_READINGS:]
 
     def current_temps(self):
         """ returns (wort_temp, fridge_temp) tuple """
@@ -87,15 +94,27 @@
 def setup_logging():
     logging.basicConfig(format='%(asctime)s %(message)s', 
             datefmt='%m/%d/%Y %I:%M:%S %p',
-            level=logging.DEBUG)
+            level=logging.INFO)
+
+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()