summaryrefslogtreecommitdiff
path: root/ebus/webapp
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
parenteceb26632fa33ac94cf5f4571ba8b68245d9b0e6 (diff)
downloadebus-alt-fab1f0878f2e1776f52667c481d3295ca829f593.tar.gz
ebus-alt-fab1f0878f2e1776f52667c481d3295ca829f593.zip
console POC
Diffstat (limited to 'ebus/webapp')
-rw-r--r--ebus/webapp/__init__.py47
-rw-r--r--ebus/webapp/static/console.html34
2 files changed, 81 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
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 @@
+<html>
+<head>
+ <script src="/static/lib/jquery-1.6.2/jquery-1.6.2.min.js"></script>
+</head>
+<body>
+<div id="log">
+</div>
+<script>
+ function log(data) {
+ for (var i in data) {
+ var sensor_name = data[i][0];
+ var timestamp = data[i][1];
+ var value = data[i][2];
+ var caption = "" + timestamp + ": " + sensor_name + " Value: " + value;
+
+ jQuery("div#log").append(
+ jQuery("<div>").text(caption));
+ }
+ }
+
+ function reload(time_stop) {
+ jQuery.getJSON("/stream/" + time_stop, function(resp) {
+ log(resp.data);
+ reload(resp.time_stop);
+ });
+ }
+
+ jQuery.getJSON("/stream", function(resp) {
+ log(resp.data);
+ reload(resp.time_stop);
+ });
+</script>
+</body>
+</html>