class Measurement(db.Model): __tablename__ = 'measurements' id = db.Column(db.Integer, primary_key=True) sensor = db.Column(db.String(24), nullable=False, index=True) type_m = db.Column(db.String(24), nullable=False, index=True) value = db.Column(db.Float, nullable=False, server_default='0') date_time = db.Column(db.DateTime, nullable=False) def __init__(self, address, type_m, val, dt=datetime.datetime.now().replace(second=0, microsecond=0)): self.sensor = address self.type_m = type_m self.value = val self.date_time = dt def __repr__(self): return json.dumps(self.as_dict()) def as_dict(self): return { 'id': self.id, 'sensor': self.sensor, 'type': self.type_m, 'value': self.value, 'date_time': datetime.datetime.strftime(self.date_time, "%m/%d/%y %H:%M"), }
class Room(db.Model): __tablename__ = 'rooms' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(32), nullable=False, unique=False) def __init__(self, name): self.name = name def __repr__(self): return self.name
class Setting(db.Model): __tablename__ = 'settings' id = db.Column(db.Integer, primary_key=True) temperature = db.Column(db.String(2), nullable=False, server_default='c') interval = db.Column(db.Integer, nullable=False, server_default='2') def __init__(self, t, i): self.temperature = t self.interval = i def __repr__(self): return "temp:%s poll_interval: %d" % (self.temperature, self.interval)
class Sensor(db.Model): __tablename__ = 'sensors' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(48), nullable=False, unique=False) address = db.Column(db.String(24), nullable=False, unique=True) def __init__(self, name, address): self.name = name self.address = address def __repr__(self): return self.name
class VesyncUser(db.Model): __tablename__ = 'vesync_users' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(48), nullable=False) password = db.Column(db.String(96), nullable=False) def __init__(self, name, password): self.username = name self.password = hashlib.md5(password.encode('utf-8')).hexdigest() def __repr__(self): return self.username
class TwilioAccount(db.Model): __tablename__ = 'twilio_settings' id = db.Column(db.Integer, primary_key=True) sid = db.Column(db.String(48), nullable=True) token = db.Column(db.String(48), nullable=True) number = db.Column(db.String(32), nullable=True) def __init__(self, sid, token, num): self.sid = sid self.token = token self.number = num def __repr__(self): return "%s/%s/%s" % (self.sid, self.token, self.number)
class ActionProcess(db.Model): __tablename__ = 'action_processes' id = db.Column(db.Integer, primary_key=True) action_id = db.Column(db.Integer, db.ForeignKey('actions.id')) """ Here, the 'on' columns are also used to store sms-message triggers as well, if the action-type is sms-message and not switch-outlet. """ on_datetime = db.Column(db.DateTime, nullable=True) on_trigger = db.Column(db.String(24), nullable=True) off_datetime = db.Column(db.DateTime, nullable=True) off_trigger = db.Column(db.String(24), nullable=True) active = db.Column(db.Boolean(), nullable=False, server_default='1') action = db.relationship("Action", backref=db.backref("processes", cascade="all,delete")) def __init__(self, aid): self.action_id = aid def assign_datetime(self, field, dt): setattr(self, field, dt.replace(second=0, microsecond=0)) def as_dict(self): return { 'id': self.id, 'action': self.action, 'on_datetime': self.on_datetime, 'on_trigger': self.on_trigger, 'off_datetime': self.off_datetime, 'off_trigger': self.off_trigger, 'active': self.active, }
class Schedule(db.Model): __tablename__ = 'schedules' id = db.Column(db.Integer, primary_key=True) outlet_id = db.Column(db.Integer, db.ForeignKey('outlets.id')) on_time = db.Column(db.String(16), nullable=False, server_default='') off_time = db.Column(db.String(16), nullable=False, server_default='') days = db.Column(db.Integer, nullable=False, server_default="127") custom = db.Column(db.Integer, nullable=False, server_default="0") active = db.Column(db.Boolean(), nullable=False, server_default='1') outlet = db.relationship("Outlet", backref=db.backref("children", cascade="all,delete")) def __init__(self, oid, ontime, offtime, days, cust): self.outlet_id = oid self.on_time = ontime self.off_time = offtime self.days = days self.custom = cust def __repr__(self): d = ",".join(self.run_days()) return "%s %s/%s (%s)" % (self.outlet.name, self.on_time, self.off_time, d) def as_dict(self): return { 'id': self.id, 'outlet': self.outlet, 'on_time': self.on_time, 'off_time': self.off_time, 'days': self.days, 'active': self.active, } def run_days(self): results = [] dow = WeekdayMap(show_first=2).reverse_ordered_list() if self.days == 127: results.append('Every Day') else: for item in dow: if (self.days & item[1]): results.append(item[0]) return results def runs_on(self, wkday): dow = WeekdayMap().reverse_ordered_list() for d in dow: k, v = d if re.search(wkday, k, re.IGNORECASE): return True return False
class Action(db.Model): __tablename__ = 'actions' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(24), nullable=False) measurement_type = db.Column(db.String(24), nullable=False) sensor_address = db.Column(db.String(24), nullable=False, unique=True) outlet_id = db.Column(db.String(128), nullable=True) action_type = db.Column(db.String(24), nullable=False, server_default='sms-message') sms_recipient = db.Column(db.String(24), nullable=True) """ Here, the 'on' columns are also used to store one time sms-message conditions as well, if the action-type is sms-message and not switch-outlet. """ on_condition = db.Column(db.String(16), nullable=True) on_threshold = db.Column(db.Integer, nullable=True) off_condition = db.Column(db.String(16), nullable=True) off_threshold = db.Column(db.Integer, nullable=True) wait_minutes = db.Column(db.Integer, nullable=False, server_default='10') active = db.Column(db.Boolean(), nullable=False, server_default='1') def __init__(self, name, mid, oid, addr, atype): self.name = name self.measurement_type = mid self.outlet_id = oid self.sensor_address = addr self.action_type = atype def __repr__(self): if not self.active: return (self.name + " *") else: return self.name def as_dict(self): return { 'id': self.id, 'name': self.name, 'measurement_type': self.measurement_type, 'outlet_id': self.outlet_id, 'sensor_address': self.sensor_address, 'on_condition': self.on_condition, 'on_threshold': self.on_threshold, 'off_condition': self.off_condition, 'off_threshold': self.off_threshold, 'wait_minutes': self.wait_minutes, 'active': self.active, }
class Outlet(db.Model): __tablename__ = 'outlets' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(24), nullable=False, server_default='', unique=True) on_code = db.Column(db.Integer, nullable=False, unique=True) off_code = db.Column(db.Integer, nullable=False, unique=True) state = db.Column(db.Boolean(), nullable=False, server_default='0') active = db.Column(db.Boolean(), nullable=False, server_default='1') def __init__(self, name, on, off): self.name = name self.on_code = on self.off_code = off def __repr__(self): return json.dumps(self.as_dict()) def as_dict(self): return { 'id': self.id, 'name': self.name, 'on_code': self.on_code, 'off_code': self.off_code, 'state': self.state, 'active': self.active } """ transmit rf code to turn the outlet 'on' params: - status: return json status of outlet after command. returns: zero on success. non-zero on failure. Unless 'status' option is set, then the return will be JSON status of the Outlet. -- OLD CODE -- def on(self, status=False): rval = self.set_state(1) if status: return str(self) else: return rval """ def on(self, status=False): tx = TXChannelControl(sudo=True) rval, msg = tx.send_code(self.on_code) if not rval: self.state = 1 db.session.commit() else: print("%d, %s" % (rval, msg)) """ same as 'on', but turns outlet 'off' -- OLD CODE -- def off(self, status=False): rval = self.set_state(0) if status: return str(self) else: return rval """ def off(self, status=False): tx = TXChannelControl(sudo=True) rval, msg = tx.send_code(self.off_code) if not rval: self.state = 0 db.session.commit() else: print("%d, %s" % (rval, msg)) """