From 7ff580a6fe60f7e3ea160f7b181ff0fa934453ce Mon Sep 17 00:00:00 2001 From: yvesf Date: Sun, 15 Aug 2010 22:54:27 +0200 Subject: So 15. Aug 22:54:27 CEST 2010 --- ebus/model/sql.py | 95 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 50 insertions(+), 45 deletions(-) (limited to 'ebus/model/sql.py') 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 "" % (self.id, self.name, self.description) + return "" % (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 "" % (self.id, self.sensor, self.value, self.timestamp) + return "" % ( + 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 -- cgit v1.2.1