1"""Utility functions for working with the BaSyx SDK framework objects."""
7import basyx.aas.adapter.json
8from basyx.aas
import model
10_logger = logging.getLogger(__name__)
14 """Get all IDs from the submodels referenced in the given AAS.
16 :param shell: The Asset Administration Shell to extract submodel IDs from.
17 :return: A list of submodel IDs referenced in the AAS.
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.")
25 submodel_ids.append(submodel.key[0].value)
30def add_submodel_to_aas(aas: model.AssetAdministrationShell, submodel: model.Submodel) -> bool:
31 """Add a given Submodel correctly to a provided AssetAdministrationShell.
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
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.")
42 aas.submodel.add(model.ModelReference.from_referable(submodel))
47 """Remove a given Submodel correctly from a provided AssetAdministrationShell.
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
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.")
58 aas.submodel.remove(model.ModelReference.from_referable(submodel))
62def convert_to_object(content: dict) -> Any |
None:
63 """Convert a dictionary to a BaSyx SDK framework object.
65 :param content: dictionary to convert
66 :return: BaSyx SDK framework object or None
68 if not content
or len(content) == 0:
69 _logger.debug(
"Empty content provided for conversion to object.")
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}")
81def convert_to_dict(object: Any) -> dict |
None:
82 """Convert a BaSyx SDK framework object. to a dictionary.
84 :param object: BaSyx SDK framework object to convert
85 :return: dictionary representation of the object or None
88 _logger.debug(
"Empty object provided for conversion to dictionary.")
92 data_string = json.dumps(object, cls=basyx.aas.adapter.json.AASToJsonEncoder)
93 model_dict = json.loads(data_string)
95 except Exception
as e:
96 _logger.error(f
"Encoding error: {e}")
97 _logger.error(f
"In object: {object}")