From d7f9b7ae7111d51d8a79cb36f99b07993ca3b78a Mon Sep 17 00:00:00 2001 From: root Date: Sun, 12 May 2019 23:26:50 +0200 Subject: [PATCH] Add retries to the key functions --- sms_functions.py | 51 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 39 insertions(+), 12 deletions(-) diff --git a/sms_functions.py b/sms_functions.py index a23b211..9be00d1 100644 --- a/sms_functions.py +++ b/sms_functions.py @@ -1,6 +1,7 @@ import logging import os +import time from pydbus import SystemBus from gi.repository import GLib @@ -31,12 +32,21 @@ def assemble_sms(): return ''.join(str_list) def get_modem(): - # Contact ModemManager, unlock SIM and send SMS to emergency numbers - with SystemBus() as bus: - mm = bus.get('.ModemManager1') - modems = mm.GetManagedObjects() + # Retry for 5 seconds and give the modems time + retries = 0 + while retries < 5: + retries += 1 + # Contact ModemManager, unlock SIM and send SMS to emergency numbers + with SystemBus() as bus: + mm = bus.get('.ModemManager1') + modems = mm.GetManagedObjects() + if not modems or len(modems) == 0: + time.sleep(5) + else: + break + if not modems or len(modems) == 0: - raise ModemManagerError('No modems found') + raise ModemManagerError('No modems found') modem_path = list(modems.keys())[0] logger.info("Going with %s", modem_path) @@ -67,18 +77,35 @@ def send_sms(recipients, smsstring): # Assemble SMS logger.info("Sending SMS: %s", smsstring) - # Get modem, unlock SIM - modem = get_modem() - unlock_sim(modem) - - # enable the modem - modem.Enable(True) + retries = 0 + # Retry the whole block, because once pydbus fetches the properties + # it does not really refresh them, including modems + while True: + try: + # enable the modem + # Get modem, unlock SIM + modem = get_modem() + unlock_sim(modem) + modem.Enable(True) + break + except SIMError: + # cannot continue with this one (could be a wrong PIN + # and then we waste our attempts) + raise + except: + retries += 1 + time.sleep(1) + if retries > 5: + raise # test sending SMS modem_msg = modem['org.freedesktop.ModemManager1.Modem.Messaging'] smses = modem_msg.List() - print(modem.GetStatus()['signal-quality']) + while not modem.GetStatus()['signal-quality'][1]: + modem = get_modem() + print(modem.GetStatus()['signal-quality']) + time.sleep(2) for line in recipients: line = line.strip()