Beispiel #1
0
class Device(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), nullable=False)
    ip = db.Column(db.String(100))
    barcode = db.Column(db.String(100))
    device_key = db.Column(db.String(1000),
                           nullable=False,
                           default=random_gen(10))
    secret_key = db.Column(db.String(1000),
                           nullable=False,
                           default=random_gen(100))
    command = db.Column(db.String(100), default=random_gen(10))
    state = db.Column(db.Integer, default=0)
    description = db.Column(db.String(500))
    toMaster = db.Column(db.Integer)
    url_path = db.Column(db.String(500))
    master_device_id = db.Column(db.Integer, db.ForeignKey("master_device.id"))
    typeId = db.Column(db.Integer, db.ForeignKey("device_type.id"))
    flatId = db.Column(db.Integer, db.ForeignKey("flat.id"))
    roomId = db.Column(db.Integer, db.ForeignKey("room.id"))
    dateAdded = db.Column(db.DateTime, default=datetime.now())
    dateUpdated = db.Column(db.DateTime,
                            default=datetime.now(),
                            onupdate=datetime.now())
    pins = db.relationship('Pin', backref='device', lazy='joined')
    sensors = db.relationship('Sensor', backref='device', lazy='joined')
    sensor_records = db.relationship('Sensor_record',
                                     backref='device',
                                     lazy=True)
    schedules = db.relationship('Schedule', backref='device', lazy=True)

    def do_update(self, **kwargs):
        for key, value in kwargs.items():
            if value is not None:
                if hasattr(self, key):
                    setattr(self, key, value)

    def json(self):
        device = {
            "id": self.id,
            "name": self.name,
            "ip": self.ip,
            "barcode": self.barcode,
            "device_key": self.device_key,
            "secret_key": self.secret_key,
            "command": self.command,
            "state": self.state,
            "description": self.description,
            "toMaster": self.toMaster,
            "url_path": self.url_path,
            "master_device_id": self.master_device_id,
            "typeId": self.typeId,
            "flatId": self.flatId,
            "roomId": self.roomId,
            "dateAdded": self.dateAdded,
            "dateUpdated": self.dateUpdated.timestamp(),
        }
        return device
Beispiel #2
0
class House(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    house_key = db.Column(db.String)
    name = db.Column(db.String(100), nullable=False)
    secret_key = db.Column(db.String(1000),
                           nullable=False,
                           default=random_gen())
    description = db.Column(db.String(500))
    longit = db.Column(db.String(100))
    latit = db.Column(db.String(100))
    regionId = db.Column(db.Integer, db.ForeignKey("region.id"))
    typeId = db.Column(db.Integer, db.ForeignKey("house_type.id"))
    dateAdded = db.Column(db.DateTime, default=datetime.now())
    dateUpdated = db.Column(db.DateTime,
                            default=datetime.now(),
                            onupdate=datetime.now())
    flats = db.relationship('Flat', backref='house', lazy=True)

    def json(self):
        houses = {
            "id": self.id,
            "house_key": self.house_key,
            "name": self.name,
            "secret_key": self.secret_key,
            "description": self.description,
            "longit": self.longit,
            "latit": self.latit,
            "regionId": self.regionId,
            "typeId": self.typeId,
            "dateAdded": self.dateAdded
        }
        return houses
Beispiel #3
0
class Sensor_record(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100))
    description = db.Column(db.String(500))
    value = db.Column(db.Float, default=0.0)
    secret_key = db.Column(db.String(1000),
                           nullable=False,
                           default=random_gen(100))
    date = db.Column(db.DateTime, nullable=False, default=datetime.now())
    master_device_id = db.Column(db.Integer, db.ForeignKey("master_device.id"))
    deviceId = db.Column(db.Integer, db.ForeignKey("device.id"))
    sensorId = db.Column(db.Integer, db.ForeignKey("sensor.id"))
    flatId = db.Column(db.Integer, db.ForeignKey("flat.id"))
    dateAdded = db.Column(db.DateTime, default=datetime.now())
    dateUpdated = db.Column(db.DateTime,
                            default=datetime.now(),
                            onupdate=datetime.now())

    def json(self, withDates=True):
        sensor_records = {
            "id": self.id,
            "name": self.name,
            "description": self.description,
            "value": self.value,
            "secret_key": self.secret_key,
            "date": self.date,
            "master_device_id": self.master_device_id,
            "deviceId": self.deviceId,
            "sensorId": self.sensorId,
            "flatId": self.flatId,
            "dateAdded": self.dateAdded if withDates else None,
            "dateUpdated": self.dateUpdated.timestamp() if withDates else None,
        }
        return sensor_records
