# Path: mppsolar/protocols/daly.py
# class daly(AbstractProtocol):
#     """
#     DALY - Daly BMS protocol handler
#     """
# 
#     def __init__(self, *args, **kwargs) -> None:
#         super().__init__()
#         self._protocol_id = b"DALY"
#         self.module_address = bytes.fromhex("80")
#         self.COMMANDS = COMMANDS
#         self.STATUS_COMMANDS = [
#             "SOC",
#         ]
#         self.SETTINGS_COMMANDS = [
#             "",
#         ]
#         self.DEFAULT_COMMAND = "SOC"
# 
#     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:
#             return None
# 
#         # DALY
#         # startFlag = bytes.fromhex("A5")
#         commandID = bytes.fromhex(self._command_defn["command_code"])
#         dataLength = bytes.fromhex("08")
#         data = bytes.fromhex("00" * 8)
#         cmd = startFlag + self.module_address + commandID + dataLength + data
# 
#         checksum = f"{dalyChecksum(cmd):02X}"
#         cmd = cmd + bytes.fromhex(checksum) + b"\n"
#         log.debug(f"full command: {cmd}")
#         return cmd
# 
#     def is_multiframe(self, response) -> bool:
#         # startFlag = bytes.fromhex("A5")
#         if (
#             "response_length" in self._command_defn
#             and len(response) > self._command_defn["response_length"]
#         ):
#             return True
#         return False
# 
#     def check_response_valid(self, response) -> Tuple[bool, dict]:
#         """
#         DALY protocol - checksum is sum of bytes
#         """
#         if not response:
#             return False, {"ERROR": ["No response", ""]}
# 
#         log.debug(f"checking validity of {response}")
# 
#         # Check to see if the response is a multi frame response
#         if self.is_multiframe(response):
#             log.info("is multiframe response - assuming ok for now")
#             # TODO: fix check_response_valid for multiframe results
#             return True, {}
# 
#         _r = response
#         # print(f"bytes response {_r}")
#         data = _r[:-1]
#         checksum = _r[-1:][0]
#         if dalyChecksum(data) == checksum:
#             log.debug(f"DALY Checksum matches response '{response}' checksum:{checksum}")
#             return True, {}
#         else:
#             # print("VED Hex Checksum does not match")
#             return False, {"ERROR": [f"DALY checksum did not match for response {response}", ""]}
# 
#     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"] == "MULTIFRAME-POSITIONAL"
#         ):
#             # Have multiple frames of positional data
#             # Split into frames
#             frame_size = self._command_defn["response_length"]
#             frames = [response[i : i + frame_size] for i in range(0, len(response), frame_size)]
#             log.info(f"Multi frame response with {len(frames)} frames")
#             # Loop through each frame and process as per definition
#             for frame in frames:
#                 if len(frame) == 0:
#                     continue
#                 items = []
#                 for defn in self._command_defn["response"]:
#                     size = defn[1]
#                     item = frame[:size]
#                     items.append(item)
#                     frame = frame[size:]
#                 responses.append(items)
#             # print(responses)
#             return responses
# 
#         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:]
#             return responses
#         else:
#             return bytearray(response)


import unittest
from mppsolar.protocols.daly import daly as pi
            "acquistion": [0.0, "V"],
            "Current": [2.1, "A"],
            "SOC": [88.8, "%"],
        }
        result = protocol.decode(response, command)
        # print(result)
        self.assertEqual(result, expected)

    def test_daly_cellMinMaxVoltages(self):
        """test the decode of a cellMinMaxVoltages response"""
        protocol = pi()
        response = b"\xa5\x01\x91\x08\r\x00\x0f\x0c\xfe\x01\x03x\xe1"
        command = "cellMinMaxVoltages"
        # needed to initialise variables
        protocol.get_full_command(command)
        expected = {
            "raw_response": ["¥\x01\x91\x08\r\x00\x0f\x0cþ\x01\x03xá", ""],
            "_command": "cellMinMaxVoltages",
            "_command_description": "Cell Minimum and Maximum Voltages",
            "Maximum Cell Voltage": [3.328, "V"],
            "Maximum Cell Number": [15, ""],
            "Minimum Cell Voltage": [3.326, "V"],
            "Minimum Cell Number": [1, ""],
        }
        result = protocol.decode(response, command)
        # print(result)
        self.assertEqual(result, expected)

    def test_daly_status(self):
        """test the decode of a status response"""

