Gsm class init

This commit is contained in:
Giulio 2019-07-16 14:47:03 +02:00
parent 9a855289a1
commit f720af1747
3 changed files with 68 additions and 1 deletions

0
agent/agent.py Normal file
View File

View File

@ -6,7 +6,8 @@
}, },
{ {
"dev":"/dev/ttyUSB1", "dev":"/dev/ttyUSB1",
"msisdn":"+443312123452" "msisdn":"+443312123452",
"pin": "12345"
}, },
{ {
"dev":"/dev/ttyUSB2", "dev":"/dev/ttyUSB2",

66
agent/gsm/__init__.py Normal file
View File

@ -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