Skip to content

Commit fbc6a60

Browse files
authored
[RSDK-2227] Add BaseResource protocol and conformances (#234)
1 parent a094929 commit fbc6a60

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+345
-265
lines changed

examples/module/src/gizmo/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
This file registers the Gizmo subtype with the Viam Registry, as well as the specific MyGizmo model.
33
"""
44

5-
from viam.resource.registry import ComponentRegistration, Registry
5+
from viam.resource.registry import ResourceRegistration, Registry
66

77
from .api import Gizmo, GizmoClient, GizmoService
88
from .my_gizmo import MyGizmo
99

10-
Registry.register_subtype(ComponentRegistration(Gizmo, GizmoService, lambda name, channel: GizmoClient(name, channel)))
10+
Registry.register_subtype(ResourceRegistration(Gizmo, GizmoService, lambda name, channel: GizmoClient(name, channel)))
1111

1212
Registry.register_component_model(Gizmo.SUBTYPE, MyGizmo.MODEL, MyGizmo.new)

examples/server/v1/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
from viam.components.base import Base, Vector3
77
from viam.components.camera import Camera
88
from viam.components.motor import Motor
9+
from viam.media.video import CameraMimeType
910
from viam.robot.client import RobotClient
1011
from viam.rpc.dial import DialOptions
1112

1213

1314
async def client():
1415
opts = RobotClient.Options(dial_options=DialOptions(insecure=True))
1516
async with await RobotClient.at_address("localhost:9090", opts) as robot:
16-
1717
print("\n#### RESOURCES ####")
1818
print(f"Resources: {robot.resource_names}")
1919

@@ -33,7 +33,7 @@ async def client():
3333

3434
print("\n#### CAMERA ####")
3535
camera = Camera.from_robot(robot, "camera0")
36-
img = await camera.get_image()
36+
img = await camera.get_image(mime_type=CameraMimeType.PNG)
3737
assert isinstance(img, Image)
3838
img.show()
3939
await asyncio.sleep(1)

src/viam/components/arm/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import asyncio
22
from viam.proto.component.arm import Status as ArmStatus
33
from viam.proto.robot import Status
4-
from viam.resource.registry import ComponentRegistration, Registry
4+
from viam.resource.registry import ResourceRegistration, Registry
55
from viam.utils import message_to_struct
66

77
from .arm import Arm, JointPositions, Pose, WorldState
@@ -34,4 +34,4 @@ async def create_status(component: Arm) -> Status:
3434
return Status(name=Arm.get_resource_name(component.name), status=message_to_struct(s))
3535

3636

37-
Registry.register_subtype(ComponentRegistration(Arm, ArmService, lambda name, channel: ArmClient(name, channel), create_status))
37+
Registry.register_subtype(ResourceRegistration(Arm, ArmService, lambda name, channel: ArmClient(name, channel), create_status))

src/viam/components/audio_input/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from viam.resource.registry import ComponentRegistration, Registry
1+
from viam.resource.registry import ResourceRegistration, Registry
22

33
from .audio_input import AudioInput
44
from .client import AudioInputClient
@@ -10,7 +10,7 @@
1010

1111

1212
Registry.register_subtype(
13-
ComponentRegistration(
13+
ResourceRegistration(
1414
AudioInput,
1515
AudioInputService,
1616
lambda name, channel: AudioInputClient(name, channel),

src/viam/components/audio_input/audio_input.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@
1515

1616

1717
class AudioInput(ComponentBase, MediaSource[Audio]):
18+
"""AudioInput represents a component that can capture audio.
19+
20+
This acts as an abstract base class for any drivers representing specific
21+
audio input implementations. This cannot be used on its own. If the ``__init__()`` function is
22+
overridden, it must call the ``super().__init__()`` function.
23+
"""
24+
1825
SUBTYPE: Final = Subtype(RESOURCE_NAMESPACE_RDK, RESOURCE_TYPE_COMPONENT, "audio_input")
1926

2027
@dataclass

src/viam/components/base/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from viam.resource.registry import ComponentRegistration, Registry
1+
from viam.resource.registry import ResourceRegistration, Registry
22
from viam.proto.common import ActuatorStatus
33
from viam.proto.robot import Status
44
from viam.utils import message_to_struct
@@ -15,4 +15,4 @@ async def create_status(component: Base) -> Status:
1515
return Status(name=Base.get_resource_name(component.name), status=message_to_struct(s))
1616

1717

18-
Registry.register_subtype(ComponentRegistration(Base, BaseService, lambda name, channel: BaseClient(name, channel), create_status))
18+
Registry.register_subtype(ResourceRegistration(Base, BaseService, lambda name, channel: BaseClient(name, channel), create_status))

src/viam/components/board/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from viam.proto.robot import Status
2-
from viam.resource.registry import ComponentRegistration, Registry
2+
from viam.resource.registry import ResourceRegistration, Registry
33
from viam.utils import message_to_struct
44

55
from .board import Board
@@ -15,4 +15,4 @@ async def create_status(component: Board) -> Status:
1515
return Status(name=Board.get_resource_name(component.name), status=message_to_struct(await component.status()))
1616

1717

18-
Registry.register_subtype(ComponentRegistration(Board, BoardService, lambda name, channel: BoardClient(name, channel), create_status))
18+
Registry.register_subtype(ResourceRegistration(Board, BoardService, lambda name, channel: BoardClient(name, channel), create_status))

src/viam/components/camera/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from viam.resource.registry import ComponentRegistration, Registry
1+
from viam.resource.registry import ResourceRegistration, Registry
22

33
from .camera import Camera, DistortionParameters, IntrinsicParameters, RawImage
44
from .client import CameraClient
@@ -12,7 +12,7 @@
1212
]
1313

1414
Registry.register_subtype(
15-
ComponentRegistration(
15+
ResourceRegistration(
1616
Camera,
1717
CameraService,
1818
lambda name, channel: CameraClient(name, channel),
Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
import abc
2-
from typing import TYPE_CHECKING, Any, SupportsBytes, ClassVar, SupportsFloat, List, Mapping, Optional, cast, Union
2+
from typing import (
3+
TYPE_CHECKING,
4+
Any,
5+
ClassVar,
6+
List,
7+
Mapping,
8+
Optional,
9+
SupportsBytes,
10+
SupportsFloat,
11+
Union,
12+
cast,
13+
)
314

415
from typing_extensions import Self
516

617
from viam.operations import Operation
7-
from viam.proto.common import ResourceName
18+
from viam.resource.types import ResourceBase
819

920
if TYPE_CHECKING:
1021
from viam.resource.types import Subtype
@@ -14,34 +25,17 @@
1425
ValueTypes = Union[bool, SupportsBytes, SupportsFloat, List, Mapping, str, None]
1526

1627

17-
class ComponentBase(abc.ABC):
28+
class ComponentBase(abc.ABC, ResourceBase):
1829
"""
1930
Base component.
2031
All components must inherit from this class.
2132
"""
2233

2334
SUBTYPE: ClassVar["Subtype"]
2435

25-
name: str
26-
2736
def __init__(self, name: str):
2837
self.name = name
2938

30-
@classmethod
31-
def get_resource_name(cls, name: str) -> ResourceName:
32-
"""
33-
Get the ResourceName for this component type with the given name
34-
35-
Args:
36-
name (str): The name of the Component
37-
"""
38-
return ResourceName(
39-
namespace=cls.SUBTYPE.namespace,
40-
type=cls.SUBTYPE.resource_type,
41-
subtype=cls.SUBTYPE.resource_subtype,
42-
name=name,
43-
)
44-
4539
@classmethod
4640
def from_robot(cls, robot: "RobotClient", name: str) -> Self:
4741
"""Get the component named ``name`` from the provided robot.
@@ -72,15 +66,4 @@ def get_operation(self, kwargs: Mapping[str, Any]) -> Operation:
7266
return kwargs.get(Operation.ARG_NAME, Operation._noop())
7367

7468
async def do_command(self, command: Mapping[str, ValueTypes], *, timeout: Optional[float] = None, **kwargs) -> Mapping[str, ValueTypes]:
75-
"""Send/Receive arbitrary commands
76-
77-
Args:
78-
command (Mapping[str, ValueTypes]): The command to execute
79-
80-
Raises:
81-
NotImplementedError: Raised if the component does not support arbitrary commands
82-
83-
Returns:
84-
Mapping[str, ValueTypes]: Result of the executed command
85-
"""
8669
raise NotImplementedError()

src/viam/components/gantry/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import asyncio
22
from viam.proto.component.gantry import Status as GantryStatus
33
from viam.proto.robot import Status
4-
from viam.resource.registry import ComponentRegistration, Registry
4+
from viam.resource.registry import ResourceRegistration, Registry
55
from viam.utils import message_to_struct
66

77
from .client import GantryClient
@@ -19,4 +19,4 @@ async def create_status(component: Gantry) -> Status:
1919
return Status(name=Gantry.get_resource_name(component.name), status=message_to_struct(s))
2020

2121

22-
Registry.register_subtype(ComponentRegistration(Gantry, GantryService, lambda name, channel: GantryClient(name, channel), create_status))
22+
Registry.register_subtype(ResourceRegistration(Gantry, GantryService, lambda name, channel: GantryClient(name, channel), create_status))

0 commit comments

Comments
 (0)