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

from utils.globalvar import SD
from utils.qt import QObject, Signal
import utils.modbus.defines as cst
from utils.delay import m_delay


class PcsCanReceived(QObject):

    show_90_signal = Signal(object)
    show_91_signal = Signal(object)
    show_92_signal = Signal(object)
    show_93_signal = Signal(object)

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

    # 接收数据
    def received(self):
        while SD.CAN_ON_OFF:
            restNum = SD.CAN_CONTROL.get_undeal_number()
            if restNum <= 0:
                continue
            revRet = SD.CAN_CONTROL.receive(restNum)
            for i in revRet:
                if int(i.RemoteFlag) != 0:
                    continue

                if i.ID == 0x18903F00:
                    self.show_90_signal.emit(i.Data)
                elif i.ID == 0x18913F00:
                    self.show_91_signal.emit(i.Data)
                elif i.ID == 0x18923F00:
                    self.show_92_signal.emit(i.Data)
                elif i.ID == 0x18933F00:
                    self.show_93_signal.emit(i.Data)
                else:
                    continue


class PcsComWork(QObject):

    show_00_com_signal = Signal(int)
    show_01_com_signal = Signal(int)
    show_02_com_signal = Signal(int)
    show_03_com_signal = Signal(int)
    show_04_com_signal = Signal(int)
    show_05_com_signal = Signal(int)
    show_06_com_signal = Signal(int)
    show_07_com_signal = Signal(int)
    show_08_com_signal = Signal(int)
    show_09_com_signal = Signal(int)
    show_0A_com_signal = Signal(int)
    show_0B_com_signal = Signal(int)
    show_0C_com_signal = Signal(int)
    show_0D_com_signal = Signal(int)
    show_0E_com_signal = Signal(int)
    show_0F_com_signal = Signal(int)

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

    # 接收数据
    def work(self):
        while SD.CAN_ON_OFF:
            ret = SD.COM_CONTROL.send(1, cst.READ_HOLDING_REGISTERS, 0x0100, 1)
            self.show_00_com_signal.emit(ret[1] << 8 | ret[0])
            m_delay(10)

            ret = SD.COM_CONTROL.send(1, cst.READ_HOLDING_REGISTERS, 0x0101, 1)
            self.show_01_com_signal.emit(ret[1] << 8 | ret[0])
            m_delay(10)

            ret = SD.COM_CONTROL.send(1, cst.READ_HOLDING_REGISTERS, 0x0102, 1)
            self.show_02_com_signal.emit(ret[1] << 8 | ret[0])
            m_delay(10)

            ret = SD.COM_CONTROL.send(1, cst.READ_HOLDING_REGISTERS, 0x0103, 1)
            self.show_03_com_signal.emit(ret[1] << 8 | ret[0])
            m_delay(10)

            ret = SD.COM_CONTROL.send(1, cst.READ_HOLDING_REGISTERS, 0x0104, 1)
            self.show_04_com_signal.emit(ret[1] << 8 | ret[0])
            m_delay(10)

            ret = SD.COM_CONTROL.send(1, cst.READ_HOLDING_REGISTERS, 0x0105, 1)
            self.show_05_com_signal.emit(ret[1] << 8 | ret[0])
            m_delay(10)

            ret = SD.COM_CONTROL.send(1, cst.READ_HOLDING_REGISTERS, 0x0106, 1)
            self.show_06_com_signal.emit(ret[1] << 8 | ret[0])
            m_delay(10)

            ret = SD.COM_CONTROL.send(1, cst.READ_HOLDING_REGISTERS, 0x0107, 1)
            self.show_07_com_signal.emit(ret[1] << 8 | ret[0])
            m_delay(10)

            ret = SD.COM_CONTROL.send(1, cst.READ_HOLDING_REGISTERS, 0x0108, 1)
            self.show_08_com_signal.emit(ret[1] << 8 | ret[0])
            m_delay(10)

            ret = SD.COM_CONTROL.send(1, cst.READ_HOLDING_REGISTERS, 0x0109, 1)
            self.show_09_com_signal.emit(ret[1] << 8 | ret[0])
            m_delay(10)

            ret = SD.COM_CONTROL.send(1, cst.READ_HOLDING_REGISTERS, 0x010A, 1)
            self.show_0A_com_signal.emit(ret[1] << 8 | ret[0])
            m_delay(10)

            ret = SD.COM_CONTROL.send(1, cst.READ_HOLDING_REGISTERS, 0x010B, 5)
            self.show_0B_com_signal.emit(ret[1] << 8 | ret[0])
            self.show_0C_com_signal.emit(ret[3] << 8 | ret[2])
            self.show_0D_com_signal.emit(ret[5] << 8 | ret[4])
            self.show_0E_com_signal.emit(ret[7] << 8 | ret[6])
            self.show_0F_com_signal.emit(ret[9] << 8 | ret[8])
            m_delay(10)

            # ret = SD.COM_CONTROL.send(1, cst.READ_HOLDING_REGISTERS, 0x010C, 1)
            # self.show_0C_com_signal.emit(ret[1] << 8 | ret[0])
            # m_delay(10)

            # ret = SD.COM_CONTROL.send(1, cst.READ_HOLDING_REGISTERS, 0x010D, 1)
            # self.show_0D_com_signal.emit(ret[1] << 8 | ret[0])
            # m_delay(10)

            # ret = SD.COM_CONTROL.send(1, cst.READ_HOLDING_REGISTERS, 0x010E, 1)
            # self.show_0E_com_signal.emit(ret[1] << 8 | ret[0])
            # m_delay(10)

            # ret = SD.COM_CONTROL.send(1, cst.READ_HOLDING_REGISTERS, 0x010F, 1)
            # self.show_0F_com_signal.emit(ret[1] << 8 | ret[0])
            # m_delay(10)
