Asset Connector Documentation
Loading...
Searching...
No Matches
set_value_payload.py
Go to the documentation of this file.
1"""Defines the SetValuePayload model for value payloads using Basyx and Pydantic."""
2
3import json
4from typing import Any
5
6from basyx.aas.model import Key, KeyTypes, ModelReference
7from pydantic import BaseModel, Field, PrivateAttr
8
9KEY_TYPE_MAPPING: dict[str, str] = {
10 "Submodel": "SUBMODEL",
11 "SubmodelElementCollection": "SUBMODEL_ELEMENT_COLLECTION",
12 "Property": "PROPERTY",
13}
14
15
16class SetValuePayload(BaseModel): # noqa: D101
17 """Defines the model class of the payload for the `set_value` (URL `/set-value`) endpoint
18 of this application using Pydantic.
19
20 The JSON payload must contain exactly the following fields:
21 - `Reference`
22 - `Value`
23
24 Inside the `Reference`-field, a properly serialized AAS reference according to the AAS JSON serialization
25 must be provided.
26 Will be parsed as `basyx.aas.model.ModelReference`.
27 The reference shall point to a SME in an AID submodel known to this application (refer to `add_or_update_config`).
28
29 Example:
30 ```
31 {
32 "Reference": {
33 "type": "ModelReference",
34 "keys": [
35 {
36 "type": "Submodel",
37 "value": "https://fluid40.de/ids/sm/4757_4856_8464_1441"
38 },
39 {
40 "type": "SubmodelElementCollection",
41 "value": "Interface_MQTT"
42 },
43 {
44 "type": "SubmodelElementCollection",
45 "value": "InteractionMetadata"
46 },
47 {
48 "type": "SubmodelElementCollection",
49 "value": "properties"
50 },
51 {
52 "type": "SubmodelElementCollection",
53 "value": "my_endpoint"
54 }
55 ]
56 },
57 "Value": "my-value"
58 }
59 ```
60 """
61
62 aid_ref_dict: dict = Field(..., alias="Reference")
63 value: Any = Field(..., alias="Value")
64 _aid_ref: ModelReference = PrivateAttr(default=None)
65
66 def __init__(self, **data: Any): # noqa: D107
67 super().__init__(**data)
69
70 def _build_model_reference(self) -> ModelReference:
71 if self.aid_ref_dict is None:
72 raise ValueError("AID Reference has not been initialized.")
73 key_list = [Key(type_=self._get_key_type(key["type"]), value=key["value"]) for key in self.aid_ref_dict["keys"]]
74 ref: ModelReference = ModelReference(key=tuple(key_list), type_=ModelReference)
76
77 def _get_key_type(self, key_str: str) -> KeyTypes:
78 key_type = KEY_TYPE_MAPPING.get(key_str)
79 if key_type is None:
80 raise ValueError(f"Unknown key type: {key_str}")
81 return KeyTypes[key_type]
Defines the model class of the payload for the set_value (URL /set-value) endpoint of this applicatio...
KeyTypes _get_key_type(self, str key_str)
ModelReference _build_model_reference(self)