Skip to content

Commit

Permalink
further work
Browse files Browse the repository at this point in the history
  • Loading branch information
Krande committed Sep 29, 2024
1 parent 7914657 commit 874e8ac
Show file tree
Hide file tree
Showing 42 changed files with 573 additions and 108 deletions.
16 changes: 11 additions & 5 deletions examples/procedure_example/list_server_file_objects.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import asyncio
import pathlib

from ada.comms.fb_model_gen import ParameterDC, ProcedureStartDC
from ada.comms.fb_model_gen import (
FileObjectDC,
FilePurposeDC,
FileTypeDC,
ParameterDC,
ProcedureStartDC,
)
from ada.comms.wsock_client_async import WebSocketClientAsync

THIS_DIR = pathlib.Path(__file__).parent
Expand All @@ -10,11 +16,11 @@

async def list_procedures():
async with WebSocketClientAsync("localhost", 8765, "local") as ws_client:
procedures = await ws_client.list_procedures()
await ws_client.run_procedure(
ProcedureStartDC("add_stiffeners", [ParameterDC(name="ifc_file", value="MyBaseStructure.ifc")])
await ws_client.update_file_server(
FileObjectDC("test_file", FileTypeDC.IFC, FilePurposeDC.DESIGN, THIS_DIR / "temp/MyBaseStructure.ifc")
)
print(procedures)
file_objects = await ws_client.list_server_file_objects()
print(file_objects)


if __name__ == "__main__":
Expand Down
4 changes: 2 additions & 2 deletions examples/procedure_example/procedures/add_stiffeners.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import numpy as np
import typer
from typing_extensions import Annotated

import ada
from ada.comms.fb_model_gen import FileTypeDC
from ada.comms.procedures import procedure_decorator
from typing_extensions import Annotated

app = typer.Typer()
THIS_FILE = pathlib.Path(__file__).resolve().absolute()
Expand Down Expand Up @@ -48,7 +48,7 @@ def add_stiffeners(pl: ada.Plate) -> list[ada.Beam]:


@procedure_decorator(app, input_file_var="ifc_file", input_file_type=FileTypeDC.IFC, export_file_type=FileTypeDC.IFC)
def main(ifc_file: pathlib.Path=None) -> pathlib.Path:
def main(ifc_file: pathlib.Path = None) -> pathlib.Path:
"""A procedure to add stiffeners to all plates in the IFC file"""

a = ada.from_ifc(ifc_file)
Expand Down
2 changes: 2 additions & 0 deletions src/ada/comms/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class ServerError(Exception):
pass
103 changes: 72 additions & 31 deletions src/ada/comms/fb_deserializer.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,36 @@
from ada.comms.fb_model_gen import (
CameraParamsDC,
CommandTypeDC,
ErrorDC,
FileObjectDC,
FilePurposeDC,
FileTypeDC,
MeshInfoDC,
MessageDC,
ParameterDC,
ProcedureDC,
ProcedureStartDC,
ProcedureStateDC,
ProcedureStoreDC,
SceneDC,
SceneOperationsDC,
ServerDC,
ServerReplyDC,
TargetTypeDC,
WebClientDC,
)
from ada.comms.wsock import Message

from ada.comms.fb_model_gen import WebClientDC, FileObjectDC, MeshInfoDC, CameraParamsDC, SceneDC, ServerDC, ProcedureStoreDC, ProcedureDC, ParameterDC, ProcedureStartDC, ErrorDC, ServerReplyDC, MessageDC,CommandTypeDC, TargetTypeDC, SceneOperationsDC, FilePurposeDC, FileTypeDC, ProcedureStateDC

def deserialize_webclient(fb_obj) -> WebClientDC | None:
if fb_obj is None:
return None

return WebClientDC(
instance_id=fb_obj.InstanceId(),
name=fb_obj.Name().decode('utf-8') if fb_obj.Name() is not None else None,
address=fb_obj.Address().decode('utf-8') if fb_obj.Address() is not None else None,
port=fb_obj.Port()
name=fb_obj.Name().decode("utf-8") if fb_obj.Name() is not None else None,
address=fb_obj.Address().decode("utf-8") if fb_obj.Address() is not None else None,
port=fb_obj.Port(),
)


Expand All @@ -19,11 +39,11 @@ def deserialize_fileobject(fb_obj) -> FileObjectDC | None:
return None

return FileObjectDC(
name=fb_obj.Name().decode('utf-8') if fb_obj.Name() is not None else None,
name=fb_obj.Name().decode("utf-8") if fb_obj.Name() is not None else None,
file_type=FileTypeDC(fb_obj.FileType()),
purpose=FilePurposeDC(fb_obj.Purpose()),
filepath=fb_obj.Filepath().decode('utf-8') if fb_obj.Filepath() is not None else None,
filedata=bytes(fb_obj.FiledataAsNumpy()) if fb_obj.FiledataLength() > 0 else None
filepath=fb_obj.Filepath().decode("utf-8") if fb_obj.Filepath() is not None else None,
filedata=bytes(fb_obj.FiledataAsNumpy()) if fb_obj.FiledataLength() > 0 else None,
)


Expand All @@ -32,9 +52,9 @@ def deserialize_meshinfo(fb_obj) -> MeshInfoDC | None:
return None

return MeshInfoDC(
object_name=fb_obj.ObjectName().decode('utf-8') if fb_obj.ObjectName() is not None else None,
object_name=fb_obj.ObjectName().decode("utf-8") if fb_obj.ObjectName() is not None else None,
face_index=fb_obj.FaceIndex(),
json_data=fb_obj.JsonData().decode('utf-8') if fb_obj.JsonData() is not None else None
json_data=fb_obj.JsonData().decode("utf-8") if fb_obj.JsonData() is not None else None,
)


Expand All @@ -49,7 +69,7 @@ def deserialize_cameraparams(fb_obj) -> CameraParamsDC | None:
fov=fb_obj.Fov(),
near=fb_obj.Near(),
far=fb_obj.Far(),
force_camera=fb_obj.ForceCamera()
force_camera=fb_obj.ForceCamera(),
)


