Mercurial > templog
comparison web/log.py @ 146:3b4277aaed3c
update web to handle new style params
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Wed, 05 Dec 2012 22:44:13 +0800 |
parents | d36fe81077cc |
children | 683cfd134f6a |
comparison
equal
deleted
inserted
replaced
145:6517ddee3187 | 146:3b4277aaed3c |
---|---|
144 print>>sys.stderr, ' '.join("'%s'" % s for s in args) | 144 print>>sys.stderr, ' '.join("'%s'" % s for s in args) |
145 rrdtool.graph(*args) | 145 rrdtool.graph(*args) |
146 #return tempf | 146 #return tempf |
147 return tempf.read() | 147 return tempf.read() |
148 | 148 |
149 def validate_values(measurements): | 149 def validate_value(m): |
150 for m in measurements: | 150 if m == 85: |
151 if m == 85: | 151 yield 'U' |
152 yield 'U' | 152 else: |
153 else: | 153 yield '%f' % m |
154 yield '%f' % m | 154 |
155 | 155 def sensor_update(sensor_id, measurements): |
156 def sensor_update(sensor_id, measurements, first_real_time, time_step): | |
157 try: | 156 try: |
158 open(sensor_rrd_path(sensor_id)) | 157 open(sensor_rrd_path(sensor_id)) |
159 except IOError, e: | 158 except IOError, e: |
160 create_rrd(sensor_id) | 159 create_rrd(sensor_id) |
161 | 160 |
162 if measurements: | 161 if measurements: |
163 values = ['%d:%s' % p for p in | 162 values = ['%d:%s' % (t, validate_value(m)) for (t, m) in measurements] |
164 zip((first_real_time + time_step*t for t in xrange(len(measurements))), | |
165 validate_values(measurements))] | |
166 | 163 |
167 rrdfile = sensor_rrd_path(sensor_id) | 164 rrdfile = sensor_rrd_path(sensor_id) |
168 # XXX what to do here when it fails... | 165 # XXX what to do here when it fails... |
169 for v in values: | 166 for v in values: |
170 try: | 167 try: |
178 #os.fsync(f.fileno()) | 175 #os.fsync(f.fileno()) |
179 | 176 |
180 def debug_file(mode='r'): | 177 def debug_file(mode='r'): |
181 return open('%s/debug.log' % config.DATA_PATH, mode) | 178 return open('%s/debug.log' % config.DATA_PATH, mode) |
182 | 179 |
183 def record_debug(lines): | 180 def record_debug(params): |
184 f = debug_file('a+') | 181 f = debug_file('a+') |
185 f.write('===== %s =====\n' % time.strftime('%a, %d %b %Y %H:%M:%S')) | 182 f.write('===== %s =====\n' % time.strftime('%a, %d %b %Y %H:%M:%S')) |
186 f.writelines(('%s\n' % s for s in lines)) | 183 json.dump(params, f, sort_keys=True, indent=4)) |
187 f.flush() | 184 f.flush() |
188 return f | 185 return f |
189 | 186 |
190 | 187 |
191 def tail_debug_log(): | 188 def tail_debug_log(): |
204 val_rem = int(entries['%s_rem' % name]) | 201 val_rem = int(entries['%s_rem' % name]) |
205 tick_wake = int(entries['tick_wake']) + 1 | 202 tick_wake = int(entries['tick_wake']) + 1 |
206 tick_secs = int(entries['tick_secs']) | 203 tick_secs = int(entries['tick_secs']) |
207 return val_ticks + float(val_rem) * tick_secs / tick_wake | 204 return val_ticks + float(val_rem) * tick_secs / tick_wake |
208 | 205 |
209 def parse(lines): | 206 def parse(params): |
210 | 207 |
211 start_time = time.time() | 208 start_time = time.time() |
212 | 209 |
213 debugf = record_debug(lines) | 210 debugf = record_debug(params) |
214 | 211 |
215 entries = dict(l.split('=', 1) for l in lines) | 212 remote_now = params['now'] |
216 if len(entries) != len(lines): | 213 |
217 raise Exception("Keys are not unique") | 214 time_diff = start_time - remote_now |
218 | 215 |
219 if 'sensors' not in entries: | 216 # readings is [ ({sensorname: value, ...}, time), ... ] |
220 # only debug info, it's been recorded | 217 readings = params['readings'] |
221 return | 218 |
222 | 219 # measurements is {sensorname: [(time, value), ...], ...} |
223 num_sensors = int(entries['sensors']) | 220 measurements = {} |
224 num_measurements = int(entries['measurements']) | 221 for rs, t in readings: |
225 | 222 real_t = t + time_diff |
226 sensors = [entries['sensor_id%d' % n] for n in xrange(num_sensors)] | 223 for s, v in rs.iteritems(): |
227 | 224 measurements.getdefault(s, []).append((real_t, v)) |
228 meas = [] | 225 |
229 for s in sensors: | 226 # one-off measurements here |
230 meas.append([]) | 227 measurements['fridge_on'] = [ (time.time(), params['fridge_on']) ] |
231 | 228 |
232 for n in xrange(num_measurements): | 229 for s, vs in measurements.iteritems(): |
233 vals = [convert_ds18b20_12bit(x) for x in entries["meas%d" % n].strip().split()] | 230 sensor_update(s, vs) |
234 if len(vals) != num_sensors: | |
235 raise Exception("Wrong number of sensors for measurement %d" % n) | |
236 # we make an array of values for each sensor | |
237 for s in xrange(num_sensors): | |
238 meas[s].append(vals[s]) | |
239 | |
240 avr_now = time_rem('now', entries) | |
241 avr_first_time = time_rem('first_time', entries) | |
242 avr_comms_time = time_rem('comms_time', entries) | |
243 time_step = float(entries['time_step']) | |
244 | |
245 debugf.write('now %f, comms_time %f, first_time %f, delta %f\n' % | |
246 (avr_now, avr_comms_time, avr_first_time, avr_now - avr_comms_time)) | |
247 | |
248 if 'avrtemp' in entries: | |
249 avrtemp = val_scale(int(entries['avrtemp'])) | |
250 sensor_update('avrtemp', [avrtemp], time.time(), 1) | |
251 | |
252 if 'voltage' in entries: | |
253 voltage = 0.001 * int(entries['voltage']) | |
254 sensor_update('voltage', [voltage], time.time(), 1) | |
255 | |
256 if 'fridge_status' in entries: | |
257 fridge_on = int(entries['fridge_status']) | |
258 sensor_update('fridge_on', [fridge_on], time.time(), 1) | |
259 | |
260 if 'fridge' in entries: | |
261 fridge_setpoint = float(entries['fridge']) | |
262 sensor_update('fridge_setpoint', [fridge_setpoint], time.time(), 1) | |
263 #sqlite | |
264 # - time | |
265 # - voltage | |
266 # - boot time | |
267 | |
268 first_real_time = time.time() - (avr_now - avr_first_time) | |
269 | |
270 for sensor_id, measurements in zip(sensors, meas): | |
271 # XXX sqlite add | |
272 sensor_update(sensor_id, measurements, first_real_time, time_step) | |
273 | 231 |
274 timedelta = time.time() - start_time | 232 timedelta = time.time() - start_time |
275 debugf.write("Updated %d sensors in %.2f secs\n" % (len(sensors), timedelta)) | 233 debugf.write("Updated %d sensors in %.2f secs\n" % (len(sensors), timedelta)) |
276 debugf.flush() | 234 debugf.flush() |