def initial(self): year_res = self.apis.usage.get(year=True, year_n=5) month_res = self.apis.usage.get(month=True, month_n=5) day_res = self.apis.usage.get(day=True, day_n=5) usagelog_update(day_res[2]['day']) if year_res[0] and month_res[0] and day_res[0]: self.day_times = list(map(lambda data: data[1], day_res[2]['day'])) self.month_times = list(map(lambda data: data[1], month_res[2]['month'])) self.year_times = list(map(lambda data: data[1], year_res[2]['year'])) self.init_data() self.my_html.load(QUrl('file:///' + get_project_root() + '/conf/usage_time.html')) else: request_failed('USAGE.GET') self.my_html.load(QUrl('file:///' + get_project_root() + '/usage_time_request_failed.html'))
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_devices(): devices = [] with open(get_project_root() + '/conf/devices.json') as devices_json: devices_data = json.load(devices_json) for i in range(len(devices_data)): devices.append(Device(devices_data[i]['id'], devices_data[i]['type'], devices_data[i]['unit_count'])) devices[i].load() return devices
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 req(): with open(get_project_root() + '/conf/devices.json') as devices_json: devices_data = json.load(devices_json) for device in devices_data: headers = {'Content-Type': 'application/json'} data = { 'device_id': device['id'], 'device_type': device['type'], 'unit_count': device['unit_count'], 'device_ip': '0.0.0.0' } res = requests.post(SERVER_URL + '/api/device', headers=headers, data=json.dumps(data)) print(device['id'], res, res.json())
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], ))
def init_data(self): op = opts.InitOpts(width="500px", height="331px") for i in range(5 - len(self.day_times)): self.day_times.append(0) for i in range(5 - len(self.month_times)): self.month_times.append(0) for i in range(5 - len(self.year_times)): self.year_times.append(0) self.day_times = list(reversed(self.day_times)) self.month_times = list(reversed(self.month_times)) self.year_times = list(reversed(self.year_times)) bar = ( Bar(init_opts=op) .add_xaxis(["-4", "-3", "-2", "-1", "0"]) .add_yaxis("day", self.day_times) .add_yaxis("month", self.month_times) .add_yaxis("year", self.year_times) ) bar.render(path=get_project_root()+'/conf/usage_time.html')
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 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 setup_ui(self): self.ui.setupUi(self) self.my_html.load(QUrl('file:///' + get_project_root() + '/conf/usage_time.html')) self.ui.verticalLayout.addWidget(self.my_html)
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)