Skip to content

Commit 82ea6e7

Browse files
authored
Fix simple module, update readme (#480)
1 parent 2d3891e commit 82ea6e7

File tree

10 files changed

+18
-12
lines changed

10 files changed

+18
-12
lines changed

docs/examples/example.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@
288288
" async def close(self):\n",
289289
" # This is a completely optional function to include. This will be called when the resource is removed from the config or the module\n",
290290
" # is shutting down.\n",
291-
" LOGGER.debug(f\"{self.name} is closed.\")\n",
291+
" LOGGER.info(f\"{self.name} is closed.\")\n",
292292
"\n",
293293
"# Anything below this line is optional and will be replaced later, but may come in handy for debugging and testing.\n",
294294
"# To use, call `python wifi_sensor_module.py` in the command line while in the `src` directory.\n",
@@ -371,7 +371,7 @@
371371
" @classmethod\n",
372372
" def validate_config(cls, config: ComponentConfig) -> Sequence[str]:\n",
373373
" if \"multiplier\" in config.attributes.fields:\n",
374-
" if not isinstance(config.attributes.fields[\"multiplier\"], float):\n",
374+
" if not config.attributes.fields[\"multiplier\"].HasField(\"number_value\"):\n",
375375
" raise Exception(\"Multiplier must be a float.\")\n",
376376
" multiplier = config.attributes.fields[\"multiplier\"].number_value\n",
377377
" if multiplier == 0:\n",

docs/examples/module_step2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ async def get_readings(self, extra: Optional[Dict[str, Any]] = None, **kwargs) -
3232
async def close(self):
3333
# This is a completely optional function to include. This will be called when the resource is removed from the config or the module
3434
# is shutting down.
35-
LOGGER.debug(f"{self.name} is closed.")
35+
LOGGER.info(f"{self.name} is closed.")
3636

3737

3838
async def main():

docs/examples/module_step2_optional.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def new(cls, config: ComponentConfig, dependencies: Mapping[ResourceName, Resour
2727
@classmethod
2828
def validate_config(cls, config: ComponentConfig) -> Sequence[str]:
2929
if "multiplier" in config.attributes.fields:
30-
if not isinstance(config.attributes.fields["multiplier"], float):
30+
if not config.attributes.fields["multiplier"].HasField("number_value"):
3131
raise Exception("Multiplier must be a float.")
3232
multiplier = config.attributes.fields["multiplier"].number_value
3333
if multiplier == 0:
@@ -54,7 +54,7 @@ def reconfigure(self, config: ComponentConfig, dependencies: Mapping[ResourceNam
5454
async def close(self):
5555
# This is a completely optional function to include. This will be called when the resource is removed from the config or the module
5656
# is shutting down.
57-
LOGGER.debug(f"{self.name} is closed.")
57+
LOGGER.info(f"{self.name} is closed.")
5858

5959

6060
async def main():

docs/examples/module_step3.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ async def get_readings(self, extra: Optional[Dict[str, Any]] = None, **kwargs) -
3333
async def close(self):
3434
# This is a completely optional function to include. This will be called when the resource is removed from the config or the module
3535
# is shutting down.
36-
LOGGER.debug(f"{self.name} is closed.")
36+
LOGGER.info(f"{self.name} is closed.")
3737

3838

3939
async def main():

docs/examples/my_cool_arm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,4 @@ async def get_kinematics(self, extra: Optional[Dict[str, Any]] = None, **kwargs)
109109
async def close(self):
110110
# This is a completely optional function to include. This will be called when the resource is removed from the config or the module
111111
# is shutting down.
112-
LOGGER.debug(f"{self.name} is closed.")
112+
LOGGER.info(f"{self.name} is closed.")

examples/complex_module/src/arm/my_arm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ async def get_kinematics(self, extra: Optional[Dict[str, Any]] = None, **kwargs)
112112
async def close(self):
113113
# This is a completely optional function to include. This will be called when the resource is removed from the config or the module
114114
# is shutting down.
115-
LOGGER.debug(f"{self.name} is closed.")
115+
LOGGER.info(f"{self.name} is closed.")
116116

117117

118118
Registry.register_resource_creator(Arm.SUBTYPE, MyArm.MODEL, ResourceCreatorRegistration(MyArm.new))

examples/complex_module/src/gizmo/my_gizmo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def reconfigure(self, config: ComponentConfig, dependencies: Mapping[ResourceNam
7676
async def close(self):
7777
# This is a completely optional function to include. This will be called when the resource is removed from the config or the module
7878
# is shutting down.
79-
LOGGER.debug(f"{self.name} is closed.")
79+
LOGGER.info(f"{self.name} is closed.")
8080

8181

8282
Registry.register_resource_creator(Gizmo.SUBTYPE, MyGizmo.MODEL, ResourceCreatorRegistration(MyGizmo.new, MyGizmo.validate_config))

examples/simple_module/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ An example configuration for a Sensor component could look like this:
3838
"name": "sensor1",
3939
"type": "sensor",
4040
"model": "viam:sensor:mysensor",
41-
"attributes": {},
41+
"attributes": {
42+
"multiplier": 2,
43+
},
4244
"depends_on": []
4345
}
4446
],

examples/simple_module/client.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ async def main():
2222
reading = await sensor.get_readings()
2323
print(f"The reading is {reading}")
2424

25+
response = await sensor.do_command({"hello": "world"})
26+
print(f"The response is {response}")
27+
2528
await robot.close()
2629

2730

examples/simple_module/src/main.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def new(cls, config: ComponentConfig, dependencies: Mapping[ResourceName, Resour
3232
@classmethod
3333
def validate_config(cls, config: ComponentConfig) -> Sequence[str]:
3434
if "multiplier" in config.attributes.fields:
35-
if not isinstance(config.attributes.fields["multiplier"], float):
35+
if not config.attributes.fields["multiplier"].HasField("number_value"):
3636
raise Exception("Multiplier must be a float.")
3737
multiplier = config.attributes.fields["multiplier"].number_value
3838
if multiplier == 0:
@@ -43,6 +43,7 @@ async def get_readings(self, extra: Optional[Dict[str, Any]] = None, **kwargs) -
4343
return {"signal": 1 * self.multiplier}
4444

4545
async def do_command(self, command: Mapping[str, ValueTypes], *, timeout: Optional[float] = None, **kwargs) -> Mapping[str, ValueTypes]:
46+
LOGGER.info(f"received {command=}.")
4647
return command
4748

4849
def reconfigure(self, config: ComponentConfig, dependencies: Mapping[ResourceName, ResourceBase]):
@@ -55,7 +56,7 @@ def reconfigure(self, config: ComponentConfig, dependencies: Mapping[ResourceNam
5556
async def close(self):
5657
# This is a completely optional function to include. This will be called when the resource is removed from the config or the module
5758
# is shutting down.
58-
LOGGER.debug(f"{self.name} is closed.")
59+
LOGGER.info(f"{self.name} is closed.")
5960

6061

6162
async def main():

0 commit comments

Comments
 (0)