Various fixes

This commit is contained in:
Giulio 2019-07-17 15:15:43 +02:00
parent 83213dbc46
commit dd08dfd93d
2 changed files with 14 additions and 7 deletions

View File

@ -28,7 +28,9 @@ messages_queue = list()
while True: while True:
for gsm_object in gsm_modules: for gsm_object in gsm_modules:
messages = gsm_object.fetch_messages() messages = gsm_object.fetch_messages()
messages_queue.extend(messages) if messages:
messages_queue.extend(messages)
messages = None
for message in messages_queue: for message in messages_queue:
try: try:

View File

@ -1,28 +1,33 @@
import logging import logging
import serial import serial
import time
logging.basicConfig(level=logging.DEBUG)
class Gsm: class Gsm:
def __init__(self, port, msisdn, imei=None, pin=None): def __init__(self, port, msisdn, imei=None, pin=None):
self.port = port self.port = port
logging.info('Initializing G clsm cass for port ' + port) logging.info('Initializing Gsm cass for port ' + port)
try: try:
self.console = serial.Serial(self.port, 112500, timeout=3) self.console = serial.Serial(self.port, 112500, timeout=3)
except: except:
logging.error('Error connecting to ' + self.port) logging.error('Error connecting to ' + self.port)
self.msisdn = msisdn self.msisdn = msisdn
self.cmd("AT+CMGF=1") #self.cmd("AT+CMGF=1")
logging.info('Connection established')
self.status = self.check_status() self.status = self.check_status()
self.unlock_sim(pin) self.unlock_sim(pin)
loggin.info("Current status: " + self.status) logging.info("Current status: " + str(self.status))
if imei is not None: if imei is not None:
r = self.set_imei(imei) r = self.set_imei(imei)
if r: if r:
self.imei = imei self.imei = imei
loggin.info("IMEI Changed") logging.info("IMEI Changed")
def cmd(self, cmd): def cmd(self, cmd):
cmd += "\r\n"
self.console.write(cmd.encode("ascii")) self.console.write(cmd.encode("ascii"))
return list(map(lambda elem: elem.decode("ascii", errors="replace"), port.readlines())) time.sleep(0.5)
return list(map(lambda elem: elem.decode("ascii", errors="replace"), self.console.readlines()))
def check_status(self): def check_status(self):
r = self.cmd("ATI") r = self.cmd("ATI")
@ -39,7 +44,7 @@ class Gsm:
elif "+CPIN:SIM PIN" in status: elif "+CPIN:SIM PIN" in status:
auth = self.cmd('AT+CPIN="' + pin + '"') auth = self.cmd('AT+CPIN="' + pin + '"')
if "OK" in auth: if "OK" in auth:
loggin.info("Pin correct") logging.info("Pin correct")
return True return True
else: else:
logging.error("Wrong PIN") logging.error("Wrong PIN")