remotivelabs.topology.ecu_mock

ECU mock runner for RemotiveTopology.

remotivelabs.topology.ecu_mock.ECUMock creates a BehavioralModel with no actual behavior by starting a restbus that sends all frames each ECU is listed as sender of. Useful for putting ECUs on the network without writing any behavioral logic.

Run directly from the command line via the mock CLI:

python -m remotivelabs.topology.cli.mock -n NAMESPACE=ECU1,ECU2 -u http://127.0.0.1:50051
 1"""
 2# ECU mock runner for RemotiveTopology.
 3
 4<!-- start-include -->
 5`remotivelabs.topology.ecu_mock.ECUMock` creates a BehavioralModel with no actual behavior by starting a restbus that
 6sends all frames each ECU is listed as sender of. Useful for putting ECUs on the network without
 7writing any behavioral logic.
 8
 9Run directly from the command line via the `mock` CLI:
10
11```bash
12python -m remotivelabs.topology.cli.mock -n NAMESPACE=ECU1,ECU2 -u http://127.0.0.1:50051
13```
14<!-- end-include -->
15"""
16
17from __future__ import annotations
18
19import asyncio
20import logging
21from contextlib import AsyncExitStack
22from typing import Any
23
24from remotivelabs.broker import BrokerClient, filters
25
26from remotivelabs.topology.behavioral_model import BehavioralModel
27from remotivelabs.topology.namespaces.factory import make_namespace
28from remotivelabs.topology.namespaces.generic import RestbusConfig
29from remotivelabs.topology.namespaces.namespace import Namespace
30
31_logger = logging.getLogger(__name__)
32
33
34class ECUMock:
35    """
36    Creates a BehavioralModel with no actual behavior which is sending all frames each ECU is listed as sender of via a restbus.
37    Useful when you need ECUs on the network without writing behavioral logic.
38    """
39
40    def __init__(self, namespaces: dict[str, list[str]], broker_url: str, delay_multiplier: float):
41        """
42        Create a new ECU mock capable of simulating multiple ECUs with different namespaces.
43
44        Args:
45            namespaces: Mapping from ECU name to the list of broker namespaces it participates in.
46            broker_url: URL of the RemotiveBroker gRPC endpoint (e.g. ``http://127.0.0.1:50051``).
47            delay_multiplier: Scaling factor applied to all restbus frame intervals.
48                Use ``1.0`` for real-time, values below ``1.0`` to speed up, above to slow down.
49        """
50        self._namespaces = namespaces
51        self._broker_url = broker_url
52        self._delay_multiplier = delay_multiplier
53
54    async def run(self) -> None:
55        _logger.info("Starting ECUMock at %s", self._broker_url)
56
57        async with BrokerClient(self._broker_url) as broker_client:
58            models = await self._build_models(broker_client)
59            async with AsyncExitStack() as stack:
60                for model in models:
61                    await stack.enter_async_context(model)
62                _logger.info("ECUMock running")
63                await asyncio.gather(*(model.run_forever() for model in models))
64
65    async def _build_models(self, broker_client: BrokerClient) -> list[BehavioralModel]:
66        """Build one BehavioralModel per ECU."""
67        models: list[BehavioralModel] = []
68        for ecu, namespace_names in self._namespaces.items():
69            namespaces: list[Namespace[Any, Any]] = []
70            for name in namespace_names:
71                namespace = await self._make_namespace(broker_client, ecu, name)
72                if namespace is None:
73                    _logger.warning("Skipping namespace %s for ECU %s", name, ecu)
74                    continue
75                namespaces.append(namespace)
76            models.append(BehavioralModel(ecu, namespaces=namespaces, broker_client=broker_client))
77        return models
78
79    async def _make_namespace(self, broker_client: BrokerClient, ecu: str, name: str) -> Namespace[Any, Any] | None:
80        """Create a protocol-specific Namespace for the given name.
81
82        A mock ECU restbuses every frame it sends (CAN) and seeds the LIN cache from its own frames,
83        so the restbus/cache configs are keyed on the ECU's sender identity.
84        """
85        return await make_namespace(
86            broker_client,
87            name,
88            restbus_configs=[RestbusConfig([filters.SenderFilter(ecu_name=ecu)], delay_multiplier=self._delay_multiplier)],
89            cache_config=ecu,
90        )
class ECUMock:
35class ECUMock:
36    """
37    Creates a BehavioralModel with no actual behavior which is sending all frames each ECU is listed as sender of via a restbus.
38    Useful when you need ECUs on the network without writing behavioral logic.
39    """
40
41    def __init__(self, namespaces: dict[str, list[str]], broker_url: str, delay_multiplier: float):
42        """
43        Create a new ECU mock capable of simulating multiple ECUs with different namespaces.
44
45        Args:
46            namespaces: Mapping from ECU name to the list of broker namespaces it participates in.
47            broker_url: URL of the RemotiveBroker gRPC endpoint (e.g. ``http://127.0.0.1:50051``).
48            delay_multiplier: Scaling factor applied to all restbus frame intervals.
49                Use ``1.0`` for real-time, values below ``1.0`` to speed up, above to slow down.
50        """
51        self._namespaces = namespaces
52        self._broker_url = broker_url
53        self._delay_multiplier = delay_multiplier
54
55    async def run(self) -> None:
56        _logger.info("Starting ECUMock at %s", self._broker_url)
57
58        async with BrokerClient(self._broker_url) as broker_client:
59            models = await self._build_models(broker_client)
60            async with AsyncExitStack() as stack:
61                for model in models:
62                    await stack.enter_async_context(model)
63                _logger.info("ECUMock running")
64                await asyncio.gather(*(model.run_forever() for model in models))
65
66    async def _build_models(self, broker_client: BrokerClient) -> list[BehavioralModel]:
67        """Build one BehavioralModel per ECU."""
68        models: list[BehavioralModel] = []
69        for ecu, namespace_names in self._namespaces.items():
70            namespaces: list[Namespace[Any, Any]] = []
71            for name in namespace_names:
72                namespace = await self._make_namespace(broker_client, ecu, name)
73                if namespace is None:
74                    _logger.warning("Skipping namespace %s for ECU %s", name, ecu)
75                    continue
76                namespaces.append(namespace)
77            models.append(BehavioralModel(ecu, namespaces=namespaces, broker_client=broker_client))
78        return models
79
80    async def _make_namespace(self, broker_client: BrokerClient, ecu: str, name: str) -> Namespace[Any, Any] | None:
81        """Create a protocol-specific Namespace for the given name.
82
83        A mock ECU restbuses every frame it sends (CAN) and seeds the LIN cache from its own frames,
84        so the restbus/cache configs are keyed on the ECU's sender identity.
85        """
86        return await make_namespace(
87            broker_client,
88            name,
89            restbus_configs=[RestbusConfig([filters.SenderFilter(ecu_name=ecu)], delay_multiplier=self._delay_multiplier)],
90            cache_config=ecu,
91        )

