summaryrefslogtreecommitdiff
path: root/ebus/webapp/__init__.py
diff options
context:
space:
mode:
authorYves Fischer <yvesf-git@xapek.org>2011-12-05 23:15:38 +0100
committerYves Fischer <yvesf-git@xapek.org>2011-12-05 23:15:38 +0100
commitfab1f0878f2e1776f52667c481d3295ca829f593 (patch)
tree1f2cce58ee0b337da22c31d649051674f7f6fedb /ebus/webapp/__init__.py
parenteceb26632fa33ac94cf5f4571ba8b68245d9b0e6 (diff)
downloadebus-alt-fab1f0878f2e1776f52667c481d3295ca829f593.tar.gz
ebus-alt-fab1f0878f2e1776f52667c481d3295ca829f593.zip
console POC
Diffstat (limited to 'ebus/webapp/__init__.py')
-rw-r--r--ebus/webapp/__init__.py47
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