***************************** Create a Shell with Submodels ***************************** In this chapter, we create an Asset Administration Shell for a motor, with two submodels: one which identifies the motor, and one which specifies it. We store the submodels in a Submodel Repository and the shell in an AAS Repository, and read them back. Connect to the Servers ====================== The client does not depend on any specific HTTP library, so it works with many different ones, such as `requests`_, `httpx`_ or `urllib3`_, and fits into the HTTP stack which your application already uses. .. _httpx: https://pypi.org/project/httpx/ .. _urllib3: https://pypi.org/project/urllib3/ Each call of the client builds an HTTP request, and parses the HTTP response which comes back, but the client never opens a connection itself. It leaves that to a *transport*, an object which takes a request, sends it over the wire, and gives back the response. The transport is thus where the network lives: the connection pool, the authentication, the certificates, the proxies, the timeouts and the retries. Any object with the method ``send`` of :py:class:`~aas_client3_2.http.Transport` will do. Here we use the ready-made transport based on `requests`_, which sends the requests through a :py:class:`requests.Session`; see :doc:`transports` for how to configure it, or write your own. You describe each server with a :py:class:`~aas_client3_2.http.Server`, which specifies the base URL of the server and the transport to reach it with. .. _requests: https://pypi.org/project/requests/ .. code-block:: python import requests import aas_core3_2.types as aas_types from aas_client3_2 import ( http as aas_client_http, requests_transport as aas_client_requests_transport, ) transport = aas_client_requests_transport.RequestsTransport( requests.Session() ) aas_repository_server = aas_client_http.Server( transport=transport, base_url="http://localhost:5004" ) submodel_repository_server = aas_client_http.Server( transport=transport, base_url="http://localhost:5005" ) One transport can serve many servers: here both servers share the same session, and with it its connections and settings. Create the Submodels ==================== The client sends and receives the types of :py:mod:`aas_core3_2.types`, so we create the submodels with their constructors, bottom-up: .. code-block:: python identification = aas_types.Submodel( id="https://example.com/ids/sm/getting-started/identification", id_short="Identification", submodel_elements=[ aas_types.MultiLanguageProperty( id_short="ManufacturerName", value=[ aas_types.LangStringTextType( language="en", text="Example Motors Ltd." ) ], ), aas_types.Property( id_short="SerialNumber", value_type=aas_types.DataTypeDefXSD.STRING, value="SN-0042", ), ], ) specification = aas_types.Submodel( id="https://example.com/ids/sm/getting-started/specification", id_short="Specification", submodel_elements=[ aas_types.Property( id_short="MaxRotationSpeed", value_type=aas_types.DataTypeDefXSD.INT, value="5000", ), aas_types.Property( id_short="Weight", value_type=aas_types.DataTypeDefXSD.DOUBLE, value="12.5", ), ], ) To keep the examples short, these two submodels are made up. In practice, model your submodels after the `submodel templates`_ of the Industrial Digital Twin Association, such as the Digital Nameplate or the Technical Data, so that other parties understand them: a template prescribes the semantic IDs, the ID-shorts, the structure and which elements are mandatory. .. _submodel templates: https://github.com/admin-shell-io/submodel-templates It is a good idea to verify the model before sending it, so that you find the mistakes on your side before the server does: .. code-block:: python import aas_core3_2.verification as aas_verification for submodel in (identification, specification): for error in aas_verification.verify(submodel): print(f"{error.path}: {error.cause}") The functions of the Submodel Repository API live in :py:mod:`aas_client3_2.submodel_repository`. Each function implements one operation of the specification, and is named after it: ``PostSubmodel`` becomes :py:func:`~aas_client3_2.submodel_repository.post_submodel`. The first argument is always the server: .. code-block:: python from aas_client3_2 import ( submodel_repository as aas_client_submodel_repository, ) aas_client_submodel_repository.post_submodel( submodel_repository_server, identification ) aas_client_submodel_repository.post_submodel( submodel_repository_server, specification ) Create the Shell ================ The shell does not contain its submodels, but refers to them by model references. A model reference to a submodel has a single key, which gives the identifier of the submodel: .. code-block:: python shell = aas_types.AssetAdministrationShell( id="https://example.com/ids/aas/getting-started/motor", id_short="Motor", asset_information=aas_types.AssetInformation( asset_kind=aas_types.AssetKind.INSTANCE, global_asset_id="https://example.com/ids/asset/motor-0042", ), submodels=[ aas_types.Reference( type=aas_types.ReferenceTypes.MODEL_REFERENCE, keys=[ aas_types.Key( type=aas_types.KeyTypes.SUBMODEL, value=identification.id, ) ], ), aas_types.Reference( type=aas_types.ReferenceTypes.MODEL_REFERENCE, keys=[ aas_types.Key( type=aas_types.KeyTypes.SUBMODEL, value=specification.id, ) ], ), ], ) The functions of the AAS Repository API live in :py:mod:`aas_client3_2.aas_repository`: .. code-block:: python from aas_client3_2 import ( aas_repository as aas_client_aas_repository, ) aas_client_aas_repository.post_asset_administration_shell( aas_repository_server, shell ) Read Them Back ============== We get the shell by its identifier, and follow its references to the submodels. The identifiers are often URLs or URNs, such as ``https://example.com/ids/aas/getting-started/motor``, whose slashes would split the identifier into several segments of the path. Hence, the specification requires that an identifier in a path is encoded in UTF-8 and then in base64url, the variant of base64 whose alphabet is safe in URLs. The client does this encoding for you, so you always pass the identifiers as they are: .. code-block:: python got = aas_client_aas_repository.get_asset_administration_shell_by_id( aas_repository_server, "https://example.com/ids/aas/getting-started/motor", ) print(got.id_short) # Prints: Motor for reference in got.over_submodels_or_empty(): a_submodel = aas_client_submodel_repository.get_submodel_by_id( submodel_repository_server, reference.keys[0].value ) print(a_submodel.id_short) # Prints: # Identification # Specification The results are again the types of :py:mod:`aas_core3_2.types`, so you can, for example, serialize them to JSON with :py:mod:`aas_core3_2.jsonization`: .. code-block:: python import json import aas_core3_2.jsonization as aas_jsonization print(json.dumps(aas_jsonization.to_jsonable(got), indent=2)) # Prints: # { # "idShort": "Motor", # "id": "https://example.com/ids/aas/getting-started/motor", # "assetInformation": { # "assetKind": "Instance", # "globalAssetId": "https://example.com/ids/asset/motor-0042" # }, # "submodels": [ # { # "type": "ModelReference", # "keys": [ # { # "type": "Submodel", # "value": "https://example.com/ids/sm/getting-started/identification" # } # ] # }, # { # "type": "ModelReference", # "keys": [ # { # "type": "Submodel", # "value": "https://example.com/ids/sm/getting-started/specification" # } # ] # } # ], # "modelType": "AssetAdministrationShell" # } We continue with the shell and its submodels in the next chapter, :doc:`change`.