summaryrefslogtreecommitdiff
path: root/ebus/webapp/__init__.py
diff options
context:
space:
mode:
Diffstat (limited to 'ebus/webapp/__init__.py')
-rw-r--r--ebus/webapp/__init__.py47
1 files changed, 30 insertions, 17 deletions
diff --git a/ebus/webapp/__init__.py b/ebus/webapp/__init__.py
index d0e0ff6..9c98ac3 100644
--- a/ebus/webapp/__init__.py
+++ b/ebus/webapp/__init__.py
@@ -9,13 +9,16 @@ from sqlalchemy import text
app = bottle.Bottle("ebus")
-def maketime(datetime):
- """rechnet zeit so um wie Javascript es braucht XXX"""
- return (time.mktime(datetime.timetuple())+time.altzone*-1)*1000
+def maketime(dt):
+ """Rechnet CET DateTime in CET timestamp"""
+ return time.mktime(dt.timetuple())*1000 # - time.altzone)*1000
def parsetime(timestamp):
- """macht aus javascript zeit systemzeit"""
- return ((timestamp/1000) - time.altzone*-1)
+ """Machtaus CET timestamp Local DateTime"""
+ return datetime.datetime.fromtimestamp((timestamp/1000)) # + time.altzone)
+
+def now(conn):
+ return conn.execute(text("SELECT CURRENT_TIMESTAMP t")).first().t
@app.route('/')
def index_file():
@@ -108,11 +111,12 @@ 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
+
+ time_start = startdate != None and parsetime(float(startdate)) \
+ or now(connection)
+
conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
@@ -120,14 +124,20 @@ def stream(soup, startdate=None):
cursor.execute("LISTEN 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:
+ fails = 0
+ while fails < 5:
+ if select.select([conn],[],[],10) == ([],[],[]):
+ time_stop = now(connection)
+ fails += 1
+ else:
+ conn.poll()
+
+ notify = conn.notifies.pop()
+ if not notify:
+ continue
+
+ time_stop = now(connection)
+ print "time_stop %s"%time_stop
sql = text("""SELECT sensor.name,
value.timestamp,
COALESCE(value.value_int,value.value_float) "value_real",
@@ -144,6 +154,8 @@ def stream(soup, startdate=None):
},
connection.execute(sql, time_start=time_start, time_stop=time_stop))
+ break
+
cursor.close()
return {'time_start' : maketime(time_start), 'time_stop':maketime(time_stop),
'data':values}
@@ -166,6 +178,7 @@ def all_values(soup):
AND value.sensor_id = last_value.sensor_id
AND value.sensor_id = sensor.id""")
+ time_stop = now(conn)
values = map(lambda row: {
"name":row.name,
"timestamp":maketime(row.timestamp),
@@ -173,6 +186,6 @@ def all_values(soup):
"value_string":row.value_string},
conn.execute(sql))
- return {'data':values}
+ return {'data':values, 'time_stop':maketime(time_stop)}
# vim: autoindent tabstop=4 shiftwidth=4 expandtab softtabstop=4 filetype=python