From dd08dfd93d707fa319c924fbb4fa3c8fdaa56070 Mon Sep 17 00:00:00 2001 From: Giulio Date: Wed, 17 Jul 2019 15:15:43 +0200 Subject: [PATCH] Various fixes --- agent/agent.py | 4 +++- agent/gsm/__init__.py | 17 +++++++++++------ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/agent/agent.py b/agent/agent.py index 5d8194d..c2dbf33 100644 --- a/agent/agent.py +++ b/agent/agent.py @@ -28,7 +28,9 @@ messages_queue = list() while True: for gsm_object in gsm_modules: messages = gsm_object.fetch_messages() - messages_queue.extend(messages) + if messages: + messages_queue.extend(messages) + messages = None for message in messages_queue: try: diff --git a/agent/gsm/__init__.py b/agent/gsm/__init__.py index b7ac94b..6d57c1c 100644 --- a/agent/gsm/__init__.py +++ b/agent/gsm/__init__.py @@ -1,28 +1,33 @@ import logging import serial +import time +logging.basicConfig(level=logging.DEBUG) class Gsm: def __init__(self, port, msisdn, imei=None, pin=None): self.port = port - logging.info('Initializing G clsm cass for port ' + port) + logging.info('Initializing Gsm cass for port ' + port) try: self.console = serial.Serial(self.port, 112500, timeout=3) except: logging.error('Error connecting to ' + self.port) self.msisdn = msisdn - self.cmd("AT+CMGF=1") + #self.cmd("AT+CMGF=1") + logging.info('Connection established') self.status = self.check_status() self.unlock_sim(pin) - loggin.info("Current status: " + self.status) + logging.info("Current status: " + str(self.status)) if imei is not None: r = self.set_imei(imei) if r: self.imei = imei - loggin.info("IMEI Changed") + logging.info("IMEI Changed") def cmd(self, cmd): + cmd += "\r\n" 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): r = self.cmd("ATI") @@ -39,7 +44,7 @@ class Gsm: elif "+CPIN:SIM PIN" in status: auth = self.cmd('AT+CPIN="' + pin + '"') if "OK" in auth: - loggin.info("Pin correct") + logging.info("Pin correct") return True else: logging.error("Wrong PIN")