Beispiel #4
0
class Region(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), nullable=False)
    secret_key = db.Column(db.String(1000),
                           nullable=False,
                           default=random_gen())
    description = db.Column(db.String(500))
    cityId = db.Column(db.Integer, db.ForeignKey("city.id"))
    typeId = db.Column(db.Integer, db.ForeignKey("region_type.id"))
    dateAdded = db.Column(db.DateTime, default=datetime.now())
    dateUpdated = db.Column(db.DateTime,
                            default=datetime.now(),
                            onupdate=datetime.now())
    houses = db.relationship('House', backref='region', lazy=True)

    def json(self):
        regions = {
            "id": self.id,
            "name": self.name,
            "secret_key": self.secret_key,
            "description": self.description,
            "cityId": self.cityId,
            "typeId": self.typeId,
            "dateAdded": self.dateAdded
        }
        return regions
Beispiel #5
0
class Flats(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), nullable=False)
    secret_key = db.Column(db.String(1000),
                           nullable=False,
                           default=random_gen())
    description = db.Column(db.String(500))
    houseId = db.Column(db.Integer, db.ForeignKey("houses.id"))
    typeId = db.Column(db.Integer, db.ForeignKey("flat_types.id"))
    dateAdded = db.Column(db.DateTime, nullable=False, default=datetime.now)
    residents = db.relationship('Residents', backref='flats', lazy=True)
    devices = db.relationship('Devices', backref='flats', lazy=True)
    rooms = db.relationship('Rooms', backref='flats', lazy=True)

    def json(self):
        flats = {
            "id": self.id,
            "name": self.name,
            "secret_key": self.secret_key,
            "description": self.description,
            "houseId": self.houseId,
            "typeId": self.typeId,
            "dateAdded": self.dateAdded
        }
        return flats
Beispiel #6
0
class Pin(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), nullable=False)
    command = db.Column(db.String(100), nullable=False)
    process_key = db.Column(db.String(1000), default=random_gen(10))
    secret_key = db.Column(db.String(1000),
                           nullable=False,
                           default=random_gen(100))
    description = db.Column(db.String(500))
    action = db.Column(db.String(500))
    master_device_id = db.Column(db.Integer, db.ForeignKey("master_device.id"))
    deviceId = db.Column(db.Integer, db.ForeignKey("device.id"))
    dateAdded = db.Column(db.DateTime, default=datetime.now())
    dateUpdated = db.Column(db.DateTime,
                            default=datetime.now(),
                            onupdate=datetime.now())
    schedules = db.relationship('Schedule', backref='pin', lazy=True)

    def do_update(self, **kwargs):
        for key, value in kwargs.items():
            if value is not None:
                if hasattr(self, key):
                    setattr(self, key, value)

    def json(self):
        pin = {
            "id": self.id,
            "name": self.name,
            "command": self.command,
            "process_key": self.process_key,
            "secret_key": self.secret_key,
            "description": self.description,
            "action": self.action,
            "master_device_id": self.master_device_id,
            "deviceId": self.deviceId,
            "dateAdded": self.dateAdded,
            "dateUpdated": self.dateUpdated.timestamp(),
        }
        return pin
