AAS HTTP Client Documentation
Loading...
Searching...
No Matches
sdk_tools.py
Go to the documentation of this file.
1"""Utility functions for working with the BaSyx SDK framework objects."""
2
3import json
4import logging
5from typing import Any
6
7import basyx.aas.adapter.json
8from basyx.aas import model
9
10_logger = logging.getLogger(__name__)
11
12
13def get_submodel_ids(shell: model.AssetAdministrationShell) -> list[str]:
14 """Get all IDs from the submodels referenced in the given AAS.
15
16 :param shell: The Asset Administration Shell to extract submodel IDs from.
17 :return: A list of submodel IDs referenced in the AAS.
18 """
19 submodel_ids = []
20 for submodel in shell.submodel:
21 if len(submodel.key) < 1 or submodel.key[0].type != model.KeyTypes.SUBMODEL:
22 _logger.warning(f"Submodel reference {submodel} does not start with SUBMODEL key type.")
23 continue
24
25 submodel_ids.append(submodel.key[0].value)
26
27 return submodel_ids
28
29
30def add_submodel_to_aas(aas: model.AssetAdministrationShell, submodel: model.Submodel) -> bool:
31 """Add a given Submodel correctly to a provided AssetAdministrationShell.
32
33 :param aas: provided AssetAdministrationShell to which the Submodel should be added
34 :param submodel: given Submodel to add
35 :return: True if the Submodel was added, False if it was already referenced
36 """
37 existing_submodel_ids = get_submodel_ids(aas)
38 if submodel.id in existing_submodel_ids:
39 _logger.warning(f"Submodel with ID {submodel.id} is already referenced in the AAS. Skipping addition.")
40 return False
41
42 aas.submodel.add(model.ModelReference.from_referable(submodel))
43 return True
44
45
46def remove_submodel_from_aas(aas: model.AssetAdministrationShell, submodel: model.Submodel) -> bool:
47 """Remove a given Submodel correctly from a provided AssetAdministrationShell.
48
49 :param aas: provided AssetAdministrationShell from which the Submodel should be removed
50 :param submodel: given Submodel to remove
51 :return: True if the Submodel was removed, False if it was not referenced
52 """
53 existing_submodel_ids = get_submodel_ids(aas)
54 if submodel.id not in existing_submodel_ids:
55 _logger.warning(f"Submodel with ID {submodel.id} is not referenced in the AAS. Skipping removal.")
56 return False
57
58 aas.submodel.remove(model.ModelReference.from_referable(submodel))
59 return True
60
61
62def convert_to_object(content: dict) -> Any | None:
63 """Convert a dictionary to a BaSyx SDK framework object.
64
65 :param content: dictionary to convert
66 :return: BaSyx SDK framework object or None
67 """
68 if not content or len(content) == 0:
69 _logger.debug("Empty content provided for conversion to object.")
70 return None
71
72 try:
73 dict_string = json.dumps(content)
74 return json.loads(dict_string, cls=basyx.aas.adapter.json.json_deserialization.AASFromJsonDecoder)
75 except Exception as e:
76 _logger.error(f"Decoding error: {e}")
77 _logger.error(f"In JSON: {content}")
78 return None
79
80
81def convert_to_dict(object: Any) -> dict | None:
82 """Convert a BaSyx SDK framework object. to a dictionary.
83
84 :param object: BaSyx SDK framework object to convert
85 :return: dictionary representation of the object or None
86 """
87 if not object:
88 _logger.debug("Empty object provided for conversion to dictionary.")
89 return None
90
91 try:
92 data_string = json.dumps(object, cls=basyx.aas.adapter.json.AASToJsonEncoder)
93 model_dict = json.loads(data_string)
94 return model_dict
95 except Exception as e:
96 _logger.error(f"Encoding error: {e}")
97 _logger.error(f"In object: {object}")
98 return None
bool remove_submodel_from_aas(model.AssetAdministrationShell aas, model.Submodel submodel)
Remove a given Submodel correctly from a provided AssetAdministrationShell.
Definition sdk_tools.py:55
list[str] get_submodel_ids(model.AssetAdministrationShell shell)
Get all IDs from the submodels referenced in the given AAS.
Definition sdk_tools.py:21
bool add_submodel_to_aas(model.AssetAdministrationShell aas, model.Submodel submodel)
Add a given Submodel correctly to a provided AssetAdministrationShell.
Definition sdk_tools.py:39