# Path: conpot/protocols/kamstrup_meter/kamstrup_constants.py
# REQUEST_MAGIC = 0x80
# RESPONSE_MAGIC = 0x40
# EOT_MAGIC = 0x0D
# ESCAPE = 0x1B
# NEED_ESCAPE = [0x06, EOT_MAGIC, ESCAPE, RESPONSE_MAGIC, REQUEST_MAGIC]
# UNITS = {
#     0: "None",
#     1: "Wh",
#     2: "kWh",
#     3: "MWh",
#     4: "GWh",
#     5: "j",
#     6: "kj",
#     7: "Mj",
#     8: "Gj",
#     9: "Cal",
#     10: "kCal",
#     11: "MCal",
#     12: "GCal",
#     13: "varh",
#     14: "kvarh",
#     15: "Mvarh",
#     16: "Gvarh",
#     17: "VAh",
#     18: "kVAh",
#     19: "MVAh",
#     20: "GVAh",
#     21: "W",
#     22: "kW",
#     23: "MW",
#     24: "GW",
#     25: "var",
#     26: "kvar",
#     27: "MVar",
#     28: "Gvar",
#     29: "VA",
#     30: "kVA",
#     31: "MVA",
#     32: "GVA",
#     33: "V",
#     34: "A",
#     35: "kV",
#     36: "kA",
#     37: "C",
#     38: "K",
#     39: "I",
#     40: "m3",
#     41: "I_h",
#     42: "m3_h",
#     43: "m3xC",
#     44: "ton",
#     45: "ton_h",
#     46: "h",
#     47: "clock",  # hh:mm:ss
#     48: "date1",  # yy:mm:dd
#     49: "date2",  # yyyy:mm:dd
#     50: "date3",  # mm:dd
#     51: "number",
#     52: "bar",
#     53: "RTC",
#     54: "ASCII",
#     55: "m3x10",
#     56: "tonx10",
#     57: "GJx10",
#     58: "minutes",
#     59: "Bitfield",
#     60: "s",
#     61: "ms",
#     62: "days",
#     63: "RTC_Q",
#     64: "Datetime",
#     65: "imp_L",
#     66: "L_imp",
#     67: "Hz",
#     68: "Degree",
#     69: "Percent",
#     70: "USgal",
#     71: "USgal_min",
#     72: "KamDateTime",
#     73: "IPv4Address",
#     74: "IPv6Address",
# }
#     K382M = (1,)
#     K162M = (2,)
#     K351C = (3,)
#     OMNIA = (4,)
# class MeterTypes(Enum):


import logging
import socket
import json
import calendar
import time
import argparse
import crc16
import xml.dom.minidom
from datetime import datetime
from conpot.protocols.kamstrup_meter import kamstrup_constants
logger = logging.getLogger(__name__)

port_start_range = 1
port_end_range = 65535
default_comm_port = 63


class KamstrupRegisterCopier(object):
    def __init__(self, ip_address, port, comm_address):
        self._sock = None
        self.ip_address = ip_address
        self.port = port
        self.comm_address = comm_address
        self._connect()

    def _connect(self):
        logger.info("Connecting to {0}:{1}".format(self.ip_address, self.port))
        if self._sock is not None:
            self._sock.close()
            time.sleep(1)
        self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self._sock.settimeout(2)
        try:
            self._sock.connect((self.ip_address, self.port))
        except socket.error as socket_err:
            logger.exception("Error while connecting: {0}".format(str(socket_err)))
            self._connect()

    def get_register(self, register):
        message = [

# Path: conpot/protocols/kamstrup_meter/kamstrup_constants.py
# REQUEST_MAGIC = 0x80
# RESPONSE_MAGIC = 0x40
# EOT_MAGIC = 0x0D
# ESCAPE = 0x1B
# NEED_ESCAPE = [0x06, EOT_MAGIC, ESCAPE, RESPONSE_MAGIC, REQUEST_MAGIC]
# UNITS = {
#     0: "None",
#     1: "Wh",
#     2: "kWh",
#     3: "MWh",
#     4: "GWh",
#     5: "j",
#     6: "kj",
#     7: "Mj",
#     8: "Gj",
#     9: "Cal",
#     10: "kCal",
#     11: "MCal",
#     12: "GCal",
#     13: "varh",
#     14: "kvarh",
#     15: "Mvarh",
#     16: "Gvarh",
#     17: "VAh",
#     18: "kVAh",
#     19: "MVAh",
#     20: "GVAh",
#     21: "W",
#     22: "kW",
#     23: "MW",
#     24: "GW",
#     25: "var",
#     26: "kvar",
#     27: "MVar",
#     28: "Gvar",
#     29: "VA",
#     30: "kVA",
#     31: "MVA",
#     32: "GVA",
#     33: "V",
#     34: "A",
#     35: "kV",
#     36: "kA",
#     37: "C",
#     38: "K",
#     39: "I",
#     40: "m3",
#     41: "I_h",
#     42: "m3_h",
#     43: "m3xC",
#     44: "ton",
#     45: "ton_h",
#     46: "h",
#     47: "clock",  # hh:mm:ss
#     48: "date1",  # yy:mm:dd
#     49: "date2",  # yyyy:mm:dd
#     50: "date3",  # mm:dd
#     51: "number",
#     52: "bar",
#     53: "RTC",
#     54: "ASCII",
#     55: "m3x10",
#     56: "tonx10",
#     57: "GJx10",
#     58: "minutes",
#     59: "Bitfield",
#     60: "s",
#     61: "ms",
#     62: "days",
#     63: "RTC_Q",
#     64: "Datetime",
#     65: "imp_L",
#     66: "L_imp",
#     67: "Hz",
#     68: "Degree",
#     69: "Percent",
#     70: "USgal",
#     71: "USgal_min",
#     72: "KamDateTime",
#     73: "IPv4Address",
#     74: "IPv6Address",
# }
#     K382M = (1,)
#     K162M = (2,)
#     K351C = (3,)
#     OMNIA = (4,)
# class MeterTypes(Enum):


# Path: bin/kamstrup_prober.py
import logging
import socket
import json
import calendar
import time
import argparse
import crc16
import xml.dom.minidom
from datetime import datetime
from conpot.protocols.kamstrup_meter import kamstrup_constants

logger = logging.getLogger(__name__)

port_start_range = 1
port_end_range = 65535
default_comm_port = 63


class KamstrupRegisterCopier(object):
    def __init__(self, ip_address, port, comm_address):
        self._sock = None
        self.ip_address = ip_address
        self.port = port
        self.comm_address = comm_address
        self._connect()

    def _connect(self):
        logger.info("Connecting to {0}:{1}".format(self.ip_address, self.port))
        if self._sock is not None:
            self._sock.close()
            time.sleep(1)
        self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self._sock.settimeout(2)
        try:
            self._sock.connect((self.ip_address, self.port))
        except socket.error as socket_err:
            logger.exception("Error while connecting: {0}".format(str(socket_err)))
            self._connect()

    def get_register(self, register):
        message = [

            kamstrup_constants.REQUEST_MAGIC,