Skip to content

Fix simple module, update readme #480

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 3, 2023
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
4 changes: 2 additions & 2 deletions docs/examples/example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/module_step2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/module_step2_optional.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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():
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/module_step3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/my_cool_arm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
2 changes: 1 addition & 1 deletion examples/complex_module/src/arm/my_arm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
2 changes: 1 addition & 1 deletion examples/complex_module/src/gizmo/my_gizmo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
4 changes: 3 additions & 1 deletion examples/simple_module/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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": []
}
],
Expand Down
3 changes: 3 additions & 0 deletions examples/simple_module/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down
5 changes: 3 additions & 2 deletions examples/simple_module/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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]):
Expand All @@ -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():
Expand Down