From c112df16fbbb801d04db90585976eb650413a847 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Wed, 1 Aug 2018 11:00:53 +0200 Subject: Support variable number of sensors Signed-off-by: Florian Pritz --- plot.py | 103 +++++++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 66 insertions(+), 37 deletions(-) diff --git a/plot.py b/plot.py index 647a083..01def4c 100755 --- a/plot.py +++ b/plot.py @@ -27,28 +27,23 @@ argmax = 50000 #### start of config -# If you change this you also need to change the create and graph lists below. -# Also note that samples have to be in the same order every time (the key is currently ignored). -samples_per_line=2 - template_values = { # 0 = everything from the input # otherwise = only the last n "total_samples": 0, } -# The 4th field of the DS lines is the heartbeat. If you sample at an -# interval < that value you will not see a graph. Simply adjust if necessary. -create = [ +# heartbeat. If you sample at an interval < that value you will not see a +# graph. Simply adjust if necessary. +heartbeat = 50 + +create_prefix = [ # Index of the 0 after --start is used below. Adjust code if you # change the amount of values before it. 'rrdtool','create',rrd_file,'--step','30','--start',0, +] - 'DS:temp1:GAUGE:50:U:U', - 'DS:temp2:GAUGE:50:U:U', - #'DS:hum1:GAUGE:50:U:U', - 'DS:abshum1:GAUGE:50:U:U', - +create_suffix = [ 'RRA:AVERAGE:0.5:1:$total_samples', 'RRA:MIN:0.5:1:$total_samples', 'RRA:MAX:0.5:1:$total_samples', @@ -66,22 +61,18 @@ graph = [ '--right-axis-label', 'Humidity g/m³', '--right-axis', '0.5:0', #'--alt-autoscale', - '-l','10','-u','35', '--rigid', + '-l','-5','-u','30', '--rigid', '--left-axis-format', '%4.2lf', '--right-axis-format', '%4.2lf', - - 'DEF:temp1='+rrd_file+':temp1:AVERAGE', - 'LINE1:temp1#339966:temp 1\l', - 'DEF:temp2='+rrd_file+':temp2:AVERAGE', - 'LINE1:temp2#663399:temp 2\l', - #'DEF:hum1='+rrd_file+':hum1:AVERAGE', - #'CDEF:hum11=hum1,2,/', - #'LINE1:hum11#996633:hum 1\l', - 'DEF:abshum1='+rrd_file+':abshum1:AVERAGE', - 'CDEF:abshum11=abshum1,2,*', - 'LINE1:abshum11#996633:abshum 1\l', ] +colors = [ + '#339966', + '#663399', + '#993366', + '#336699', + ] + #### end of config def chunker(seq, size): @@ -93,27 +84,65 @@ if template_values["total_samples"] == 0: # load samples samples = [] +value_names = [] with open(log_file,"r") as f: + first_line = True for line in f: if calc_total_from_input: template_values["total_samples"] += 1 - elem = line.split(' ') + elements = line.split(' ') values = [] # timestamp - values.append(int(elem[0])) + values.append(int(elements[0])) # values - for i in range(1, samples_per_line + 1): - values.append(float(elem[i].split("=")[1])) + for i in range(1, len(elements)): + [key, value] = elements[i].split("=") + [elem_type, sensorID] = key.split(":") + if first_line: + value_names.append(key) + + if elem_type == "t": + values.append(float(value)) + elif elem_type == "h": + # absolute humidity + hum=float(value) + temp=values[i - 1] + # Source: https://carnotcycle.wordpress.com/2012/08/04/how-to-convert-relative-humidity-to-absolute-humidity/ + abshum=(6.112 * math.e**(17.67 * temp / (temp + 243.5)) * hum * 2.1674) / (273.15+temp) + values.append(abshum) + else: + raise ValueError("Unhandled element type") - # absolute humidity - hum=float(elem[3].split("=")[1]) - temp=values[2] - # Source: https://carnotcycle.wordpress.com/2012/08/04/how-to-convert-relative-humidity-to-absolute-humidity/ - abshum=(6.112 * math.e**(17.67 * temp / (temp + 243.5)) * hum * 2.1674) / (273.15+temp) - values.append(abshum) samples.append(values) + first_line = False + +create_values = [] +color_counter = 0 +for elem in value_names: + [elem_type, sensorID] = elem.split(":") + color = colors[color_counter] + color_counter = (color_counter + 1) % len(colors) + if elem_type == "t": + create_values.append("DS:temp{sensorID}:GAUGE:{heartbeat}:U:U".format(sensorID=sensorID, heartbeat=heartbeat)) + graph.extend([ + # TODO change color for each value. select from a list of defined colors + 'DEF:temp{sensorID}={rrd_file}:temp{sensorID}:AVERAGE'.format(sensorID=sensorID, rrd_file=rrd_file), + 'LINE1:temp{sensorID}{color}:temp {sensorID}\l'.format(sensorID=sensorID, color=color), + ]) + elif elem_type == "h": + create_values.append("DS:abshum{sensorID}:GAUGE:{heartbeat}:U:U".format(sensorID=sensorID, heartbeat=heartbeat)) + graph.extend([ + # TODO change color for each value. select from a list of defined colors + 'DEF:abshum{sensorID}={rrd_file}:abshum{sensorID}:AVERAGE'.format(sensorID=sensorID, rrd_file=rrd_file), + 'CDEF:abshum{sensorID}_1=abshum{sensorID},2,*'.format(sensorID=sensorID), + 'LINE1:abshum{sensorID}_1{color}:abshum {sensorID}\l'.format(sensorID=sensorID, color=color), + ]) + else: + raise ValueError("Unhandled element type") + +create = create_prefix + create_values + create_suffix # create rrd start_ts = samples[0][0] @@ -126,7 +155,7 @@ for elem in create: #print("creating rrdb:",create) ret = subprocess.call(cmd) if ret != 0: - #print("ERROR calling: {}".format(" ".join(create))) + print("ERROR calling: {}".format(" ".join(create))) sys.exit(1) @@ -139,7 +168,7 @@ for group in chunker(samples, argmax): ret = subprocess.call(cmd) if ret != 0: - #print("ERROR calling: {}".format(" ".join(cmd))) + print("ERROR calling: {}".format(" ".join(cmd))) sys.exit(1) @@ -148,7 +177,7 @@ graph[4] = str(start_ts) graph[6] = str(end_ts) ret = subprocess.call(graph) if ret != 0: - #print("ERROR calling: {}".format(" ".join(graph))) + print("ERROR calling: {}".format(" ".join(graph))) sys.exit(1) os.unlink(rrd_file) -- cgit v1.2.3-24-g4f1b