# Path: conpot/protocols/kamstrup_management/kamstrup_management_server.py
# class KamstrupManagementServer(object):
#     def __init__(self, template, template_directory, args):
#         self.command_responder = CommandResponder()
#         self.banner = "\r\nWelcome...\r\nConnected to [{0}]\r\n"
#         logger.info("Kamstrup management protocol server initialized.")
#         self.server = None
# 
#     def handle(self, sock, address):
#         session = conpot_core.get_session(
#             "kamstrup_management_protocol",
#             address[0],
#             address[1],
#             sock.getsockname()[0],
#             sock.getsockname()[1],
#         )
#         logger.info(
#             "New Kamstrup connection from %s:%s. (%s)",
#             address[0],
#             address[1],
#             session.id,
#         )
#         session.add_event({"type": "NEW_CONNECTION"})
# 
#         try:
#             sock.send(
#                 str_to_bytes(
#                     self.banner.format(
#                         conpot_core.get_databus().get_value("mac_address")
#                     )
#                 )
#             )
# 
#             while True:
#                 data = sock.recv(1024)
#                 if not data:
#                     logger.info("Kamstrup client disconnected. (%s)", session.id)
#                     session.add_event({"type": "CONNECTION_LOST"})
#                     break
#                 request = data.decode()
#                 logdata = {"request": request}
#                 response = self.command_responder.respond(request)
#                 logdata["response"] = response
#                 logger.info(
#                     "Kamstrup management traffic from %s: %s (%s)",
#                     address[0],
#                     logdata,
#                     session.id,
#                 )
#                 session.add_event(logdata)
#                 gevent.sleep(0.25)  # TODO measure delay and/or RTT
# 
#                 if response is None:
#                     session.add_event({"type": "CONNECTION_LOST"})
#                     break
#                 # encode data before sending
#                 reply = str_to_bytes(response)
#                 sock.send(reply)
# 
#         except socket.timeout:
#             logger.debug("Socket timeout, remote: %s. (%s)", address[0], session.id)
#             session.add_event({"type": "CONNECTION_LOST"})
# 
#         sock.close()
# 
#     def start(self, host, port):
#         self.host = host
#         self.port = port
#         connection = (host, port)
#         self.server = StreamServer(connection, self.handle)
#         logger.info("Kamstrup management protocol server started on: %s", connection)
#         self.server.serve_forever()
# 
#     def stop(self):
#         self.server.stop()


from gevent import monkey
from gevent import socket
from conpot.protocols.kamstrup_management.kamstrup_management_server import (
    KamstrupManagementServer,
)
from conpot.tests.data.kamstrup_management_data import RESPONSES
from conpot.utils.greenlet import spawn_test_server, teardown_test_server
import unittest
# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.


monkey.patch_all()


def check_command_resp_help_message(
    packet_type, help_msg_command, packet_msg_command, kamstrup_management_server
):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect(("127.0.0.1", kamstrup_management_server.server.server_port))
    _ = s.recv(1024)  # receive the banner
    s.sendall(help_msg_command)  # test the help command
    help_data = s.recv(1024)
    help_response = help_data == RESPONSES["H"][packet_type]
    s.sendall(packet_msg_command)
    pkt_data = s.recv(1024)
    packet_resp = pkt_data == RESPONSES[packet_type]
    s.close()
    return help_response and packet_resp


class TestKamstrupManagementProtocol(unittest.TestCase):
    """
    All tests work in similar way. We send a get command check for a valid reply. We send in set command and
    expect things to change in the databus.
    """

    def setUp(self):
        self.kamstrup_management_server, self.server_greenlet = spawn_test_server(

# Path: conpot/protocols/kamstrup_management/kamstrup_management_server.py
# class KamstrupManagementServer(object):
#     def __init__(self, template, template_directory, args):
#         self.command_responder = CommandResponder()
#         self.banner = "\r\nWelcome...\r\nConnected to [{0}]\r\n"
#         logger.info("Kamstrup management protocol server initialized.")
#         self.server = None
# 
#     def handle(self, sock, address):
#         session = conpot_core.get_session(
#             "kamstrup_management_protocol",
#             address[0],
#             address[1],
#             sock.getsockname()[0],
#             sock.getsockname()[1],
#         )
#         logger.info(
#             "New Kamstrup connection from %s:%s. (%s)",
#             address[0],
#             address[1],
#             session.id,
#         )
#         session.add_event({"type": "NEW_CONNECTION"})
# 
#         try:
#             sock.send(
#                 str_to_bytes(
#                     self.banner.format(
#                         conpot_core.get_databus().get_value("mac_address")
#                     )
#                 )
#             )
# 
#             while True:
#                 data = sock.recv(1024)
#                 if not data:
#                     logger.info("Kamstrup client disconnected. (%s)", session.id)
#                     session.add_event({"type": "CONNECTION_LOST"})
#                     break
#                 request = data.decode()
#                 logdata = {"request": request}
#                 response = self.command_responder.respond(request)
#                 logdata["response"] = response
#                 logger.info(
#                     "Kamstrup management traffic from %s: %s (%s)",
#                     address[0],
#                     logdata,
#                     session.id,
#                 )
#                 session.add_event(logdata)
#                 gevent.sleep(0.25)  # TODO measure delay and/or RTT
# 
#                 if response is None:
#                     session.add_event({"type": "CONNECTION_LOST"})
#                     break
#                 # encode data before sending
#                 reply = str_to_bytes(response)
#                 sock.send(reply)
# 
#         except socket.timeout:
#             logger.debug("Socket timeout, remote: %s. (%s)", address[0], session.id)
#             session.add_event({"type": "CONNECTION_LOST"})
# 
#         sock.close()
# 
#     def start(self, host, port):
#         self.host = host
#         self.port = port
#         connection = (host, port)
#         self.server = StreamServer(connection, self.handle)
#         logger.info("Kamstrup management protocol server started on: %s", connection)
#         self.server.serve_forever()
# 
#     def stop(self):
#         self.server.stop()


# Path: conpot/tests/test_kamstrup_management.py
from gevent import monkey
from gevent import socket
from conpot.protocols.kamstrup_management.kamstrup_management_server import (
    KamstrupManagementServer,
)
from conpot.tests.data.kamstrup_management_data import RESPONSES
from conpot.utils.greenlet import spawn_test_server, teardown_test_server
import unittest

# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.


monkey.patch_all()


def check_command_resp_help_message(
    packet_type, help_msg_command, packet_msg_command, kamstrup_management_server
):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect(("127.0.0.1", kamstrup_management_server.server.server_port))
    _ = s.recv(1024)  # receive the banner
    s.sendall(help_msg_command)  # test the help command
    help_data = s.recv(1024)
    help_response = help_data == RESPONSES["H"][packet_type]
    s.sendall(packet_msg_command)
    pkt_data = s.recv(1024)
    packet_resp = pkt_data == RESPONSES[packet_type]
    s.close()
    return help_response and packet_resp


class TestKamstrupManagementProtocol(unittest.TestCase):
    """
    All tests work in similar way. We send a get command check for a valid reply. We send in set command and
    expect things to change in the databus.
    """

    def setUp(self):
        self.kamstrup_management_server, self.server_greenlet = spawn_test_server(

            KamstrupManagementServer, "kamstrup_382", "kamstrup_management"