#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@文件    :ccuWork.py
@时间    :2021/12/09 08:31:48
@作者    :None
@版本    :1.0
@说明    :二级BMS工作线程
'''

from utils.delay import m_delay
from utils.globalvar import SD
from utils.hex_bit import hex_bcd
from utils.log_signal import LogSignal
from utils.qt import QObject, Signal, QDateTime


class BmsSend(QObject):

    def __init__(self):
        super(BmsSend, self).__init__()
        self.page = 1

    def send(self):
        i = 0
        while SD.START_RUN:
            if SD.FRAM_READ_START:
                if SD.FRAM_SEND_PAGE:
                    self._fram_read(self.page)
                    self.page += 1
                    SD.FRAM_SEND_PAGE = False
                else:
                    if SD.FRAM_DELAY_TIME < 10000:
                        m_delay(1)
                        SD.FRAM_DELAY_TIME = SD.FRAM_DELAY_TIME + 1
                    else:
                        LogSignal.print_log_signal().log_emit("BCMU 铁电数据接收超时重发！" if SD.SYSTEM_LANGUAGE == 0 else "BCMU Fram Timeout！")
                        self.page -= 1
                        SD.FRAM_SEND_PAGE = True
            else:
                if SD.CCU_CHIP == 0 or SD.CCU_CHIP == 1:
                    self._run_old()
                elif SD.CCU_CHIP == 2:
                    self._run_new(i)
                    m_delay(100)
                    self._run_relay(i)
                    i = i + 1
                    if i == 256:
                        i = 0
                if SD.LIFE_CTL == 2:
                    self._life_date()
                m_delay(1000)

    def _fram_read(self, page):
        data = []
        if len(data) < 8:
            data += (8 - len(data)) * [0]
        data[0] = 0x01
        data[1] = page & 0xFF
        data[2] = (page & 0xFF00) >> 8
        data[3] = 0x01
        data[4] = 0xFF
        data[5] = 0xFF
        data[6] = 0xFF
        data[7] = 0xFF
        SD.CAN_CONTROL.send(0x1801B1C1, data, extern_flag=True)

    def _run_old(self):
        if SD.CCU_CHIP == 0:
            id = 0x18F403F0
        elif SD.CCU_CHIP == 1:
            id = 0x18F403F0 | ((SD.BCU_ID & 0xF0) << 8) | (SD.BCU_ID & 0xF)
        SD.CAN_CONTROL.send(id, [0x52, 0x55, 0x4E, 0, 0, 0, 0, 0xF5], extern_flag=True)

    def _run_new(self, i):
        data = []
        if len(data) < 8:
            data += (8 - len(data)) * [0]
        data[0] = 2
        data[1] = SD.SINGLE_ACQUISITION_OR_BALANCE
        data[6] = i
        data[7] = data[0] + data[1] + data[2] + data[3] + data[4] + data[5] + data[6]

        SD.CAN_CONTROL.send((0x18 << 24) | (SD.BCU_ID << 16) | 0x0020, data, extern_flag=True)

    def _run_relay(self, i):
        data = []
        if len(data) < 8:
            data += (8 - len(data)) * [0]
        data[0] = 4
        data[6] = i
        data[7] = data[0] + data[1] + data[2] + data[3] + data[4] + data[5] + data[6]

        SD.CAN_CONTROL.send((0x18 << 24) | (SD.BCU_ID << 16) | 0x0020, data, extern_flag=True)

    def _life_date(self):
        data = []
        if len(data) < 8:
            data += (8 - len(data)) * [0x00]
        SD.CAN_CONTROL.send(0x18FF0000, data, extern_flag=True)


class BmsReceived(QObject):

    to_show_masterid_signal = Signal(int)
    to_show_0000_signal = Signal(object)
    to_show_0001_signal = Signal(object)
    to_show_0002_signal = Signal(object)
    to_show_0003_signal = Signal(object)
    to_show_0004_signal = Signal(object)
    to_show_0005_signal = Signal(object)
    to_show_0006_signal = Signal(object)
    to_show_0007_signal = Signal(object)
    to_show_0008_signal = Signal(object)
    to_show_0009_signal = Signal(object)
    to_show_000A_signal = Signal(object)
    to_show_000B_signal = Signal(object)
    to_show_000C_signal = Signal(object)
    to_show_000D_signal = Signal(object)
    to_show_000E_signal = Signal(object)
    to_show_000F_signal = Signal(object)
    to_show_0010_signal = Signal(object)
    to_show_0011_signal = Signal(object)
    to_show_0012_signal = Signal(object)
    to_show_0013_signal = Signal(object)
    to_show_0014_signal = Signal(object)
    to_show_0015_signal = Signal(object)
    to_show_0016_signal = Signal(object)
    to_show_0017_signal = Signal(object)
    to_show_0018_signal = Signal(object)
    to_show_0019_signal = Signal(object)
    to_show_001A_signal = Signal(object)
    to_show_001B_signal = Signal(object)
    to_show_001C_signal = Signal(object)
    to_show_001D_signal = Signal(object)
    to_show_001E_signal = Signal(object)
    to_show_001F_signal = Signal(object)
    to_show_0020_signal = Signal(object)
    to_show_0021_signal = Signal(object)
    to_show_0022_signal = Signal(object)
    to_show_0023_signal = Signal(object)

    def __init__(self):
        super(BmsReceived, self).__init__()

        self.dantidianya = [0 for i in range(10000)]
        self.dantiwendu = [0 for i in range(10000)]
        self.masterVersion = ["" for i in range(18)]
        self.fram_param = ["" for i in range(24)]
        self.single_low_vol_x = ""
        self.single_high_temp_x = ""

    # 接收数据
    def received(self):
        while SD.CAN_ON_OFF:
            result = SD.CAN_CONTROL.receive()
            if (result[1]):
                # ID主机号识别
                if result[1] & 0xFFFF == 1:
                    self.to_show_masterid_signal.emit((result[1] >> 16) & 0xFF)
                # 0001
                if result[1] == 0x18000000 | (SD.BCU_ID << 16):
                    self.to_show_0000_signal.emit(result[2])
                # 0001
                if result[1] == 0x18000001 | (SD.BCU_ID << 16):
                    self.to_show_0001_signal.emit(result[2])
                # 0002  
                elif result[1] == 0x18000002 | (SD.BCU_ID << 16):
                    self.to_show_0002_signal.emit(result[2])
                # 0003
                elif result[1] == 0x18000003 | (SD.BCU_ID << 16):
                    self.to_show_0003_signal.emit(result[2])
                # 0004  
                elif result[1] == 0x18000004 | (SD.BCU_ID << 16):
                    self.to_show_0004_signal.emit(result[2])
                # 0005
                elif result[1] == 0x18000005 | (SD.BCU_ID << 16):
                    self.to_show_0005_signal.emit(result[2])
                # 0006  
                elif result[1] == 0x18000006 | (SD.BCU_ID << 16):
                    self.to_show_0006_signal.emit(result[2])
                # 0007
                elif result[1] == 0x18000007 | (SD.BCU_ID << 16):
                    self.to_show_0007_signal.emit(result[2])
                # 0008
                elif result[1] == 0x18000008 | (SD.BCU_ID << 16):
                    self.to_show_0008_signal.emit(result[2])
                # 0009
                elif result[1] == 0x18000009 | (SD.BCU_ID << 16):
                    self.to_show_0009_signal.emit(result[2])
                # 000A
                elif result[1] == 0x1800000A | (SD.BCU_ID << 16):
                    self.to_show_000A_signal.emit(result[2])
                # 000B
                elif result[1] == 0x1800000B | (SD.BCU_ID << 16):
                    self.to_show_000B_signal.emit(result[2])
                # 000C 
                elif result[1] == 0x1800000C | (SD.BCU_ID << 16):
                    self.to_show_000C_signal.emit(result[2])
                # 000D
                elif result[1] == 0x1800000D | (SD.BCU_ID << 16):
                    self.to_show_000D_signal.emit(result[2])
                # 000E
                elif result[1] == 0x1800000E | (SD.BCU_ID << 16):
                    self.to_show_000E_signal.emit(result[2])
                # 000F
                elif result[1] == 0x1800000F | (SD.BCU_ID << 16):
                    self.to_show_000F_signal.emit(result[2])
                # 0010
                elif result[1] == 0x18000010 | (SD.BCU_ID << 16):
                    self.to_show_0010_signal.emit(result[2])
                # 0011
                elif result[1] == 0x18000011 | (SD.BCU_ID << 16):
                    self.to_show_0011_signal.emit(result[2])
                # 0012
                elif result[1] == 0x18000012 | (SD.BCU_ID << 16):
                    self.to_show_0012_signal.emit(result[2])
                # 0013
                elif result[1] == 0x18000013 | (SD.BCU_ID << 16):
                    self.to_show_0013_signal.emit(result[2])
                # 0014
                elif result[1] == 0x18000014 | (SD.BCU_ID << 16):
                    self.to_show_0014_signal.emit(result[2])
                # 0015
                elif result[1] == 0x18000015 | (SD.BCU_ID << 16):
                    self.to_show_0015_signal.emit(result[2])
                # 0016
                elif result[1] == 0x18000016 | (SD.BCU_ID << 16):
                    self.to_show_0016_signal.emit(result[2])
                # 0017
                elif result[1] == 0x18000017 | (SD.BCU_ID << 16):
                    self.to_show_0017_signal.emit(result[2])
                # 0018
                elif result[1] == 0x18000018 | (SD.BCU_ID << 16):
                    self.to_show_0018_signal.emit(result[2])
                # 0019
                elif result[1] == 0x18000019 | (SD.BCU_ID << 16):
                    self.to_show_0019_signal.emit(result[2])
                # 001A
                elif result[1] == 0x1800001A | (SD.BCU_ID << 16):
                    self.to_show_001A_signal.emit(result[2])
                # 001B
                elif result[1] == 0x1800001B | (SD.BCU_ID << 16):
                    self.to_show_001B_signal.emit(result[2])
                # 001C
                elif result[1] == 0x1800001C | (SD.BCU_ID << 16):
                    self.to_show_001C_signal.emit(result[2])
                # 001D
                elif result[1] == 0x1800001D | (SD.BCU_ID << 16):
                    self.to_show_001D_signal.emit(result[2])
                # 001E
                elif result[1] == 0x1800001E | (SD.BCU_ID << 16):
                    self.to_show_001E_signal.emit(result[2])
                # 001F
                elif result[1] == 0x1800001F | (SD.BCU_ID << 16):
                    self.to_show_001F_signal.emit(result[2])
                # 0020
                elif result[1] == 0x18000020 | (SD.BCU_ID << 16):
                    self.to_show_0020_signal.emit(result[2])
                # 0021
                elif result[1] == 0x18000021 | (SD.BCU_ID << 16):
                    self.to_show_0021_signal.emit(result[2])
                # 0022
                elif result[1] == 0x18000022 | (SD.BCU_ID << 16):
                    self.to_show_0022_signal.emit(result[2])
                # 0023
                elif result[1] == 0x18000023 | (SD.BCU_ID << 16):
                    self.to_show_0023_signal.emit(result[2])
                    
    # # 接收数据
    # def received(self):
    #     while SD.CAN_ON_OFF:
    #         restNum = SD.CAN_CONTROL.get_undeal_number()
    #         if restNum <= 0:
    #             continue
    #         revRet = SD.CAN_CONTROL.receive()
    #         for i in revRet:
    #             print(i)
    #             if int(i.RemoteFlag) == 0:
    #                 print(i.ID)
    #                 # ID主机号识别
    #                 if i.ID & 0xFFFF == 1:
    #                     self.to_show_masterid_signal.emit((i.ID >> 16) & 0xFF)
    #                 # 0001
    #                 if i.ID == 0x18000001 | (SD.BCU_ID << 16):
    #                     self.to_show_0001_signal.emit(i.Data)
    #                 # 0002  
    #                 elif i.ID == 0x18000002 | (SD.BCU_ID << 16):
    #                     self.to_show_0002_signal.emit(i.Data)
    #                 # 0003
    #                 elif i.ID == 0x18000003 | (SD.BCU_ID << 16):
    #                     self.to_show_0003_signal.emit(i.Data)
    #                 # 0004  
    #                 elif i.ID == 0x18000004 | (SD.BCU_ID << 16):
    #                     self.to_show_0004_signal.emit(i.Data)
    #                 # 0005
    #                 elif i.ID == 0x18000005 | (SD.BCU_ID << 16):
    #                     self.to_show_0005_signal.emit(i.Data)
    #                 # 0006  
    #                 elif i.ID == 0x18000006 | (SD.BCU_ID << 16):
    #                     self.to_show_0006_signal.emit(i.Data)
    #                 # 0007
    #                 elif i.ID == 0x18000007 | (SD.BCU_ID << 16):
    #                     self.to_show_0007_signal.emit(i.Data)
    #                 # 0008
    #                 elif i.ID == 0x18000008 | (SD.BCU_ID << 16):
    #                     self.to_show_0008_signal.emit(i.Data)
    #                 # 0009
    #                 elif i.ID == 0x18000009 | (SD.BCU_ID << 16):
    #                     self.to_show_0009_signal.emit(i.Data)
    #                 # 000A
    #                 elif i.ID == 0x1800000A | (SD.BCU_ID << 16):
    #                     self.to_show_000A_signal.emit(i.Data)
    #                 # 000B
    #                 elif i.ID == 0x1800000B | (SD.BCU_ID << 16):
    #                     self.to_show_000B_signal.emit(i.Data)
    #                 # 000C 
    #                 elif i.ID == 0x1800000C | (SD.BCU_ID << 16):
    #                     self.to_show_000C_signal.emit(i.Data)
    #                 # 000D
    #                 elif i.ID == 0x1800000D | (SD.BCU_ID << 16):
    #                     self.to_show_000D_signal.emit(i.Data)
    #                 # 000E
    #                 elif i.ID == 0x1800000E | (SD.BCU_ID << 16):
    #                     self.to_show_000E_signal.emit(i.Data)
    #                 # 000F
    #                 elif i.ID == 0x1800000F | (SD.BCU_ID << 16):
    #                     self.to_show_000F_signal.emit(i.Data)
    #                 # 0010
    #                 elif i.ID == 0x18000010 | (SD.BCU_ID << 16):
    #                     self.to_show_0010_signal.emit(i.Data)
    #                 # 0011
    #                 elif i.ID == 0x18000011 | (SD.BCU_ID << 16):
    #                     self.to_show_0011_signal.emit(i.Data)
    #                 # 0012
    #                 elif i.ID == 0x18000012 | (SD.BCU_ID << 16):
    #                     self.to_show_0012_signal.emit(i.Data)
    #                 # 0013
    #                 elif i.ID == 0x18000013 | (SD.BCU_ID << 16):
    #                     self.to_show_0013_signal.emit(i.Data)
    #                 # 0014
    #                 elif i.ID == 0x18000014 | (SD.BCU_ID << 16):
    #                     self.to_show_0014_signal.emit(i.Data)
    #                 # 0015
    #                 elif i.ID == 0x18000015 | (SD.BCU_ID << 16):
    #                     self.to_show_0015_signal.emit(i.Data)
    #                 # 0016
    #                 elif i.ID == 0x18000016 | (SD.BCU_ID << 16):
    #                     self.to_show_0016_signal.emit(i.Data)
    #                 # 0017
    #                 elif i.ID == 0x18000017 | (SD.BCU_ID << 16):
    #                     self.to_show_0017_signal.emit(i.Data)
    #                 # 0018
    #                 elif i.ID == 0x18000018 | (SD.BCU_ID << 16):
    #                     self.to_show_0018_signal.emit(i.Data)
    #                 # 0019
    #                 elif i.ID == 0x18000019 | (SD.BCU_ID << 16):
    #                     self.to_show_0019_signal.emit(i.Data)
    #                 # 001A
    #                 elif i.ID == 0x1800001A | (SD.BCU_ID << 16):
    #                     self.to_show_001A_signal.emit(i.Data)
    #                 # 001B
    #                 elif i.ID == 0x1800001B | (SD.BCU_ID << 16):
    #                     self.to_show_001B_signal.emit(i.Data)
    #                 # 001C
    #                 elif i.ID == 0x1800001C | (SD.BCU_ID << 16):
    #                     self.to_show_001C_signal.emit(i.Data)
    #                 # 001D
    #                 elif i.ID == 0x1800001D | (SD.BCU_ID << 16):
    #                     self.to_show_001D_signal.emit(i.Data)
    #                 # 001E
    #                 elif i.ID == 0x1800001E | (SD.BCU_ID << 16):
    #                     self.to_show_001E_signal.emit(i.Data)
    #                 # 001F
    #                 elif i.ID == 0x1800001F | (SD.BCU_ID << 16):
    #                     self.to_show_001F_signal.emit(i.Data)
    #                 # 0020
    #                 elif i.ID == 0x18000020 | (SD.BCU_ID << 16):
    #                     self.to_show_0020_signal.emit(i.Data)
    #                 # 0021
    #                 elif i.ID == 0x18000021 | (SD.BCU_ID << 16):
    #                     self.to_show_0021_signal.emit(i.Data)
    #                 # 0022
    #                 elif i.ID == 0x18000022 | (SD.BCU_ID << 16):
    #                     self.to_show_0022_signal.emit(i.Data)
    #                 # 0023
    #                 elif i.ID == 0x18000023 | (SD.BCU_ID << 16):
    #                     self.to_show_0023_signal.emit(i.Data)