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 add_submodel_to_aas(aas: model.AssetAdministrationShell, submodel: model.Submodel) -> None:
14 """Add a given Submodel correctly to a provided AssetAdministrationShell.
15
16 :param aas: provided AssetAdministrationShell to which the Submodel should be added
17 :param submodel: given Submodel to add
18 """
19 aas.submodel.add(model.ModelReference.from_referable(submodel))
20
22def remove_submodel_from_aas(aas: model.AssetAdministrationShell, submodel: model.Submodel) -> None:
23 """Remove a given Submodel correctly from a provided AssetAdministrationShell.
24
25 :param aas: provided AssetAdministrationShell from which the Submodel should be removed
26 :param submodel: given Submodel to remove
27 """
28 aas.submodel.remove(model.ModelReference.from_referable(submodel))
29
31def convert_to_object(content: dict) -> Any | None:
32 """Convert a dictionary to a BaSyx SDK framework object.
33
34 :param content: dictionary to convert
35 :return: BaSyx SDK framework object or None
36 """
37 if not content or len(content) == 0:
38 _logger.debug("Empty content provided for conversion to object.")
39 return None
40
41 try:
42 dict_string = json.dumps(content)
43 return json.loads(dict_string, cls=basyx.aas.adapter.json.json_deserialization.AASFromJsonDecoder)
44 except Exception as e:
45 _logger.error(f"Decoding error: {e}")
46 _logger.error(f"In JSON: {content}")
47 return None
48
49
50def convert_to_dict(object: Any) -> dict | None:
51 """Convert a BaSyx SDK framework object. to a dictionary.
52
53 :param object: BaSyx SDK framework object to convert
54 :return: dictionary representation of the object or None
55 """
56 if not object:
57 _logger.debug("Empty object provided for conversion to dictionary.")
58 return None
59
60 try:
61 data_string = json.dumps(object, cls=basyx.aas.adapter.json.AASToJsonEncoder)
62 model_dict = json.loads(data_string)
63 return model_dict
64 except Exception as e:
65 _logger.error(f"Encoding error: {e}")
66 _logger.error(f"In object: {object}")
67 return None
None add_submodel_to_aas(model.AssetAdministrationShell aas, model.Submodel submodel)
Add a given Submodel correctly to a provided AssetAdministrationShell.
Definition sdk_tools.py:21
None remove_submodel_from_aas(model.AssetAdministrationShell aas, model.Submodel submodel)
Remove a given Submodel correctly from a provided AssetAdministrationShell.
Definition sdk_tools.py:30