Skip to content

Commit

Permalink
add probe module; add test connections
Browse files Browse the repository at this point in the history
Signed-off-by: Fabian Klemm <[email protected]>
  • Loading branch information
klemmpnx committed Oct 19, 2023
1 parent 6360c33 commit 3cfd3de
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
66 changes: 66 additions & 0 deletions everest-testing/src/everest/testing/core_utils/probe_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import asyncio
import logging
from queue import Queue
from typing import Any, Callable

from everest.framework import Module, RuntimeSession


class ProbeModule:

def __init__(self, session: RuntimeSession, connection_vars=None):
self._connection_vars = {} if not connection_vars else connection_vars
logging.info("ProbeModule init start")
m = Module('probe', session)
self._setup = m.say_hello()
self._mod = m

# subscribe to session events
logging.info(self._setup.connections)

self._message_queues = {connection: {var_name: Queue() for var_name in variable_names}
for connection, variable_names in self._connection_vars.items()
}
for connection, variable_names in self._connection_vars.items():
for variable_name in variable_names:
self._mod.subscribe_variable(self._setup.connections[connection][0], variable_name,
lambda message, _variable_name=variable_name,
_connection=connection: self._handle_subscribed_message(
queue=self._message_queues[_connection][_variable_name],
message=message,
variable_name=_variable_name,
connection=_connection))
self._ready_event = asyncio.Event()
m.init_done(self._ready)
logging.info("ProbeModule init done")

def _ready(self):
logging.info("ProbeModule ready")
self._ready_event.set()

# handlers for receiving publications
@staticmethod
def _handle_subscribed_message(queue, message, connection, variable_name):
queue.put(message)
logging.info(f"Got {connection} - {variable_name}: {message}!")

def get_queue(self, connection: str, variable_name: str):
return self._message_queues[connection][variable_name]

def call_command(self, interface_name: str, command_name: str, args: dict) -> Any:
interface = self._setup.connections[interface_name][0]
try:
return self._mod.call_command(interface, command_name, args)
except Exception as e:
logging.info(f"Exception in calling command {command_name}: {type(e)}: {e}")
raise e

def publish_variable(self, implementation_id: str, variable_name: str, value: dict | str):
self._mod.publish_variable(implementation_id, variable_name, value)

def implement_evse_manager_command(self, implementation_id: str, command_name: str,
handler: Callable[[dict], dict]):
self._mod.implement_command(implementation_id, command_name, handler)

async def wait_to_be_ready(self, timeout=3):
await asyncio.wait_for(self._ready_event.wait(), timeout)
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import yaml
from paho.mqtt import client as mqtt_client

from everest.testing.core_utils.everest_core import EverestCore
from everest.testing.core_utils.everest_core import EverestCore, Connections
from everest.testing.ocpp_utils.common import OCPPVersion
from everest.testing.ocpp_utils.controller.test_controller_interface import TestController
from everest.testing.ocpp_utils.libocpp_configuration_helper import \
Expand Down Expand Up @@ -48,7 +48,7 @@ def __init__(self, everest_core_path: Path, libocpp_path: Path, config_path: Pat
self.mqtt_external_prefix = ""
self.ocpp_module_id = ocpp_module_id

def start(self, central_system_port=None, standalone_module=None):
def start(self, central_system_port=None, standalone_module=None, test_connections: Connections = None):
logging.info(f"Central system port: {central_system_port}")
# modify ocpp config with given central system port and modify everest-core config as well
everest_config = yaml.safe_load(self.everest_core.everest_config_path.read_text())
Expand Down Expand Up @@ -94,7 +94,7 @@ def start(self, central_system_port=None, standalone_module=None):
logging.info(f"temp ocpp user config: {self.temp_ocpp_user_config_file.name}")
logging.info(f"temp ocpp certs path: {self.temp_ocpp_certs_dir.name}")

self.everest_core.start(standalone_module=standalone_module)
self.everest_core.start(standalone_module=standalone_module, test_connections=test_connections)

self.initialize_external_mqtt_client(self.everest_core.mqtt_external_prefix)
self.initialize_nodered_sil()
Expand Down

0 comments on commit 3cfd3de

Please sign in to comment.