From fab1f0878f2e1776f52667c481d3295ca829f593 Mon Sep 17 00:00:00 2001 From: Yves Fischer Date: Mon, 5 Dec 2011 23:15:38 +0100 Subject: console POC --- ebus/webapp/__init__.py | 47 +++++++++++++++++++++++++++++++++++++++++ ebus/webapp/static/console.html | 34 +++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 ebus/webapp/static/console.html (limited to 'ebus/webapp') diff --git a/ebus/webapp/__init__.py b/ebus/webapp/__init__.py index b639c66..1435912 100644 --- a/ebus/webapp/__init__.py +++ b/ebus/webapp/__init__.py @@ -13,6 +13,10 @@ def maketime(datetime): """rechnet zeit so um wie Javascript es braucht XXX""" return (time.mktime(datetime.timetuple())+time.altzone*-1)*1000 +def parsetime(timestamp): + """macht aus javascript zeit systemzeit""" + return ((timestamp/1000) - time.altzone*-1) + @app.route('/') def index_file(): return static_files("index.html") @@ -98,4 +102,47 @@ def sensor_data_cached_fromto(soup, name, timestamp_from): except Exception,e: return {'sensor':name, 'data':None, 'error':str(e)} +import select +import psycopg2 + +@app.route('/stream') +@app.route('/stream/:startdate') +def stream(soup, startdate=None): + time_start = startdate != None and datetime.datetime.fromtimestamp(parsetime(float(startdate))) \ + or datetime.datetime.now() + + connection = soup.connection() + conn = connection.connection + + conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT) + + 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() + else: + time_stop = datetime.datetime.now() + conn.poll() + + notify = conn.notifies.pop() + if notify: + sql = text("""SELECT sensor.name, + value.timestamp, + COALESCE(value.value_int,value.value_float) "value_real" + 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) + + cursor.close() + return {'time_start' : maketime(time_start), 'time_stop':maketime(time_stop), + 'data':values} + # vim: autoindent tabstop=4 shiftwidth=4 expandtab softtabstop=4 filetype=python diff --git a/ebus/webapp/static/console.html b/ebus/webapp/static/console.html new file mode 100644 index 0000000..97d6b63 --- /dev/null +++ b/ebus/webapp/static/console.html @@ -0,0 +1,34 @@ + + + + + +
+
+ + + -- cgit v1.2.1