diff options
author | yvesf <yvesf@d0e8fea9-7529-0410-93fb-d39fd5b9c1dd> | 2010-06-09 11:14:18 +0000 |
---|---|---|
committer | yvesf <yvesf@d0e8fea9-7529-0410-93fb-d39fd5b9c1dd> | 2010-06-09 11:14:18 +0000 |
commit | feebdc2e14426921a585c31c5279886bca3e2eca (patch) | |
tree | 82fbb3700d5d43d1d302d95c639d0d540b7bcabb /ebus | |
parent | 602182ed488c9713d793206f6da2f3758051c32d (diff) | |
download | ebus-alt-feebdc2e14426921a585c31c5279886bca3e2eca.tar.gz ebus-alt-feebdc2e14426921a585c31c5279886bca3e2eca.zip |
foo
git-svn-id: http://10.2.2.13/svn/common/ebus@1676 d0e8fea9-7529-0410-93fb-d39fd5b9c1dd
Diffstat (limited to 'ebus')
-rw-r--r-- | ebus/web/controller.py | 98 | ||||
-rw-r--r-- | ebus/web/model.py | 41 |
2 files changed, 139 insertions, 0 deletions
diff --git a/ebus/web/controller.py b/ebus/web/controller.py new file mode 100644 index 0000000..f3fa438 --- /dev/null +++ b/ebus/web/controller.py @@ -0,0 +1,98 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +import sys, os, time +sys.path.insert(0,"lib") +from itty import itty +from werkzeug.contrib.cache import SimpleCache +from simplejson import dumps as dump_json + +""" +def data(name): + if name in ( + "solarDaten.tempKollektor", + "solarDaten.tempWarmwasserSolar", + "betriebsdatenRegler1.boilerTemperatur", + "betriebsdatenRegler1.kesselTemperatur", + "betriebsdatenRegler1.aussenTemperatur", + "betriebsdatenFeuerungsautomat.brauchwasserSollwert", + "betriebsdatenFeuerungsautomat.kesselSollwertTemperatur"): + m = Code(''' + function () { + emit(parseInt(this.time/900)*900000, {sum: this.values[field], count:1}); + }''') + r = Code(''' + function (key, values) { + var n = { sum:0, count:0 }; + for (var i = 0; i<values.length; i++) { + n.sum += values[i].sum; + n.count += values[i].count; + }; + return n; + }''') + f = Code(''' + function (who,r) { + r.avg = r.sum/r.count; + return r; + }''') + q={ + "name":name.split(".")[0], + "time":{"$gt": ( time.time() - (72*3600))}, + } + fs=["name", "values.%s"%name.split(".")[1]] + s={ + "field":name.split(".")[1], + } + print "query q=%s fs=%s s=%s" % (q,fs,s) + collection.ensure_index([('time',pymongo.ASCENDING),('name',pymongo.ASCENDING)]) + data = collection.map_reduce(m,r,query=q,finalize=f,fields=fs,scope=s).find().sort('time',pymongo.ASCENDING) + return map(lambda it: [it['_id'], it['value']['avg']], data) + elif name in ( + "betriebsdatenFeuerungsautomat.betriebszustand.reduced", + "solarDaten.solarPumpe.reduced"): + pName = name.split(".")[0] + pField = name.split(".")[1] + q={ + "name":pName, + "time":{"$gt":(time.time()-(72*3600))} + } + fs=["values.%s"%pField, "time"] + collection.ensure_index([('time',pymongo.ASCENDING),('name',pymongo.ASCENDING)]) + data=collection.find(q,fields=fs).sort('time', pymongo.ASCENDING) + #TODO.map(Code('''function(it) { return [it.time, it.values.betriebszustand]; }''')) + data=map(lambda it: [it['time']*1000, it['values'][pField]], data) + + #Fast die ände auf einen Eintrag pro Änderung zusammen + def reduce(data): + prev = None + for item in data: + if item[1] != prev: + prev = item[1] + yield item + + return list(reduce(data)) + else: + return [] +""" + + +@itty.get("/") +def index(req): + filename=os.path.join(os.path.dirname(__file__), "static", "index.html") + return open(filename,"r").read() + +json_cache = SimpleCache(default_timeout=20) +@itty.get('/json/<name>') +def json(req,name): + value = json_cache.get(name) + if value is None: + value = deny.Response(dump_json(dict(map(lambda r: (r, {"label":r, "data":data(r)}), name.split(",")))), mimetype='application/json') + json_cache.set(name, value) + return value + +@itty.get("/static/(?P<filename>.+)") +def static(request,filename): + file = itty.static_file(filename, root=os.path.join(os.path.dirname(__file__), 'static')) + return itty.Response(file, content_type=itty.content_type(file)) + +if __name__ == '__main__': + sys.exit(itty.run_itty(host="0.0.0.0",port=5000)) diff --git a/ebus/web/model.py b/ebus/web/model.py new file mode 100644 index 0000000..b8405dc --- /dev/null +++ b/ebus/web/model.py @@ -0,0 +1,41 @@ +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 + +Base = declarative_base() + +class Sensor(Base): + __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 "<Sensor('%s','%s','%s')>" % (self.id, self.name, self.description) + + + + +class Value(Base): + __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)) + value = Column(Float(precision=4)) + + def __init__(self, sensor, value, timestamp=datetime.now()): + self.sensor = sensor + self.value = value + self.timestamp = timestamp + + def __repr__(self): + return "<Value('%s','%s','%s','%s'>" % (self.id, self.sensor, self.value, self.timestamp) + |