Beispiel #7
0
def qr_register():
    req = request.get_json()

    status = 400
    response = {"secret_key": ""}

    try:
        qr = req['secret_key']
        if not qr:
            print("no qr")
            raise Exception

        this_code = QR_code.query.filter_by(secret_key=qr).first()

        if not this_code:
            print("no such code")
            raise Exception

        if this_code.registered or this_code.dateAdded < datetime.now(
        ) - timedelta(minutes=5):
            print("registered date or value issue")
            raise Exception

        qr_code = random_gen(20)
        if this_code.typeId != 1:
            this_code.registered = True
            qr_code = this_code.secret_key

        status = 200

        new_user = {
            "name": f"qr_user_{randint(1,99999)}",
            "secret_key": qr_code,
            "flatId": this_code.flatId
        }

        user = Resident(**new_user)
        db.session.add(user)
        db.session.commit()

        response["secret_key"] = qr_code

    except Exception as ex:
        print(ex)
        return make_response(response), status

    return make_response(response), status
Beispiel #8
0
class Sensor(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), nullable=False)
    command = db.Column(db.String(100), nullable=False)
    description = db.Column(db.String(500))
    value = db.Column(db.Float, default=0.0)
    secret_key = db.Column(db.String(1000),
                           nullable=False,
                           default=random_gen(100))
    flatId = db.Column(db.Integer, db.ForeignKey("flat.id"))
    typeId = db.Column(
        db.Integer,
        db.ForeignKey("sensor_type.id"))  # 1 = appending 2 = rewriting
    master_device_id = db.Column(db.Integer, db.ForeignKey("master_device.id"))
    deviceId = db.Column(db.Integer, db.ForeignKey("device.id"))
    dateAdded = db.Column(db.DateTime, default=datetime.now())
    dateUpdated = db.Column(db.DateTime,
                            default=datetime.now(),
                            onupdate=datetime.now())
    sensor_records = db.relationship('Sensor_record',
                                     backref='sensor',
                                     lazy=True)
    schedules = db.relationship('Schedule', backref='sensor', lazy=True)

    def do_update(self, **kwargs):
        for key, value in kwargs.items():
            if value is not None:
                if hasattr(self, key):
                    setattr(self, key, value)

    def json(self):
        sensor = {
            "id": self.id,
            "name": self.name,
            "command": self.command,
            "description": self.description,
            "value": self.value,
            "secret_key": self.secret_key,
            "flatId": self.flatId,
            "typeId": self.typeId,
            "master_device_id": self.master_device_id,
            "deviceId": self.deviceId,
            "dateAdded": self.dateAdded,
            "dateUpdated": self.dateUpdated.timestamp(),
        }
        return sensor
