Files
appointment_tool/wait_for_sms.py
T
2022-04-14 19:06:11 +02:00

100 lines
3.1 KiB
Python

import logging
import sys
import time
from concurrent.futures import ThreadPoolExecutor
from typing import Union
from gsmmodem import GsmModem
import params
from definitions import SMS_TIMEOUT
from logs.AppLogging import init_logger
from modems.ModemPool import ModemPool
from modems.card_pool import CardPool
from params import MODEM_POOL_PORTS, CARD_POOL_PORT
from pojo.serial_modem import SerialModem
from utils.excel_reader import ExcelHelper
from workers.wait_sms_worker import WaitSmsWorker
thread_event = None
current_gsm_modem = None
card_pool = CardPool(CARD_POOL_PORT)
init_logger()
logger = logging.getLogger()
logger.addHandler(logging.StreamHandler(stream=sys.stdout))
def get_devices_ports() -> list:
return MODEM_POOL_PORTS
def create_modem_for_port(port: str) -> Union[SerialModem, None]:
logger.info('Initializing modem... for ' + port)
serial_modem = None
try:
modem = GsmModem(port)
return SerialModem(modem=modem)
except Exception as ext:
logger.error(ext)
return serial_modem
def timeout_occurred(serial_modem: SerialModem):
logger.info("will close timeout modem")
serial_modem.modem.close()
def init_modems() -> list:
modems = []
for port in get_devices_ports():
serial_modem = create_modem_for_port(port)
if serial_modem:
modems.append(serial_modem)
return modems
def start_waiting_sms():
params.oracle_log_sender.send_wait_sms_log()
start_slot_number = 1
end_slot_sum = 2
slot_list = list(range(start_slot_number, end_slot_sum + 1))
for i in reversed(slot_list):
card_pool.reset()
logger.info("will switch to " + str(i))
card_pool.switch_to_slot(i)
modem_pool = ModemPool(get_devices_ports())
modem_pool.reset_all_modems()
modem_list = init_modems()
# read the contact, and merge the 2 objects together
excel_reader = ExcelHelper()
contacts = excel_reader.read_contacts()
with ThreadPoolExecutor(max_workers=len(MODEM_POOL_PORTS)) as executor:
for modem in modem_list:
try:
modem.get_ccid()
# find the contact with ccid
contact = [contact for contact in contacts if
contact.ccid.replace("F", "") == modem.ccid.replace("F", "")]
if len(contact) > 0:
modem.phone_number = contact[0].phone
modem.contact = contact[0]
if modem.contact:
logger.info("contact found for this ccid")
commandor = WaitSmsWorker(modem)
# start the task in thread
executor.submit(commandor.run())
except Exception as error:
print(error)
continue
listen_at = time.time()
now = time.time()
while (listen_at + SMS_TIMEOUT + 5) > now:
now = time.time()
# print("sleep for 2 s")
time.sleep(2)
print("will call continue")
continue
if __name__ == '__main__':
start_waiting_sms()