diff options
author | yvesf <yvesf-git@xapek.org> | 2010-08-15 22:54:27 +0200 |
---|---|---|
committer | yvesf <yvesf-git@xapek.org> | 2010-08-15 22:54:27 +0200 |
commit | 7ff580a6fe60f7e3ea160f7b181ff0fa934453ce (patch) | |
tree | 55ea82fdffaaf50ed099f3bc663cb083952d1fc5 /ebus/model | |
parent | 3e70cb0892a703888e913ee5a1545f488a22a4a3 (diff) | |
download | ebus-alt-7ff580a6fe60f7e3ea160f7b181ff0fa934453ce.tar.gz ebus-alt-7ff580a6fe60f7e3ea160f7b181ff0fa934453ce.zip |
So 15. Aug 22:54:27 CEST 2010
Diffstat (limited to 'ebus/model')
-rw-r--r-- | ebus/model/__init__.py | 19 | ||||
-rw-r--r-- | ebus/model/sql.py | 95 |
2 files changed, 60 insertions, 54 deletions
diff --git a/ebus/model/__init__.py b/ebus/model/__init__.py index ecdf3bc..1e2d080 100644 --- a/ebus/model/__init__.py +++ b/ebus/model/__init__.py @@ -1,3 +1,4 @@ +# -*- coding:utf8 -*- class DataField(object): def __init__(self, name, offset): self.name = name @@ -5,20 +6,20 @@ class DataField(object): def value(self,data): raise NotImplemented() - class Data1b(DataField): - """ +class Data1b(DataField): + """ Beispiel für die Berechnung: if ((x & 80h) == 80h) // y negativ y = - [dez(!x) + 1] else y = dez(x) """ - def value(self,data): - x = ord(data[self.offset]) - if x & 0x80 == 0x80: - return (-1) * (0xff^x + 1) - else: - return x + def value(self,data): + x = ord(data[self.offset]) + if x & 0x80 == 0x80: + return (-1) * (0xff^x + 1) + else: + return x class Data1c(DataField): def value(self,data): @@ -81,7 +82,7 @@ class Byte(DataField): class ByteEnum(DataField): def __init__(self, name, offset, values): - fields.DataField.__init__(self, name, offset) + DataField.__init__(self, name, offset) self.values = values def value(self, data): 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 |