summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xbin/ebus_dump.py20
-rw-r--r--ebus/__init__.py8
-rw-r--r--ebus/model/sql.py23
-rwxr-xr-x[-rw-r--r--]test.py0
4 files changed, 33 insertions, 18 deletions
diff --git a/bin/ebus_dump.py b/bin/ebus_dump.py
index a437b0d..7507738 100755
--- a/bin/ebus_dump.py
+++ b/bin/ebus_dump.py
@@ -6,9 +6,18 @@ 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
-engine = create_engine('sqlite:///:memory:', echo=True)
-#engine = create_engine("postgresql://yvesf:iii@10.2.2.13:5432/yvesf", echo=True)
+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://jack:iii@localhost:5432/ebus")
ModelBase.metadata.create_all(engine)
@@ -25,10 +34,13 @@ class MyEbusReader(EbusReader):
def handle_ebus(self,ebus_packet):
for field in self.sql.get_values(ebus_packet):
session.add(field)
+ session.commit()
+ logger.info("committed packet %s" % ebus_packet.name())
MyEbusReader()
try:
asyncore.loop()
except KeyboardInterrupt:
- for data in session.query(Value).all():
- print data
+ pass
+# for data in session.query(Value).all():
+# print data
diff --git a/ebus/__init__.py b/ebus/__init__.py
index 58192b9..7b8c8ed 100644
--- a/ebus/__init__.py
+++ b/ebus/__init__.py
@@ -44,15 +44,15 @@ class EbusPacket(object):
self.primary_command,
self.secondary_command))
- def _get_source_name(self):
+ def get_source_name(self):
s=EbusPacket.ebus_xml.xpath("/ebus/devices/device[@address=$address]",
address="#x%.2x"%self.source)
- return len(s)>0 and s[0] or None
+ return len(s)>0 and s[0].get("name") or None
- def _get_destination_name(self):
+ def get_destination_name(self):
d=EbusPacket.ebus_xml.xpath("/ebus/devices/device[@address=$address]",
address="#x%.2x"%self.destination)
- return len(d)>0 and d[0] or None
+ return len(d)>0 and d[0].get("name") or None
def name(self):
return self.get_packet_description().get("name")
diff --git a/ebus/model/sql.py b/ebus/model/sql.py
index 630e31c..de13239 100644
--- a/ebus/model/sql.py
+++ b/ebus/model/sql.py
@@ -26,7 +26,7 @@ class Value(ModelBase):
__tablename__ = 'value'
id = Column(Integer, primary_key=True)
- timestamp = Column(DateTime)
+ timestamp = Column(DateTime,index=True)
sensor_id = Column(Integer, ForeignKey("sensor.id"))
sensor = relationship(Sensor, backref=backref('values', order_by=timestamp))
@@ -84,22 +84,25 @@ class EbusSQL(object):
def get_values(self,packet):
packet_description = packet.get_packet_description()
- sensor_name = str(packet._get_source_name())
- sensor = self.session.query(Sensor) \
- .filter(Sensor.name==sensor_name).first()
- if sensor is None:
- sensor = Sensor(sensor_name)
- self.session.add(sensor)
-
+ packet_source_name = packet.get_source_name()
+ packet_name = packet.name()
for field in packet_description.fields.iterchildren():
try:
name = field.get("name")
offset = int(field.get("offset"))
if not name:
continue
- elif field.tag == "bit":
+
+ sensor_name = "%s.%s.%s" % (packet_source_name, packet_name, name )
+ sensor = self.session.query(Sensor) \
+ .filter(Sensor.name==sensor_name).first()
+ if sensor is None:
+ sensor = Sensor(sensor_name)
+ self.session.add(sensor)
+
+ if field.tag == "bit":
yield ValueInt(sensor, \
- model.Bit(name, offset).value(packet.data))
+ model.Bit(name, offset).value(packet.data) and 1 or 0)
elif field.tag == "byte":
yield ValueInt(sensor, \
model.Byte(name, offset).value(packet.data))
diff --git a/test.py b/test.py
index 958c7b1..958c7b1 100644..100755
--- a/test.py
+++ b/test.py