Mercurial > templog
changeset 460:bd01dca5f5b4
merge
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Thu, 10 Jan 2013 19:46:32 +0800 |
parents | 176fc5b68a3f (current diff) c34083c078db (diff) |
children | 1eb68df9f8ab |
files | |
diffstat | 6 files changed, 59 insertions(+), 19 deletions(-) [+] |
line wrap: on
line diff
--- a/py/config.py Tue Jan 08 22:31:12 2013 +0800 +++ b/py/config.py Thu Jan 10 19:46:32 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 Tue Jan 08 22:31:12 2013 +0800 +++ b/py/params.py Thu Jan 10 19:46:32 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 Tue Jan 08 22:31:12 2013 +0800 +++ b/py/sensor_ds18b20.py Thu Jan 10 19:46:32 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 Tue Jan 08 22:31:12 2013 +0800 +++ b/py/tempserver.py Thu Jan 10 19:46:32 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 Tue Jan 08 22:31:12 2013 +0800 +++ b/py/uploader.py Thu Jan 10 19:46:32 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)
--- a/web/log.py Tue Jan 08 22:31:12 2013 +0800 +++ b/web/log.py Thu Jan 10 19:46:32 2013 +0800 @@ -14,12 +14,13 @@ import datetime import struct import binascii +import json from colorsys import hls_to_rgb import config def sensor_rrd_path(s): - return '%s/sensor_%s.rrd' % (config.DATA_PATH, s) + return '%s/sensor_%s.rrd' % (config.DATA_PATH, str(s)) # returns (path, sensor_name) tuples def all_sensors(): @@ -45,6 +46,8 @@ 'DS:temp:GAUGE:600:-100:500', 'RRA:AVERAGE:0.5:1:1051200'] + print>>sys.stderr, sensor_rrd_path(sensor_id) + rrdtool.create(sensor_rrd_path(sensor_id), '--start', 'now-60d', *args) @@ -148,9 +151,9 @@ def validate_value(m): if m == 85: - yield 'U' + return 'U' else: - yield '%f' % m + return '%f' % m def sensor_update(sensor_id, measurements): try: @@ -180,11 +183,10 @@ def record_debug(params): f = debug_file('a+') f.write('===== %s =====\n' % time.strftime('%a, %d %b %Y %H:%M:%S')) - json.dump(params, f, sort_keys=True, indent=4)) + json.dump(params, f, sort_keys=True, indent=4) f.flush() return f - def tail_debug_log(): f = debug_file() f.seek(0, 2) @@ -221,14 +223,15 @@ for rs, t in readings: real_t = t + time_diff for s, v in rs.iteritems(): - measurements.getdefault(s, []).append((real_t, v)) + measurements.setdefault(s, []).append((real_t, v)) # one-off measurements here measurements['fridge_on'] = [ (time.time(), params['fridge_on']) ] + measurements['fridge_setpoint'] = [ (time.time(), params['fridge_setpoint']) ] for s, vs in measurements.iteritems(): sensor_update(s, vs) timedelta = time.time() - start_time - debugf.write("Updated %d sensors in %.2f secs\n" % (len(sensors), timedelta)) + debugf.write("Updated sensors in %.2f secs\n" % timedelta) debugf.flush()