summaryrefslogtreecommitdiff
path: root/ebus/model/sql.py
diff options
context:
space:
mode:
Diffstat (limited to 'ebus/model/sql.py')
-rw-r--r--ebus/model/sql.py95
1 files changed, 50 insertions, 45 deletions
diff --git a/ebus/model/sql.py b/ebus/model/sql.py
index fa9b479..630e31c 100644
--- a/ebus/model/sql.py
+++ b/ebus/model/sql.py
@@ -3,6 +3,7 @@ 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
+from ebus import model
ModelBase = declarative_base()
@@ -18,7 +19,7 @@ class Sensor(ModelBase):
self.description = description
def __repr__(self):
- return "<Sensor('%s','%s','%s')>" % (self.id, self.name, self.description)
+ return "<Sensor(id='%s',name='%s',description='%s')>" % (self.id, self.name, self.description)
# http://www.sqlalchemy.org/docs/05/reference/ext/declarative.html#inheritance-configuration
class Value(ModelBase):
@@ -37,7 +38,8 @@ class Value(ModelBase):
self.sensor = sensor
def __repr__(self):
- return "<Value('%s','%s','%s','%s'>" % (self.id, self.sensor, self.value, self.timestamp)
+ return "<Value(id='%s',sensor='%s',value='%s',timestamp='%s'>" % (
+ self.id, self.sensor, self.value(), self.timestamp)
def value(self):
raise NotImplementedException()
@@ -76,53 +78,56 @@ class ValueString(Value):
return self.value_string
-class EbusPacketSQL(object):
+class EbusSQL(object):
def __init__(self,session):
self.session = session
def get_values(self,packet):
packet_description = packet.get_packet_description()
- sensor_name = packet._get_source_name()
+ sensor_name = str(packet._get_source_name())
sensor = self.session.query(Sensor) \
- .filter(Sensor.name==sensor_name)
-
- if packet is None:
- return
-
- for field in packet_description.fields:
- name = field.name
- offset = int(field.offset)
- if not name:
- continue
- elif field.tag == "bit":
- yield ValueInt(sensor, \
- fields.Bit(name, offset).value())
- elif field.tag == "byte":
- yield ValueInt(sensor, \
- fields.Byte(name, offset).value())
- elif field.tag == "word":
- yield ValueInt(sensor, \
- fields.Word(name, offset).value())
- elif field.tag == "data1b":
- yield ValueInt(sensor, \
- fields.Data1b(name, offset).value())
- elif field.tag == "data1c":
- yield ValueInt(sensor, \
- fields.Data1c(name, offset).value())
- elif field.tag == "data2b":
- yield ValueInt(sensor, \
- fields.Data2b(name, offset).value())
- elif field.tag == "data2c":
- yield ValueInt(sensor,
- fields.Data2c(name, offset).value())
- elif field.tag == "bcd":
- yield ValueString(sensor,
- fields.Bcd(name, offset).value())
- elif field.tag == "byteEnum":
- options = dict(map(lambda opt: ( int(opt.get("value")[2:],16), opt.get("name")),
- field.xpath("./option")))
- yield ValueString(sensor,
- fields.ByteEnum(name, offset, options).value())
- else:
- print "Unsupported field-type %s" % (name)
+ .filter(Sensor.name==sensor_name).first()
+ if sensor is None:
+ sensor = Sensor(sensor_name)
+ self.session.add(sensor)
+
+ 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":
+ yield ValueInt(sensor, \
+ model.Bit(name, offset).value(packet.data))
+ elif field.tag == "byte":
+ yield ValueInt(sensor, \
+ model.Byte(name, offset).value(packet.data))
+ elif field.tag == "word":
+ yield ValueInt(sensor, \
+ model.Word(name, offset).value(packet.data))
+ elif field.tag == "data1b":
+ yield ValueInt(sensor, \
+ model.Data1b(name, offset).value(packet.data))
+ elif field.tag == "data1c":
+ yield ValueInt(sensor, \
+ model.Data1c(name, offset).value(packet.data))
+ elif field.tag == "data2b":
+ yield ValueInt(sensor, \
+ model.Data2b(name, offset).value(packet.data))
+ elif field.tag == "data2c":
+ yield ValueInt(sensor,
+ model.Data2c(name, offset).value(packet.data))
+ elif field.tag == "bcd":
+ yield ValueString(sensor,
+ model.Bcd(name, offset).value(packet.data))
+ elif field.tag == "byteEnum":
+ options = dict(map(lambda opt: ( int(opt.get("value")[2:],16), opt.get("name")),
+ field.xpath("./option")))
+ yield ValueString(sensor,
+ model.ByteEnum(name, offset, options).value(packet.data))
+ else:
+ print "Unsupported field-type %s" % (name)
+ except Exception,e:
+ print "skip",e