Skip to content

Commit

Permalink
Make ModemManager more bulletproof
Browse files Browse the repository at this point in the history
It should not be used on Windows. Also if the setup fails, we should log
it and continue even without it.
  • Loading branch information
lukipuki committed Jun 9, 2024
1 parent 452114a commit 4827f4f
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions python/yaroc/clients/mqtt.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import math
import random
import sys
from asyncio import Lock, sleep
from dataclasses import dataclass
from datetime import timedelta
Expand All @@ -10,12 +11,12 @@
from aiomqtt import MqttCodeError, MqttError
from aiomqtt.client import Will

from .. import utils
from ..pb.punches_pb2 import Punch, Punches
from ..pb.status_pb2 import Disconnected, Status
from ..pb.utils import create_punch_proto
from ..rs import SiPunchLog
from ..utils.async_serial import AsyncATCom
from ..utils.modem_manager import ModemManager, NetworkType
from ..utils.retries import BackoffBatchedRetries
from ..utils.sim7020 import SIM7020Interface
from .client import Client
Expand Down Expand Up @@ -55,6 +56,7 @@ def __init__(
self.mac_addr = mac_addr
self.broker_url = BROKER_URL if broker_url is None else broker_url
self.broker_port = BROKER_PORT if broker_port is None else broker_port
self.mm = None

disconnected = Disconnected()
disconnected.client_name = self.name
Expand Down Expand Up @@ -96,7 +98,7 @@ async def send_punch(

async def send_status(self, status: Status, mac_addr: str) -> bool:
try:
if status.WhichOneof("msg") == "mini_call_home":
if status.WhichOneof("msg") == "mini_call_home" and self.mm is not None:
modems = await self.mm.get_modems()
if len(modems) > 0:
network_state = await self.mm.get_signal(modems[0])
Expand All @@ -109,7 +111,10 @@ async def send_status(self, status: Status, mac_addr: str) -> bool:
cellid = await self.mm.get_cellid(modems[0])
if cellid is not None:
status.mini_call_home.cellid = cellid
if network_state.type == NetworkType.Unknown and random.randint(0, 4) == 2:
if (
network_state.type == utils.modem_manager.NetworkType.Unknown
and random.randint(0, 4) == 2
):
await self.mm.signal_setup(modems[0], 20)
except Exception as e:
logging.error(f"Error while getting signal strength: {e}")
Expand All @@ -127,7 +132,12 @@ async def _send(self, topic: str, msg: bytes, qos: int, message_type: str):
return False

async def loop(self):
self.mm = await ModemManager.new()
try:
if sys.platform == "linux":
self.mm = await utils.modem_manager.ModemManager.new()
except Exception as err:
logging.error(f"Error while setting up modem manager: {err}")

while True:
try:
async with self.client:
Expand Down Expand Up @@ -207,7 +217,7 @@ async def send_status(self, status: Status, mac_addr: str) -> bool:
mch.signal_dbm = rssi_dbm
mch.signal_snr = snr
mch.cellid = cellid
mch.network_type = NetworkType.NbIot.value
mch.network_type = utils.modem_manager.NetworkType.NbIot.value

return await self._send(self.topics.status, status.SerializeToString(), "MiniCallHome")

Expand Down

0 comments on commit 4827f4f

Please sign in to comment.