#!/usr/bin/env python
# -*- encoding: utf-8 -*-


from widget.bms_main import Win_Main
from utils.globalvar import SD
from utils.qt import Qt

class BmsMainControll:
    def __init__(self):
        self._view = Win_Main()
        self.i = 0
        self.init()

    def init(self):
        self._view.up_signal.connect(self.sys_up)
        self._view.down_signal.connect(self.sys_down)
        self._view.controll_signal.connect(self.sys_controller)
        self._view.do1_signal.connect(self.do1)
        self._view.do2_signal.connect(self.do2)
        self._view.do3_signal.connect(self.do3)
        self._view.do4_signal.connect(self.do4)
        self._view.do5_signal.connect(self.do5)
        self._view.do6_signal.connect(self.do6)
        self._view.do7_signal.connect(self.do7)
        self._view.do8_signal.connect(self.do8)
        
        self._view.fault_release_signal.connect(self.operate1)
        self._view.jy_signal.connect(self.operate2)
        self._view.jy_one_signal.connect(self.operate3)
        self._view.chg_mos_signal.connect(self.operate4)
        self._view.dchg_mos_signal.connect(self.operate5)
        self._view.pre_mos_signal.connect(self.operate6)
        self._view.gjd1_signal.connect(self.operate7)
        self._view.gjd2_signal.connect(self.operate8)
        self._view.chg_signal.connect(self.operate9)
        self._view.sleep_signal.connect(self.operate10)
        self._view.reset_signal.connect(self.operate11)
        self._view.clear_balance_signal.connect(self.operate12)
        self._view.fan_signal.connect(self.operate13)

    def sys_up(self):
        try:
            data = []
            if len(data) < 8:
                data += (8 - len(data)) * [0]
            data[0] = 0xFF
            data[1] = 0xFF
            data[2] = 0xAA
            data[6] = self.i
            self.i = self.i + 1
            if self.i == 255:
                self.i = 0
            data[7] = data[0] + data[1] + data[2] + data[3] + data[4] + data[5] + data[6]
            msg = SD.CAN_CONTROL.send((0x18 << 24) | (SD.BCU_ID << 16) | 0x1801, data)
        except:
            self._view.can_connect_error()


    def sys_down(self):
        try:
            data = []
            if len(data) < 8:
                data += (8 - len(data)) * [0]
            data[0] = 0xFF
            data[1] = 0xFF
            data[2] = 0xBB
            data[6] = self.i
            self.i = self.i + 1
            if self.i == 255:
                self.i = 0
            data[7] = data[0] + data[1] + data[2] + data[3] + data[4] + data[5] + data[6]
            msg = SD.CAN_CONTROL.send((0x18 << 24) | (SD.BCU_ID << 16) | 0x1801, data)
        except:
            self._view.can_connect_error()


    def sys_controller(self, check_status):
        if check_status:
            try:
                data = []
                if len(data) < 8:
                    data += (8 - len(data)) * [0]
                data[0] = 0xFF
                data[1] = 0xFF
                data[2] = 0xAA
                data[6] = self.i
                self.i = self.i + 1
                if self.i == 255:
                    self.i = 0
                data[7] = data[0] + data[1] + data[2] + data[3] + data[4] + data[5] + data[6]
                msg = SD.CAN_CONTROL.send((0x18 << 24) | (SD.BCU_ID << 16) | 0x1801, data)
                data[0] = 4
                data[1] = 0xFF
                data[2] = 0
                data[6] = self.i
                self.i = self.i + 1
                if self.i == 255:
                    self.i = 0
                data[7] = data[0] + data[1] + data[2] + data[3] + data[4] + data[5] + data[6]
                msg = SD.CAN_CONTROL.send((0x18 << 24) | (SD.BCU_ID << 16) | 0x1801, data)
            except:
                # self._view.under_widget.snkz_check_box.setCheckState(Qt.Unchecked)
                self._view.can_connect_error()
        else:
            try:
                data = []
                if len(data) < 8:
                    data += (8 - len(data)) * [0]
                data[0] = 5
                data[1] = 0xFF
                data[2] = 0
                data[6] = self.i
                self.i = self.i + 1
                if self.i == 255:
                    self.i = 0
                data[7] = data[0] + data[1] + data[2] + data[3] + data[4] + data[5] + data[6]
                msg = SD.CAN_CONTROL.send((0x18 << 24) | (SD.BCU_ID << 16) | 0x1801, data)
            except:
                self._view.can_connect_error()

    def do1(self, do1_status):
        try:
            data = []
            if len(data) < 8:
                data += (8 - len(data)) * [0]
            data[0] = 0x01
            data[1] = 0xC0
            data[2] = (do1_status >> 1) & 0xFF
            data[3] = 0
            msg = SD.CAN_CONTROL.send((0x18 << 24) | (SD.BCU_ID << 16) | 0x1804, data)
        except:
            self._view.can_connect_error()


    def do2(self, do2_status):
        try:
            data = []
            if len(data) < 8:
                data += (8 - len(data)) * [0]
            data[0] = 0x01
            data[1] = 0xC0
            data[2] = do2_status & 0xFF
            data[3] = 0
            msg = SD.CAN_CONTROL.send((0x18 << 24) | (SD.BCU_ID << 16) | 0x1804, data)
        except:
            self._view.can_connect_error()


    def do3(self, do3_status):
        try:
            data = []
            if len(data) < 8:
                data += (8 - len(data)) * [0]
            data[0] = 0x01
            data[1] = 0xC0
            data[2] = (do3_status << 1) & 0xFF
            data[3] = 0
            msg = SD.CAN_CONTROL.send((0x18 << 24) | (SD.BCU_ID << 16) | 0x1804, data)
        except:
            self._view.can_connect_error()


    def do4(self, do4_status):
        try:
            data = []
            if len(data) < 8:
                data += (8 - len(data)) * [0]
            data[0] = 0x01
            data[1] = 0xC0
            data[2] = (do4_status << 2) & 0xFF
            data[3] = 0
            msg = SD.CAN_CONTROL.send((0x18 << 24) | (SD.BCU_ID << 16) | 0x1804, data)
        except:
            self._view.can_connect_error()


    def do5(self, do5_status):
        try:
            data = []
            if len(data) < 8:
                data += (8 - len(data)) * [0]
            data[0] = 0x01
            data[1] = 0xC0
            data[2] = (do5_status << 3) & 0xFF
            data[3] = 0
            msg = SD.CAN_CONTROL.send((0x18 << 24) | (SD.BCU_ID << 16) | 0x1804, data)
        except:
            self._view.can_connect_error()


    def do6(self, do6_status):
        try:
            data = []
            if len(data) < 8:
                data += (8 - len(data)) * [0]
            data[0] = 0x01
            data[1] = 0xC0
            data[2] = (do6_status << 4) & 0xFF
            data[3] = 0
            msg = SD.CAN_CONTROL.send((0x18 << 24) | (SD.BCU_ID << 16) | 0x1804, data)
        except:
            self._view.can_connect_error()


    def do7(self, do7_status):
        try:
            data = []
            if len(data) < 8:
                data += (8 - len(data)) * [0]
            data[0] = 0x01
            data[1] = 0xC0
            data[2] = (do7_status << 1) & 0xFF
            data[3] = 0
            msg = SD.CAN_CONTROL.send((0x18 << 24) | (SD.BCU_ID << 16) | 0x1804, data)
        except:
            self._view.can_connect_error()


    def do8(self, do8_status):
        try:
            data = []
            if len(data) < 8:
                data += (8 - len(data)) * [0]
            data[0] = 0x01
            data[1] = 0xC0
            data[2] = (do8_status << 1) & 0xFF
            data[3] = 0
            msg = SD.CAN_CONTROL.send((0x18 << 24) | (SD.BCU_ID << 16) | 0x1804, data)
        except:
            self._view.can_connect_error()

    def operate1(self, status):
        try:
            data = []
            if len(data) < 8:
                data += (8 - len(data)) * [0]
            data[0] = 0x00
            data[1] = 0xC0
            data[2] = (status >> 1) & 0xFF
            data[3] = 0
            msg = SD.CAN_CONTROL.send((0x18 << 24) | (SD.BCU_ID << 16) | 0x1804, data)
        except:
            self._view.can_connect_error()


    def operate2(self, status):
        try:
            data = []
            if len(data) < 8:
                data += (8 - len(data)) * [0]
            data[0] = 0x00
            data[1] = 0xC0
            data[2] = (status) & 0xFF
            data[3] = 0
            msg = SD.CAN_CONTROL.send((0x18 << 24) | (SD.BCU_ID << 16) | 0x1804, data)
        except:
            self._view.can_connect_error()


    def operate3(self, status):
        try:
            data = []
            if len(data) < 8:
                data += (8 - len(data)) * [0]
            data[0] = 0x00
            data[1] = 0xC0
            data[2] = (status << 1) & 0xFF
            data[3] = 0
            msg = SD.CAN_CONTROL.send((0x18 << 24) | (SD.BCU_ID << 16) | 0x1804, data)
        except:
            self._view.can_connect_error()


    def operate4(self, status):
        try:
            data = []
            if len(data) < 8:
                data += (8 - len(data)) * [0]
            data[0] = 0x00
            data[1] = 0xC0
            data[2] = (status << 2) & 0xFF
            data[3] = 0
            msg = SD.CAN_CONTROL.send((0x18 << 24) | (SD.BCU_ID << 16) | 0x1804, data)
        except:
            self._view.can_connect_error()


    def operate5(self, status):
        try:
            data = []
            if len(data) < 8:
                data += (8 - len(data)) * [0]
            data[0] = 0x00
            data[1] = 0xC0
            data[2] = (status << 3) & 0xFF
            data[3] = 0
            msg = SD.CAN_CONTROL.send((0x18 << 24) | (SD.BCU_ID << 16) | 0x1804, data)
        except:
            self._view.can_connect_error()


    def operate6(self, status):
        try:
            data = []
            if len(data) < 8:
                data += (8 - len(data)) * [0]
            data[0] = 0x00
            data[1] = 0xC0
            data[2] = (status << 4) & 0xFF
            data[3] = 0
            msg = SD.CAN_CONTROL.send((0x18 << 24) | (SD.BCU_ID << 16) | 0x1804, data)
        except:
            self._view.can_connect_error()


    def operate7(self, status):
        try:
            data = []
            if len(data) < 8:
                data += (8 - len(data)) * [0]
            data[0] = 0x00
            data[1] = 0xC0
            data[2] = (status << 5) & 0xFF
            data[3] = 0
            msg = SD.CAN_CONTROL.send((0x18 << 24) | (SD.BCU_ID << 16) | 0x1804, data)
        except:
            self._view.can_connect_error()


    def operate8(self, status):
        try:
            data = []
            if len(data) < 8:
                data += (8 - len(data)) * [0]
            data[0] = 0x00
            data[1] = 0xC0
            data[2] = (status << 6) & 0xFF
            data[3] = 0
            msg = SD.CAN_CONTROL.send((0x18 << 24) | (SD.BCU_ID << 16) | 0x1804, data)
        except:
            self._view.can_connect_error()


    def operate9(self, status):
        try:
            data = []
            if len(data) < 8:
                data += (8 - len(data)) * [0]
            data[0] = 0x00
            data[1] = 0xC0
            data[2] = 0
            data[3] = (status >> 1) & 0xFF
            msg = SD.CAN_CONTROL.send((0x18 << 24) | (SD.BCU_ID << 16) | 0x1804, data)
        except:
            self._view.can_connect_error()

    def operate10(self, status):
        try:
            data = []
            if len(data) < 8:
                data += (8 - len(data)) * [0]
            data[0] = 0x00
            data[1] = 0xC0
            data[2] = 0
            data[3] = (status) & 0xFF
            msg = SD.CAN_CONTROL.send((0x18 << 24) | (SD.BCU_ID << 16) | 0x1804, data)
        except:
            self._view.can_connect_error()


    def operate11(self, status):
        try:
            data = []
            if len(data) < 8:
                data += (8 - len(data)) * [0]
            data[0] = 0x00
            data[1] = 0xC0
            data[2] = 0
            data[3] = (status >> 1) & 0xFF
            msg = SD.CAN_CONTROL.send((0x18 << 24) | (SD.BCU_ID << 16) | 0x1804, data)
        except:
            self._view.can_connect_error()


    def operate12(self, status):
        try:
            data = []
            if len(data) < 8:
                data += (8 - len(data)) * [0]
            data[0] = 0x00
            data[1] = 0xC0
            data[2] = 0
            data[3] = (status >> 2) & 0xFF
            msg = SD.CAN_CONTROL.send((0x18 << 24) | (SD.BCU_ID << 16) | 0x1804, data)
        except:
            self._view.can_connect_error()

    def operate13(self, status):
        try:
            data = []
            if len(data) < 8:
                data += (8 - len(data)) * [0]
            data[0] = 0x00
            data[1] = 0xC0
            data[2] = 0
            data[3] = (status >> 4) & 0xFF
            msg = SD.CAN_CONTROL.send((0x18 << 24) | (SD.BCU_ID << 16) | 0x1804, data)
        except:
            self._view.can_connect_error()