# Path: conpot/utils/networking.py
# def chr_py3(x):
#     return bytearray((x,))


import logging
import binascii
import conpot.core as conpot_core
from crc16.crc16pure import crc16xmodem
from conpot.utils.networking import chr_py3
from . import kamstrup_constants

    def _parse_register_bytes(self):
        register_count = self.message_bytes[0]
        if len(self.message_bytes[1:]) * 2 < register_count:
            raise Exception("Invalid register count in register request")
        for count in range(register_count):
            register = (
                self.message_bytes[1 + count * 2] * 256
                + self.message_bytes[2 + count * 2]
            )
            self.registers.append(register)


# ############ RESPONSE MESSAGES ##############
class KamstrupResponseBase(KamstrupProtocolBase):
    def __init__(self, communication_address):
        super(KamstrupResponseBase, self).__init__(communication_address)

    def serialize(self, message):
        final_message = list()

        # prefix message
        final_message.append(kamstrup_constants.RESPONSE_MAGIC)
        final_message.append(self.communication_address)

        # add the original content
        for c in message:
            final_message.append(c)

        # generate and append checksum

# Path: conpot/utils/networking.py
# def chr_py3(x):
#     return bytearray((x,))


# Path: conpot/protocols/kamstrup_meter/messages.py
import logging
import binascii
import conpot.core as conpot_core
from crc16.crc16pure import crc16xmodem
from conpot.utils.networking import chr_py3
from . import kamstrup_constants


    def _parse_register_bytes(self):
        register_count = self.message_bytes[0]
        if len(self.message_bytes[1:]) * 2 < register_count:
            raise Exception("Invalid register count in register request")
        for count in range(register_count):
            register = (
                self.message_bytes[1 + count * 2] * 256
                + self.message_bytes[2 + count * 2]
            )
            self.registers.append(register)


# ############ RESPONSE MESSAGES ##############
class KamstrupResponseBase(KamstrupProtocolBase):
    def __init__(self, communication_address):
        super(KamstrupResponseBase, self).__init__(communication_address)

    def serialize(self, message):
        final_message = list()

        # prefix message
        final_message.append(kamstrup_constants.RESPONSE_MAGIC)
        final_message.append(self.communication_address)

        # add the original content
        for c in message:
            final_message.append(c)

        # generate and append checksum

        crc = crc16xmodem(b"".join([chr_py3(item) for item in final_message[1:]]))