From 4f67e6e30f3300c948afbc498ac7fb71ea8e431f Mon Sep 17 00:00:00 2001 From: Yves Fischer Date: Tue, 6 Dec 2011 19:31:51 +0100 Subject: svg demo --- ebus/webapp/__init__.py | 42 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 6 deletions(-) (limited to 'ebus/webapp/__init__.py') diff --git a/ebus/webapp/__init__.py b/ebus/webapp/__init__.py index 1435912..d0e0ff6 100644 --- a/ebus/webapp/__init__.py +++ b/ebus/webapp/__init__.py @@ -119,8 +119,6 @@ def stream(soup, startdate=None): cursor = conn.cursor() cursor.execute("LISTEN evt_ebus_value_insert;") - print "Waiting for notifications on channel 'evt_ebus_value_insert'" - values = [] if select.select([conn],[],[],10) == ([],[],[]): time_stop = datetime.datetime.now() @@ -132,17 +130,49 @@ def stream(soup, startdate=None): if notify: sql = text("""SELECT sensor.name, value.timestamp, - COALESCE(value.value_int,value.value_float) "value_real" + COALESCE(value.value_int,value.value_float) "value_real", + value_string FROM value, sensor WHERE value.sensor_id = sensor.id AND timestamp >= :time_start AND timestamp < :time_stop""") - values = connection.execute(sql, time_start=time_start, time_stop=time_stop) - values = map(lambda row: (row.name, maketime(row.timestamp), row.value_real != None and row.value_real.__float__() or None), - values) + values = map(lambda row: { + "name":row.name, + "timestamp":maketime(row.timestamp), + "value_real":row.value_real != None and row.value_real.__float__() or None, + "value_string":row.value_string + }, + connection.execute(sql, time_start=time_start, time_stop=time_stop)) cursor.close() return {'time_start' : maketime(time_start), 'time_stop':maketime(time_stop), 'data':values} +@app.route("/all_values") +def all_values(soup): + conn = soup.connection() + + sql = text(""" + SELECT sensor.name, + value.timestamp, + COALESCE(value.value_int,value.value_float) "value_real", + value_string + FROM value, sensor, (SELECT MAX(timestamp) as timestamp, + sensor_id + FROM value + WHERE timestamp > CURRENT_TIMESTAMP - '15 minutes'::interval + GROUP BY sensor_id) last_value + WHERE value.timestamp = last_value.timestamp + AND value.sensor_id = last_value.sensor_id + AND value.sensor_id = sensor.id""") + + values = map(lambda row: { + "name":row.name, + "timestamp":maketime(row.timestamp), + "value_real":row.value_real != None and row.value_real.__float__() or None, + "value_string":row.value_string}, + conn.execute(sql)) + + return {'data':values} + # vim: autoindent tabstop=4 shiftwidth=4 expandtab softtabstop=4 filetype=python -- cgit v1.2.1