Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 3 additions & 24 deletions server/app/adapter/jsonization.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import logging
from typing import Callable, Dict, Optional, Set, Type
from typing import Callable, Dict, Type

from basyx.aas import model
from basyx.aas.adapter._generic import ASSET_KIND, ASSET_KIND_INVERSE, JSON_AAS_TOP_LEVEL_KEYS_TO_TYPES, PathOrIO
from basyx.aas.adapter._generic import ASSET_KIND, ASSET_KIND_INVERSE, JSON_AAS_TOP_LEVEL_KEYS_TO_TYPES
from basyx.aas.adapter.json import AASToJsonEncoder
from basyx.aas.adapter.json.json_deserialization import AASFromJsonDecoder, _get_ts, read_aas_json_file_into
from basyx.aas.adapter.json.json_deserialization import AASFromJsonDecoder, _get_ts

import app.model as server_model

Expand Down Expand Up @@ -207,27 +207,6 @@ class ServerStrictStrippedAASFromJsonDecoder(ServerStrictAASFromJsonDecoder, Ser
pass


def read_server_aas_json_file_into(
object_store: model.AbstractObjectStore,
file: PathOrIO,
replace_existing: bool = False,
ignore_existing: bool = False,
failsafe: bool = True,
stripped: bool = False,
decoder: Optional[Type[AASFromJsonDecoder]] = None,
) -> Set[model.Identifier]:
return read_aas_json_file_into(
object_store=object_store,
file=file,
replace_existing=replace_existing,
ignore_existing=ignore_existing,
failsafe=failsafe,
stripped=stripped,
decoder=decoder,
keys_to_types=JSON_SERVER_AAS_TOP_LEVEL_KEYS_TO_TYPES,
)


class ServerAASToJsonEncoder(AASToJsonEncoder):

@classmethod
Expand Down
44 changes: 24 additions & 20 deletions server/app/model/provider.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import json
from pathlib import Path
from typing import IO, Dict, Iterable, Iterator, Union

from basyx.aas import model
from basyx.aas.model import provider as sdk_provider

import app.adapter as adapter
from app.adapter import ServerAASFromJsonDecoder
from app.model import descriptor

PathOrIO = Union[Path, IO]
Expand Down Expand Up @@ -53,27 +54,30 @@ def __iter__(self) -> Iterator[_DESCRIPTOR_TYPE]:

def load_directory(directory: Union[Path, str]) -> DictDescriptorStore:
"""
Create a new :class:`~basyx.aas.model.provider.DictIdentifiableStore` and use it to load Asset Administration Shell
and Submodel files in ``AASX``, ``JSON`` and ``XML`` format from a given directory into memory. Additionally, load
all embedded supplementary files into a new :class:`~basyx.aas.adapter.aasx.DictSupplementaryFileContainer`.

:param directory: :class:`~pathlib.Path` or ``str`` pointing to the directory containing all Asset Administration
Shell and Submodel files to load
:return: Tuple consisting of a :class:`~basyx.aas.model.provider.DictIdentifiableStore` and a
:class:`~basyx.aas.adapter.aasx.DictSupplementaryFileContainer` containing all loaded data
"""

dict_descriptor_store: DictDescriptorStore = DictDescriptorStore()
Load AAS/Submodel descriptor JSON files from a directory into a :class:`DictDescriptorStore`.

:param directory: Path to the directory containing JSON descriptor files
:return: Populated :class:`DictDescriptorStore`
"""
store = DictDescriptorStore()
directory = Path(directory)

for file in directory.iterdir():
if not file.is_file():
if not file.is_file() or file.suffix.lower() != ".json":
continue

suffix = file.suffix.lower()
if suffix == ".json":
with open(file) as f:
adapter.read_server_aas_json_file_into(dict_descriptor_store, f)
Comment thread
zrgt marked this conversation as resolved.

return dict_descriptor_store
with open(file) as f:
data = json.load(f, cls=ServerAASFromJsonDecoder)
for item in data.get("assetAdministrationShellDescriptors", []):
if isinstance(item, descriptor.AssetAdministrationShellDescriptor):
try:
store.add(item)
except KeyError:
pass
for item in data.get("submodelDescriptors", []):
if isinstance(item, descriptor.SubmodelDescriptor):
try:
store.add(item)
except KeyError:
pass

return store
Loading