Beispiel #9
0
class QR_codes(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    secret_key = db.Column(db.String(1000),
                           nullable=False,
                           default=random_gen())
    dateAdded = db.Column(db.DateTime, nullable=False, default=datetime.now)
    registered = db.Column(db.Boolean, default=False)
    typeId = db.Column(db.Integer, default=0)

    def json(self):
        data = {
            "id": self.id,
            "secret_key": self.secret_key,
            "dateAdded": self.dateAdded,
            "registered": self.registered,
            "typeId": self.typeId,
        }
        return data
Beispiel #10
0
class Flat(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    flat_key = db.Column(db.String)
    house_key = db.Column(db.String)
    name = db.Column(db.String(100), nullable=False)
    ip = db.Column(db.String(100))
    secret_key = db.Column(db.String(1000),
                           nullable=False,
                           default=random_gen())
    description = db.Column(db.String(500))
    houseId = db.Column(db.Integer, db.ForeignKey("house.id"))
    typeId = db.Column(db.Integer, db.ForeignKey("flat_type.id"))
    dateAdded = db.Column(db.DateTime, default=datetime.now())
    dateUpdated = db.Column(db.DateTime,
                            default=datetime.now(),
                            onupdate=datetime.now())
    residents = db.relationship('Resident', backref='flat', lazy=True)
    sensors = db.relationship('Sensor', backref='flat', lazy=True)
    devices = db.relationship('Device', backref='flat', lazy=True)
    master_devices = db.relationship('Master_device',
                                     backref='flat',
                                     lazy=True)
    rooms = db.relationship('Room', backref='flat', lazy=True)
    qr_codes = db.relationship('QR_code', backref='flat', lazy=True)
    sensor_records = db.relationship('Sensor_record',
                                     backref='flat',
                                     lazy=True)

    def json(self):
        flats = {
            "id": self.id,
            "flat_key": self.flat_key,
            "name": self.name,
            "ip": self.ip,
            "secret_key": self.secret_key,
            "description": self.description,
            "houseId": self.houseId,
            "typeId": self.typeId,
            "dateAdded": self.dateAdded
        }
        return flats
Beispiel #11
0
class City(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), nullable=False)
    secret_key = db.Column(db.String(1000),
                           nullable=False,
                           default=random_gen())
    description = db.Column(db.String(500))
    typeId = db.Column(db.Integer, db.ForeignKey("city_types.id"))
    dateAdded = db.Column(db.DateTime, nullable=False, default=datetime.now)
    regions = db.relationship('Regions', backref='city', lazy=True)

    def json(self):
        city = {
            "id": self.id,
            "name": self.name,
            "secret_key": self.secret_key,
            "description": self.description,
            "typeId": self.typeId,
            "dateAdded": self.dateAdded
        }
        return city
Beispiel #12
0
class Resident(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), nullable=False)
    surname = db.Column(db.String(100))
    birthDate = db.Column(db.DateTime)
    email = db.Column(db.String(100))
    username = db.Column(db.String(100))
    password = db.Column(db.String(100))
    phoneNumber = db.Column(db.String(100))
    passportCode = db.Column(db.String(100))
    secret_key = db.Column(db.String(1000),
                           nullable=False,
                           default=random_gen(20))
    description = db.Column(db.String(500))
    flatId = db.Column(db.Integer, db.ForeignKey("flat.id"))
    typeId = db.Column(db.Integer, db.ForeignKey("resident_type.id"))
    dateAdded = db.Column(db.DateTime, default=datetime.now())
    dateUpdated = db.Column(db.DateTime,
                            default=datetime.now(),
                            onupdate=datetime.now())
    rfidTags = db.relationship('RfidTag', backref='resident', lazy=True)

    def json(self):
        residents = {
            "id": self.id,
            "name": self.name,
            "surname": self.surname,
            "birthDate": self.birthDate,
            "email": self.email,
            "username": self.username,
            "password": self.password,
            "phoneNumber": self.phoneNumber,
            "passportCode": self.passportCode,
            "secret_key": self.secret_key,
            "description": self.description,
            "flatId": self.flatId,
            "typeId": self.typeId,
            "dateAdded": self.dateAdded
        }
        return residents
Beispiel #13
0
class Devices(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), nullable=False)
    ip = db.Column(db.String(100))
    barcode = db.Column(db.String(100))
    device_key = db.Column(db.String(1000),
                           nullable=False,
                           default=random_gen())
    command = db.Column(db.String(100), nullable=False)
    state = db.Column(db.Integer, nullable=False, default=0)
    description = db.Column(db.String(500))
    typeId = db.Column(db.Integer, db.ForeignKey("device_types.id"))
    flatId = db.Column(db.Integer, db.ForeignKey("flats.id"))
    roomId = db.Column(db.Integer, db.ForeignKey("rooms.id"))
    dateAdded = db.Column(db.DateTime, nullable=False, default=datetime.now)
    pins = db.relationship('Pins', backref='devices', lazy='joined')
    sensors = db.relationship('Sensors', backref='devices', lazy='joined')
    sensor_records = db.relationship('Sensor_records',
                                     backref='devices',
                                     lazy=True)
    schedules = db.relationship('Schedules', backref='devices', lazy=True)

    def json(self):
        device = {
            "id": self.id,
            "name": self.name,
            "ip": self.ip,
            "barcode": self.barcode,
            "device_key": self.device_key,
            "command": self.command,
            "state": self.state,
            "description": self.description,
            "typeId": self.typeId,
            "flatId": self.flatId,
            "roomId": self.roomId,
            "dateAdded": self.dateAdded
        }
        return device