summaryrefslogtreecommitdiff
path: root/ebus/webapp/__init__.py
blob: 76cec16fe0bbe572076d881b05c8e2b530ad901b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# -*- coding: utf-8 -*-

import bottle 
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

from ebus.sql import model
import os
import json
import datetime
import time
app = bottle.Bottle("ebus")
bottle.debug()

engine = create_engine("postgresql://ebus:ebus@10.2.2.26/ebus", echo=True)
Session = sessionmaker()
Session.configure(bind=engine)

@app.route('/')
def index_file():
	return static_files("index.html")

@app.route('/static/:filename')
def static_files(filename):
	return bottle.static_file(filename, root=os.path.join(os.path.dirname(__file__),"static"))

@app.route('/sensor/:name')
def sensor_data_single(name):
	#s = Session()
	#sensorid = s.query(model.Sensor).filter(model.Sensor.name == name).first()
	#values = Session().query(model.Value).add_columns("timestamp","value_float","value_string","value_int")
	#values = values.filter(model.Value.sensor == sensorid)
	#return json.dumps(values.first().value_int)
	return sensor_data_fromto(name,None,None)

@app.route('/sensor/:name/:startdate/:enddate')
def sensor_data_fromto(name,startdate,enddate):
	s = Session()
        sensorid = s.query(model.Sensor).filter(model.Sensor.name == name).first()
	values = Session().query(model.Value).add_columns("timestamp","value_float","value_string","value_int")
        values = values.filter(model.Value.sensor == sensorid)
	
	if startdate == None or enddate == None:
		queried_values = []
		queried_values.append(values.first())
	else:
		now = datetime.datetime.now()
		f = datetime.datetime.fromtimestamp(float(startdate))
		t = datetime.datetime.fromtimestamp(float(enddate))
		values = values.filter(model.Value.timestamp.between(f,t))
		queried_values = values.all()
	v = []
	for value in queried_values:
		if value.value_float != None:
			v.append([int((time.mktime(value.timestamp.timetuple())+time.altzone*-1)*1000),value.value_float]) # time.altzone ist negativ
		elif value.value_int != None:
			v.append([int((time.mktime(value.timestamp.timetuple())+time.altzone*-1)*1000),value.value_int])
		elif value.value_string != None:
			v.append([int((time.mktime(value.timestamp.timetuple())+time.altzone*-1)*1000),value.value_string])
	return json.dumps(v)