Skip to content

Commit 1b3aba2

Browse files
authored
RSDK-6885 - Add fields to GetPropertiesResponse (#559)
1 parent 8ed2744 commit 1b3aba2

File tree

8 files changed

+50
-27
lines changed

8 files changed

+50
-27
lines changed

src/viam/gen/service/slam/v1/slam_pb2.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,4 @@
4646
_SENSORINFO._serialized_start = 991
4747
_SENSORINFO._serialized_end = 1077
4848
_SLAMSERVICE._serialized_start = 1329
49-
_SLAMSERVICE._serialized_end = 2141
49+
_SLAMSERVICE._serialized_end = 2141

src/viam/gen/service/slam/v1/slam_pb2.pyi

+1-1
Original file line numberDiff line numberDiff line change
@@ -219,4 +219,4 @@ class SensorInfo(google.protobuf.message.Message):
219219

220220
def ClearField(self, field_name: typing_extensions.Literal['name', b'name', 'type', b'type']) -> None:
221221
...
222-
global___SensorInfo = SensorInfo
222+
global___SensorInfo = SensorInfo

src/viam/services/slam/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from viam.proto.common import Pose
2-
from viam.proto.service.slam import MappingMode
2+
from viam.proto.service.slam import MappingMode, SensorInfo
33
from viam.resource.registry import Registry, ResourceRegistration
44

55
from .client import SLAMClient
@@ -9,6 +9,7 @@
99
__all__ = [
1010
"Pose",
1111
"MappingMode",
12+
"SensorInfo",
1213
"SLAMClient",
1314
"SLAM",
1415
]

src/viam/services/slam/client.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import List, Mapping, Optional, Tuple
1+
from typing import List, Mapping, Optional
22

33
from grpclib.client import Channel
44

@@ -11,13 +11,12 @@
1111
GetPositionRequest,
1212
GetPositionResponse,
1313
GetPropertiesRequest,
14-
GetPropertiesResponse,
1514
SLAMServiceStub,
1615
)
1716
from viam.resource.rpc_client_base import ReconfigurableResourceRPCClientBase
1817
from viam.utils import ValueTypes, dict_to_struct, struct_to_dict
1918

20-
from . import MappingMode, Pose
19+
from . import Pose
2120
from .slam import SLAM
2221

2322

