27
|
1 import rrdtool |
|
2 import os |
|
3 import sys |
|
4 |
28
|
5 import config |
27
|
6 |
|
7 def sensor_rrd_path(s): |
28
|
8 return '%s/sensor_%s.rrd' % (config.DATA_PATH, s) |
27
|
9 |
|
10 def create_rrd(sensor_id): |
|
11 rrdtool.create(sensor_rrd_path(sensor_id), '-s', '300', |
|
12 'DS:temp:GAUGE:600:-10:100', |
|
13 'RRA:AVERAGE:0.5:1:1051200') |
|
14 |
|
15 def sensor_update(sensor_id, measurements, first_real_time, time_step): |
|
16 try: |
|
17 open(sensor_rrd_path(sensor_id)) |
|
18 except IOError, e: |
|
19 create_rrd(sensor_id) |
|
20 |
|
21 value_text = ' '.join('%f:%f' % p for p in |
|
22 zip(measurements, |
|
23 (first_real_time + time_step*t for t in xrange(len(measurements))))) |
|
24 |
|
25 rrdtool.update(sensor_rrd_path(sensor_id), value_text) |
|
26 |
|
27 def parse(lines): |
|
28 entries = dict(l.split('=', 1) for l in lines) |
|
29 if len(entries) != len(lines); |
|
30 raise Exception("Keys are not unique") |
|
31 |
|
32 num_sensors = int(entries['sensors']) |
|
33 num_measurements = int(entries['sensors']) |
|
34 |
|
35 sensor_ids = [entries['sensor_id%d' % n] for n in xrange(num_sensors)] |
|
36 |
|
37 meas = [] |
|
38 for s in sensors: |
|
39 meas.append([]) |
|
40 |
28
|
41 def val_scale(v): |
|
42 # convert decidegrees to degrees |
|
43 return 0.1 * v |
|
44 |
27
|
45 for n in xrange(num_measurements): |
28
|
46 vals = [val_scale(int(entries["meas%d" % n].strip().split()))] |
27
|
47 if len(vals) != num_sensors: |
|
48 raise Exception("Wrong number of sensors for measurement %d" % n) |
|
49 # we make an array of values for each sensor |
|
50 for s in xrange(num_sensors): |
|
51 meas[s].append(vals[s]) |
|
52 |
|
53 avr_now = float(entries['now') |
|
54 avr_first_time = float(entries['first_time']) |
|
55 time_step = float(entries['time_step']) |
|
56 |
|
57 first_real_time = time.time() - (avr_now - avr_first_time) |
|
58 |
|
59 for sensor_id, measurements in zip(sensors, meas): |
|
60 sensor_update(sensor_id, measurements, first_real_time, time_step) |