*********************** ``submodel_repository`` *********************** :py:mod:`aas_client3_2.submodel_repository` implements the Submodel Repository API: it stores the submodels, and gives access to their elements, the attachments of their files, and the invocation of their operations. We introduced the basic operations in :doc:`../create` and :doc:`../change`, the forms of the results in :doc:`../modifiers`, and the attachments in :doc:`../files`. .. 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, submodel_repository as aas_client_submodel_repository, ) server = aas_client_http.Server( transport=aas_client_requests_transport.RequestsTransport( requests.Session() ), base_url="http://localhost:5005", ) sensor_a = aas_types.Submodel( id="https://example.com/ids/sm/getting-started/sensors/a", id_short="Sensor", submodel_elements=[ aas_types.Property( id_short="Temperature", value_type=aas_types.DataTypeDefXSD.DOUBLE, value="21.5", ), ], ) aas_client_submodel_repository.post_submodel(server, sensor_a) sensor_b = aas_types.Submodel( id="https://example.com/ids/sm/getting-started/sensors/b", id_short="Sensor", submodel_elements=[ aas_types.Property( id_short="Temperature", value_type=aas_types.DataTypeDefXSD.DOUBLE, value="85.0", ), ], ) aas_client_submodel_repository.post_submodel(server, sensor_b) sensor_c = aas_types.Submodel( id="https://example.com/ids/sm/getting-started/sensors/c", id_short="Sensor", submodel_elements=[ aas_types.Property( id_short="Temperature", value_type=aas_types.DataTypeDefXSD.DOUBLE, value="90.5", ), ], ) aas_client_submodel_repository.post_submodel(server, sensor_c) Query the Submodels =================== The AAS Query Language selects the submodels by conditions on their content. The queries are the types of :py:mod:`aas_core3_2.types`, such as :py:class:`~aas_core3_2.types.Query`: .. code-block:: python hot = aas_types.Query( condition=aas_types.AndExpression( and_operands=[ aas_types.EqualComparison( eq=( aas_types.FieldOperand(field="$sm#idShort"), aas_types.StringLiteral(str_val="Sensor"), ) ), aas_types.GreaterThanComparison( gt=( aas_types.FieldOperand( field="$sme.Temperature#value" ), aas_types.NumberLiteral(num_val=80), ) ), ] ) ) page = aas_client_submodel_repository.query_submodels(server, hot) for a_submodel in page.over_result_or_empty(): print(a_submodel.id) # Prints: # https://example.com/ids/sm/getting-started/sensors/b # https://example.com/ids/sm/getting-started/sensors/c A query which selects only the identifiers gives the pages of strings, :py:class:`~aas_client3_2.query_results.IdsResult`. Since the type of the result differs, the client has a separate function for it: .. code-block:: python ids_only = aas_types.Query( select=aas_types.QuerySelect.ID, condition=hot.condition ) page = aas_client_submodel_repository.query_submodel_ids( server, ids_only ) for identifier in page.over_result_or_empty(): print(identifier) # Prints: # https://example.com/ids/sm/getting-started/sensors/b # https://example.com/ids/sm/getting-started/sensors/c Both are paged, see :doc:`../pagination`. Invoke an Operation =================== An :py:class:`~aas_core3_2.types.Operation` element is carried out by the server, or by a service the server delegates to. Invoke it synchronously, and get the output variables in the result: .. skip-live-test: The operation needs a service which carries it out, which the servers of the live tests do not have for this submodel. .. code-block:: python result = aas_client_submodel_repository.invoke_operation( server, "https://example.com/ids/sm/getting-started/controller", "Calibrate", aas_types.OperationRequest( input_arguments=[ aas_types.OperationVariable( value=aas_types.Property( id_short="Offset", value_type=aas_types.DataTypeDefXSD.DOUBLE, value="0.5", ) ) ] ), ) print(result.execution_state) For long-running operations, :py:func:`~aas_client3_2.submodel_repository.invoke_operation_async` gives a handle instead, with which you poll the status by :py:func:`~aas_client3_2.submodel_repository.get_operation_async_status` and fetch the result by :py:func:`~aas_client3_2.submodel_repository.get_operation_async_result`. The functions ending in ``_value_only`` exchange the variables in the value-only serialization. Clean Up ======== Delete what we created in this chapter, so that you can run the code of this chapter again. The identifiers are unique, so the server would refuse to create the submodels a second time: .. code-block:: python aas_client_submodel_repository.delete_submodel_by_id( server, sensor_a.id ) aas_client_submodel_repository.delete_submodel_by_id( server, sensor_b.id ) aas_client_submodel_repository.delete_submodel_by_id( server, sensor_c.id )