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 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()