From 8b00931169594f1ac3ac6849fce97b66b3f35e9a Mon Sep 17 00:00:00 2001 From: Lei PAN Date: Thu, 3 Mar 2022 15:33:13 +0100 Subject: [PATCH] connect modem one by one to receive sms --- ModemPool.py | 3 +++ main.py | 48 ++++++++++++++++++++++++++++-------------------- 2 files changed, 31 insertions(+), 20 deletions(-) diff --git a/ModemPool.py b/ModemPool.py index 25aaa5d..5b3f473 100644 --- a/ModemPool.py +++ b/ModemPool.py @@ -28,12 +28,15 @@ class ModemPool: self._serial_list.append(ser) def reset_all_modems(self): + print("will reset modem pool") for ser in self._serial_list: # may encontre exception here, multi-access to serial port time.sleep(2) self._send_command("AT+CFUN=1,1\r", ser) # wait for 20 second, so that the modem can init all the sims time.sleep(20) + for ser in self._serial_list: + ser.close() def _generate_error_msg(self, slot_position, index, error: SIMError): msg = "slot({}) SIM({}), error:{}".format(slot_position, index + 1, diff --git a/main.py b/main.py index 9b6a635..27adbf0 100644 --- a/main.py +++ b/main.py @@ -19,11 +19,11 @@ from logs.AppLogging import init_logger from utils.message_receiver import MessageReceiver BAUDRATE = 115200 -OTP_TIMEOUT = 70 +OTP_TIMEOUT = 90 is_finished = False commandor = Commandor() contacts = [] - +current_gsm_modem = None card_pool = CardPool(CARD_POOL_PORT) @@ -70,11 +70,6 @@ def create_modem_for_port(port: str) -> Union[SerialModem, None]: serial_modem = None try: modem = GsmModem(port) - # time.sleep(1) - # modem.connect() - # number = modem.ownNumber - # logger.info("The SIM card phone number is:") - # logger.info(number) return SerialModem(modem=modem) except Exception as ext: print(ext) @@ -87,6 +82,8 @@ def timeout_occurred(serial_modem: SerialModem): def start_to_handle_sms(serial_modem: SerialModem): + global current_gsm_modem + current_gsm_modem = serial_modem.modem serial_modem.modem.smsReceivedCallback = handle_sms global is_finished is_finished = False @@ -101,6 +98,7 @@ def start_to_handle_sms(serial_modem: SerialModem): logger.info("time out for {}, switch to next contact".format(serial_modem.phone_number)) # save the contact in timeout timeout_occurred(serial_modem) + current_gsm_modem.close() return return @@ -110,7 +108,9 @@ def handle_sms(sms): u'== SMS message received ==\nFrom: {0}\nTime: {1}\nMessage:\n{2}\n'.format(sms.number, sms.time, sms.text)) # extract the otp number logger.info("try to extract the otp") - match = re.search(r'\d{6,8}', sms.text) + pattern = r'\d{6,8}' + # if re.match(pattern, sms.text): + match = re.search(pattern, sms.text) otp = match.group(0) logger.info("otp is " + otp) commandor.send_otp(otp) @@ -119,6 +119,9 @@ def handle_sms(sms): while not is_finished: time.sleep(2) is_finished = True + global current_gsm_modem + if current_gsm_modem: + current_gsm_modem.close() def select_sim_storage(ser) -> bool: @@ -173,8 +176,8 @@ def read_all_the_phone_number(): def start_book(): - slot_number = 12 - slot_sum = 31 + slot_number = 7 + slot_sum = 29 for i in range(slot_number, slot_sum + 1): card_pool.reset() print("will switch to " + str(i)) @@ -187,16 +190,21 @@ def start_book(): excel_reader = ExcelHelper() global contacts contacts = excel_reader.read_contacts() - for index, modem in modem_list: - # get contact for current modem - modem.get_ccid() - contact = [contact for contact in contacts if contact.ccid == modem.ccid] - if len(contact) > 0: - modem.phone_number = contact[0].phone - modem.contact = contact[0] - if modem.contact: - commandor.start_page(modem.contact) - start_to_handle_sms(modem) + for modem in modem_list: + try: + # get contact for current modem + modem.get_ccid() + # find the contact with ccid + contact = [contact for contact in contacts if contact.ccid == modem.ccid] + if len(contact) > 0: + modem.phone_number = contact[0].phone + modem.contact = contact[0] + if modem.contact: + commandor.start_page(modem.contact) + start_to_handle_sms(modem) + except Exception as error: + print(error) + continue if __name__ == '__main__':