Creates a BehavioralModel with no actual behavior which is sending all frames each ECU is listed as sender of via a restbus. Useful when you need ECUs on the network without writing behavioral logic.

ECUMock( namespaces: dict[str, list[str]], broker_url: str, delay_multiplier: float)
41    def __init__(self, namespaces: dict[str, list[str]], broker_url: str, delay_multiplier: float):
42        """
43        Create a new ECU mock capable of simulating multiple ECUs with different namespaces.
44
45        Args:
46            namespaces: Mapping from ECU name to the list of broker namespaces it participates in.
47            broker_url: URL of the RemotiveBroker gRPC endpoint (e.g. ``http://127.0.0.1:50051``).
48            delay_multiplier: Scaling factor applied to all restbus frame intervals.
49                Use ``1.0`` for real-time, values below ``1.0`` to speed up, above to slow down.
50        """
51        self._namespaces = namespaces
52        self._broker_url = broker_url
53        self._delay_multiplier = delay_multiplier

Create a new ECU mock capable of simulating multiple ECUs with different namespaces.

Arguments:
  • namespaces: Mapping from ECU name to the list of broker namespaces it participates in.
  • broker_url: URL of the RemotiveBroker gRPC endpoint (e.g. http://127.0.0.1:50051).
  • delay_multiplier: Scaling factor applied to all restbus frame intervals. Use 1.0 for real-time, values below 1.0 to speed up, above to slow down.
async def run(self) -> None:
55    async def run(self) -> None:
56        _logger.info("Starting ECUMock at %s", self._broker_url)
57
58        async with BrokerClient(self._broker_url) as broker_client:
59            models = await self._build_models(broker_client)
60            async with AsyncExitStack() as stack:
61                for model in models:
62                    await stack.enter_async_context(model)
63                _logger.info("ECUMock running")
64                await asyncio.gather(*(model.run_forever() for model in models))