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


from widget.bms_download import Win_Download
from utils.globalvar import SD


class BmsDownloadControll:
    def __init__(self):
        self._view = Win_Download()
        self.init()

    def init(self):
        self._view.bms_sn_signal.connect(self._bms_sn_set)
        self._view.bat_sn_signal.connect(self._bat_sn_set)
        

    def _bms_sn_set(self):
        bms_sn_str = self._view.edt_bms_sn.text()
        bms_sn_list = list(bms_sn_str)
        try:
            data = []
            if len(data) < 8:
                data += (8 - len(data)) * [0]
            data[0] = 0x00
            data[1] = 0xB2
            data[2] = ord(bms_sn_list[0])
            data[3] = ord(bms_sn_list[1])
            data[4] = ord(bms_sn_list[2])
            data[5] = ord(bms_sn_list[3])
            data[6] = ord(bms_sn_list[4])
            data[7] = ord(bms_sn_list[5])
            msg = SD.CAN_CONTROL.send((0x18 << 24) | (SD.BCU_ID << 16) | 0x1803, data)
            
            data[0] = 0x01
            data[1] = 0xB2
            data[2] = ord(bms_sn_list[6])
            data[3] = ord(bms_sn_list[7])
            data[4] = ord(bms_sn_list[8])
            data[5] = ord(bms_sn_list[9])
            data[6] = ord(bms_sn_list[10])
            data[7] = ord(bms_sn_list[11])
            msg = SD.CAN_CONTROL.send((0x18 << 24) | (SD.BCU_ID << 16) | 0x1803, data)

            data[0] = 0x02
            data[1] = 0xB2
            data[2] = ord(bms_sn_list[12])
            data[3] = ord(bms_sn_list[13])
            data[4] = ord(bms_sn_list[14])
            data[5] = ord(bms_sn_list[15])
            data[6] = 0xFF
            data[7] = 0xFF
            msg = SD.CAN_CONTROL.send((0x18 << 24) | (SD.BCU_ID << 16) | 0x1803, data)
        except IndexError:
            self._view.index_error()
        except:
            self._view.can_connect_error()

    def _bat_sn_set(self):
        bat_sn_str = self._view.edt_battery_sn.text()
        bat_sn_list = list(bat_sn_str)
        try:
            data = []
            if len(data) < 8:
                data += (8 - len(data)) * [0]
            data[0] = 0x03
            data[1] = 0xB2
            data[2] = ord(bat_sn_list[0])
            data[3] = ord(bat_sn_list[1])
            data[4] = ord(bat_sn_list[2])
            data[5] = ord(bat_sn_list[3])
            data[6] = ord(bat_sn_list[4])
            data[7] = ord(bat_sn_list[5])
            msg = SD.CAN_CONTROL.send((0x18 << 24) | (SD.BCU_ID << 16) | 0x1803, data)
            
            data[0] = 0x04
            data[1] = 0xB2
            data[2] = ord(bat_sn_list[6])
            data[3] = ord(bat_sn_list[7])
            data[4] = ord(bat_sn_list[8])
            data[5] = ord(bat_sn_list[9])
            data[6] = ord(bat_sn_list[10])
            data[7] = ord(bat_sn_list[11])
            msg = SD.CAN_CONTROL.send((0x18 << 24) | (SD.BCU_ID << 16) | 0x1803, data)

            data[0] = 0x05
            data[1] = 0xB2
            data[2] = ord(bat_sn_list[12])
            data[3] = ord(bat_sn_list[13])
            data[4] = ord(bat_sn_list[14])
            data[5] = ord(bat_sn_list[15])
            data[6] = 0xFF
            data[7] = 0xFF
            msg = SD.CAN_CONTROL.send((0x18 << 24) | (SD.BCU_ID << 16) | 0x1803, data)
        except IndexError:
            self._view.index_error()
        except:
            self._view.can_connect_error()