class Relsen(db.Model): __tablename__ = 'relsen' rowid = db.Column(db.Integer, primary_key=True) # built-in autoincrement field device_id = db.Column(db.String(16), db.ForeignKey('device.device_id'), nullable=False) relsen_id = db.Column(db.String(32), nullable=False) # 'POWER1', 'A0' relsen_name = db.Column(db.String(32), default='Generic') # 'Hall light' relsen_type = db.Column( db.String(32), default='Generic') # 'bulb', 'Temperature', 'Light' room_name = db.Column(db.String(32)) # 'guest room' room_type = db.Column(db.String(32)) # 'bed room' group_name = db.Column(db.String(32)) # 'ground floor' schedule = db.Column( db.String(256) ) # stringified JSON: "{'schedule' : [[10.30,11.30],[14.0,15.20]]}" repeat = db.Column(db.Boolean) def get_friendly_identifier(self): jidentifier = { 'device_id': self.device_id, 'relsen_id': self.relsen_id, 'relsen_name': self.relsen_name, 'relsen_type': self.relsen_type, 'room_name': self.room_name, 'group_name': self.group_name, } return (jidentifier) def toJSON(self): jrelsen = { 'device_id': self.device_id, 'relsen_id': self.relsen_id, 'relsen_name': self.relsen_name, 'relsen_type': self.relsen_type, 'room_name': self.room_name, 'room_type': self.room_type, 'group_name': self.group_name, 'repeat': self.repeat } if self.schedule is not None: jrelsen.update(json.loads( self.schedule)) # merge the two json objects else: jrelsen['schedule'] = [ ] # create a 'schedule' node with an empty array return (jrelsen) def __repr__(self): return ('<{}.{}>'.format(self.device_id, self.relsen_id))
class Status(db.Model): # TODO: store realtime data and events in database __tablename__ = 'status' rowid = db.Column(db.Integer, primary_key=True) # built-in autoincrement field device_id = db.Column(db.String(16), db.ForeignKey('device.device_id'), nullable=False) time_stamp = db.Column( db.DateTime(timezone=True), default=datetime.now) # db.func.current_timestamp()) relay_status = db.Column(db.String(24)) # array within JSON sensor_values = db.Column(db.String(32)) # JSON event_type = db.Column(db.String( 16)) # autonomous, command, response, event, health, info, error online = db.Column(db.Boolean) def __repr__(self): OL = 'Offline' if self.online: OL = 'Online' return ('<{}.{}({})>'.format(self.rowid, self.device_id, OL))