Skip to content

Commit

Permalink
Fix the return values of MqttClient's functions
Browse files Browse the repository at this point in the history
  • Loading branch information
lukipuki committed Oct 29, 2023
1 parent 541b460 commit 1e20c49
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
16 changes: 9 additions & 7 deletions src/yaroc/clients/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,27 +73,29 @@ async def send_punch(
code: int,
mode: int,
process_time: datetime | None = None,
):
) -> bool:
punches = Punches()
punches.punches.append(create_punch_proto(card_number, si_time, code, mode, process_time))
punches.sending_timestamp.GetCurrentTime()
await self._send(self.topic_punches, punches.SerializeToString(), qos=1)
return await self._send(self.topic_punches, punches.SerializeToString(), qos=1)

async def send_coords(self, lat: float, lon: float, alt: float, timestamp: datetime):
async def send_coords(self, lat: float, lon: float, alt: float, timestamp: datetime) -> bool:
coords = create_coords_proto(lat, lon, alt, timestamp)
await self._send(self.topic_coords, coords.SerializeToString(), qos=0)
return await self._send(self.topic_coords, coords.SerializeToString(), qos=0)

async def send_mini_call_home(self, mch: MiniCallHome):
async def send_mini_call_home(self, mch: MiniCallHome) -> bool:
status = Status()
status.mini_call_home.CopyFrom(mch)
await self._send(self.topic_status, status.SerializeToString(), qos=0)
return await self._send(self.topic_status, status.SerializeToString(), qos=0)

async def _send(self, topic: str, msg: bytes, qos: int):
async def _send(self, topic: str, msg: bytes, qos: int) -> bool:
try:
await self.client.publish(topic, payload=msg, qos=qos)
logging.info("Message sent")
return True
except MqttCodeError as e:
logging.error(f"Message not sent: {e}")
return False

async def loop(self):
while True:
Expand Down
6 changes: 5 additions & 1 deletion src/yaroc/scripts/send_punch.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ async def udev_events(self):

async def send_mini_call_home(self, mch: MiniCallHome):
handles = [client.send_mini_call_home(mch) for client in self.clients]
await asyncio.gather(*handles)
res = await asyncio.gather(*handles)
if all(res):
logging.info("MiniCallHome sent")
else:
logging.error("MiniCallHome not sent")

def loop(self):
async_loop = asyncio.get_event_loop()
Expand Down

0 comments on commit 1e20c49

Please sign in to comment.