Mercurial > templog
comparison web/log.py @ 159:683cfd134f6a
Fix the updated json web code to work
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Thu, 03 Jan 2013 22:35:54 +0800 |
parents | 3b4277aaed3c |
children | 632d436d227b |
comparison
equal
deleted
inserted
replaced
157:6d799bee3508 | 159:683cfd134f6a |
---|---|
12 import sqlite3 | 12 import sqlite3 |
13 import traceback | 13 import traceback |
14 import datetime | 14 import datetime |
15 import struct | 15 import struct |
16 import binascii | 16 import binascii |
17 import json | |
17 from colorsys import hls_to_rgb | 18 from colorsys import hls_to_rgb |
18 | 19 |
19 import config | 20 import config |
20 | 21 |
21 def sensor_rrd_path(s): | 22 def sensor_rrd_path(s): |
22 return '%s/sensor_%s.rrd' % (config.DATA_PATH, s) | 23 return '%s/sensor_%s.rrd' % (config.DATA_PATH, str(s)) |
23 | 24 |
24 # returns (path, sensor_name) tuples | 25 # returns (path, sensor_name) tuples |
25 def all_sensors(): | 26 def all_sensors(): |
26 return [(r, os.path.basename(r[:-4])) | 27 return [(r, os.path.basename(r[:-4])) |
27 for r in glob.glob('%s/*.rrd' % config.DATA_PATH)] | 28 for r in glob.glob('%s/*.rrd' % config.DATA_PATH)] |
42 else: | 43 else: |
43 args = [ | 44 args = [ |
44 '--step', '300', | 45 '--step', '300', |
45 'DS:temp:GAUGE:600:-100:500', | 46 'DS:temp:GAUGE:600:-100:500', |
46 'RRA:AVERAGE:0.5:1:1051200'] | 47 'RRA:AVERAGE:0.5:1:1051200'] |
48 | |
49 print>>sys.stderr, sensor_rrd_path(sensor_id) | |
47 | 50 |
48 rrdtool.create(sensor_rrd_path(sensor_id), | 51 rrdtool.create(sensor_rrd_path(sensor_id), |
49 '--start', 'now-60d', | 52 '--start', 'now-60d', |
50 *args) | 53 *args) |
51 | 54 |
146 #return tempf | 149 #return tempf |
147 return tempf.read() | 150 return tempf.read() |
148 | 151 |
149 def validate_value(m): | 152 def validate_value(m): |
150 if m == 85: | 153 if m == 85: |
151 yield 'U' | 154 return 'U' |
152 else: | 155 else: |
153 yield '%f' % m | 156 return '%f' % m |
154 | 157 |
155 def sensor_update(sensor_id, measurements): | 158 def sensor_update(sensor_id, measurements): |
156 try: | 159 try: |
157 open(sensor_rrd_path(sensor_id)) | 160 open(sensor_rrd_path(sensor_id)) |
158 except IOError, e: | 161 except IOError, e: |
178 return open('%s/debug.log' % config.DATA_PATH, mode) | 181 return open('%s/debug.log' % config.DATA_PATH, mode) |
179 | 182 |
180 def record_debug(params): | 183 def record_debug(params): |
181 f = debug_file('a+') | 184 f = debug_file('a+') |
182 f.write('===== %s =====\n' % time.strftime('%a, %d %b %Y %H:%M:%S')) | 185 f.write('===== %s =====\n' % time.strftime('%a, %d %b %Y %H:%M:%S')) |
183 json.dump(params, f, sort_keys=True, indent=4)) | 186 json.dump(params, f, sort_keys=True, indent=4) |
184 f.flush() | 187 f.flush() |
185 return f | 188 return f |
186 | |
187 | 189 |
188 def tail_debug_log(): | 190 def tail_debug_log(): |
189 f = debug_file() | 191 f = debug_file() |
190 f.seek(0, 2) | 192 f.seek(0, 2) |
191 size = f.tell() | 193 size = f.tell() |
219 # measurements is {sensorname: [(time, value), ...], ...} | 221 # measurements is {sensorname: [(time, value), ...], ...} |
220 measurements = {} | 222 measurements = {} |
221 for rs, t in readings: | 223 for rs, t in readings: |
222 real_t = t + time_diff | 224 real_t = t + time_diff |
223 for s, v in rs.iteritems(): | 225 for s, v in rs.iteritems(): |
224 measurements.getdefault(s, []).append((real_t, v)) | 226 measurements.setdefault(s, []).append((real_t, v)) |
225 | 227 |
226 # one-off measurements here | 228 # one-off measurements here |
227 measurements['fridge_on'] = [ (time.time(), params['fridge_on']) ] | 229 measurements['fridge_on'] = [ (time.time(), params['fridge_on']) ] |
230 measurements['fridge_setpoint'] = [ (time.time(), params['fridge_setpoint']) ] | |
228 | 231 |
229 for s, vs in measurements.iteritems(): | 232 for s, vs in measurements.iteritems(): |
230 sensor_update(s, vs) | 233 sensor_update(s, vs) |
231 | 234 |
232 timedelta = time.time() - start_time | 235 timedelta = time.time() - start_time |
233 debugf.write("Updated %d sensors in %.2f secs\n" % (len(sensors), timedelta)) | 236 debugf.write("Updated sensors in %.2f secs\n" % timedelta) |
234 debugf.flush() | 237 debugf.flush() |