summaryrefslogtreecommitdiff
path: root/ebus/webhdf/__init__.py
diff options
context:
space:
mode:
authorEbus-at-dockstar <ebus@dockstar>2013-02-25 18:21:49 +0100
committerEbus-at-dockstar <ebus@dockstar>2013-02-25 18:21:49 +0100
commit13222c58142aa6b3bdf50525a250a81c4ab52d55 (patch)
treefde396c8cff36a944019f9615274c947400daff5 /ebus/webhdf/__init__.py
parent37e334d24a099e5367597ee18ac0c6d5ae2fba32 (diff)
downloadebus-alt-13222c58142aa6b3bdf50525a250a81c4ab52d55.tar.gz
ebus-alt-13222c58142aa6b3bdf50525a250a81c4ab52d55.zip
web hdf server
Diffstat (limited to 'ebus/webhdf/__init__.py')
-rw-r--r--ebus/webhdf/__init__.py56
1 files changed, 36 insertions, 20 deletions
diff --git a/ebus/webhdf/__init__.py b/ebus/webhdf/__init__.py
index 78a85ff..af13f87 100644
--- a/ebus/webhdf/__init__.py
+++ b/ebus/webhdf/__init__.py
@@ -4,6 +4,8 @@ import json
import datetime
import time
import itertools
+import logging
+logging.basicConfig(level=logging.DEBUG)
import numpy
@@ -15,17 +17,6 @@ datastore = ebus.datastore.Datastore("testhdffiles")
app = bottle.Bottle("ebus")
-def maketime(dt):
- """Rechnet CET DateTime in CET timestamp"""
- return time.mktime(dt.timetuple())*1000 # - time.altzone)*1000
-
-def parsetime(timestamp):
- """Macht aus 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():
return static_files("index.html")
@@ -34,6 +25,7 @@ def index_file():
def static_files(filename):
return bottle.static_file(filename, root=os.path.join(os.path.dirname(__file__),"static"))
+
@app.get('/sensor/:name')
def sensor_data_get(name):
try:
@@ -45,11 +37,12 @@ def sensor_data_get(name):
return {'sensor':name,'data':None, 'error':str(e)}
@app.put('/sensor/:name')
-def sensor_data_put(name):
+@app.put('/sensor/:name/:timestamp')
+def sensor_data_put(name,timestamp=None):
+ if not timestamp: timestamp = int(time.time())
try:
value = bottle.request.POST.value
type = bottle.request.POST.type
- timestamp = int(time.time())
if type == "int":
klass = ebus.datastore.ValueInt
@@ -61,29 +54,53 @@ def sensor_data_put(name):
return {'error':'INVALID_TYPE', msg:'Type {0} is invalid'.format(type)}
datastore.addValue(name, timestamp, value, klass, flush=True)
-
- return {'error':None,'msg':"Stored {0} of type {1} with timestamp {2} to {3}".format(value,type,timestamp,name)}
+ msg = "Stored {0} of type {1} with timestamp {2} to {3}".format(value,type,timestamp,name)
+ logging.info(msg)
+ return {'error':None,'msg':msg}
except Exception,e:
return {'error':e,'msg':e}
+
@app.route('/sensor/:name/:startdate/:enddate')
def sensor_name_start_end(name,startdate,enddate):
try:
startdate, enddate = int(startdate), int(enddate)
+ logging.info("/sensor/ start={0} end={1}".format(startdate, enddate))
table=datastore.getTable(name)
with datastore:
- data = [(x['timestamp']*1000, x['value'])
- for x in table.where("(timestamp >= startdate) & (timestamp <= enddate)")]
+ i = table.where("(timestamp >= startdate) & (timestamp <= enddate)",step=100)
+ timestamps = []
+ try:
+ for x in range(20):
+ i.next()
+ timestamps.append(i['timestamp'])
+ except:
+ pass
+ if len(timestamps) > 10:
+ diff = map(lambda (x1,x2): (x2-x1)/100, zip(timestamps[:-1], timestamps[1:]))
+ diff_avg = numpy.average(diff)
+ time_period = enddate - startdate
+ samples = time_period / diff_avg
+ step = numpy.ceil(samples / 400.0)
+
+ data = [(x['timestamp']*1000, x['value'])
+ for x in table.where("(timestamp >= startdate) & (timestamp <= enddate)", step=step)]
+ logging.info("diff={0} samples={1} step={2} len={3} ({4})".format(diff_avg, samples, step, len(data),name))
+ else:
+ logging.info("No data found ({0})".format(name))
+ data = []
return {'sensor':name, 'error':None,'data':data}
except Exception,e:
+ logging.error("Error: " + str(e) + str(type(e)))
return {'sensor':name,'data':None, 'error':str(e)}
@app.route('/avg/:name/:startdate')
-def sensor_avg_start(name, startdate):
- period = 60*15 # 15min
+@app.route('/avg/:name/:startdate/:period')
+def sensor_avg_start(name, startdate, period=60*15): #15min
try:
startdate, enddate = int(startdate), int(time.time())
+ logging.info("/avg/ start={0} end={1}".format(startdate, enddate))
table=datastore.getTable(name)
with datastore:
sel_rows = table.where("(timestamp >= startdate) & (timestamp <=enddate)")
@@ -95,4 +112,3 @@ def sensor_avg_start(name, startdate):
except Exception,e:
return {'sensor':name, 'error':str(e), 'data':None}
-# vim: autoindent tabstop=4 shiftwidth=4 expandtab softtabstop=4 filetype=python