Skip to content

Commit

Permalink
add _on_init coro
Browse files Browse the repository at this point in the history
  • Loading branch information
sjev committed Nov 16, 2024
1 parent ec2eea3 commit 31d9bbe
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 11 deletions.
1 change: 0 additions & 1 deletion examples/node_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ def __init__(self) -> None:
super().__init__()

# add coroutines to run in main()
self._coros.append(self._on_init)
self._coros.append(self.talker_coro)

async def _on_init(self) -> None:
Expand Down
3 changes: 2 additions & 1 deletion src/roxbot/adapters/mqtt_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,13 @@ def publish_nowait(self, topic: str, data: JsonSerializableType) -> None:
async def subscribe(self, topic: str) -> None:
"""subscribe to topic"""

self._log.info(f"Subscribing to {topic}")

await asyncio.wait_for(self._client_ready.wait(), timeout=1)

if self._client is None:
raise RuntimeError("MQTT client not initialized")

self._log.info(f"Subscribing to {topic}")
await self._client.subscribe(topic)

async def unsubscribe(self, topic: str) -> None:
Expand Down
12 changes: 9 additions & 3 deletions src/roxbot/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,14 @@ def __init__(self, name: str | None = None) -> None:
self.mqtt = MqttAdapter(parent=self)

# list of coroutines to run in main(). Append to this list in __init__ of derived class. Provide as a reference to the coro, not a call.
self._coros: List[Callable] = [
self.mqtt.main,
]
self._coros: List[Callable] = []

self._main_started = False
self._tasks: List[asyncio.Task] = []

async def _on_init(self) -> None:
"""init coroutine to run in main(), override in derived class"""

async def main(self) -> None:
"""main coroutine"""
self._log.debug("starting main")
Expand All @@ -56,6 +57,11 @@ async def main(self) -> None:

self._main_started = True

# start mqtt
self._tasks.append(asyncio.create_task(self.mqtt.main()))

await self._on_init()

async with asyncio.TaskGroup() as tg:
for coro in self._coros:
self._log.info(f"starting {coro}")
Expand Down
6 changes: 0 additions & 6 deletions tests/test_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,24 @@ async def main(self) -> None:

def test_data_classes():

now = time.time()

latlon = interfaces.GpsLatlon(1, 2)

assert latlon.lat == 1
assert latlon.lon == 2
assert latlon.gps_qual == 0
assert abs(latlon.ts - now) < 1

head = interfaces.GpsHeading(1, 2)
assert head.heading == 1
assert head.heading_stdev == 2
assert abs(head.ts - now) < 1

pos = interfaces.PositionData(1, 2)
assert pos.lat == 1
assert pos.lon == 2
assert pos.x == 0
assert pos.y == 0
assert pos.gps_qual == 0
assert abs(pos.ts - now) < 1

head = interfaces.HeadingData(1, 2, 3)
assert head.heading == 1
assert head.heading_stdev == 2
assert head.theta == 3
assert abs(head.ts - now) < 1

0 comments on commit 31d9bbe

Please sign in to comment.