2019-07-16 14:47:03 +02:00
|
|
|
import logging
|
|
|
|
import serial
|
2019-07-17 15:15:43 +02:00
|
|
|
import time
|
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
2019-07-16 14:47:03 +02:00
|
|
|
|
|
|
|
class Gsm:
|
|
|
|
def __init__(self, port, msisdn, imei=None, pin=None):
|
|
|
|
self.port = port
|
2019-07-17 16:31:12 +02:00
|
|
|
logging.info('Initializing Gsm class for port ' + port)
|
2019-07-16 14:47:03 +02:00
|
|
|
try:
|
2019-07-17 16:31:12 +02:00
|
|
|
self.console = serial.Serial(self.port, 115200, timeout=3)
|
|
|
|
if not self.console.isOpen():
|
|
|
|
self.console.open()
|
2019-07-16 14:47:03 +02:00
|
|
|
except:
|
|
|
|
logging.error('Error connecting to ' + self.port)
|
|
|
|
self.msisdn = msisdn
|
2019-07-17 15:15:43 +02:00
|
|
|
#self.cmd("AT+CMGF=1")
|
|
|
|
logging.info('Connection established')
|
2019-07-16 14:47:03 +02:00
|
|
|
self.status = self.check_status()
|
|
|
|
self.unlock_sim(pin)
|
2019-07-17 15:15:43 +02:00
|
|
|
logging.info("Current status: " + str(self.status))
|
2019-07-16 14:47:03 +02:00
|
|
|
if imei is not None:
|
|
|
|
r = self.set_imei(imei)
|
|
|
|
if r:
|
|
|
|
self.imei = imei
|
2019-07-17 15:15:43 +02:00
|
|
|
logging.info("IMEI Changed")
|
2019-07-16 14:47:03 +02:00
|
|
|
|
|
|
|
def cmd(self, cmd):
|
2019-07-17 15:15:43 +02:00
|
|
|
cmd += "\r\n"
|
2019-07-16 14:47:03 +02:00
|
|
|
self.console.write(cmd.encode("ascii"))
|
2019-07-17 16:31:12 +02:00
|
|
|
time.sleep(1)
|
|
|
|
output = self.console.readlines()
|
|
|
|
return ''.join(l.decode('ascii') for l in output)
|
2019-07-16 14:47:03 +02:00
|
|
|
|
|
|
|
def check_status(self):
|
|
|
|
r = self.cmd("ATI")
|
|
|
|
if "OK" in r:
|
|
|
|
return True
|
2019-07-16 15:12:57 +02:00
|
|
|
else:
|
2019-07-16 14:47:03 +02:00
|
|
|
return False
|
|
|
|
|
|
|
|
def unlock_sim(self, pin):
|
|
|
|
status = self.cmd("AT+CPIN?")
|
2019-07-17 16:31:12 +02:00
|
|
|
logging.info(status)
|
|
|
|
|
|
|
|
if "+CPIN:READY" in status:
|
2019-07-16 14:47:03 +02:00
|
|
|
return True
|
2019-07-17 16:31:12 +02:00
|
|
|
elif "+CME ERROR:10" in status:
|
|
|
|
logging.error("Sim not inserted")
|
|
|
|
return False
|
2019-07-16 14:47:03 +02:00
|
|
|
elif "+CPIN:SIM PIN" in status:
|
|
|
|
auth = self.cmd('AT+CPIN="' + pin + '"')
|
|
|
|
if "OK" in auth:
|
2019-07-17 15:15:43 +02:00
|
|
|
logging.info("Pin correct")
|
2019-07-16 14:47:03 +02:00
|
|
|
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
|
2019-07-16 15:12:57 +02:00
|
|
|
else:
|
2019-07-16 14:47:03 +02:00
|
|
|
return False
|
|
|
|
|
2019-07-16 15:12:57 +02:00
|
|
|
def fetch_messages(self):
|
2019-07-16 14:47:03 +02:00
|
|
|
r = self.cmd("AT+CMGL=REC UNREAD")
|
|
|
|
print(r)
|
|
|
|
|
|
|
|
def delete_messages(self):
|
|
|
|
r = self.cmd("")
|
|
|
|
|
|
|
|
def parse_message(self, message):
|
|
|
|
return True
|