diff web/log.py @ 61:62112fc2af21

sort out voltage logging
author Matt Johnston <matt@ucc.asn.au>
date Tue, 26 Jun 2012 08:08:48 +0800
parents 79761ee67134
children 43ec670f1b75
line wrap: on
line diff
--- a/web/log.py	Mon Jun 25 01:23:34 2012 +0800
+++ b/web/log.py	Tue Jun 26 08:08:48 2012 +0800
@@ -25,13 +25,17 @@
 def create_rrd(sensor_id):
     # start date of 10 seconds into 1970 is used so that we can
     # update with prior values straight away.
-    args = [sensor_rrd_path(sensor_id), 
-                    '--start', '10',
-                    '--step', '300',
-                    'DS:temp:GAUGE:600:-10:100',
-                    'RRA:AVERAGE:0.5:1:1051200']
+    if 'voltage' in sensor_id:
+        args = [ 'DS:temp:GAUGE:7200:-1:10',
+                'RRA:AVERAGE:0.9999:1:1051200']
+    else:
+        args = [ 'DS:temp:GAUGE:600:-10:100',
+                'RRA:AVERAGE:0.5:1:1051200']
 
-    rrdtool.create(*args)
+    rrdtool.create(sensor_rrd_path(sensor_id), 
+                '--start', '10',
+                '--step', '300',
+                *args)
 
 # stolen from viewmtn, stolen from monotone-viz
 def colour_from_string(str):
@@ -47,9 +51,18 @@
     rrds = all_sensors()
 
     graph_args = []
+    have_volts = False
     for n, (rrdfile, sensor) in enumerate(rrds):
-        vname = 'temp%d' % n
-        graph_args.append('DEF:%(vname)s=%(rrdfile)s:temp:AVERAGE' % locals())
+        if 'avrtemp' in sensor:
+            continue
+        if 'voltage' in sensor:
+            have_volts = True
+            vname = 'scalevolts'
+            graph_args = ['DEF:rawvolts=%(rrdfile)s:temp:AVERAGE:step=3600' % locals(),
+                        'CDEF:scalevolts=rawvolts,0.2,/'] + graph_args
+        else:
+            vname = 'temp%d' % n
+            graph_args.append('DEF:%(vname)s=%(rrdfile)s:temp:AVERAGE' % locals())
         width = config.LINE_WIDTH
         legend = config.SENSOR_NAMES.get(sensor, sensor)
         colour = config.SENSOR_COLOURS.get(legend, colour_from_string(sensor))
@@ -62,16 +75,22 @@
         '-h', str(config.GRAPH_HEIGHT),
         '--slope-mode',
         '--border', '0',
+        '--vertical-label', 'Temperature',
         '--y-grid', '1:1',
         '--grid-dash', '1:0',
         '--color', 'GRID#00000000',
         '--color', 'MGRID#aaaaaa',
         '--color', 'BACK#ffffff',
+        'VRULE:%d#ee0000' % time.time(),
         '--imgformat', 'PNG'] \
         + graph_args
     if config.GRAPH_FONT:
         args += ['--font', 'DEFAULT:11:%s' % config.GRAPH_FONT]
-    print>>sys.stderr, args
+    if have_volts:
+        args += ['--right-axis', '0.2:0', # matches the scalevolts CDEF above
+            '--right-axis-format', '%.2lf',
+            '--right-axis-label', 'Voltage']
+
     rrdtool.graph(*args)
     return tempf.read()
 
@@ -81,15 +100,12 @@
     except IOError, e:
         create_rrd(sensor_id)
 
-    print>>sys.stderr, sensor_id, measurements, first_real_time, time_step
-
     if measurements:
         values = ['%d:%f' % p for p in 
             zip((first_real_time + time_step*t for t in xrange(len(measurements))),
                 measurements)]
 
         rrdfile = sensor_rrd_path(sensor_id)
-        print>>sys.stderr, values
         # XXX what to do here when it fails...
         for v in values:
             try:
@@ -142,6 +158,14 @@
     avr_first_time = float(entries['first_time'])
     time_step = float(entries['time_step'])
 
+    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)
+
     #sqlite 
     # - time
     # - voltage