summaryrefslogtreecommitdiff
path: root/ebus/web/model.py
blob: e9fc04c106a68c441d22eafd95383932864e8d70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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

ModelBase = declarative_base()

class Sensor(ModelBase):
    __tablename__ = 'sensor'
    
    id = Column(Integer, primary_key=True)
    name = Column(String)
    description = Column(String, default="")

    def __init__(self, name, description=""):
        self.name = name
        self.description = description
        
    def __repr__(self):
        return "<Sensor('%s','%s','%s')>" % (self.id, self.name, self.description)



class Value(ModelBase):
    __tablename__ = 'value'

    id = Column(Integer, primary_key=True)
    timestamp = Column(DateTime)
    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)

    def __init__(self, sensor, value_float=None, value_int=None, value_string=None, 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)