diff options
-rw-r--r-- | ebus/web/controller.py | 25 |
1 files changed, 16 insertions, 9 deletions
diff --git a/ebus/web/controller.py b/ebus/web/controller.py index f581e13..c5b50d2 100644 --- a/ebus/web/controller.py +++ b/ebus/web/controller.py @@ -8,7 +8,6 @@ from sqlalchemy.orm import sessionmaker from ebus.web import model - Session = sessionmaker() class CacheDecorator(object): @@ -29,14 +28,8 @@ class CacheDecorator(object): self.func = func return self.cachedFunc -def data(sensor, from_date=None, till_date=None): - session = Session() - values = session.query(model.Value)\ - .join(model.Sensor)\ - .filter(model.Sensor.name == sensor)\ - .all() - return {"sensor":sensor, "from_date":from_date, "till_date":till_date, "values":values} +## Web Requests @itty.get("/") def index(req): filename=os.path.join(os.path.dirname(__file__), "static", "index.html") @@ -49,7 +42,19 @@ def index(req): timeout=20) def json(req,sensor=None,from_date=None,till_date=None): print "sensor=%s from_date=%s till_date=%s" % (sensor,from_date,till_date) - return itty.Response(dump_json(data(sensor)), + values = Session().query(model.Value).join(model.Sensor) + + if sensor: + values = values.filter(model.Sensor.name == sensor)\ + + if from_date: + values = values.filter(model.Value.timestamp >= from_date) + + if till_date: + values = values.filter(model.Value.timestamp <= till_date) + + response = {"sensor":sensor, "from_date":from_date, "till_date":till_date, "values":values.all()} + return itty.Response(dump_json(response), content_type="application/json") @itty.get("/static/(?P<filename>.+)") @@ -58,6 +63,8 @@ def static(request,filename): return itty.Response(file, content_type=itty.content_type(file)) + +## Start method def run(db_engine,host="0.0.0.0",port=5000): Session.configure(bind=db_engine) return itty.run_itty(host=host,port=port) |