import logging import serial class Gsm: def __init__(self, port, msisdn, imei=None, pin=None): self.port = port logging.info('Initializing G clsm 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.status = self.check_status() self.unlock_sim(pin) loggin.info("Current status: " + self.status) if imei is not None: r = self.set_imei(imei) if r: self.imei = imei loggin.info("IMEI Changed") def cmd(self, cmd): self.console.write(cmd.encode("ascii")) return list(map(lambda elem: elem.decode("ascii", errors="replace"), port.readlines())) def check_status(self): r = self.cmd("ATI") if "OK" in r: return True else: return False def unlock_sim(self, pin): status = self.cmd("AT+CPIN?") if status == "+CPIN:READY": return True elif "+CPIN:SIM PIN" in status: auth = self.cmd('AT+CPIN="' + pin + '"') if "OK" in auth: loggin.info("Pin correct") return True else: logging.error("Wrong PIN") return False else: logging.error("Unknown PIN error") return False def set_imei(self, imei): r = self.cmd('AT+EMGR=1,7,"' + imei + '"') if "OK" in r: return True else: return False def fetch_messages(self): r = self.cmd("AT+CMGL=REC UNREAD") print(r) def delete_messages(self): r = self.cmd("") def parse_message(self, message): return True