maj du code de l'intercom
This commit is contained in:
@@ -12,6 +12,7 @@ import sys, os, re
|
||||
import serial
|
||||
import logging as log
|
||||
import time
|
||||
from datetime import datetime
|
||||
|
||||
###################################################################
|
||||
# Class et Methods #
|
||||
@@ -38,7 +39,7 @@ def send_at_cmd(cmd='', serObj=None, log=None):
|
||||
time.sleep(.5)
|
||||
out = ''
|
||||
outlst = []
|
||||
while serObj.inWaiting() > 0:
|
||||
while serObj.in_waiting > 0:
|
||||
c = serObj.read(1).decode('utf-8')
|
||||
if c == '\r' or c == '\n':
|
||||
if out != '':
|
||||
@@ -59,6 +60,14 @@ def send_at_cmd(cmd='', serObj=None, log=None):
|
||||
|
||||
def init_com(serObj=None, log=None):
|
||||
''' Init GSM Communication
|
||||
source : SIM800_Series_AT_command Manual_v1.09
|
||||
AT : test command
|
||||
AT+CPIN? : Enter PIN (response READY: MT is not pending for any password
|
||||
AT+CREG? : Network registration - return the status of result code
|
||||
AT+CLIP=1 : The calling line identifty (CLI) of calling party when
|
||||
receiving a mobile terminated call
|
||||
AT+VTD=1 : Tone Duration (in 1/10 seconds)
|
||||
ATS0=2 : Set Number of Ring before Automatically Answering the call
|
||||
|
||||
:param serObj:
|
||||
serial object
|
||||
@@ -68,7 +77,7 @@ def init_com(serObj=None, log=None):
|
||||
|
||||
:return bool:
|
||||
'''
|
||||
cmd_lst = ['AT', 'AT+CPIN?', 'AT+CREG?', 'AT+CLIP=1', 'AT+VTD=1']
|
||||
cmd_lst = ['AT', 'AT+CPIN?', 'AT+CREG?', 'AT+CLIP=1', 'AT+VTD=1', 'ATS0=2']
|
||||
for cmd in cmd_lst:
|
||||
ret = send_at_cmd(cmd=cmd, serObj=serObj, log=log)
|
||||
if not ret:
|
||||
@@ -77,6 +86,25 @@ def init_com(serObj=None, log=None):
|
||||
return False
|
||||
return True
|
||||
|
||||
def verify_caller(buf="", log=None):
|
||||
''' Verify phone number of caller
|
||||
example : +CLIP: "0607297154",129,"",0,"",0
|
||||
|
||||
:param buf:
|
||||
Serial input buffer
|
||||
|
||||
:param log:
|
||||
logger object
|
||||
|
||||
:return bool:
|
||||
True if authorized, False otherwise
|
||||
'''
|
||||
|
||||
outlst = buf[7:].split(',')
|
||||
log.debug("=> {}".format(outlst))
|
||||
phone_number = outlst[0].replace("\"","")
|
||||
return True, phone_number
|
||||
|
||||
###################################################################
|
||||
# Corps principal du programme #
|
||||
|
||||
@@ -94,7 +122,7 @@ def main():
|
||||
parity=serial.PARITY_NONE,
|
||||
stopbits=serial.STOPBITS_ONE,
|
||||
bytesize=serial.EIGHTBITS,
|
||||
timeout=1, write_timeout=2,
|
||||
timeout=None, write_timeout=2,
|
||||
xonxoff=False,
|
||||
rtscts=False,
|
||||
dsrdtr=False)
|
||||
@@ -104,9 +132,28 @@ def main():
|
||||
try:
|
||||
ser.flushInput() #flush input buffer, discarding all its contents
|
||||
ser.flushOutput() #flush output buffer, aborting current output
|
||||
# Initialize GSM communication
|
||||
ret = init_com(serObj=ser, log=logger)
|
||||
if not ret:
|
||||
logger.error("Erreur d'initialisation de la communication avec le module GSM")
|
||||
return
|
||||
out = ''
|
||||
while True:
|
||||
# While the number of bytes in the input buffer > 0
|
||||
while ser.in_waiting > 0:
|
||||
# remove \r and \n chars from out string
|
||||
out += ser.read_until().decode('utf-8').replace('\r','').replace('\n','')
|
||||
if len(out) > 0 :
|
||||
logger.debug("out: {}".format(out))
|
||||
time.sleep(.1)
|
||||
if out.startswith('+CLIP: '):
|
||||
# Verify caller phone number
|
||||
ret, phone_number = verify_caller(buf=out, log=logger)
|
||||
if not ret:
|
||||
# Disconnect connexion
|
||||
send_at_cmd(cmd="ATH", serObj=ser, log=log)
|
||||
logger.warning("Phone number not authorized : {}".format(phone_number))
|
||||
out = ''
|
||||
except Exception as e:
|
||||
logger.error("error communicating: {}".format(e))
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user