Add retries to the key functions

This commit is contained in:
root 2019-05-12 23:26:50 +02:00
parent 6334bbda32
commit d7f9b7ae71

View File

@ -1,6 +1,7 @@
import logging import logging
import os import os
import time
from pydbus import SystemBus from pydbus import SystemBus
from gi.repository import GLib from gi.repository import GLib
@ -31,12 +32,21 @@ def assemble_sms():
return ''.join(str_list) return ''.join(str_list)
def get_modem(): def get_modem():
# Contact ModemManager, unlock SIM and send SMS to emergency numbers # Retry for 5 seconds and give the modems time
with SystemBus() as bus: retries = 0
mm = bus.get('.ModemManager1') while retries < 5:
modems = mm.GetManagedObjects() 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: if not modems or len(modems) == 0:
raise ModemManagerError('No modems found') raise ModemManagerError('No modems found')
modem_path = list(modems.keys())[0] modem_path = list(modems.keys())[0]
logger.info("Going with %s", modem_path) logger.info("Going with %s", modem_path)
@ -67,18 +77,35 @@ def send_sms(recipients, smsstring):
# Assemble SMS # Assemble SMS
logger.info("Sending SMS: %s", smsstring) logger.info("Sending SMS: %s", smsstring)
# Get modem, unlock SIM retries = 0
modem = get_modem() # Retry the whole block, because once pydbus fetches the properties
unlock_sim(modem) # it does not really refresh them, including modems
while True:
# enable the modem try:
modem.Enable(True) # 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 # test sending SMS
modem_msg = modem['org.freedesktop.ModemManager1.Modem.Messaging'] modem_msg = modem['org.freedesktop.ModemManager1.Modem.Messaging']
smses = modem_msg.List() 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: for line in recipients:
line = line.strip() line = line.strip()