|
| 1 | +from typing import ClassVar, Mapping, Sequence, cast |
| 2 | + |
| 3 | +from typing_extensions import Self |
| 4 | + |
| 5 | +from viam.components.generic import Generic |
| 6 | +from viam.components.motor import Motor |
| 7 | +from viam.module.types import Reconfigurable |
| 8 | +from viam.proto.app.robot import ComponentConfig |
| 9 | +from viam.resource.base import ResourceBase |
| 10 | +from viam.proto.common import ResourceName |
| 11 | +from viam.resource.registry import Registry, ResourceCreatorRegistration |
| 12 | +from viam.resource.types import Model, ModelFamily |
| 13 | +from viam.utils import struct_to_dict |
| 14 | +from viam.module.module import Module |
| 15 | +import asyncio |
| 16 | + |
| 17 | + |
| 18 | +class Foo(Generic, Reconfigurable): |
| 19 | + MODEL: ClassVar[Model] = Model(ModelFamily("acme", "demo"), "foo") |
| 20 | + |
| 21 | + def __init__(self, name: str): |
| 22 | + super().__init__(name) |
| 23 | + |
| 24 | + @classmethod |
| 25 | + def new(cls, config: ComponentConfig, dependencies: Mapping[ResourceName, ResourceBase]) -> Self: |
| 26 | + foo = cls(config.name) |
| 27 | + foo.reconfigure(config, dependencies) |
| 28 | + return foo |
| 29 | + |
| 30 | + # Validate validates the config and returns a required dependency on |
| 31 | + # `required_motor` and an optional dependency on `optional_motor`. |
| 32 | + @classmethod |
| 33 | + def validate_config(cls, config: ComponentConfig) -> tuple[Sequence[str], Sequence[str]]: |
| 34 | + attributes_dict = struct_to_dict(config.attributes) |
| 35 | + |
| 36 | + cfg_required_motor: str = cast(str, attributes_dict.get("required_motor")) |
| 37 | + cfg_optional_motor: str = cast(str, attributes_dict.get("optional_motor")) |
| 38 | + |
| 39 | + required_deps = [] |
| 40 | + optional_deps = [] |
| 41 | + |
| 42 | + if cfg_required_motor is None or cfg_required_motor == "": |
| 43 | + raise Exception(f'expected "required_motor" attribute for foo {config}') |
| 44 | + |
| 45 | + required_deps.append(cfg_required_motor) |
| 46 | + |
| 47 | + if cfg_optional_motor is not None and cfg_optional_motor != "": |
| 48 | + optional_deps.append(cfg_optional_motor) |
| 49 | + |
| 50 | + return required_deps, optional_deps |
| 51 | + |
| 52 | + # Reconfigure with latest dependencies |
| 53 | + def reconfigure(self, config: ComponentConfig, dependencies: Mapping[ResourceName, ResourceBase]): |
| 54 | + attributes_dict = struct_to_dict(config.attributes) |
| 55 | + |
| 56 | + cfg_required_motor: str = cast(str, attributes_dict.get("required_motor")) |
| 57 | + cfg_optional_motor: str = cast(str, attributes_dict.get("optional_motor")) |
| 58 | + |
| 59 | + required_motor = Motor.get_resource_name(cfg_required_motor) |
| 60 | + if required_motor not in dependencies: |
| 61 | + raise Exception(f"could not get required motor {cfg_required_motor} from dependencies") |
| 62 | + else: |
| 63 | + self.required_motor = required_motor |
| 64 | + |
| 65 | + optional_motor = Motor.get_resource_name(cfg_optional_motor) |
| 66 | + if optional_motor not in dependencies: |
| 67 | + print(f'could not get optional motor {cfg_optional_motor} from dependencies; continuing') |
| 68 | + else: |
| 69 | + self.optional_motor = optional_motor |
| 70 | + |
| 71 | + |
| 72 | +async def main(): |
| 73 | + """This function creates and starts a new module, after adding all desired resource models. |
| 74 | + Resource creators must be registered to the resource registry before the module adds the resource model. |
| 75 | + """ |
| 76 | + |
| 77 | + Registry.register_resource_creator(Generic.API, Foo.MODEL, ResourceCreatorRegistration(Foo.new, Foo.validate_config)) |
| 78 | + |
| 79 | + module = Module.from_args() |
| 80 | + module.add_model_from_registry(Generic.API, Foo.MODEL) |
| 81 | + await module.start() |
| 82 | + |
| 83 | + |
| 84 | +if __name__ == "__main__": |
| 85 | + asyncio.run(main()) |
0 commit comments