# Path: conpot/utils/networking.py
# def chr_py3(x):
#     return bytearray((x,))
# 
# Path: conpot/protocols/kamstrup_meter/messages.py
# class KamstrupRequestGetRegisters(KamstrupRequestBase):
#     command_byte = 0x10
# 
#     def __init__(self, communication_address, command_byte, message_bytes):
#         assert command_byte is command_byte
#         super(KamstrupRequestGetRegisters, self).__init__(
#             communication_address,
#             KamstrupRequestGetRegisters.command_byte,
#             message_bytes,
#         )
#         self.registers = []
#         self._parse_register_bytes()
#         logger.debug(
#             "Kamstrup request for registers: %s", str(self.registers).strip("[]")
#         )
# 
#     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)
# 
# class KamstrupRequestUnknown(KamstrupRequestBase):
#     def __init__(self, communication_address, command_byte, message_bytes):
#         super(KamstrupRequestUnknown, self).__init__(
#             communication_address, command_byte, message_bytes
#         )
#         logger.warning("Unknown Kamstrup request: %s", self)


import logging
from crc16.crc16pure import crc16xmodem
from conpot.utils.networking import chr_py3
from . import kamstrup_constants
from .messages import KamstrupRequestGetRegisters, KamstrupRequestUnknown
# Copyright (C) 2014  Johnny Vestergaard <jkv@unixcluster.dk>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.




logger = logging.getLogger(__name__)


class KamstrupRequestParser(object):
    def __init__(self):
        self.bytes = list()
        self.parsing = False
        self.data_escaped = False
        self.done = False
        self.request_map = {

# Path: conpot/utils/networking.py
# def chr_py3(x):
#     return bytearray((x,))
# 
# Path: conpot/protocols/kamstrup_meter/messages.py
# class KamstrupRequestGetRegisters(KamstrupRequestBase):
#     command_byte = 0x10
# 
#     def __init__(self, communication_address, command_byte, message_bytes):
#         assert command_byte is command_byte
#         super(KamstrupRequestGetRegisters, self).__init__(
#             communication_address,
#             KamstrupRequestGetRegisters.command_byte,
#             message_bytes,
#         )
#         self.registers = []
#         self._parse_register_bytes()
#         logger.debug(
#             "Kamstrup request for registers: %s", str(self.registers).strip("[]")
#         )
# 
#     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)
# 
# class KamstrupRequestUnknown(KamstrupRequestBase):
#     def __init__(self, communication_address, command_byte, message_bytes):
#         super(KamstrupRequestUnknown, self).__init__(
#             communication_address, command_byte, message_bytes
#         )
#         logger.warning("Unknown Kamstrup request: %s", self)


# Path: conpot/protocols/kamstrup_meter/request_parser.py
import logging
from crc16.crc16pure import crc16xmodem
from conpot.utils.networking import chr_py3
from . import kamstrup_constants
from .messages import KamstrupRequestGetRegisters, KamstrupRequestUnknown

# Copyright (C) 2014  Johnny Vestergaard <jkv@unixcluster.dk>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.




logger = logging.getLogger(__name__)


class KamstrupRequestParser(object):
    def __init__(self):
        self.bytes = list()
        self.parsing = False
        self.data_escaped = False
        self.done = False
        self.request_map = {

            KamstrupRequestGetRegisters.command_byte: KamstrupRequestGetRegisters