diff options
Diffstat (limited to 'ebus/webapp/__init__.py')
-rw-r--r-- | ebus/webapp/__init__.py | 47 |
1 files changed, 47 insertions, 0 deletions
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 |