From a85541d9f7622813ec98bf042164414edc1fb366 Mon Sep 17 00:00:00 2001 From: yvesf Date: Sat, 25 Jun 2011 17:14:05 +0200 Subject: web -> bottle --- bin/ebus_web.py | 10 ------- bin/web.py | 7 +++++ ebus/sql/__init__.py | 0 ebus/sql/model.py | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++ ebus/web/__init__.py | 67 ++++++++++++++++++++++++++++++++++++++++++ ebus/web/controller.py | 17 ++++++++--- ebus/web/model.py | 79 -------------------------------------------------- 7 files changed, 166 insertions(+), 93 deletions(-) delete mode 100755 bin/ebus_web.py create mode 100644 bin/web.py create mode 100644 ebus/sql/__init__.py create mode 100644 ebus/sql/model.py delete mode 100644 ebus/web/model.py diff --git a/bin/ebus_web.py b/bin/ebus_web.py deleted file mode 100755 index 74fe9b9..0000000 --- a/bin/ebus_web.py +++ /dev/null @@ -1,10 +0,0 @@ -#!/usr/bin/env python -import sys -from ebus.web import controller - -from sqlalchemy import create_engine - -#db_engine = create_engine('sqlite:///:memory:', echo=True) -db_engine = create_engine("postgresql://yvesf:iii@10.2.2.13:5432/yvesf", echo=True) - -sys.exit(controller.run(db_engine)) diff --git a/bin/web.py b/bin/web.py new file mode 100644 index 0000000..c0e2f5d --- /dev/null +++ b/bin/web.py @@ -0,0 +1,7 @@ +#!/usr/bin/python +# coding: utf-8 +import bottle +from ebus import web + +if __name__ == "__main__": + bottle.run(web.app, host="0.0.0.0", port=8000, reloader=True) diff --git a/ebus/sql/__init__.py b/ebus/sql/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/ebus/sql/model.py b/ebus/sql/model.py new file mode 100644 index 0000000..7e21e82 --- /dev/null +++ b/ebus/sql/model.py @@ -0,0 +1,79 @@ +from datetime import datetime +from sqlalchemy import Column, Integer, Float, DateTime, String, ForeignKey +from sqlalchemy.orm import relationship, backref +from sqlalchemy.ext.declarative import declarative_base + +ModelBase = declarative_base() + +class Sensor(ModelBase): + __tablename__ = 'sensor' + + id = Column(Integer, primary_key=True) + name = Column(String) + description = Column(String, default="") + + def __init__(self, name, description=""): + self.name = name + self.description = description + + def __repr__(self): + return "" % (self.id, self.name, self.description) + +# http://www.sqlalchemy.org/docs/05/reference/ext/declarative.html#inheritance-configuration +class Value(ModelBase): + __tablename__ = 'value' + + id = Column(Integer, primary_key=True) + timestamp = Column(DateTime) + sensor_id = Column(Integer, ForeignKey("sensor.id")) + sensor = relationship(Sensor, backref=backref('values', order_by=timestamp)) + + discriminator = Column('type', String(50)) + __mapper_args__ = {'polymorphic_on': discriminator} + + def __init__(self, sensor, timestamp=datetime.now()): + self.timestamp = timestamp + self.sensor = sensor + + def __repr__(self): + return "" % (self.id, self.sensor, self.value, self.timestamp) + + def value(self): + raise NotImplementedException() + +class ValueFloat(Value): + __mapper_args__ = {'polymorphic_identity': 'float'} + value_float = Column(Float(precision=4)) + + def __init__(self, sensor, value, timestamp=datetime.now()): + Value.__init__(self, sensor, timestamp) + self.value_float = value + + def value(self): + return self.value_float + +class ValueInt(Value): + __mapper_args__ = {'polymorphic_identity': 'int'} + value_int = Column(Integer) + + def __init__(self, sensor, value, timestamp=datetime.now()): + Value.__init__(self, sensor, timestamp) + self.value_int = value + + def value(self): + return self.value_int + +class ValueString(Value): + __mapper_args__ = {'polymorphic_identity': 'string'} + value_string = Column(String) + + def __init__(self, sensor, value, timestamp=datetime.now()): + Value.__init__(self, sensor, timestamp) + self.value_string = value + + def value(self): + return self.value_string + + + + diff --git a/ebus/web/__init__.py b/ebus/web/__init__.py index e69de29..cfec3f0 100644 --- a/ebus/web/__init__.py +++ b/ebus/web/__init__.py @@ -0,0 +1,67 @@ +# coding: utf-8 +import os +import time +import datetime +import calendar + +import bottle +from sqlalchemy import create_engine +from sqlalchemy.orm import sessionmaker + +from ebus.sql import model + +app = bottle.Bottle("ebus") +bottle.debug() + +engine = create_engine("postgresql://jack:jack@10.2.2.22:5432/ebus",echo=True) +Session = sessionmaker() +Session.configure(bind=engine) + +@app.route("/") +def do_index(): + return serve_static("index.html") + +@app.route('/static/:filename') +def serve_static(filename): + return bottle.static_file(filename, + root=os.path.join(os.path.dirname(__file__),"static")) + +@app.route("/list") +def list(): + sensors = Session().query(model.Sensor).all() + return {"type":"list","data":map(lambda sensor: sensor.name, sensors)} + +@app.route("/:when#today|yesterday|week|month#/:sensor") +def get_value(when,sensor): + s = Session() + sensor = s.query(model.Sensor).filter(model.Sensor.name == sensor).first() + values = Session().query(model.Value).add_columns("value_float","value_string","value_int") + values = values.filter(model.Value.sensor == sensor) + + now = datetime.datetime.now() + f,t = None, None + if when == "today": + f = now.date() + t = now.date() + datetime.timedelta(1) + elif when == "yesterday": + f = now.date() - datetime.timedelta(1) + t = now + elif when == "week": + f = now.date() - datetime.timedelta(now.isoweekday()) + t = now.date() + datetime.timedelta(7-now.isoweekday()) + elif when == "month": + f = now.date() - datetime.timedelta(now.day) + t = f + datetime.timedelta( calendar.monthrange(f.year, f.month)[1] ) + else: + raise Exception("illegal timerange") + + values = values.filter(model.Value.timestamp.between(f,t)) + x = values.all() + + v=[] + for value in values.all(): + v.append(value.value()) + return {"type":when,"data":v} + + + diff --git a/ebus/web/controller.py b/ebus/web/controller.py index 683bb6c..99d7053 100644 --- a/ebus/web/controller.py +++ b/ebus/web/controller.py @@ -6,7 +6,7 @@ from werkzeug.contrib.cache import SimpleCache from simplejson import dumps as dump_json from sqlalchemy.orm import sessionmaker -from ebus import model +from ebus.web import model Session = sessionmaker() @@ -35,13 +35,14 @@ def index(req): filename=os.path.join(os.path.dirname(__file__), "static", "index.html") return open(filename,"r").read() -@itty.get('/json/(?P[^/]+)') -@itty.get('/json/(?P[^/]+)/from_date/(?P[^/]+)') -@itty.get('/json/(?P[^/]+)/from_date/(?P[^/]+)/till_date/(?P[^/]+)') +#@itty.get('/json/(?P[^/]+)') +#@itty.get('/json/(?P[^/]+)/from_date/(?P[^/]+)') +#@itty.get('/json/(?P[^/]+)/from_date/(?P[^/]+)/till_date/(?P[^/]+)') @CacheDecorator(key=lambda args,kwargs: tuple(kwargs.keys()) + tuple(kwargs.values()), 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) + """ values = Session().query(model.Value).join(model.Sensor) if sensor: @@ -56,9 +57,17 @@ def json(req,sensor=None,from_date=None,till_date=None): response = {"sensor":sensor, "from_date":from_date, "till_date":till_date, "values":values.all()} return itty.Response(dump_json(response), content_type="application/json") + """ + return itty.Response("") + +@itty.get('/json/list') +def list(req): + sensors = Session().query(model.Sensor) + return str(sensors.all()) @itty.get("/static/(?P.+)") def static(request,filename): + return "" file = itty.static_file(filename, root=os.path.join(os.path.dirname(__file__), 'static')) return itty.Response(file, content_type=itty.content_type(file)) diff --git a/ebus/web/model.py b/ebus/web/model.py deleted file mode 100644 index 7e21e82..0000000 --- a/ebus/web/model.py +++ /dev/null @@ -1,79 +0,0 @@ -from datetime import datetime -from sqlalchemy import Column, Integer, Float, DateTime, String, ForeignKey -from sqlalchemy.orm import relationship, backref -from sqlalchemy.ext.declarative import declarative_base - -ModelBase = declarative_base() - -class Sensor(ModelBase): - __tablename__ = 'sensor' - - id = Column(Integer, primary_key=True) - name = Column(String) - description = Column(String, default="") - - def __init__(self, name, description=""): - self.name = name - self.description = description - - def __repr__(self): - return "" % (self.id, self.name, self.description) - -# http://www.sqlalchemy.org/docs/05/reference/ext/declarative.html#inheritance-configuration -class Value(ModelBase): - __tablename__ = 'value' - - id = Column(Integer, primary_key=True) - timestamp = Column(DateTime) - sensor_id = Column(Integer, ForeignKey("sensor.id")) - sensor = relationship(Sensor, backref=backref('values', order_by=timestamp)) - - discriminator = Column('type', String(50)) - __mapper_args__ = {'polymorphic_on': discriminator} - - def __init__(self, sensor, timestamp=datetime.now()): - self.timestamp = timestamp - self.sensor = sensor - - def __repr__(self): - return "" % (self.id, self.sensor, self.value, self.timestamp) - - def value(self): - raise NotImplementedException() - -class ValueFloat(Value): - __mapper_args__ = {'polymorphic_identity': 'float'} - value_float = Column(Float(precision=4)) - - def __init__(self, sensor, value, timestamp=datetime.now()): - Value.__init__(self, sensor, timestamp) - self.value_float = value - - def value(self): - return self.value_float - -class ValueInt(Value): - __mapper_args__ = {'polymorphic_identity': 'int'} - value_int = Column(Integer) - - def __init__(self, sensor, value, timestamp=datetime.now()): - Value.__init__(self, sensor, timestamp) - self.value_int = value - - def value(self): - return self.value_int - -class ValueString(Value): - __mapper_args__ = {'polymorphic_identity': 'string'} - value_string = Column(String) - - def __init__(self, sensor, value, timestamp=datetime.now()): - Value.__init__(self, sensor, timestamp) - self.value_string = value - - def value(self): - return self.value_string - - - - -- cgit v1.2.1