From 92bc86c48bb40b5f1b97b764f45f0d8a723bb775 Mon Sep 17 00:00:00 2001 From: Guido Longoni Date: Fri, 27 Sep 2019 18:55:52 +0200 Subject: [PATCH] Bugfix quasi terminato --- MkpWIFI/commands.py | 46 +++++++++++----------- MkpWIFI/protocol.py | 95 +++++++++++++++++++++++++-------------------- 2 files changed, 75 insertions(+), 66 deletions(-) diff --git a/MkpWIFI/commands.py b/MkpWIFI/commands.py index 27e6368..e27009a 100644 --- a/MkpWIFI/commands.py +++ b/MkpWIFI/commands.py @@ -5,9 +5,9 @@ Created on 27 set 2019 ''' DEFprefix = { - 'begin': '\x1b', #ESC + 'begin': '\x1b', # ESC 'end': '\r' - } +} MKPcommands = { 'handshake': { @@ -15,102 +15,102 @@ MKPcommands = { 'command': '*', 'success': 'Ok\r', 'error': None - }, + }, 'reset': { 'defPrefix': True, 'command': 'RST', 'success': 'Ok\r', 'error': None, - }, + }, 'deleteMemory': { 'defPrefix': True, 'command': 'C', 'success': None, 'error': None - }, + }, 'printPause': { 'defPrefix': True, 'command': 'C0', 'success': 'Ok\r', 'error': None, - }, + }, 'printStart': { 'defPrefix': True, 'command': 'C1', 'success': 'Ok\r', 'error': None, - }, + }, 'callPrintImage': { 'defPrefix': False, - 'begin': '\x02', #STX - 'end': '\x03', #ETX - 'command': 'TZ{imagename};{printMode};{queueNo};{delay}\r{fieldContent}\r{counterStart}', + 'begin': '\x02', # STX + 'end': '\x03', # ETX + 'command': 'TZ{imageName};{printMode};{queueNo};{delay}\r{fieldContent}\r{counterStart}', 'success': '\x06\x02([0-9])OK\x03', 'error': '([0-9])E([0-9])', 'args': ['imageName', 'printMode', 'fieldContent'], 'optArgs': ['queueNo', 'delay', 'counterStart'] - }, + }, 'sendImage': { 'defPrefix': True, 'command': 'EW----;{imageName}', 'success': None, 'error': None, 'args': ['imageName'] - }, + }, 'sendImageEnd': { 'defPrefix': True, 'command': 'EX----;', 'success': 'OK\r', 'error': None, - }, + }, 'receiveImage': { 'defPrefix': True, 'command': 'EL----;', 'success': True, 'error': None - }, + }, 'receiveDir': { 'defPrefix': True, 'command': 'ED----;', 'success': True, 'error': None - }, + }, 'deleteImage': { 'defPrefix': True, 'command': 'EQ----;{imageName}', 'success': 'OK\r', 'error': 'Err\r', 'args': ['imageName'] - }, + }, 'sendDate': { 'defPrefix': True, 'command': '*STD{dateTime};{offset}', 'success': 'Ok\r', 'error': 'Err\r', - 'args': ['dateTime','offset'] - }, + 'args': ['dateTime', 'offset'] + }, 'receiveDate': { 'defPrefix': True, 'command': '*GSTD', 'success': True, 'error': None, - }, + }, 'status': { 'defPrefix': True, 'command': 'SV', 'success': True, 'error': None - }, + }, 'status1': { 'defPrefix': True, 'command': 'S1', 'success': True, 'error': None - }, + }, 'status2': { 'defPrefix': True, 'command': 'S2', 'success': True, 'error': None - }, - } + }, +} diff --git a/MkpWIFI/protocol.py b/MkpWIFI/protocol.py index 471eb34..f697d5f 100644 --- a/MkpWIFI/protocol.py +++ b/MkpWIFI/protocol.py @@ -11,52 +11,61 @@ import re def cmdString(cmdName, **cmdArgs): - cmdDict = cmd.MKPcommands[cmdName] - mandatory_args = set(cmdDict['args']) - optional_args = set(cmdDict['optargs']) - my_args = set(cmdArgs.keys()) - if my_args.intersection(mandatory_args) != my_args: - raise ValueError('Argomenti obbligatori errati o mancanti.') - if len(my_args-mandatory_args-optional_args) > 0: - raise Warning('Forniti argomenti sconosciuti!') - argDict = {**{a: cmdArgs[a] for a in mandatory_args} - ** {a: cmdArgs[a] if a in my_args else '' for a in optional_args} - } - return str(cmd.DEFprefix['begin'] if cmdDict['defPrefix'] else cmdDict['begin']) \ - + re.sub(r';+', ';', cmdDict['command'].format(argDict)) \ - + str(cmd.DEFprefix['end'] - if cmdDict['defPrefix'] else cmdDict['end']) + cmdDict = cmd.MKPcommands[cmdName] + mandatory_args = set(cmdDict['args']) + if 'optArgs' in cmdDict and cmdDict['optArgs'] is not None: + optional_args = set(cmdDict['optArgs']) + else: + optional_args = set() + my_args = set(cmdArgs.keys()) + if len(my_args) < len(mandatory_args) or my_args.intersection(mandatory_args) != my_args: + raise ValueError('Argomenti obbligatori errati o mancanti.') + if len(my_args-mandatory_args-optional_args) > 0: + raise Warning('Forniti argomenti sconosciuti!') + argDict = { ** { a: cmdArgs[a] for a in mandatory_args}, + ** { a: cmdArgs[a] if a in my_args else '' for a in optional_args} } + + arg_groups = [i.group(1) for i in re.finditer(r'[^;]((\{[^}]*\};)+\{[^}]*\})[^;]',cmd.MKPcommands[cmdName]['command'])] + replacements = [(g,';'.join([a.format(**argDict) for a in g.split(';') if len(a.format(**argDict)) > 0])) for g in arg_groups] + + return str(cmd.DEFprefix['begin'] if cmdDict['defPrefix'] else cmdDict['begin']) \ + + re.sub(r';+', ';', cmdDict['command'].format(**argDict)) \ + + 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 + 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 + 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 + + +testala=cmdString('callPrintImage',imageName='Pippus',printMode='10',fieldContent='qualcosa\rqualcosaltro\rciao')