def __init__(self):
        super().__init__()
        self.server = Server()
        self.device_setting = DeviceSetting()
        self.apis = Apis(self.server, self.device_setting)

        self.initial_setup_ui = InitialSetupUI(self.device_setting,
                                               self.server, self.apis)
        self.main_energy_protector_ui = MainEnergyProtectorUI(
            self.device_setting, self.server, self.apis)
        self.error_load_setting_ui = ErrorLoadSettingUi()

        self.setup_ui()
        self.set_signals()

        res = self.device_setting.register(self.server)
        if res is not None:
            if get_token(self.server, self.apis) is None:
                request_failed('GET_TOKEN')
        else:
            request_failed('DEVICE_SETTING.REGISTER')

        if not self.device_setting.load_conf():
            self.show_initial_setting()
        else:
            self.show_main_energy_protector()
    def finish_initial_setting(self):
        if get_token(self.server, self.apis) is None:
            request_failed('GET_TOKEN')
        else:
            res = self.apis.raspberry.put(self.server.token.access)
            print(res)
            if not res[0]:
                request_failed('RASPBERRY.PUT')
            else:
                self.device_setting.write()

        self.next_page()
 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 toggle_state(self):
        self.state = not self.state
        res = self.apis.device.control(self.device_id, self.device_type, self.state, self.unit_index,
                                       self.server.token.access)
        print(res)
        if res[0] is False:
            self.state = not self.state
            request_failed('DEVICE.CONTROL')
            self.ui.button_push.setText('ERR')
            self.ui.button_push.setStyleSheet("border:none;\n"
                                              "background-color: rgb(255, 110, 112);\n"
                                              "color: white;\n"
                                              "border-top-right-radius: 15%;\n"
                                              "border-bottom-right-radius: 15%;")
            return

        self.set_style()
    def change_setting(self):
        is_pw = False
        password_is_change = False
        remote_control_is_change = False
        auto_control_is_change = False

        if self.is_initial:
            self.is_initial = False
            self.ui.initial_button.setText("에너지 지킴이 초기화")

        if self.remote_control is not self.device_setting.remote_control:
            self.device_setting.remote_control = self.remote_control
            remote_control_is_change = True
        if self.auto_control is not self.device_setting.auto_control:
            self.device_setting.auto_control = self.auto_control
            auto_control_is_change = True

        if len(self.password) > 0:
            is_pw = True
            if finish_set_password(self, self.ui.change_setting_button):
                if self.password is not self.device_setting.password:
                    self.device_setting.password = self.password
                    password_is_change = True
            else:
                self.exist_message = True
        else:
            if len(self.re_password) > 0:
                self.ui.password_re_edit.setText("")
            else:
                self.ui.notice_wrong_label.setText("")
                self.ui.notice_wrong_label_2.setText("")

        if password_is_change and not remote_control_is_change and not auto_control_is_change:
            self.ui.change_setting_button.setText("비밀번호가 변경되었습니다.")
            self.exist_message = True
        elif remote_control_is_change and not password_is_change and not auto_control_is_change:
            self.ui.change_setting_button.setText("원격 제어 설정이 변경되었습니다.")
            self.exist_message = True
        elif auto_control_is_change and not password_is_change and not remote_control_is_change:
            self.ui.change_setting_button.setText("자동 OFF 설정이 변경되었습니다.")
            self.exist_message = True
        elif remote_control_is_change and auto_control_is_change and not password_is_change:
            self.ui.change_setting_button.setText("원격 제어/자동 OFF 설정이 변경되었습니다.")
            self.exist_message = True
        elif password_is_change and remote_control_is_change and not auto_control_is_change:
            self.ui.change_setting_button.setText("원격제어 설정/비밀번호가 변경되었습니다.")
            self.exist_message = True
        elif password_is_change and auto_control_is_change and not remote_control_is_change:
            self.ui.change_setting_button.setText("자동 OFF 설정/비밀번호가 변경되었습니다.")
            self.exist_message = True
        elif password_is_change and auto_control_is_change and remote_control_is_change:
            self.ui.change_setting_button.setText("비밀번호/원격 제어/자동 OFF 설정이 변경되었습니다.")
            self.exist_message = True
        else:
            if not is_pw:
                self.ui.change_setting_button.setText("설정이 변경되지 않았습니다.")
                self.exist_message = True
        self.password = ""
        self.re_password = ""

        ## 라즈베리파이 설정 반영
        if not password_is_change and not remote_control_is_change and auto_control_is_change:
            self.device_setting.write()
            return

        is_change = password_is_change or remote_control_is_change
        if is_change:
            if get_token(self.server, self.apis) is None:
                request_failed('GET_TOKEN')
                self.ui.change_setting_button.setText("오류가 발생했습니다. 설정 변경에 실패하였습니다.")
                self.exist_message = True
                self.device_setting.load_conf()
                set_control_setting_style(self.ui.auto_control_button, self.device_setting.auto_control)
                set_control_setting_style(self.ui.remote_control_button, self.device_setting.remote_control)
            else:
                res = self.apis.raspberry.put(self.server.token.access)
                if not res[0]:
                    request_failed('RASPBERRY.PUT')
                    self.ui.change_setting_button.setText("오류가 발생했습니다. 설정 변경에 실패하였습니다.")
                    self.exist_message = True
                    self.device_setting.load_conf()
                    set_control_setting_style(self.ui.auto_control_button, self.device_setting.auto_control)
                    set_control_setting_style(self.ui.remote_control_button, self.device_setting.remote_control)
                else:
                    self.device_setting.write()
                    self.ui.id_read_only.setText(
                        "ID & PW : '" + self.device_setting.id + "' & '" + self.device_setting.password + "'")
        self.auto_control = self.device_setting.auto_control
        self.remote_control = self.device_setting.remote_control