Expand All @@ -60,7 +80,7 @@ def deserialize_scene(fb_obj) -> SceneDC | None:
return SceneDC(
operation=SceneOperationsDC(fb_obj.Operation()),
camera_params=deserialize_cameraparams(fb_obj.CameraParams()),
current_file=deserialize_fileobject(fb_obj.CurrentFile())
current_file=deserialize_fileobject(fb_obj.CurrentFile()),
)


Expand All @@ -70,7 +90,11 @@ def deserialize_server(fb_obj) -> ServerDC | None:

return ServerDC(
add_file_object=deserialize_fileobject(fb_obj.AddFileObject()),
all_file_objects=[deserialize_fileobject(fb_obj.AllFileObjects(i)) for i in range(fb_obj.AllFileObjectsLength())] if fb_obj.AllFileObjectsLength() > 0 else None
all_file_objects=(
[deserialize_fileobject(fb_obj.AllFileObjects(i)) for i in range(fb_obj.AllFileObjectsLength())]
if fb_obj.AllFileObjectsLength() > 0
else None
),
)


Expand All @@ -79,8 +103,12 @@ def deserialize_procedurestore(fb_obj) -> ProcedureStoreDC | None:
return None

return ProcedureStoreDC(
procedures=[deserialize_procedure(fb_obj.Procedures(i)) for i in range(fb_obj.ProceduresLength())] if fb_obj.ProceduresLength() > 0 else None,
start_procedure=deserialize_procedurestart(fb_obj.StartProcedure())
procedures=(
[deserialize_procedure(fb_obj.Procedures(i)) for i in range(fb_obj.ProceduresLength())]
if fb_obj.ProceduresLength() > 0
else None
),
start_procedure=deserialize_procedurestart(fb_obj.StartProcedure()),
)


Expand All @@ -89,14 +117,20 @@ def deserialize_procedure(fb_obj) -> ProcedureDC | None:
return None

return ProcedureDC(
name=fb_obj.Name().decode('utf-8') if fb_obj.Name() is not None else None,
description=fb_obj.Description().decode('utf-8') if fb_obj.Description() is not None else None,
script_file_location=fb_obj.ScriptFileLocation().decode('utf-8') if fb_obj.ScriptFileLocation() is not None else None,
parameters=[deserialize_parameter(fb_obj.Parameters(i)) for i in range(fb_obj.ParametersLength())] if fb_obj.ParametersLength() > 0 else None,
input_file_var=fb_obj.InputFileVar().decode('utf-8') if fb_obj.InputFileVar() is not None else None,
name=fb_obj.Name().decode("utf-8") if fb_obj.Name() is not None else None,
description=fb_obj.Description().decode("utf-8") if fb_obj.Description() is not None else None,
script_file_location=(
fb_obj.ScriptFileLocation().decode("utf-8") if fb_obj.ScriptFileLocation() is not None else None
),
parameters=(
[deserialize_parameter(fb_obj.Parameters(i)) for i in range(fb_obj.ParametersLength())]
if fb_obj.ParametersLength() > 0
else None
),
input_file_var=fb_obj.InputFileVar().decode("utf-8") if fb_obj.InputFileVar() is not None else None,
input_file_type=FileTypeDC(fb_obj.InputFileType()),
export_file_type=FileTypeDC(fb_obj.ExportFileType()),
state=ProcedureStateDC(fb_obj.State())
state=ProcedureStateDC(fb_obj.State()),
)


Expand All @@ -105,9 +139,9 @@ def deserialize_parameter(fb_obj) -> ParameterDC | None:
return None

return ParameterDC(
name=fb_obj.Name().decode('utf-8') if fb_obj.Name() is not None else None,
type=fb_obj.Type().decode('utf-8') if fb_obj.Type() is not None else None,
value=fb_obj.Value().decode('utf-8') if fb_obj.Value() is not None else None
name=fb_obj.Name().decode("utf-8") if fb_obj.Name() is not None else None,
type=fb_obj.Type().decode("utf-8") if fb_obj.Type() is not None else None,
value=fb_obj.Value().decode("utf-8") if fb_obj.Value() is not None else None,
)


Expand All @@ -116,8 +150,12 @@ def deserialize_procedurestart(fb_obj) -> ProcedureStartDC | None:
return None

return ProcedureStartDC(
procedure_name=fb_obj.ProcedureName().decode('utf-8') if fb_obj.ProcedureName() is not None else None,
parameters=[deserialize_parameter(fb_obj.Parameters(i)) for i in range(fb_obj.ParametersLength())] if fb_obj.ParametersLength() > 0 else None
procedure_name=fb_obj.ProcedureName().decode("utf-8") if fb_obj.ProcedureName() is not None else None,
parameters=(
[deserialize_parameter(fb_obj.Parameters(i)) for i in range(fb_obj.ParametersLength())]
if fb_obj.ParametersLength() > 0
else None
),
)


Expand All @@ -126,8 +164,7 @@ def deserialize_error(fb_obj) -> ErrorDC | None:
return None

return ErrorDC(
code=fb_obj.Code(),
message=fb_obj.Message().decode('utf-8') if fb_obj.Message() is not None else None
code=fb_obj.Code(), message=fb_obj.Message().decode("utf-8") if fb_obj.Message() is not None else None
)


Expand All @@ -136,9 +173,9 @@ def deserialize_serverreply(fb_obj) -> ServerReplyDC | None:
return None

return ServerReplyDC(
message=fb_obj.Message().decode('utf-8') if fb_obj.Message() is not None else None,
message=fb_obj.Message().decode("utf-8") if fb_obj.Message() is not None else None,
reply_to=CommandTypeDC(fb_obj.ReplyTo()),
error=deserialize_error(fb_obj.Error())
error=deserialize_error(fb_obj.Error()),
)


Expand All @@ -155,9 +192,13 @@ def deserialize_message(fb_obj) -> MessageDC | None:
target_group=TargetTypeDC(fb_obj.TargetGroup()),
client_type=TargetTypeDC(fb_obj.ClientType()),
target_id=fb_obj.TargetId(),
web_clients=[deserialize_webclient(fb_obj.WebClients(i)) for i in range(fb_obj.WebClientsLength())] if fb_obj.WebClientsLength() > 0 else None,
web_clients=(
[deserialize_webclient(fb_obj.WebClients(i)) for i in range(fb_obj.WebClientsLength())]
if fb_obj.WebClientsLength() > 0
else None
),
procedure_store=deserialize_procedurestore(fb_obj.ProcedureStore()),
server_reply=deserialize_serverreply(fb_obj.ServerReply())
server_reply=deserialize_serverreply(fb_obj.ServerReply()),
)


Expand Down
26 changes: 22 additions & 4 deletions src/ada/comms/fb_model_gen.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from __future__ import annotations
from enum import Enum
from dataclasses import dataclass
from typing import Optional, List
import pathlib

import pathlib
from dataclasses import dataclass
from enum import Enum
from typing import List, Optional


class CommandTypeDC(Enum):
Expand All @@ -20,39 +20,46 @@ class CommandTypeDC(Enum):
ERROR = 10
SERVER_REPLY = 11


class TargetTypeDC(Enum):
WEB = 0
LOCAL = 1
SERVER = 2


class SceneOperationsDC(Enum):
ADD = 0
REMOVE = 1
REPLACE = 2


class FilePurposeDC(Enum):
DESIGN = 0
ANALYSIS = 1
FABRICATE = 2


class FileTypeDC(Enum):
IFC = 0
GLB = 1
SQLITE = 2


class ProcedureStateDC(Enum):
IDLE = 0
RUNNING = 1
FINISHED = 2
ERROR = 3


@dataclass
class WebClientDC:
instance_id: int = None
name: str = ""
address: str = ""
port: int = None


@dataclass
class FileObjectDC:
name: str = ""
Expand All @@ -61,12 +68,14 @@ class FileObjectDC:
filepath: pathlib.Path | str = ""
filedata: bytes = None


@dataclass
class MeshInfoDC:
object_name: str = ""
face_index: int = None
json_data: str = ""


@dataclass
class CameraParamsDC:
position: List[float] = None
Expand All @@ -77,22 +86,26 @@ class CameraParamsDC:
far: float = None
force_camera: bool = None


@dataclass
class SceneDC:
operation: Optional[SceneOperationsDC] = None
camera_params: Optional[CameraParamsDC] = None
current_file: Optional[FileObjectDC] = None


@dataclass
class ServerDC:
add_file_object: Optional[FileObjectDC] = None
all_file_objects: Optional[List[FileObjectDC]] = None


@dataclass
class ProcedureStoreDC:
procedures: Optional[List[ProcedureDC]] = None
start_procedure: Optional[ProcedureStartDC] = None


@dataclass
class ProcedureDC:
name: str = ""
Expand All @@ -104,28 +117,33 @@ class ProcedureDC:
export_file_type: Optional[FileTypeDC] = None
state: Optional[ProcedureStateDC] = None


@dataclass
class ParameterDC:
name: str = ""
type: str = ""
value: str = ""


@dataclass
class ProcedureStartDC:
procedure_name: str = ""
parameters: Optional[List[ParameterDC]] = None


@dataclass
class ErrorDC:
code: int = None
message: str = ""


@dataclass
class ServerReplyDC:
message: str = ""
reply_to: Optional[CommandTypeDC] = None
error: Optional[ErrorDC] = None


@dataclass
class MessageDC:
instance_id: int = None
Expand Down
Loading

0 comments on commit 874e8ac

Please sign in to comment.