submodel

aas_client3_2.submodel implements the Submodel API, the interface of a single submodel. Its functions take no identifier of the submodel: the server is the endpoint of the submodel, whose address you take from the descriptor in a registry (see submodel_registry).

Here, we register a submodel of the Submodel Repository in the Submodel Registry, look it up, and talk to it at its endpoint:

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 as aas_client_submodel,
    submodel_registry as aas_client_submodel_registry,
    submodel_repository as aas_client_submodel_repository,
)

transport = aas_client_requests_transport.RequestsTransport(
    requests.Session()
)
repository_server = aas_client_http.Server(
    transport=transport, base_url="http://localhost:5005"
)
registry_server = aas_client_http.Server(
    transport=transport, base_url="http://localhost:5008"
)

counter = aas_types.Submodel(
    id="https://example.com/ids/sm/getting-started/endpoint",
    id_short="Counter",
    submodel_elements=[
        aas_types.Property(
            id_short="Count",
            value_type=aas_types.DataTypeDefXSD.INT,
            value="0",
        ),
    ],
)
aas_client_submodel_repository.post_submodel(
    repository_server, counter
)

aas_client_submodel_registry.post_submodel_descriptor(
    registry_server,
    aas_types.SubmodelDescriptor(
        id=counter.id,
        endpoints=[
            aas_types.Endpoint(
                interface="SUBMODEL-3.2",
                protocol_information=aas_types.ProtocolInformation(
                    href="http://localhost:5005/submodels/"
                    + aas_client_http.encode_base64url(counter.id),
                ),
            )
        ],
    ),
)

Look up the endpoint which speaks the Submodel API:

descriptor = (
    aas_client_submodel_registry.get_submodel_descriptor_by_id(
        registry_server, counter.id
    )
)
href = None
for an_endpoint in descriptor.endpoints:
    if an_endpoint.interface == "SUBMODEL-3.2":
        href = an_endpoint.protocol_information.href

assert href is not None

endpoint = aas_client_http.Server(transport=transport, base_url=href)

And work with the submodel there:

print(aas_client_submodel.get_submodel(endpoint).id_short)
# Prints: Counter

aas_client_submodel.patch_submodel_element_by_path_value_only(
    endpoint,
    "Count",
    aas_types.Property(
        id_short="Count",
        value_type=aas_types.DataTypeDefXSD.INT,
        value="1",
    ),
)
print(
    aas_client_submodel.get_submodel_element_by_path_value_only(
        endpoint, "Count"
    )
)
# Prints: 1.0

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 descriptor and the submodel a second time:

aas_client_submodel_registry.delete_submodel_descriptor_by_id(
    registry_server, counter.id
)
aas_client_submodel_repository.delete_submodel_by_id(
    repository_server, counter.id
)