class TemperatureSensorData(BaseIotData): __tablename__ = 'temperature_data' id = db.Column(db.Integer, primary_key=True) ''' All temperature value is stored in ceclius units ''' temperature = db.Column(db.Float, nullable=False) #Each data entry is associated with a sensor. sensor_id = db.Column(db.Integer, db.ForeignKey('iot_sensor.id')) sensor = db.relationship('Sensor', backref="temperatue_data") def __init__(self, temp): self.temperature = temp @staticmethod def add_data(req_json): temp = req_json['temperature'] unit = req_json['unit'] if unit != 'C' and unit != 'F': return (False, "Incorrect temperature unit") else: if (unit == "F"): # TODO: convert units to ceclius since all # temperature data is stored in celcius unit = unit sensor = Sensor.get_sensor(req_json["device_id"]) sensor_data = TemperatureSensorData(temp) sensor.temperatue_data.append(sensor_data) db.session.add(sensor) db.session.commit() return (True, "Upload Success")
class RainSensorData(BaseIotData): __tablename__ = 'rain_data' id = db.Column(db.Integer, primary_key=True) rain_data = db.Column(db.Float, nullable=False) #Each data entry is associated with a sensor. sensor_id = db.Column(db.Integer, db.ForeignKey('iot_sensor.id')) sensor = db.relationship('Sensor', backref="rain_data")
class Base(db.Model): __abstract__ = True ''' Lets the models that inherit define their own primary key ''' id = db.Column(db.Integer, primary_key=True) date_created = db.Column(db.DateTime, default=db.func.current_timestamp()) date_modified = db.Column(db.DateTime, default=db.func.current_timestamp(), onupdate=db.func.current_timestamp())
class IoTSwitchData(BaseIotData): __tablename__ = 'switch_data' id = db.Column(db.Integer, primary_key=True) state_change = db.Column(db.Boolean, default=None) sensor_id = db.Column(db.Integer, db.ForeignKey('iot_switch.id')) sensor = db.relationship('IoTSwitch', backref="data") @staticmethod def add_change_to_db(req_json): pass
class IoTDevice(Base): __abstract__ = True device_name = db.Column(db.String(30), nullable=False) ''' Device ID is a 32 byte unique GUID. ''' device_id = db.Column(db.String(32), unique=True, nullable=False) ''' By default, every iot-device is de-activated for now. ''' active = db.Column(db.Boolean, default=False) ''' Device is either indoor or outdoor. ''' indoor = db.Column(db.Boolean, default=True)
class Sensor(IoTDevice): __tablename__ = 'iot_sensor' sensor_type = db.Column(db.Enum(SensorType), nullable=False) def __init__(self, name, id, stype): self.device_name = name self.device_id = id self.sensor_type = stype @staticmethod def get_sensor(id): return Sensor.query.filter_by(device_id=id).first()
class IoTSwitch(IoTDevice): __tablename__ = 'iot_switch' switch_type = db.Column(db.Enum(SwitchType), nullable=False) def __init__(self, name, id, stype): self.device_name = name self.device_id = id self.switch_type = stype @staticmethod def get_switch(id): return IoTSwitch.query.filter_by(device_id=id).first()
class BaseIotData(db.Model): __abstract__ = True date_recorded = db.Column(db.DateTime, default=db.func.current_timestamp()) remote_ip = db.Column(db.String(24), default=remote_addr)