#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@文件    :bms_home.py
@时间    :2021/12/09 13:33:56
@作者    :None
@版本    :1.0
@说明    :主页面逻辑
'''


from utils.globalvar import SD
from ui.home import UiBmsHomePage
from utils.hex_bit import bcd_hex
from utils.log_signal import LogSignal
from utils.qt import QMainWindow, Qt, QStyle, QMessageBox, QGuiApplication, QTimer, QDateTime, Signal


class Win_GotionBMS(UiBmsHomePage, QMainWindow):
    main_control_signal = Signal(int)
    cell_inf_signal = Signal(int)
    double_ccu_signal = Signal(int)
    protect_inf_signal = Signal(int)
    hard_cali_signal = Signal(int)
    balance_inf_signal = Signal(int)
    data_inf_signal = Signal(int)
    download_signal = Signal(int)
    set_inf_signal = Signal(int)
    bcu_online_list_signal = Signal(list)

    can_start_signal = Signal()
    can_stop_signal = Signal()
    cut_start_signal = Signal()
    
    def __init__(self, parent=None):
        super(Win_GotionBMS, self).__init__(parent)
        self.setupUi(self)

        self.btn_main_control.clicked.connect(self._main_control)
        self.btn_cell_inf.clicked.connect(self._cell_inf)
        self.btn_double_ccu.clicked.connect(self._double_ccu)
        self.btn_protect_inf.clicked.connect(self._protect_inf)
        self.btn_hard_cali.clicked.connect(self._hard_cali)
        self.btn_balance_inf.clicked.connect(self._balance_inf)
        self.btn_data_inf.clicked.connect(self._data_inf)
        self.btn_download.clicked.connect(self._download)
        self.btn_set_inf.clicked.connect(self._set_inf)
        self.btn_start.clicked.connect(self._can_start)
        self.btn_stop.clicked.connect(self._can_stop)
        self.btn_cut.clicked.connect(self._app_cut)
        
        self.cb_bcu.currentIndexChanged.connect(self._bcu_masterid_set)
        self.cb_bmu.currentIndexChanged.connect(self._bmu_id_set)
    
    def can_connect_error(self):
        QMessageBox.critical(self, "连接状态" if SD.SYSTEM_LANGUAGE == 0 else "Connect State", "CAN 连接失败！" if SD.SYSTEM_LANGUAGE == 0 else "CAN Connect Error！")

    def _main_control(self):
        self.main_control_signal.emit(0)
    
    def _cell_inf(self):
        self.cell_inf_signal.emit(1)

    def _double_ccu(self):
        self.double_ccu_signal.emit(2)

    def _protect_inf(self):
        self.protect_inf_signal.emit(3)

    def _hard_cali(self):
        self.hard_cali_signal.emit(4)

    def _balance_inf(self):
        self.balance_inf_signal.emit(5)

    def _data_inf(self):
        self.data_inf_signal.emit(6)

    def _download(self):
        self.download_signal.emit(7)

    def _set_inf(self):
        self.set_inf_signal.emit(8)

    def _can_start(self):
        self.can_start_signal.emit()

    def _can_stop(self):
        self.can_stop_signal.emit()

    def _app_cut(self):
        self.cut_start_signal.emit()
        
    def _bcu_masterid_set(self):
        SD.BCU_ID = int(self.cb_bcu.currentIndex() +  1)

    def _bmu_id_set(self):
        int(self.cb_bmu.currentIndex() +  1)

    def _bcu_connect_get(self, data):
        bcu_on_line = ["BCU1", "BCU2", "BCU3", "BCU4", "BCU5", "BCU6", "BCU7", "BCU8", "BCU9", "BCU10", "BCU11", "BCU12", "BCU13", "BCU14", "BCU15"]
        bcu_on_line_set = []
        if data[0] == 1:
            bcu_on_line[0] = "BCU1"
        else:
            bcu_on_line[0] = ""

        if data[1] == 1:
            bcu_on_line[1] = "BCU2"
        else:
            bcu_on_line[1] = ""

        if data[2] == 1:
            bcu_on_line[2] = "BCU3"
        else:
            bcu_on_line[2] = ""

        if data[3] == 1:
            bcu_on_line[3] = "BCU4"
        else:
            bcu_on_line[3] = ""

        if data[4] == 1:
            bcu_on_line[4] = "BCU5"
        else:
            bcu_on_line[4] = ""

        if data[5] == 1:
            bcu_on_line[5] = "BCU6"
        else:
            bcu_on_line[5] = ""

        if data[6] == 1:
            bcu_on_line[6] = "BCU7"
        else:
            bcu_on_line[6] = ""

        if data[7] == 1:
            bcu_on_line[7] = "BCU8"
        else:
            bcu_on_line[7] = ""

        if data[8] == 1:
            bcu_on_line[8] = "BCU9"
        else:
            bcu_on_line[8] = ""

        if data[9] == 1:
            bcu_on_line[9] = "BCU10"
        else:
            bcu_on_line[9] = ""

        if data[10] == 1:
            bcu_on_line[10] = "BCU11"
        else:
            bcu_on_line[10] = ""

        if data[11] == 1:
            bcu_on_line[11] = "BCU12"
        else:
            bcu_on_line[11] = ""
        
        if data[12] == 1:
            bcu_on_line[12] = "BCU13"
        else:
            bcu_on_line[12] = ""

        if data[13] == 1:
            bcu_on_line[13] = "BCU14"
        else:
            bcu_on_line[13] = ""

        if data[14] == 1:
            bcu_on_line[14] = "BCU15"
        else:
            bcu_on_line[14] = ""

        for a in list(bcu_on_line):
            if a != "":
                bcu_on_line_set.append(a)
        self.edt_online.setText('、'.join([i for i in list(bcu_on_line_set)]))
    
    
    # 居中显示
    def center(self):
        # 新方法
        self.setGeometry(
            QStyle.alignedRect(
                Qt.LeftToRight,
                Qt.AlignCenter,
                self.size(),
                QGuiApplication.primaryScreen().availableGeometry(),
            ),
        )