Skip to content
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
13 changes: 3 additions & 10 deletions examples/module/src/main.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
import asyncio
import sys

from viam.module.module import Module

from .gizmo import Gizmo, MyGizmo
from .summation import MySummationService, SummationService


async def main(address: str):
async def main():
"""This function creates and starts a new module, after adding all desired resources.
Resources must be pre-registered. For an example, see the `gizmo.__init__.py` file.

Args:
address (str): The address to serve the module on
"""

module = Module(address)
module = Module.from_args()
module.add_model_from_registry(Gizmo.SUBTYPE, MyGizmo.MODEL)
module.add_model_from_registry(SummationService.SUBTYPE, MySummationService.MODEL)
await module.start()


if __name__ == "__main__":
if len(sys.argv) < 2:
raise Exception("Need socket path as command line argument")

asyncio.run(main(sys.argv[1]))
asyncio.run(main())
24 changes: 23 additions & 1 deletion src/viam/module/module.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import sys
from inspect import iscoroutinefunction
from threading import Lock
from typing import List, Mapping, Optional, Sequence, Tuple
from typing_extensions import Self

from grpclib.utils import _service_name

Expand Down Expand Up @@ -42,7 +44,27 @@ class Module:
parent: Optional[RobotClient] = None
server: Server

def __init__(self, address: str, *, log_level: int = logging.DEBUG) -> None:
@classmethod
def from_args(cls) -> Self:
"""Create a new Module with the args provided in the command line. The first argument after the command must be
the socket path. If the second argument after the command is "--log-level=debug", the Module's logger will be
DEBUG level. Otherwise, it will be INFO level. See LogLevel documentation in the RDK for more information on how
to start modules with a "log-level" commandline argument.

Raises:
Exception: If there is no socket path provided in the command line argument

Returns:
Module: a new Module instance
"""
args = sys.argv
if len(args) < 2:
raise Exception("Need socket path as command line argument")
address = args[1]
log_level = logging.DEBUG if (len(args) == 3 and "=debug" in args[2].lower()) else logging.INFO
return cls(address, log_level=log_level)

def __init__(self, address: str, *, log_level: int = logging.INFO) -> None:
self._address = address
self.server = Server(resources=[], module_service=ModuleRPCService(self))
self._log_level = log_level
Expand Down
27 changes: 0 additions & 27 deletions tests/mocks/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,33 +168,6 @@ async def move_straight(
self.extra = extra
self.timeout = timeout

async def move_arc(
self,
distance: int,
velocity: float,
angle: float,
*,
extra: Optional[Dict[str, Any]] = None,
timeout: Optional[float] = None,
**kwargs,
):
if distance == 0:
return await self.spin(angle, velocity)

if velocity == 0:
return await self.stop()

if velocity > 0:
self.position += distance
self.angle += angle
else:
self.position -= distance
self.angle -= angle

self.stopped = False
self.extra = extra
self.timeout = timeout

async def spin(
self, angle: float, velocity: float, *, extra: Optional[Dict[str, Any]] = None, timeout: Optional[float] = None, **kwargs
):
Expand Down
5 changes: 0 additions & 5 deletions tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,6 @@ async def test_stop(self, base: MockBase):
await base.move_straight(0, 0)
assert base.stopped is True

await base.move_arc(1, 1, 1)
assert base.stopped is False
await base.move_arc(0, 0, 0)
assert base.stopped is True

await base.spin(1, 1)
assert base.stopped is False
await base.spin(0, 0)
Expand Down