From 82ea6e7d147da2af4484e7e9194e70835aaf2e0e Mon Sep 17 00:00:00 2001 From: Cheuk <90270663+cheukt@users.noreply.github.com> Date: Fri, 3 Nov 2023 10:26:51 -0400 Subject: [PATCH] Fix simple module, update readme (#480) --- docs/examples/example.ipynb | 4 ++-- docs/examples/module_step2.py | 2 +- docs/examples/module_step2_optional.py | 4 ++-- docs/examples/module_step3.py | 2 +- docs/examples/my_cool_arm.py | 2 +- examples/complex_module/src/arm/my_arm.py | 2 +- examples/complex_module/src/gizmo/my_gizmo.py | 2 +- examples/simple_module/README.md | 4 +++- examples/simple_module/client.py | 3 +++ examples/simple_module/src/main.py | 5 +++-- 10 files changed, 18 insertions(+), 12 deletions(-) diff --git a/docs/examples/example.ipynb b/docs/examples/example.ipynb index 0a478ec61..445ada991 100644 --- a/docs/examples/example.ipynb +++ b/docs/examples/example.ipynb @@ -288,7 +288,7 @@ " async def close(self):\n", " # This is a completely optional function to include. This will be called when the resource is removed from the config or the module\n", " # is shutting down.\n", - " LOGGER.debug(f\"{self.name} is closed.\")\n", + " LOGGER.info(f\"{self.name} is closed.\")\n", "\n", "# Anything below this line is optional and will be replaced later, but may come in handy for debugging and testing.\n", "# To use, call `python wifi_sensor_module.py` in the command line while in the `src` directory.\n", @@ -371,7 +371,7 @@ " @classmethod\n", " def validate_config(cls, config: ComponentConfig) -> Sequence[str]:\n", " if \"multiplier\" in config.attributes.fields:\n", - " if not isinstance(config.attributes.fields[\"multiplier\"], float):\n", + " if not config.attributes.fields[\"multiplier\"].HasField(\"number_value\"):\n", " raise Exception(\"Multiplier must be a float.\")\n", " multiplier = config.attributes.fields[\"multiplier\"].number_value\n", " if multiplier == 0:\n", diff --git a/docs/examples/module_step2.py b/docs/examples/module_step2.py index 5bfbcc8c8..bc4b1d3a9 100644 --- a/docs/examples/module_step2.py +++ b/docs/examples/module_step2.py @@ -32,7 +32,7 @@ async def get_readings(self, extra: Optional[Dict[str, Any]] = None, **kwargs) - async def close(self): # This is a completely optional function to include. This will be called when the resource is removed from the config or the module # is shutting down. - LOGGER.debug(f"{self.name} is closed.") + LOGGER.info(f"{self.name} is closed.") async def main(): diff --git a/docs/examples/module_step2_optional.py b/docs/examples/module_step2_optional.py index 47ee7d426..1d296cf9f 100644 --- a/docs/examples/module_step2_optional.py +++ b/docs/examples/module_step2_optional.py @@ -27,7 +27,7 @@ def new(cls, config: ComponentConfig, dependencies: Mapping[ResourceName, Resour @classmethod def validate_config(cls, config: ComponentConfig) -> Sequence[str]: if "multiplier" in config.attributes.fields: - if not isinstance(config.attributes.fields["multiplier"], float): + if not config.attributes.fields["multiplier"].HasField("number_value"): raise Exception("Multiplier must be a float.") multiplier = config.attributes.fields["multiplier"].number_value if multiplier == 0: @@ -54,7 +54,7 @@ def reconfigure(self, config: ComponentConfig, dependencies: Mapping[ResourceNam async def close(self): # This is a completely optional function to include. This will be called when the resource is removed from the config or the module # is shutting down. - LOGGER.debug(f"{self.name} is closed.") + LOGGER.info(f"{self.name} is closed.") async def main(): diff --git a/docs/examples/module_step3.py b/docs/examples/module_step3.py index 3d0659c6e..f400e0c48 100644 --- a/docs/examples/module_step3.py +++ b/docs/examples/module_step3.py @@ -33,7 +33,7 @@ async def get_readings(self, extra: Optional[Dict[str, Any]] = None, **kwargs) - async def close(self): # This is a completely optional function to include. This will be called when the resource is removed from the config or the module # is shutting down. - LOGGER.debug(f"{self.name} is closed.") + LOGGER.info(f"{self.name} is closed.") async def main(): diff --git a/docs/examples/my_cool_arm.py b/docs/examples/my_cool_arm.py index 3a8f35385..4c9381f75 100644 --- a/docs/examples/my_cool_arm.py +++ b/docs/examples/my_cool_arm.py @@ -109,4 +109,4 @@ async def get_kinematics(self, extra: Optional[Dict[str, Any]] = None, **kwargs) async def close(self): # This is a completely optional function to include. This will be called when the resource is removed from the config or the module # is shutting down. - LOGGER.debug(f"{self.name} is closed.") + LOGGER.info(f"{self.name} is closed.") diff --git a/examples/complex_module/src/arm/my_arm.py b/examples/complex_module/src/arm/my_arm.py index 2b1bac542..0b6f1ce87 100644 --- a/examples/complex_module/src/arm/my_arm.py +++ b/examples/complex_module/src/arm/my_arm.py @@ -112,7 +112,7 @@ async def get_kinematics(self, extra: Optional[Dict[str, Any]] = None, **kwargs) async def close(self): # This is a completely optional function to include. This will be called when the resource is removed from the config or the module # is shutting down. - LOGGER.debug(f"{self.name} is closed.") + LOGGER.info(f"{self.name} is closed.") Registry.register_resource_creator(Arm.SUBTYPE, MyArm.MODEL, ResourceCreatorRegistration(MyArm.new)) diff --git a/examples/complex_module/src/gizmo/my_gizmo.py b/examples/complex_module/src/gizmo/my_gizmo.py index 591dd9a52..e885c82bf 100644 --- a/examples/complex_module/src/gizmo/my_gizmo.py +++ b/examples/complex_module/src/gizmo/my_gizmo.py @@ -76,7 +76,7 @@ def reconfigure(self, config: ComponentConfig, dependencies: Mapping[ResourceNam async def close(self): # This is a completely optional function to include. This will be called when the resource is removed from the config or the module # is shutting down. - LOGGER.debug(f"{self.name} is closed.") + LOGGER.info(f"{self.name} is closed.") Registry.register_resource_creator(Gizmo.SUBTYPE, MyGizmo.MODEL, ResourceCreatorRegistration(MyGizmo.new, MyGizmo.validate_config)) diff --git a/examples/simple_module/README.md b/examples/simple_module/README.md index ea22c0217..5baed3223 100644 --- a/examples/simple_module/README.md +++ b/examples/simple_module/README.md @@ -38,7 +38,9 @@ An example configuration for a Sensor component could look like this: "name": "sensor1", "type": "sensor", "model": "viam:sensor:mysensor", - "attributes": {}, + "attributes": { + "multiplier": 2, + }, "depends_on": [] } ], diff --git a/examples/simple_module/client.py b/examples/simple_module/client.py index d9bceba27..485d16d18 100644 --- a/examples/simple_module/client.py +++ b/examples/simple_module/client.py @@ -22,6 +22,9 @@ async def main(): reading = await sensor.get_readings() print(f"The reading is {reading}") + response = await sensor.do_command({"hello": "world"}) + print(f"The response is {response}") + await robot.close() diff --git a/examples/simple_module/src/main.py b/examples/simple_module/src/main.py index 1906fc474..b6ae0417e 100644 --- a/examples/simple_module/src/main.py +++ b/examples/simple_module/src/main.py @@ -32,7 +32,7 @@ def new(cls, config: ComponentConfig, dependencies: Mapping[ResourceName, Resour @classmethod def validate_config(cls, config: ComponentConfig) -> Sequence[str]: if "multiplier" in config.attributes.fields: - if not isinstance(config.attributes.fields["multiplier"], float): + if not config.attributes.fields["multiplier"].HasField("number_value"): raise Exception("Multiplier must be a float.") multiplier = config.attributes.fields["multiplier"].number_value if multiplier == 0: @@ -43,6 +43,7 @@ async def get_readings(self, extra: Optional[Dict[str, Any]] = None, **kwargs) - return {"signal": 1 * self.multiplier} async def do_command(self, command: Mapping[str, ValueTypes], *, timeout: Optional[float] = None, **kwargs) -> Mapping[str, ValueTypes]: + LOGGER.info(f"received {command=}.") return command def reconfigure(self, config: ComponentConfig, dependencies: Mapping[ResourceName, ResourceBase]): @@ -55,7 +56,7 @@ def reconfigure(self, config: ComponentConfig, dependencies: Mapping[ResourceNam async def close(self): # This is a completely optional function to include. This will be called when the resource is removed from the config or the module # is shutting down. - LOGGER.debug(f"{self.name} is closed.") + LOGGER.info(f"{self.name} is closed.") async def main():