class Token: def __init__(self): self.access = "" self.refresh = "" self.db = DataBase(get_project_root() + '/conf/energy_protector') self.db.execute( 'CREATE TABLE IF NOT EXISTS token (access TEXT, refresh TEXT)') count = self.db.execute('SELECT COUNT(*) FROM token')[0][0] if count is 0: self.db.execute('INSERT INTO token(access, refresh) values (?, ?)', (self.access, self.refresh)) else: self.load() def print(self): print("access token : '" + self.access + "'") print("refresh token : '" + self.refresh + "'") def load(self, access=True, refresh=True): data = self.db.execute('SELECT * FROM token LIMIT 1')[0] if access: self.access = data[0] if refresh: self.refresh = data[1] def write(self): self.db.execute('UPDATE token SET access = ?, refresh = ?', (self.access, self.refresh))
class Device: def __init__(self, device_id: str, device_type: str, unit_count: int): self.id = device_id self.d_type = device_type self.ip = '' self.units = [False for _ in range(unit_count)] self.db = DataBase(get_project_root() + '/conf/energy_protector') self.db.execute( 'CREATE TABLE IF NOT EXISTS devices (d_id TEXT, d_type TEXT, d_ip TEXT, d_units TEXT)') count = self.db.execute('SELECT COUNT(*) FROM devices WHERE d_type = ? AND d_id = ?', (self.d_type, self.id))[0][0] if count is 0: self.db.execute( 'INSERT INTO devices(d_id, d_type, d_ip, d_units) values (?, ?, ?, ?)', (self.id, self.d_type, self.ip, self.units_to_str()) ) else: self.load() self.write() def set_unit(self, i, state): self.units[i] = state self.write() def units_to_str(self): return reduce(lambda r, state: r + str(int(state)) + ';', self.units, '')[:-1] def str_to_units(self, string): self.units = list(map(lambda state: state == '1', string.split(';'))) def print(self): print('id : ' + self.id) print('type : ' + self.d_type) print('ip : ' + self.ip) print('units :', self.units) def load(self, ip=True, units=True): data = self.db.execute('SELECT * FROM devices WHERE d_type = ? AND d_id = ?', (self.d_type, self.id))[0] if ip: self.ip = data[2] if units: self.str_to_units(data[3]) def write(self): self.db.execute( 'UPDATE devices SET d_ip = ?, d_units = ? WHERE d_type = ? AND d_id = ?', (self.ip, self.units_to_str(), self.d_type, self.id))
def get_log(self): db = DataBase(get_project_root() + '/conf/energy_protector') db.execute( 'CREATE TABLE IF NOT EXISTS usage_log (date TEXT primary key, usage_time INT)' ) day_res = self.apis.usage.get(day=True, day_n=5) if day_res[0]: usagelog_update(day_res[2]['day']) log_count = db.execute('SELECT COUNT(*) FROM usage_log')[0][0] if log_count > 0: self.usage_logs = [] data_list = db.execute('SELECT * FROM usage_log') for data in data_list: temp_str = data[0] + " : " hour = data[1] // 3600 minute = (data[1] % 3600) // 60 temp_str += str(hour) + "시간 " + str(minute) + "분" self.usage_logs.append(temp_str) else: self.usage_logs.append('사용 기록이 존재하지 않습니다.') self.usage_logs.append('사용 기록은 1일을 기준으로 기록됩니다.')
def usagelog_update(day_usage_times): db = DataBase(get_project_root() + '/conf/energy_protector') db.execute( 'CREATE TABLE IF NOT EXISTS usage_log (date TEXT, usage_time INT)') for time in day_usage_times: count = db.execute('SELECT COUNT(*) FROM usage_log WHERE date = ?', (time[0], ))[0][0] if count is 0: db.execute( 'INSERT INTO usage_log (date, usage_time) values (?, ?)', ( time[0], time[1], )) else: db.execute('UPDATE usage_log SET usage_time=? WHERE date = ?', ( time[1], time[0], ))
class DeviceSetting: def __init__(self): self.is_registered = False self.group = '' self.id = '' self.password = '' self.auto_control = True self.remote_control = True self.devices = get_devices() self.switch_devices = get_type_devices(self.devices, 'switch') self.plug_devices = get_type_devices(self.devices, 'plug') self.get_group() self.get_id() self.db = DataBase(get_project_root() + '/conf/energy_protector') self.db.execute( 'CREATE TABLE IF NOT EXISTS device_setting (is_registered INT, d_group TEXT, d_id TEXT, d_password TEXT, d_auto_control INT, d_remote_control INT)') count = \ self.db.execute('SELECT COUNT(*) FROM device_setting WHERE d_group = ? AND d_id = ?', (self.group, self.id))[0][0] if count is 0: self.db.execute( 'INSERT INTO device_setting(is_registered, d_group, d_id, d_password, d_auto_control, d_remote_control) values (?, ?, ?, ?, ?, ?)', (int(self.is_registered), self.group, self.id, self.password, int(self.auto_control), int(self.remote_control))) else: self.load_conf() def get_id(self): file_name = get_project_root() + "/conf/device_id.txt" if not os.path.isfile(file_name): sys.stderr.write("No file: %s\n" % file_name) exit(1) self.id = file_read_one(file_name) def get_group(self): file_name = get_project_root() + "/conf/device_group.txt" if not os.path.isfile(file_name): sys.stderr.write("No file: %s\n" % file_name) exit(1) self.group = file_read_one(file_name) def load_conf(self): data = self.db.execute('SELECT * FROM device_setting WHERE d_group = ? AND d_id = ?', (self.group, self.id))[0] self.is_registered = data[0] == 1 self.group = data[1] self.id = data[2] self.password = data[3] self.auto_control = data[4] == 1 self.remote_control = data[5] == 1 if len(self.group) is 0: return False if len(self.id) is 0: return False if len(self.password) is 0: return False return True def print(self): print("group:'" + self.group + "'") print("id:'" + self.id + "'") print("pw:'" + str(self.password) + "'") print("auto control:'" + str(self.auto_control) + "'") print("remote_control:'" + str(self.remote_control) + "'") def write(self): self.db.execute( 'UPDATE device_setting SET is_registered = ?, d_password = ?, d_auto_control = ?, d_remote_control = ? WHERE d_group = ? AND d_id = ?', (int(self.is_registered), self.password, int(self.auto_control), int(self.remote_control), self.group, self.id)) def device_setting_init(self): self.get_group() self.get_id() self.password = "" self.auto_control = True self.remote_control = True def register(self, server: Server): self.load_conf() if self.is_registered: return False apis = Apis(server, self) res = apis.raspberry.post() if res[0] and res[1] == 201: self.is_registered = True self.write() return True if not res[0] and res[1] == 400: return False return None