@@ -48,10 +47,9 @@ async def get_internal_state(self, *, timeout: Optional[float] = None) -> List[b
4847
response: List[GetInternalStateResponse] = await self.client.GetInternalState(request, timeout=timeout)
4948
return [r.internal_state_chunk for r in response]
5049

51-
async def get_properties(self, *, timeout: Optional[float] = None) -> Tuple[bool, MappingMode.ValueType]:
50+
async def get_properties(self, *, timeout: Optional[float] = None) -> SLAM.Properties:
5251
request = GetPropertiesRequest(name=self.name)
53-
response: GetPropertiesResponse = await self.client.GetProperties(request, timeout=timeout)
54-
return (response.cloud_slam, response.mapping_mode)
52+
return await self.client.GetProperties(request, timeout=timeout)
5553

5654
async def do_command(self, command: Mapping[str, ValueTypes], *, timeout: Optional[float] = None, **__) -> Mapping[str, ValueTypes]:
5755
request = DoCommandRequest(name=self.name, command=dict_to_struct(command))

src/viam/services/slam/service.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,8 @@ async def GetProperties(self, stream: Stream[GetPropertiesRequest, GetProperties
6262
assert request is not None
6363
slam = self.get_resource(request.name)
6464
timeout = stream.deadline.time_remaining() if stream.deadline else None
65-
(cloud_slam, mapping_mode) = await slam.get_properties(timeout=timeout)
66-
response = GetPropertiesResponse(cloud_slam=cloud_slam, mapping_mode=mapping_mode)
67-
await stream.send_message(response)
65+
properties = await slam.get_properties(timeout=timeout)
66+
await stream.send_message(properties)
6867

6968
async def DoCommand(self, stream: Stream[DoCommandRequest, DoCommandResponse]) -> None:
7069
request = await stream.recv_message()

src/viam/services/slam/slam.py

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
import abc
2-
from typing import Final, List, Optional, Tuple
2+
import sys
3+
from typing import Final, List, Optional
34

5+
from viam.proto.service.slam import GetPropertiesResponse
46
from viam.resource.types import RESOURCE_NAMESPACE_RDK, RESOURCE_TYPE_SERVICE, Subtype
57

68
from ..service_base import ServiceBase
7-
from . import MappingMode, Pose
9+
from . import Pose
10+
11+
if sys.version_info >= (3, 10):
12+
from typing import TypeAlias
13+
else:
14+
from typing_extensions import TypeAlias
815

916

1017
class SLAM(ServiceBase):
@@ -18,6 +25,8 @@ class SLAM(ServiceBase):
1825

1926
SUBTYPE: Final = Subtype(RESOURCE_NAMESPACE_RDK, RESOURCE_TYPE_SERVICE, "slam") # pyright: ignore [reportIncompatibleVariableOverride]
2027

28+
Properties: "TypeAlias" = GetPropertiesResponse
29+
2130
@abc.abstractmethod
2231
async def get_internal_state(self, *, timeout: Optional[float]) -> List[bytes]:
2332
"""
@@ -71,7 +80,7 @@ async def get_position(self, *, timeout: Optional[float]) -> Pose:
7180
...
7281

7382
@abc.abstractmethod
74-
async def get_properties(self, *, timeout: Optional[float]) -> Tuple[bool, MappingMode.ValueType]:
83+
async def get_properties(self, *, timeout: Optional[float]) -> Properties:
7584
"""
7685
Get information regarding the current SLAM session.
7786
@@ -83,7 +92,6 @@ async def get_properties(self, *, timeout: Optional[float]) -> Tuple[bool, Mappi
8392
slam_properties = await slam_svc.get_properties()
8493
8594
Returns:
86-
Tuple[bool, MappingMode.ValueType]: A tuple of a boolean value representing if the SLAM session is being run in
87-
the cloud and the mapping mode of said session
95+
Properties: The properties of SLAM
8896
"""
8997
...

tests/mocks/services.py

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, Dict, List, Mapping, Optional, Sequence, Tuple, Union
1+
from typing import Any, Dict, List, Mapping, Optional, Sequence, Union
22

33
import numpy as np
44
from grpclib.server import Stream
@@ -306,7 +306,7 @@
306306
Readings,
307307
SensorsServiceBase,
308308
)
309-
from viam.proto.service.slam import MappingMode
309+
from viam.proto.service.slam import MappingMode, SensorInfo, SensorType
310310
from viam.proto.service.vision import Classification, Detection
311311
from viam.services.generic import Generic as GenericService
312312
from viam.services.mlmodel import File, LabelType, Metadata, MLModel, TensorInfo
@@ -603,10 +603,21 @@ class MockSLAM(SLAM):
603603
POSITION = Pose(x=1, y=2, z=3, o_x=2, o_y=3, o_z=4, theta=20)
604604
CLOUD_SLAM = False
605605
MAPPING_MODE = MappingMode.MAPPING_MODE_UNSPECIFIED
606+
INTERNAL_STATE_FILE_TYPE = ".pbstream"
607+
SENSOR_INFO = [
608+
SensorInfo(name="my-camera", type=SensorType.SENSOR_TYPE_CAMERA),
609+
SensorInfo(name="my-movement-sensor", type=SensorType.SENSOR_TYPE_MOVEMENT_SENSOR),
610+
]
606611

607612
def __init__(self, name: str):
608613
self.name = name
609614
self.timeout: Optional[float] = None
615+
self.properties = SLAM.Properties(
616+
cloud_slam=self.CLOUD_SLAM,
617+
mapping_mode=self.MAPPING_MODE,
618+
internal_state_file_type=self.INTERNAL_STATE_FILE_TYPE,
619+
sensor_info=self.SENSOR_INFO
620+
)
610621
super().__init__(name)
611622

612623
async def get_internal_state(self, *, timeout: Optional[float] = None) -> List[bytes]:
@@ -621,9 +632,9 @@ async def get_position(self, *, timeout: Optional[float] = None) -> Pose:
621632
self.timeout = timeout
622633
return self.POSITION
623634

624-
async def get_properties(self, *, timeout: Optional[float] = None) -> Tuple[bool, MappingMode.ValueType]:
635+
async def get_properties(self, *, timeout: Optional[float] = None) -> SLAM.Properties:
625636
self.timeout = timeout
626-
return (self.CLOUD_SLAM, self.MAPPING_MODE)
637+
return self.properties
627638

628639
async def do_command(self, command: Mapping[str, ValueTypes], *, timeout: Optional[float] = None, **kwargs) -> Mapping[str, ValueTypes]:
629640
return {"command": command}

tests/test_slam.py

+12-6
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,11 @@ async def test_do(self):
4949

5050
@pytest.mark.asyncio
5151
async def test_get_properties(self):
52-
(cloud_slam, mapping_mode) = await self.slam.get_properties()
53-
assert cloud_slam == MockSLAM.CLOUD_SLAM
54-
assert mapping_mode == MockSLAM.MAPPING_MODE
52+
properties = await self.slam.get_properties()
53+
assert properties.cloud_slam == MockSLAM.CLOUD_SLAM
54+
assert properties.mapping_mode == MockSLAM.MAPPING_MODE
55+
assert properties.internal_state_file_type == MockSLAM.INTERNAL_STATE_FILE_TYPE
56+
assert properties.sensor_info == MockSLAM.SENSOR_INFO
5557

5658

5759
class TestService:
@@ -96,6 +98,8 @@ async def test_get_properties(self):
9698
response: GetPropertiesResponse = await client.GetProperties(request)
9799
assert response.cloud_slam == MockSLAM.CLOUD_SLAM
98100
assert response.mapping_mode == MockSLAM.MAPPING_MODE
101+
assert response.internal_state_file_type == MockSLAM.INTERNAL_STATE_FILE_TYPE
102+
assert response.sensor_info == MockSLAM.SENSOR_INFO
99103

100104
@pytest.mark.asyncio
101105
async def test_do(self):
@@ -145,9 +149,11 @@ async def test_get_position(self):
145149
async def test_get_properties(self):
146150
async with ChannelFor([self.service]) as channel:
147151
client = SLAMClient(self.name, channel)
148-
(cloud_slam, mapping_mode) = await client.get_properties()
149-
assert cloud_slam == MockSLAM.CLOUD_SLAM
150-
assert mapping_mode == MockSLAM.MAPPING_MODE
152+
properties = await client.get_properties()
153+
assert properties.cloud_slam == MockSLAM.CLOUD_SLAM
154+
assert properties.mapping_mode == MockSLAM.MAPPING_MODE
155+
assert properties.internal_state_file_type == MockSLAM.INTERNAL_STATE_FILE_TYPE
156+
assert properties.sensor_info == MockSLAM.SENSOR_INFO
151157

152158
@pytest.mark.asyncio
153159
async def test_do(self):

0 commit comments

Comments
 (0)