From 508f92f78a8cf9672055e4d02afca93f3b8592ec Mon Sep 17 00:00:00 2001 From: Guido Longoni Date: Fri, 27 Sep 2019 15:50:43 +0200 Subject: [PATCH] Caricata prima bozza di libreria di funzioni per la gestione del protocollo --- MkpWIFI/test.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 MkpWIFI/test.py diff --git a/MkpWIFI/test.py b/MkpWIFI/test.py new file mode 100644 index 0000000..54265c4 --- /dev/null +++ b/MkpWIFI/test.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Created on Fri Sep 27 14:35:31 2019 + +@author: Guido Longoni - guidolongoni@gmail.com +""" + +import commands as cmd +import re + +def cmdString(cmdName): + cmdDict = cmd.MKPcommands[cmdName] + return str(cmd.DEFprefix['begin'] if cmdDict['defPrefix'] else cmdDict['begin']) + str(cmdDict['command']) + str(cmd.DEFprefix['end'] if cmdDict['defPrefix'] else cmdDict['end']) + +def checkResponse(cmdName,response): + sucKey = 'success' + cmdDict = cmd.MKPcommands[cmdName] + if sucKey in cmdDict and cmdDict[sucKey] is not None: + if cmdDict[sucKey] == True: + error = checkError(cmdName,response) + if error is None: + return None + else: + return error + if re.match(cmdDict[sucKey],response,re.I): + return True + error = checkError(cmdName,response) + if error is None: + raise NotImplementedError('Ricevuta risposta sconosciuta: {}'.format(response)) + else: + return error + else: + error = checkError(cmdName,response) + return error + +def checkError(cmdName,response): + errKey = 'error' + cmdDict = cmd.MKPcommands[cmdName] + if errKey in cmdDict and cmdDict[errKey] is not None: + return None + error_codes = re.match(cmdDict[errKey],response,re.I) + if error_codes: + return error_codes.groups() + return None \ No newline at end of file