# Path: mppsolar/protocols/jk232.py
# class jk232(AbstractProtocol):
#     def __init__(self, *args, **kwargs) -> None:
#         super().__init__()
#         self._protocol_id = b"JK232"
#         self.COMMANDS = COMMANDS
#         self.STATUS_COMMANDS = [
#             "getBalancerData",
#         ]
#         self.SETTINGS_COMMANDS = [
#             "",
#         ]
#         self.DEFAULT_COMMAND = "getBalancerData"
# 
#     def get_full_command(self, command) -> bytes:
#         """
#         Override the default get_full_command as its different
#         """
#         log.info(f"Using protocol {self._protocol_id} with {len(self.COMMANDS)} commands")
#         # These need to be set to allow other functions to work`
#         self._command = command
#         self._command_defn = self.get_command_defn(command)
#         # End of required variables setting
#         if self._command_defn is None:
#             # Maybe return a default here?
#             return None
#         if "command_code" in self._command_defn:
# 
#             # Read basic information and status
#             # DD A5 03 00 FF FD 77
#             # full command is 7 bytes long
#             cmd = bytearray(7)
# 
#             # start bit  0xDD
#             cmd[0] = 0xDD
#             log.debug(f"cmd with start bit: {cmd}")
# 
#             # status 0xA5 means read, status 0x5A means write.
#             if self._command_defn["type"] == "SETTER":
#                 cmd[1] = 0x5A
#             else:
#                 cmd[1] = 0xA5
#             # command code 0x03
#             command_code = int(self._command_defn["command_code"], 16)
#             # Data length: 1 byte, indicating the effective length of the data carried in the frame.
#             # Data content: N bytes, the content carried by the frame data, when the data length is 0, there is no such part.
#             data = ""
#             # TODO: data stuff here
#             data_len = len(data)
#             if data_len == 0:
#                 crc_high, crc_low = crc([command_code, data_len])
#                 cmd[2] = command_code
#                 cmd[3] = data_len
#                 cmd[4] = crc_high
#                 cmd[5] = crc_low
#                 cmd[6] = 0x77
# 
#             log.debug(f"cmd with crc: {cmd}")
#             return cmd
# 
#     def get_responses(self, response):
#         """
#         Override the default get_responses as its different
#         """
#         responses = []
#         # remove \n
#         # response = response.replace(b"\n", b"")
# 
#         if self._command_defn is not None and self._command_defn["response_type"] == "POSITIONAL":
#             # Have a POSITIONAL type response, so need to break it up...
#             # example defn :
#             # "response": [
#             #   ["discard", 1, "start flag", ""],
#             #   ["discard", 1, "module address", ""],
#             #   ["discard", 1, "command id", ""],
#             #   ["discard", 1, "data length", ""],
#             # ]
#             # example response data b"\xa5\x01\x90\x08\x02\x10\x00\x00uo\x03\xbc\xf3",
#             for defn in self._command_defn["response"]:
#                 size = defn[1]
#                 item = response[:size]
#                 responses.append(item)
#                 response = response[size:]
#             if response:
#                 responses.append(response)
#             log.debug(f"get_responses: responses {responses}")
#             return responses
#         else:
#             return bytearray(response)


import unittest
from mppsolar.protocols.jk232 import jk232 as pi


class test_jk232_decode(unittest.TestCase):
    maxDiff = None

    def test_getBalancerData(self):
        """test the decode of a getBalancerData response"""

# Path: mppsolar/protocols/jk232.py
# class jk232(AbstractProtocol):
#     def __init__(self, *args, **kwargs) -> None:
#         super().__init__()
#         self._protocol_id = b"JK232"
#         self.COMMANDS = COMMANDS
#         self.STATUS_COMMANDS = [
#             "getBalancerData",
#         ]
#         self.SETTINGS_COMMANDS = [
#             "",
#         ]
#         self.DEFAULT_COMMAND = "getBalancerData"
# 
#     def get_full_command(self, command) -> bytes:
#         """
#         Override the default get_full_command as its different
#         """
#         log.info(f"Using protocol {self._protocol_id} with {len(self.COMMANDS)} commands")
#         # These need to be set to allow other functions to work`
#         self._command = command
#         self._command_defn = self.get_command_defn(command)
#         # End of required variables setting
#         if self._command_defn is None:
#             # Maybe return a default here?
#             return None
#         if "command_code" in self._command_defn:
# 
#             # Read basic information and status
#             # DD A5 03 00 FF FD 77
#             # full command is 7 bytes long
#             cmd = bytearray(7)
# 
#             # start bit  0xDD
#             cmd[0] = 0xDD
#             log.debug(f"cmd with start bit: {cmd}")
# 
#             # status 0xA5 means read, status 0x5A means write.
#             if self._command_defn["type"] == "SETTER":
#                 cmd[1] = 0x5A
#             else:
#                 cmd[1] = 0xA5
#             # command code 0x03
#             command_code = int(self._command_defn["command_code"], 16)
#             # Data length: 1 byte, indicating the effective length of the data carried in the frame.
#             # Data content: N bytes, the content carried by the frame data, when the data length is 0, there is no such part.
#             data = ""
#             # TODO: data stuff here
#             data_len = len(data)
#             if data_len == 0:
#                 crc_high, crc_low = crc([command_code, data_len])
#                 cmd[2] = command_code
#                 cmd[3] = data_len
#                 cmd[4] = crc_high
#                 cmd[5] = crc_low
#                 cmd[6] = 0x77
# 
#             log.debug(f"cmd with crc: {cmd}")
#             return cmd
# 
#     def get_responses(self, response):
#         """
#         Override the default get_responses as its different
#         """
#         responses = []
#         # remove \n
#         # response = response.replace(b"\n", b"")
# 
#         if self._command_defn is not None and self._command_defn["response_type"] == "POSITIONAL":
#             # Have a POSITIONAL type response, so need to break it up...
#             # example defn :
#             # "response": [
#             #   ["discard", 1, "start flag", ""],
#             #   ["discard", 1, "module address", ""],
#             #   ["discard", 1, "command id", ""],
#             #   ["discard", 1, "data length", ""],
#             # ]
#             # example response data b"\xa5\x01\x90\x08\x02\x10\x00\x00uo\x03\xbc\xf3",
#             for defn in self._command_defn["response"]:
#                 size = defn[1]
#                 item = response[:size]
#                 responses.append(item)
#                 response = response[size:]
#             if response:
#                 responses.append(response)
#             log.debug(f"get_responses: responses {responses}")
#             return responses
#         else:
#             return bytearray(response)


# Path: tests/test_protocol_jk232.py
import unittest
from mppsolar.protocols.jk232 import jk232 as pi



class test_jk232_decode(unittest.TestCase):
    maxDiff = None

    def test_getBalancerData(self):
        """test the decode of a getBalancerData response"""

        protocol = pi()