summaryrefslogtreecommitdiff
path: root/ebus
diff options
context:
space:
mode:
Diffstat (limited to 'ebus')
-rw-r--r--ebus/__init__.py2
-rw-r--r--ebus/web/model.py52
2 files changed, 44 insertions, 10 deletions
diff --git a/ebus/__init__.py b/ebus/__init__.py
index ad79846..23e6595 100644
--- a/ebus/__init__.py
+++ b/ebus/__init__.py
@@ -163,7 +163,7 @@ class EbusXMLMixin(object):
elif field.tag == "bcd":
yield fields.Bcd( name, int(offset) )
elif field.tag == "byteEnum":
- options = dict(map(lambda opt: ( int(opt.get("value"),16), opt.get("name")),
+ options = dict(map(lambda opt: ( int(opt.get("value")[2:],16), opt.get("name")),
field.xpath("./option")))
yield fields.ByteEnum( name, int(offset), options)
else:
diff --git a/ebus/web/model.py b/ebus/web/model.py
index e9fc04c..04b15ed 100644
--- a/ebus/web/model.py
+++ b/ebus/web/model.py
@@ -19,8 +19,7 @@ class Sensor(ModelBase):
def __repr__(self):
return "<Sensor('%s','%s','%s')>" % (self.id, self.name, self.description)
-
-
+# http://www.sqlalchemy.org/docs/05/reference/ext/declarative.html#inheritance-configuration
class Value(ModelBase):
__tablename__ = 'value'
@@ -29,17 +28,52 @@ class Value(ModelBase):
sensor_id = Column(Integer, ForeignKey("sensor.id"))
sensor = relationship(Sensor, backref=backref('values', order_by=timestamp))
- value_float = Column(Float(precision=4))
- value_int = Column(Integer)
- value_string = Column(String)
+ discriminator = Column('type', String(50))
+ __mapper_args__ = {'polymorphic_on': discriminator}
- def __init__(self, sensor, value_float=None, value_int=None, value_string=None, timestamp=datetime.now()):
+ def __init__(self, sensor, timestamp=datetime.now()):
self.timestamp = timestamp
self.sensor = sensor
- self.value_float = value_float
- self.value_int = value_int
- self.value_string = value_string
def __repr__(self):
return "<Value('%s','%s','%s','%s'>" % (self.id, self.sensor, self.value, self.timestamp)
+ def value(self):
+ raise NotImplementedException()
+
+class ValueInt(Value):
+ __mapper_args__ = {'polymorphic_identity': 'value'}
+ value_float = Column(Float(precision=4))
+
+ def __init__(self, sensor, value, timestamp=datetime.now()):
+ Value.__init__(self, sensor, timestamp)
+ self.value_float = value
+
+ def value(self):
+ return self.value_float
+
+class ValueInt(Value):
+ __mapper_args__ = {'polymorphic_identity': 'value'}
+ value_int = Column(Integer)
+
+ def __init__(self, sensor, value, timestamp=datetime.now()):
+ Value.__init__(self, sensor, timestamp)
+ self.value_int = value
+
+ def value(self):
+ return self.value_int
+
+class ValueString(Value):
+ __mapper_args__ = {'polymorphic_identity': 'value'}
+ value_string = Column(String)
+
+ def __init__(self, sensor, value, timestamp=datetime.now()):
+ Value.__init__(self, sensor, timestamp)
+ self.value_int = value
+
+ def value(self):
+ return self.value_string
+
+
+
+