forked from eclipse-basyx/basyx-python-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
api/server: Add aas registry server and submodel registry server to s…
…erver. sdk/basyx/object_store: add filter function
- Loading branch information
Showing
7 changed files
with
285 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from typing import Any | ||
|
||
from aas_core3.types import Identifiable | ||
from fastapi import APIRouter, Request, HTTPException | ||
|
||
from basyx import ObjectStore | ||
from services.aas_registry_server_service import AasRegistryServerService | ||
|
||
|
||
class AasRegistryRouter: | ||
def __init__(self, global_obj_store: ObjectStore[Identifiable]): | ||
self.router = APIRouter() | ||
self.obj_store = global_obj_store | ||
self.service = AasRegistryServerService(global_obj_store) | ||
self._setup_routes() | ||
|
||
def _setup_routes(self): | ||
@self.router.get("/") | ||
async def GetAllAssetAdministrationShellDescriptors() -> Any: | ||
return self.service.GetAllAssetAdministrationShellDescriptors() | ||
|
||
@self.router.get("/{aas_descriptor_id}") | ||
async def GetAssetAdministrationShellDescriptorById(aas_descriptor_id: str) -> Any: | ||
return self.service.GetAssetAdministrationShellDescriptorById(aas_descriptor_id) | ||
|
||
@self.router.post("/") | ||
async def PostAssetAdministrationShellDescriptor(request: Request) -> Any: | ||
body = await request.json() | ||
return self.service.PostAssetAdministrationShellDescriptor(body) | ||
|
||
@self.router.put("/") | ||
async def PutAssetAdministrationShellDescriptorById(request: Request) -> Any: | ||
body = await request.json() | ||
return self.service.PutAssetAdministrationShellDescriptorById(body) | ||
|
||
@self.router.delete("/{aas_descriptor_id}") | ||
async def DeleteAssetAdministrationShellDescriptorById(aas_descriptor_id: str) -> Any: | ||
return self.service.DeleteAssetAdministrationShellDescriptorById(aas_descriptor_id) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from typing import Any | ||
|
||
from aas_core3.types import Identifiable | ||
from fastapi import APIRouter, Request, HTTPException | ||
|
||
from basyx import ObjectStore | ||
from services.submodel_registry_server_service import SubmodelRegistryServerService | ||
|
||
|
||
class SubmodelRegistryRouter: | ||
def __init__(self, global_obj_store: ObjectStore[Identifiable]): | ||
self.router = APIRouter() | ||
self.obj_store = global_obj_store | ||
self.service = SubmodelRegistryServerService(global_obj_store) | ||
self._setup_routes() | ||
|
||
def _setup_routes(self): | ||
@self.router.get("/") | ||
async def GetAllSubmodelDescriptors() -> Any: | ||
return self.service.GetAllSubmodelDescriptors() | ||
|
||
@self.router.get("/{submodel_id}") | ||
async def GetSubmodelDescriptorById(submodel_id: str) -> Any: | ||
return self.service.GetSubmodelDescriptorById(submodel_id) | ||
|
||
@self.router.post("/") | ||
async def PostSubmodelDescriptor(request: Request) -> Any: | ||
body = await request.json() | ||
return self.service.PostSubmodelDescriptor(body) | ||
|
||
@self.router.put("/") | ||
async def PutSubmodelDescriptorById(request: Request) -> Any: | ||
body = await request.json() | ||
return self.service.PutSubmodelDescriptorById(body) | ||
|
||
@self.router.delete("/{submodel_id}") | ||
async def DeleteSubmodelDescriptorById(submodel_id: str) -> Any: | ||
return self.service.DeleteSubmodelDescriptorById(submodel_id) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
from basyx import ObjectStore | ||
from aas_core3.types import AssetAdministrationShell, ConceptDescription | ||
from aas_core3 import jsonization | ||
from fastapi import HTTPException | ||
from typing import Any, MutableMapping | ||
from aas_core3 import jsonization | ||
|
||
|
||
class AasRegistryServerService: | ||
def __init__(self, global_object_store: ObjectStore): | ||
self.obj_store = global_object_store | ||
|
||
def GetAllAssetAdministrationShellDescriptors(self) -> list[str]: | ||
all_descriptors = self.obj_store.filter_identifiables_by_instance(ConceptDescription) | ||
print(all_descriptors.__dict__) | ||
print(all_descriptors) | ||
print("test") | ||
aas_descriptors_store = ObjectStore() | ||
for descriptor in all_descriptors: | ||
reference_list = descriptor.is_case_of | ||
print(reference_list) | ||
for element in reference_list: | ||
reference_ids = element.keys | ||
for reference_id in reference_ids: | ||
try: | ||
identifiable = self.obj_store.get_identifiable(reference_id) | ||
except KeyError as e: | ||
# TODO: Provide a stacktrace | ||
# Wenn anders in Spezifikation, Stacktrace in server log | ||
raise HTTPException(status_code=400, detail=str(e)) | ||
|
||
if isinstance(identifiable, AssetAdministrationShell): | ||
try: | ||
aas_descriptors_store.add(descriptor) | ||
except KeyError as e: | ||
pass | ||
return [jsonization.to_jsonable(descriptor) for descriptor in aas_descriptors_store] | ||
|
||
def GetAssetAdministrationShellDescriptorById(self, descriptor_id) \ | ||
-> list[bool | int | float | str | list[Any] | MutableMapping[str, Any]]: | ||
aas_descriptor = self.obj_store.get_identifiable(descriptor_id) | ||
assert isinstance(aas_descriptor, ConceptDescription) | ||
return jsonization.to_jsonable(aas_descriptor) | ||
|
||
def PostAssetAdministrationShellDescriptor(self, json): | ||
aas_descriptor = jsonization.concept_description_from_jsonable(json) | ||
try: | ||
self.obj_store.add(aas_descriptor) | ||
except KeyError as e: | ||
# TODO: Provide a stacktrace | ||
# Wenn anders in Spezifikation, Stacktrace in server log | ||
raise HTTPException(status_code=400, detail=str(e)) | ||
return {"message": "AAS Descriptor processed"} | ||
|
||
def PutAssetAdministrationShellDescriptorById(self, json): | ||
aas_descriptor = jsonization.asset_administration_shell_from_jsonable(json) | ||
try: | ||
self.obj_store.delete(aas_descriptor.id) # should there be an exception if there is no aasx_package to | ||
# update? | ||
self.obj_store.add(aas_descriptor) | ||
except KeyError as e: | ||
# TODO: Provide a stacktrace | ||
# Wenn anders in Spezifikation, Stacktrace in server log | ||
raise HTTPException(status_code=400, detail=str(e)) | ||
return {"message": "AASX package updated"} | ||
|
||
def DeleteAssetAdministrationShellDescriptorById(self, descriptor_id): | ||
try: | ||
self.obj_store.delete(descriptor_id) # should there be an exception if there is no aasx_package to delete? | ||
except KeyError as e: | ||
# TODO: Provide a stacktrace | ||
# Wenn anders in Spezifikation, Stacktrace in server log | ||
raise HTTPException(status_code=400, detail=str(e)) | ||
return {"message": "AASX descriptor deleted"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
api/server/services/submodel_registry_server_service.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
from basyx import ObjectStore | ||
from aas_core3.types import AssetAdministrationShell, Submodel | ||
from aas_core3 import jsonization | ||
from fastapi import HTTPException | ||
from typing import Any, MutableMapping | ||
from aas_core3.types import AssetAdministrationShell, ConceptDescription | ||
|
||
|
||
class SubmodelRegistryServerService: | ||
def __init__(self, global_object_store: ObjectStore): | ||
self.obj_store = global_object_store | ||
|
||
def GetAllSubmodelDescriptors(self) -> list[str]: | ||
#print(self.obj_store.__dict__) | ||
|
||
all_descriptors = self.obj_store.filter_identifiables_by_instance(ConceptDescription) | ||
#print(all_descriptors.__dict__) | ||
print(all_descriptors) | ||
#print("test") | ||
submodel_descriptors_store = ObjectStore() | ||
for descriptor in all_descriptors: | ||
reference_list = descriptor.is_case_of | ||
print(reference_list) | ||
for element in reference_list: | ||
reference_ids = element.keys | ||
print(reference_ids) | ||
for reference_id in reference_ids: | ||
try: | ||
identifiable = self.obj_store.get_identifiable(reference_id.value) | ||
except KeyError as e: | ||
# TODO: Provide a stacktrace | ||
# Wenn anders in Spezifikation, Stacktrace in server log | ||
identifiable = [] | ||
if isinstance(identifiable, Submodel): | ||
try: | ||
submodel_descriptors_store.add(descriptor) | ||
except KeyError as e: | ||
pass | ||
return [jsonization.to_jsonable(descriptor) for descriptor in submodel_descriptors_store] | ||
|
||
def GetSubmodelDescriptorById(self, descriptor_id) \ | ||
-> list[bool | int | float | str | list[Any] | MutableMapping[str, Any]]: | ||
try: | ||
aas_descriptor = self.obj_store.get_identifiable(descriptor_id) | ||
except KeyError as e: | ||
# TODO: Provide a stacktrace | ||
# Wenn anders in Spezifikation, Stacktrace in server log | ||
raise HTTPException(status_code=400, detail=str(e)) | ||
assert isinstance(aas_descriptor, ConceptDescription) | ||
return jsonization.to_jsonable(aas_descriptor) | ||
|
||
def PostSubmodelDescriptor(self, json): | ||
submodel_descriptor = jsonization.concept_description_from_jsonable(json) | ||
|
||
# Check if all referenced submodels exist in the obeject_store | ||
|
||
for reference in submodel_descriptor.is_case_of: | ||
reference_ids = reference.keys | ||
for reference_id in reference_ids: | ||
try: | ||
self.obj_store.get_identifiable(reference_id.value) | ||
except KeyError as e: | ||
# TODO: Provide a stacktrace | ||
# Wenn anders in Spezifikation, Stacktrace in server log | ||
raise HTTPException(status_code=400, detail= "A referenced submodel of the concept description " | ||
"with the following id does not exist in the " | ||
"object_store:" + str(e)) | ||
|
||
try: | ||
self.obj_store.add(submodel_descriptor) | ||
except KeyError as e: | ||
# TODO: Provide a stacktrace | ||
# Wenn anders in Spezifikation, Stacktrace in server log | ||
raise HTTPException(status_code=400, detail="A referenced submodel of the concept description " | ||
"with the following id does not exist in the " | ||
"object_store:" + str(e)) | ||
return {"message": "Submodel descriptor processed"} | ||
|
||
def PutSubmodelDescriptorById(self, json): | ||
submodel_descriptor = jsonization.concept_description_from_jsonable(json) | ||
|
||
# Check if all referenced submodels exist in the obeject_store | ||
|
||
for reference in submodel_descriptor.is_case_of: | ||
reference_ids = reference.keys | ||
for reference_id in reference_ids: | ||
try: | ||
self.obj_store.get_identifiable(reference_id.value) | ||
except KeyError as e: | ||
# TODO: Provide a stacktrace | ||
# Wenn anders in Spezifikation, Stacktrace in server log | ||
raise HTTPException(status_code=400, detail= "A referenced submodel of the concept description " | ||
"with the following id does not exist in the " | ||
"object_store:" + str(e)) | ||
|
||
try: | ||
self.obj_store.delete(submodel_descriptor.id) # should there be an exception if there is no aasx_package to | ||
# update? | ||
self.obj_store.add(submodel_descriptor) | ||
except KeyError as e: | ||
# TODO: Provide a stacktrace | ||
# Wenn anders in Spezifikation, Stacktrace in server log | ||
raise HTTPException(status_code=400, detail=str(e)) | ||
return {"message": "AASX package updated"} | ||
|
||
def DeleteSubmodelDescriptorById(self, descriptor_id): | ||
try: | ||
self.obj_store.delete(descriptor_id) # should there be an exception if there is no aasx_package to delete? | ||
except KeyError as e: | ||
# TODO: Provide a stacktrace | ||
# Wenn anders in Spezifikation, Stacktrace in server log | ||
raise HTTPException(status_code=400, detail=str(e)) | ||
return {"message": "Submodel descriptor deleted"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters