From 7d6f2d9b0f8be0cb3ab5a72f18ed59bb62b97cc4 Mon Sep 17 00:00:00 2001 From: Stefan Scherzinger Date: Thu, 18 Jul 2024 15:29:55 +0200 Subject: [PATCH] Implement instance-based `data.jon` get requests Also split the processing of GET requests in the server depending on the path. This makes the individual functions smaller and better to read. --- .../schunk_egu_egk_gripper_dummy/main.py | 10 +++- schunk_egu_egk_gripper_dummy/src/dummy.py | 58 ++++++++++--------- .../tests/test_dummy.py | 37 +++++++----- 3 files changed, 63 insertions(+), 42 deletions(-) diff --git a/schunk_egu_egk_gripper_dummy/schunk_egu_egk_gripper_dummy/main.py b/schunk_egu_egk_gripper_dummy/schunk_egu_egk_gripper_dummy/main.py index 5f913c2..0692ccc 100644 --- a/schunk_egu_egk_gripper_dummy/schunk_egu_egk_gripper_dummy/main.py +++ b/schunk_egu_egk_gripper_dummy/schunk_egu_egk_gripper_dummy/main.py @@ -34,4 +34,12 @@ async def put(msg: Message): @server.get("/adi/{path}") async def get(request: Request): - return dummy.get(request.path_params["path"], request.query_params) + path = request.path_params["path"] + params = request.query_params + if path == "info.json": + return dummy.get_info(params) + if path == "enum.json": + return dummy.get_enum(params) + if path == "data.json": + return dummy.get_data(params) + return None diff --git a/schunk_egu_egk_gripper_dummy/src/dummy.py b/schunk_egu_egk_gripper_dummy/src/dummy.py index 07d9ac6..73948ef 100644 --- a/schunk_egu_egk_gripper_dummy/src/dummy.py +++ b/schunk_egu_egk_gripper_dummy/src/dummy.py @@ -46,34 +46,38 @@ def _run(self) -> None: time.sleep(1) print("Done") - def get(self, path: str, query: dict[str, str]) -> dict | list | None: - print(f"path: {path}") - print(f"query: {query}") + def get_info(self, query: dict[str, str]) -> dict: + return {"dataformat": 0} # 0: Little endian, 1: Big endian - if path == "info.json": - return {"dataformat": 0} # 0: Little endian, 1: Big endian + def get_enum(self, query: dict[str, str]) -> list: + inst = query["inst"] + value = int(query["value"]) + if inst in self.enum: + string = self.enum[inst][value]["string"] + return [{"string": string, "value": value}] + else: + return [] - if path == "enum.json": - inst = query["inst"] - value = int(query["value"]) - if inst in self.enum: - string = self.enum[inst][value]["string"] - return [{"string": string, "value": value}] - else: - return [] + def get_data(self, query: dict[str, str]) -> list: + result: list = [] + if "offset" in query and "count" in query: + offset = int(query["offset"]) + count = int(query["count"]) + if offset < 0 or count < 0: + return result + if offset + count >= len(self.metadata): + return result + for i in range(count): + result.append(self.metadata[offset + i]) + return result - if path == "data.json": - result: list = [] - if "offset" in query and "count" in query: - offset = int(query["offset"]) - count = int(query["count"]) - if offset < 0 or count < 0: - return result - if offset + count >= len(self.metadata): - return result - for i in range(count): - result.append(self.metadata[offset + i]) + if "inst" in query and "count" in query: + inst = query["inst"] + count = int(query["count"]) + if count != 1: + return result + if inst not in self.data: return result - else: - return [] - return None + return self.data[inst] + else: + return [] diff --git a/schunk_egu_egk_gripper_dummy/tests/test_dummy.py b/schunk_egu_egk_gripper_dummy/tests/test_dummy.py index 9bd26ea..747b44a 100644 --- a/schunk_egu_egk_gripper_dummy/tests/test_dummy.py +++ b/schunk_egu_egk_gripper_dummy/tests/test_dummy.py @@ -30,51 +30,60 @@ def test_dummy_reads_configuration_on_startup(): def test_dummy_responds_correctly_to_info_requests(): dummy = Dummy() - path = "info.json" query = "" expected = {"dataformat": 0} - assert dummy.get(path, query) == expected + assert dummy.get_info(query) == expected def test_dummy_responds_correctly_to_enum_requests(): dummy = Dummy() - path = "enum.json" inst = "0x0118" value = 0 query = {"inst": inst, "value": value} expected = [dummy.enum[inst][value]] - assert dummy.get(path, query) == expected + assert dummy.get_enum(query) == expected def test_dummy_survives_invalid_enum_requests(): dummy = Dummy() - path = "enum.json" invalid_inst = "0x0" query = {"inst": invalid_inst, "value": 0} expected = [] - assert dummy.get(path, query) == expected + assert dummy.get_enum(query) == expected -def test_dummy_responds_correctly_to_data_requests(): +def test_dummy_responds_correctly_to_data_offset_requests(): dummy = Dummy() - path = "data.json" query = {"offset": 15, "count": 3} expected = [dummy.metadata[15], dummy.metadata[16], dummy.metadata[17]] - assert dummy.get(path, query) == expected + assert dummy.get_data(query) == expected + + +def test_dummy_responds_correctly_to_data_instance_requests(): + dummy = Dummy() + inst = "0x0040" + query = {"inst": inst, "count": 1} + expected = dummy.data[inst] + assert dummy.get_data(query) == expected def test_dummy_survives_invalid_data_requests(): dummy = Dummy() - path = "data.json" query = {"offset": 1000, "count": "2"} expected = [] - assert dummy.get(path, query) == expected + assert dummy.get_data(query) == expected query = {"offset": 100, "count": "90"} expected = [] - assert dummy.get(path, query) == expected + assert dummy.get_data(query) == expected query = {"offset": 1000, "count": "-1"} expected = [] - assert dummy.get(path, query) == expected + assert dummy.get_data(query) == expected query = {"offset": -1, "count": "1000"} expected = [] - assert dummy.get(path, query) == expected + assert dummy.get_data(query) == expected + query = {"inst": "0x0040", "count": "0"} + expected = [] + assert dummy.get_data(query) == expected + query = {"inst": "0x0040", "count": "2"} + expected = [] + assert dummy.get_data(query) == expected