# Path: mppsolar/protocols/daly.py
# class daly(AbstractProtocol):
#     """
#     DALY - Daly BMS protocol handler
#     """
# 
#     def __init__(self, *args, **kwargs) -> None:
#         super().__init__()
#         self._protocol_id = b"DALY"
#         self.module_address = bytes.fromhex("80")
#         self.COMMANDS = COMMANDS
#         self.STATUS_COMMANDS = [
#             "SOC",
#         ]
#         self.SETTINGS_COMMANDS = [
#             "",
#         ]
#         self.DEFAULT_COMMAND = "SOC"
# 
#     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:
#             return None
# 
#         # DALY
#         # startFlag = bytes.fromhex("A5")
#         commandID = bytes.fromhex(self._command_defn["command_code"])
#         dataLength = bytes.fromhex("08")
#         data = bytes.fromhex("00" * 8)
#         cmd = startFlag + self.module_address + commandID + dataLength + data
# 
#         checksum = f"{dalyChecksum(cmd):02X}"
#         cmd = cmd + bytes.fromhex(checksum) + b"\n"
#         log.debug(f"full command: {cmd}")
#         return cmd
# 
#     def is_multiframe(self, response) -> bool:
#         # startFlag = bytes.fromhex("A5")
#         if (
#             "response_length" in self._command_defn
#             and len(response) > self._command_defn["response_length"]
#         ):
#             return True
#         return False
# 
#     def check_response_valid(self, response) -> Tuple[bool, dict]:
#         """
#         DALY protocol - checksum is sum of bytes
#         """
#         if not response:
#             return False, {"ERROR": ["No response", ""]}
# 
#         log.debug(f"checking validity of {response}")
# 
#         # Check to see if the response is a multi frame response
#         if self.is_multiframe(response):
#             log.info("is multiframe response - assuming ok for now")
#             # TODO: fix check_response_valid for multiframe results
#             return True, {}
# 
#         _r = response
#         # print(f"bytes response {_r}")
#         data = _r[:-1]
#         checksum = _r[-1:][0]
#         if dalyChecksum(data) == checksum:
#             log.debug(f"DALY Checksum matches response '{response}' checksum:{checksum}")
#             return True, {}
#         else:
#             # print("VED Hex Checksum does not match")
#             return False, {"ERROR": [f"DALY checksum did not match for response {response}", ""]}
# 
#     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"] == "MULTIFRAME-POSITIONAL"
#         ):
#             # Have multiple frames of positional data
#             # Split into frames
#             frame_size = self._command_defn["response_length"]
#             frames = [response[i : i + frame_size] for i in range(0, len(response), frame_size)]
#             log.info(f"Multi frame response with {len(frames)} frames")
#             # Loop through each frame and process as per definition
#             for frame in frames:
#                 if len(frame) == 0:
#                     continue
#                 items = []
#                 for defn in self._command_defn["response"]:
#                     size = defn[1]
#                     item = frame[:size]
#                     items.append(item)
#                     frame = frame[size:]
#                 responses.append(items)
#             # print(responses)
#             return responses
# 
#         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:]
#             return responses
#         else:
#             return bytearray(response)


# Path: tests/test_protocol_daly.py
import unittest
from mppsolar.protocols.daly import daly as pi

            "acquistion": [0.0, "V"],
            "Current": [2.1, "A"],
            "SOC": [88.8, "%"],
        }
        result = protocol.decode(response, command)
        # print(result)
        self.assertEqual(result, expected)

    def test_daly_cellMinMaxVoltages(self):
        """test the decode of a cellMinMaxVoltages response"""
        protocol = pi()
        response = b"\xa5\x01\x91\x08\r\x00\x0f\x0c\xfe\x01\x03x\xe1"
        command = "cellMinMaxVoltages"
        # needed to initialise variables
        protocol.get_full_command(command)
        expected = {
            "raw_response": ["¥\x01\x91\x08\r\x00\x0f\x0cþ\x01\x03xá", ""],
            "_command": "cellMinMaxVoltages",
            "_command_description": "Cell Minimum and Maximum Voltages",
            "Maximum Cell Voltage": [3.328, "V"],
            "Maximum Cell Number": [15, ""],
            "Minimum Cell Voltage": [3.326, "V"],
            "Minimum Cell Number": [1, ""],
        }
        result = protocol.decode(response, command)
        # print(result)
        self.assertEqual(result, expected)

    def test_daly_status(self):
        """test the decode of a status response"""

        protocol = pi()