1"""Asset connector module for interfacing with assets via AID and MQTT."""
6from aas_standard_parser.reference_helpers
import construct_id_short_path_from_reference
7from basyx.aas.model
import ModelReference, SubmodelElementCollection
9from ..core.asset_connector
import IAssetConnector
10from .mqtt_client
import MqttClient
12logger = logging.getLogger(__name__)
16 """Class to connect to an asset using its AID."""
18 _mqtt_client: MqttClient =
None
20 def __init__(self, aid_id: str, interface_smc: SubmodelElementCollection):
21 logger.debug(f
"Initializing MQTTAssetConnector for AID '{aid_id}'")
25 super().
__init__(aid_id, interface_smc)
28 """Connect to the MQTT broker and subscribe to relevant topics."""
29 logger.info(f
"Connecting to MQTT Asset for AID '{self._aid_id}'")
33 except Exception
as e:
34 logger.error(f
"Failed to connect to MQTT topics: {topics}. Error: {e}")
35 raise ConnectionError(f
"Failed to connect to MQTT topics: {topics}. Error: {e}")
from e
38 """Connect to the MQTT topics using a MQTT connector.
40 :param mqtt_topics: A dictionary of MQTT topics to subscribe to.
43 logger.info(f
"Create MQTT client to '{self.base}'")
45 logger.info(f
"Subscribing to MQTT topics: {mqtt_topics}")
49 except ConnectionError
as ce:
50 logger.error(f
"Failed to connect to MQTT topics: {mqtt_topics}. Error: {ce}")
51 raise ConnectionError(f
"Failed to connect to MQTT topics: {mqtt_topics}. Error: {ce}")
from ce
55 async def get_value(self, model_reference: ModelReference) -> str |
None:
56 """Get the value for a specific model reference."""
59 raise ConnectionError(
"AssetConnector is not connected.")
62 raise ConnectionError(
"MQTT Client not properly initialized.")
64 property_idshort_path = construct_id_short_path_from_reference(model_reference)
70 raise KeyError(f
"Property {property_idshort_path} not found.")
74 if value_in_payload
is None:
80 value_in_payload = json.dumps(json.loads(value_in_payload)[k])
82 result = str(value_in_payload)
85 async def set_value(self, endpoint_reference: ModelReference, value: dict[str]):
86 """Set the value for a specific model reference."""
88 raise ConnectionError(
"AssetConnector is not connected.")
91 raise ConnectionError(
"MQTT Client not properly initialized.")
93 property_idshort_path = construct_id_short_path_from_reference(endpoint_reference)
96 topic_name = self._property_to_href_map[property_idshort_path].href
98 raise KeyError(f
"Property {property_idshort_path} not found.")
100 payload = json.dumps(value)
102 print(f
"Published to topic {topic_name} with payload {payload}")
Interface for asset connectors.
Class to connect to an asset using its AID.
str|None get_value(self, ModelReference model_reference)
Get the value for a specific model reference.
__init__(self, str aid_id, SubmodelElementCollection interface_smc)
Initialize the AssetConnector with AID ID and interface submodel collection.
bool _connect_to_mqtt_topics(self, list[str] mqtt_topics)
Connect to the MQTT topics using a MQTT connector.
set_value(self, ModelReference endpoint_reference, dict[str] value)
Set the value for a specific model reference.
connect(self)
Connect to the MQTT broker and subscribe to relevant topics.
Connector for managing connections to MQTT topics.