changeset 459:c34083c078db

uploader works
author Matt Johnston <matt@ucc.asn.au>
date Thu, 03 Jan 2013 22:37:33 +0800
parents 94932e7051e5
children bd01dca5f5b4
files py/config.py py/params.py py/sensor_ds18b20.py py/tempserver.py py/uploader.py
diffstat 5 files changed, 49 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/py/config.py	Thu Jan 03 22:35:54 2013 +0800
+++ b/py/config.py	Thu Jan 03 22:37:33 2013 +0800
@@ -1,11 +1,14 @@
 
-FRIDGE_SLEEP = 10
+FRIDGE_SLEEP = 60
 SENSOR_SLEEP = 15
-UPLOAD_SLEEP = 300
+UPLOAD_SLEEP = 80
 
-FRIDGE_DELAY = 3 # 10 mins
+FRIDGE_DELAY = 600 # 10 mins
 FRIDGE_WORT_INVALID_TIME = 300 # 5 mins
 
+# 12 hours
+MAX_READINGS = 12*60*60 / SENSOR_SLEEP
+
 PARAMS_FILE = './tempserver.conf'
 
 SENSOR_BASE_DIR = '/sys/devices/w1_bus_master1'
@@ -14,3 +17,11 @@
 FRIDGE_NAME = '28-0000042cccc4'
 AMBIENT_NAME = '28-0000042c6dbb'
 INTERNAL_TEMPERATURE = '/sys/class/thermal/thermal_zone0/temp'
+
+HMAC_KEY = "a key"
+UPDATE_URL = 'https://matt.ucc.asn.au/test/templog/update'
+
+try:
+    from localconfig import *
+except ImportError:
+    pass
--- a/py/params.py	Thu Jan 03 22:35:54 2013 +0800
+++ b/py/params.py	Thu Jan 03 22:37:33 2013 +0800
@@ -2,6 +2,7 @@
 import collections
 import json
 import signal
+import StringIO
 
 import gevent
 
@@ -48,6 +49,10 @@
                 raise self.Error("Unknown parameter %s=%s in file '%s'" % (str(k), str(u[k]), getattr(f, 'name', '???')))
         self.update(u)
 
+        L("Loaded parameters")
+        L(self.save_string())
+
+
     def save(self, f = None):
         if not f:
             f = file(config.PARAMS_FILE, 'w')
@@ -55,9 +60,14 @@
         f.write('\n')
         f.flush()
 
+    def save_string(self):
+        s = StringIO.StringIO()
+        self.save(s)
+        return s.getvalue()
+
     def reload_signal(self):
         try:
             self.load()
-            L("Reloaded params")
+            L("Reloaded.")
         except self.Error, e:
             W("Problem reloading: %s" % str(e))
--- a/py/sensor_ds18b20.py	Thu Jan 03 22:35:54 2013 +0800
+++ b/py/sensor_ds18b20.py	Thu Jan 03 22:37:33 2013 +0800
@@ -75,7 +75,6 @@
         """ Returns a sequence of sensorname """
         slaves_path = os.path.join(self.master_dir, "w1_master_slaves")
         names = open(slaves_path, 'r').read().split()
-        D("returning names %s" % names)
         return names
 
     def wort_name(self):
--- a/py/tempserver.py	Thu Jan 03 22:35:54 2013 +0800
+++ b/py/tempserver.py	Thu Jan 03 22:37:33 2013 +0800
@@ -13,6 +13,7 @@
 import config
 import sensor_ds18b20
 import params
+import uploader
 
 
 class Tempserver(object):
@@ -27,6 +28,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 +46,7 @@
         self.start_time = self.now()
         self.fridge.start()
         self.sensors.start()
+        self.uploader.start()
 
         # won't return.
         while True:
@@ -69,7 +72,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 +82,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 """
--- a/py/uploader.py	Thu Jan 03 22:35:54 2013 +0800
+++ b/py/uploader.py	Thu Jan 03 22:37:33 2013 +0800
@@ -1,8 +1,15 @@
-import config
-
 import json
 import hmac
 import zlib
+import binascii
+import urllib
+import urllib2
+
+import gevent
+
+import config
+from utils import L,D,EX,W,E
+import utils
 
 class Uploader(gevent.Greenlet):
     def __init__(self, server):
@@ -10,6 +17,7 @@
         self.server = server
 
     def _run(self):
+        gevent.sleep(5)
         while True:
             self.do()
             gevent.sleep(config.UPLOAD_SLEEP)
@@ -23,7 +31,7 @@
         tosend['readings'] = readings
 
         tosend['wort_name'] = self.server.wort_name
-        tosend['fridge_name'] = self.server.wort_fridge_name
+        tosend['fridge_name'] = self.server.wort_name
 
         tosend.update(dict(self.server.params))
 
@@ -42,14 +50,18 @@
         if result != 'OK':
             raise Exception("Server returned %s" % result)
 
-    def do():
+    def do(self):
         readings = self.server.take_readings()
         try:
             tosend = self.get_tosend(readings)
-            readings = None
+            nreadings = len(readings)
             self.send(tosend)
+            readings = None
+            L("Sent updated %d readings" % nreadings)
+        except urllib2.HTTPError, e:
+            E("Error in uploader: %s" % str(e))
         except Exception, e:
-            EX"Error in uploader: %s" % str(e))
+            EX("Error in uploader: %s" % str(e))
         finally:
             if readings is not None:
                 self.server.pushfront(readings)