import time
from mcu.SerialIns import SerialIns

class DeviceHandler:

    def __init__(self, windows=None):
        super().__init__()
        self.windows = windows
        self.serial_ins = None
        self.port_name = ""
        self.connect_state = False
        self.is_running = False

    def to_connect_com(self, port_name, is_test=False):
        self.close_connect()
        time.sleep(0.5)
        try:
            # 原值为9600
            self.serial_ins = SerialIns(port_name=port_name, baud=115200)
            # self.serial_ins = SerialIns(port_name=port_name, baud=9600)
            if self.serial_ins.serial_handle:
                self.sign_data.emit(
                    {
                        "_type": "show_info",
                        "plugins_mode": "remote_control",
                        "data": "遥控设备 打开串口成功",
                    }
                )

                self.connect_state = True
                self.sign_data.emit(
                    {
                        "_type": "remote_control_connect",
                        "plugins_mode": "remote_control",
                        "data": port_name,
                    }
                )
                self.port_name = port_name
                if is_test is False:
                    self.start()
                return True

            else:
                self.sign_data.emit(
                    {
                        "_type": "show_info",
                        "plugins_mode": "remote_control",
                        "data": "遥控设备 打开串口失败",
                    }
                )

                self.serial_ins = None
                self.connect_state = False
        except:
            self.sign_data.emit(
                {
                    "_type": "show_info",
                    "plugins_mode": "remote_control",
                    "data": "遥控设备 打开串口失败",
                }
            )
            self.serial_ins = None
            self.connect_state = False
            return False

    def close_connect(self):
        self.port_name = ""
        if self.connect_state:
            self.serial_ins.close_serial_port()
            self.connect_state = False

    def __del__(self):
        self.close_connect()

    def analysis_received_data(self):
        if not self.connect_state:
            return
        receive_data = self.serial_ins.read_cmd(out_time=1, check=None)
        if receive_data is False:
            self.connect_state = False
            return False
        if not receive_data:
            return
        else:
            print(
                "read receive_data {}".format(
                    self.serial_ins.change_hex_to_int(receive_data)
                )
            )
            pass
        # 数据 结构 command，按命令解析
        if receive_data[0] == 1:
            # 扫码数据
            bar_code = receive_data[1:].decode()
            bar_code = bar_code.replace("\r", "")
            bar_code = bar_code.replace("\n", "")

            self.sign_data.emit(
                {"_type": 0, "plugins_mode": "remote_control", "data": bar_code}
            )
            return
        if receive_data[0] == 9:
            button_value = receive_data[1]
            data = {"button_value": button_value}
            self.sign_data.emit(
                {"_type": 9, "plugins_mode": "remote_control", "data": data}
            )
            return
        pass

    def run(self):
        # self.show_info.emit("未连接")
        # self.data_command_sign.emit(data)
        self.is_running = True
        while 1:
            time.sleep(0.05)
            if not self.connect_state:
                self.sign_data.emit(
                    {
                        "_type": "show_info",
                        "plugins_mode": "remote_control",
                        "data": "遥控设备 未连接",
                    }
                )
                break
            self.analysis_received_data()

        self.is_running = False
        if not self.connect_state:
            self.sign_data.emit(
                {
                    "_type": "show_info",
                    "plugins_mode": "remote_control",
                    "data": "遥控设备 未连接",
                }
            )
