diff options
Diffstat (limited to 'heap/bin')
l--------- | heap/bin/ebus | 1 | ||||
-rwxr-xr-x | heap/bin/ebus_app.py | 41 | ||||
-rwxr-xr-x | heap/bin/ebus_dump.py | 49 | ||||
-rwxr-xr-x | heap/bin/web_dev.py | 13 | ||||
-rwxr-xr-x | heap/bin/web_prod.py | 9 | ||||
-rwxr-xr-x | heap/bin/wettermichel.py | 77 |
6 files changed, 190 insertions, 0 deletions
diff --git a/heap/bin/ebus b/heap/bin/ebus new file mode 120000 index 0000000..1232ff3 --- /dev/null +++ b/heap/bin/ebus @@ -0,0 +1 @@ +../ebus/
\ No newline at end of file diff --git a/heap/bin/ebus_app.py b/heap/bin/ebus_app.py new file mode 100755 index 0000000..c9b89f6 --- /dev/null +++ b/heap/bin/ebus_app.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python +# -*- coding:utf-8 -*- +# xapek.org, 2010 +import asyncore +from sqlalchemy import create_engine +from sqlalchemy.orm import sessionmaker +from ebus.web import model +from ebus import EbusReader + +#db_engine = create_engine('sqlite:///:memory:', echo=True) +db_engine = create_engine("postgresql://yvesf:iii@10.2.2.13:5432/yvesf", echo=True) + +session = sessionmaker(bind=db_engine)() + + +def insert(ebus_packet): + try: + for valueKey in ebus_packet.values().keys(): + print "Key", valueKey + sensor = session.query(model.Sensor).filter(model.Sensor.name==valueKey).first() + if not sensor: + print "Add sensor %s" % valueKey + sensor = model.Sensor(valueKey) + session.add(sensor) + print sensor + m_value = model.Value(sensor, ebus_packet.values()[valueKey]) + session.add(m_value) + print m_value + session.commit() + except Exception,e: + session.rollback() + try: + print "Failed to insert %s -- reason %s" % (ebus_packet.name(), e) + except Exception,e: + print e + +reader = EbusReader() +reader.handle_ebus = insert +reader.debug = False + +asyncore.loop() diff --git a/heap/bin/ebus_dump.py b/heap/bin/ebus_dump.py new file mode 100755 index 0000000..c170849 --- /dev/null +++ b/heap/bin/ebus_dump.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python +# -*- coding:utf8 -*- +import asyncore +import time +from ebus import EbusReader +from ebus.model.sql import ModelBase, EbusSQL, Value +from ebus.model.sql import EbusSQL +from sqlalchemy import create_engine +from sqlalchemy.orm import sessionmaker +import logging + +logging.basicConfig() + +#logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO) +logging.getLogger('sqlalchemy.engine').setLevel(logging.WARNING) + +logger = logging.getLogger('ebus.dump') +logger.setLevel(logging.INFO) + +#engine = create_engine('sqlite:///:memory:', echo=True) +engine = create_engine("postgresql:///ebus") + +ModelBase.metadata.create_all(engine) + +Session = sessionmaker() +Session.configure(bind=engine) +session = Session() + + +class MyEbusReader(EbusReader): + def __init__(self,*args,**kwargs): + EbusReader.__init__(self,*args,**kwargs) + self.debug = True + self.sql = EbusSQL(session) + def handle_ebus(self,ebus_packet): + for field in self.sql.get_values(ebus_packet): + session.add(field) + + session.commit() + + logger.info("packet %s" % ebus_packet.name()) + +MyEbusReader() +try: + asyncore.loop() +except KeyboardInterrupt: + pass +# for data in session.query(Value).all(): +# print data diff --git a/heap/bin/web_dev.py b/heap/bin/web_dev.py new file mode 100755 index 0000000..ea51519 --- /dev/null +++ b/heap/bin/web_dev.py @@ -0,0 +1,13 @@ +#!/usr/bin/env python +# coding: utf-8 +import bottle +from ebus import webhdf + +from pympler.web import start_in_background + +if __name__ == "__main__": + start_in_background(host="0.0.0.0",port=8090) + + bottle.debug(True) + + bottle.run(webhdf.app, host="0.0.0.0", port=8000, reloader=True) diff --git a/heap/bin/web_prod.py b/heap/bin/web_prod.py new file mode 100755 index 0000000..e6c35fc --- /dev/null +++ b/heap/bin/web_prod.py @@ -0,0 +1,9 @@ +#!/usr/bin/env python +# coding: utf-8 +import bottle +from ebus import webhdf + +if __name__ == "__main__": + bottle.debug(False) + + bottle.run(webhdf.app, host="0.0.0.0", port=8000, reloader=False, server="waitress") diff --git a/heap/bin/wettermichel.py b/heap/bin/wettermichel.py new file mode 100755 index 0000000..4c0af6c --- /dev/null +++ b/heap/bin/wettermichel.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python +# -*- coding:utf8 -*- +import httplib +from StringIO import StringIO + +from lxml import objectify + +""" +from sqlalchemy import create_engine, text +from sqlalchemy.ext.sqlsoup import SqlSoup + +soup = SqlSoup( + create_engine("postgresql://ebus:ebus@10.2.2.26:5432/ebus") ) +""" +def read(): + conn = httplib.HTTPConnection("www.wettermichel.de") + conn.request("GET", "/weatherdata/params3.xml") + result = conn.getresponse() + assert result.status == 200, 'Site answered with Status 200' + + xml = objectify.parse( result ) + conn.close() + return xml + +def get_sensor_id(sensor_name): + try: + sensor_id = soup.sensor.filter(soup.sensor.name == sensor_name).one() + return sensor_id.id + except: + conn = soup.connection() + conn.execute(text("""INSERT INTO sensor(name) VALUES (:name)"""), + name = sensor_name) + soup.commit() + return soup.sensor.filter(soup.sensor.name == sensor_name).one().id + +def insert(sensor_name,value_int=None, value_float=None, value_string=None): + sql = text("""INSERT INTO value( + timestamp, sensor_id, type, value_int, value_float, value_string) + VALUES ( + NOW(), :sensor_id, :type, :value_int, :value_float, :value_string)""") + conn = soup.connection() + ttype = value_int != None and "int" or \ + value_float != None and "float" or \ + value_string != None and "string" or \ + None + assert ttype != None, 'ttype is set' + conn.execute(sql, sensor_id=get_sensor_id(sensor_name), + type=ttype, + value_int=value_int, + value_float=value_float, + value_string=value_string) + soup.commit() + +############### + +xml = read() +for keyval in xml.xpath("/data/*"): + if keyval.tag == "entry": + continue + + print type(keyval), keyval.tag, keyval.text + continue + if isinstance(keyval, objectify.IntElement): + insert("de.wettermichel.%s"%keyval.tag, + value_int=int(keyval.text)) + elif isinstance(keyval, objectify.FloatElement): + insert("de.wettermichel.%s"%keyval.tag, + value_float=float(keyval.text)) + elif isinstance(keyval, objectify.StringElement): + + pass + insert("de.wettermichel.%s"%keyval.tag, + value_string=keyval.text) + else: + print "ignore other: %s %s" % (keyval.tag, keyval.__class__) + + |