# HG changeset patch # User Matt Johnston # Date 1354718653 -28800 # Node ID 5b9dc87c988f8256492a85068d5f81d12d65b5ee # Parent c5629d79b4aca3d182df598d4189c7963d60cf42 update web to handle new style params diff -r c5629d79b4ac -r 5b9dc87c988f py/uploader.py --- a/py/uploader.py Thu Nov 29 23:50:40 2012 +0800 +++ b/py/uploader.py Wed Dec 05 22:44:13 2012 +0800 @@ -45,7 +45,7 @@ def do(): readings = self.server.take_readings() try: - tosend = self.get_to_send(readings) + tosend = self.get_tosend(readings) readings = None self.send(tosend) except Exception, e: diff -r c5629d79b4ac -r 5b9dc87c988f web/log.py --- a/web/log.py Thu Nov 29 23:50:40 2012 +0800 +++ b/web/log.py Wed Dec 05 22:44:13 2012 +0800 @@ -146,23 +146,20 @@ #return tempf return tempf.read() -def validate_values(measurements): - for m in measurements: - if m == 85: - yield 'U' - else: - yield '%f' % m +def validate_value(m): + if m == 85: + yield 'U' + else: + yield '%f' % m -def sensor_update(sensor_id, measurements, first_real_time, time_step): +def sensor_update(sensor_id, measurements): try: open(sensor_rrd_path(sensor_id)) except IOError, e: create_rrd(sensor_id) if measurements: - values = ['%d:%s' % p for p in - zip((first_real_time + time_step*t for t in xrange(len(measurements))), - validate_values(measurements))] + values = ['%d:%s' % (t, validate_value(m)) for (t, m) in measurements] rrdfile = sensor_rrd_path(sensor_id) # XXX what to do here when it fails... @@ -180,10 +177,10 @@ def debug_file(mode='r'): return open('%s/debug.log' % config.DATA_PATH, mode) -def record_debug(lines): +def record_debug(params): f = debug_file('a+') f.write('===== %s =====\n' % time.strftime('%a, %d %b %Y %H:%M:%S')) - f.writelines(('%s\n' % s for s in lines)) + json.dump(params, f, sort_keys=True, indent=4)) f.flush() return f @@ -206,70 +203,31 @@ tick_secs = int(entries['tick_secs']) return val_ticks + float(val_rem) * tick_secs / tick_wake -def parse(lines): +def parse(params): start_time = time.time() - debugf = record_debug(lines) - - entries = dict(l.split('=', 1) for l in lines) - if len(entries) != len(lines): - raise Exception("Keys are not unique") + debugf = record_debug(params) - if 'sensors' not in entries: - # only debug info, it's been recorded - return - - num_sensors = int(entries['sensors']) - num_measurements = int(entries['measurements']) + remote_now = params['now'] - sensors = [entries['sensor_id%d' % n] for n in xrange(num_sensors)] - - meas = [] - for s in sensors: - meas.append([]) + time_diff = start_time - remote_now - for n in xrange(num_measurements): - vals = [convert_ds18b20_12bit(x) for x in entries["meas%d" % n].strip().split()] - if len(vals) != num_sensors: - raise Exception("Wrong number of sensors for measurement %d" % n) - # we make an array of values for each sensor - for s in xrange(num_sensors): - meas[s].append(vals[s]) + # readings is [ ({sensorname: value, ...}, time), ... ] + readings = params['readings'] - avr_now = time_rem('now', entries) - avr_first_time = time_rem('first_time', entries) - avr_comms_time = time_rem('comms_time', entries) - time_step = float(entries['time_step']) - - debugf.write('now %f, comms_time %f, first_time %f, delta %f\n' % - (avr_now, avr_comms_time, avr_first_time, avr_now - avr_comms_time)) - - if 'avrtemp' in entries: - avrtemp = val_scale(int(entries['avrtemp'])) - sensor_update('avrtemp', [avrtemp], time.time(), 1) - - if 'voltage' in entries: - voltage = 0.001 * int(entries['voltage']) - sensor_update('voltage', [voltage], time.time(), 1) + # measurements is {sensorname: [(time, value), ...], ...} + measurements = {} + for rs, t in readings: + real_t = t + time_diff + for s, v in rs.iteritems(): + measurements.getdefault(s, []).append((real_t, v)) - if 'fridge_status' in entries: - fridge_on = int(entries['fridge_status']) - sensor_update('fridge_on', [fridge_on], time.time(), 1) + # one-off measurements here + measurements['fridge_on'] = [ (time.time(), params['fridge_on']) ] - if 'fridge' in entries: - fridge_setpoint = float(entries['fridge']) - sensor_update('fridge_setpoint', [fridge_setpoint], time.time(), 1) - #sqlite - # - time - # - voltage - # - boot time - - first_real_time = time.time() - (avr_now - avr_first_time) - - for sensor_id, measurements in zip(sensors, meas): - # XXX sqlite add - sensor_update(sensor_id, measurements, first_real_time, time_step) + 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)) diff -r c5629d79b4ac -r 5b9dc87c988f web/templog.py --- a/web/templog.py Thu Nov 29 23:50:40 2012 +0800 +++ b/web/templog.py Wed Dec 05 22:44:13 2012 +0800 @@ -1,6 +1,7 @@ #!/usr/bin/env python2.7 import binascii +import json import hmac import zlib from datetime import datetime, timedelta @@ -19,15 +20,17 @@ @route('/update', method='post') def update(): - enc_lines = request.forms.lines + js_enc = request.forms.data mac = request.forms.hmac - if hmac.new(config.HMAC_KEY, enc_lines).hexdigest() != mac: + if hmac.new(config.HMAC_KEY, js_enc).hexdigest() != mac: raise bottle.HTTPError(code = 403, output = "Bad key") - lines = zlib.decompress(binascii.a2b_base64(enc_lines)).split('\n') + js = zlib.decompress(binascii.a2b_base64(js_enc)) - log.parse(lines) + params = json.loads(js) + + log.parse(params) return "OK"