diff options
Diffstat (limited to 'ebus/web')
-rw-r--r-- | ebus/web/model.py | 52 |
1 files changed, 43 insertions, 9 deletions
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 + + + + |