From f720af174768a488323b3868c6e5ed2b7af09f16 Mon Sep 17 00:00:00 2001 From: Giulio Date: Tue, 16 Jul 2019 14:47:03 +0200 Subject: [PATCH] Gsm class init --- agent/agent.py | 0 agent/config/gsm.json | 3 +- agent/gsm/__init__.py | 66 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 agent/agent.py create mode 100644 agent/gsm/__init__.py diff --git a/agent/agent.py b/agent/agent.py new file mode 100644 index 0000000..e69de29 diff --git a/agent/config/gsm.json b/agent/config/gsm.json index bc0f418..8590d79 100644 --- a/agent/config/gsm.json +++ b/agent/config/gsm.json @@ -6,7 +6,8 @@ }, { "dev":"/dev/ttyUSB1", - "msisdn":"+443312123452" + "msisdn":"+443312123452", + "pin": "12345" }, { "dev":"/dev/ttyUSB2", diff --git a/agent/gsm/__init__.py b/agent/gsm/__init__.py new file mode 100644 index 0000000..aa49d02 --- /dev/null +++ b/agent/gsm/__init__.py @@ -0,0 +1,66 @@ +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 check_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 \ No newline at end of file