Asset Connector Documentation
Loading...
Searching...
No Matches
http_asset_connector.py
Go to the documentation of this file.
1"""Asset connector module for interfacing with assets via AID and http."""
2
3import json
4
5import requests
6from aas_standard_parser.aid_parser import HttpProtocolBinding
7from aas_standard_parser.reference_helpers import construct_id_short_path_from_reference
8from basyx.aas.model import (
9 ModelReference,
10 SubmodelElementCollection,
11)
12
13from python_connector.core.asset_connector import IAssetConnector
14
15
17 """Class to connect to an asset using its AID."""
18
19 property_cache: list
20
21 def __init__(self, aid_id: str, interface_smc: SubmodelElementCollection):
22 """Initialize the HTTP asset connector."""
23 super().__init__(aid_id, interface_smc)
26
27 async def get_value(self, model_reference: ModelReference) -> str | None:
28 """Get the value for a specific model reference."""
29 property_idshort_path = construct_id_short_path_from_reference(model_reference)
30 url_resource = self._parsed_properties[property_idshort_path].href
31 keys = self._parsed_properties[property_idshort_path].keys
32 protocol_binding: HttpProtocolBinding = self._parsed_properties[property_idshort_path].protocol_binding
33 http_method = protocol_binding.method_name
34 http_headers = protocol_binding.headers
35
36 match http_method:
37 case "GET":
38 response = requests.get(self.base + url_resource, headers=http_headers)
39 case method:
40 raise ValueError(f"http method {method} not implemented.")
41 response.raise_for_status()
42 value_in_payload = response.text
43
44 if value_in_payload is None:
45 return None
46
47 # using the keys of the potentially nested properties, dive into the complex JSON object (MQTT payload)
48 # to retrieve the requested value
49 for k in keys:
50 value_in_payload = json.dumps(json.loads(value_in_payload)[k])
51
52 return str(value_in_payload)
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 HTTP asset connector.