diff --git a/MkpWIFI/commands.py b/MkpWIFI/commands.py index e1e4cc5..27e6368 100644 --- a/MkpWIFI/commands.py +++ b/MkpWIFI/commands.py @@ -44,7 +44,7 @@ MKPcommands = { 'defPrefix': False, 'begin': '\x02', #STX 'end': '\x03', #ETX - 'command': 'TZ{imagename};{printMode};{queueNo};{delay}\r{fieldContent}{counterStart}', + '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'], diff --git a/MkpWIFI/protocol.py b/MkpWIFI/protocol.py index ad85272..471eb34 100644 --- a/MkpWIFI/protocol.py +++ b/MkpWIFI/protocol.py @@ -10,11 +10,20 @@ import commands as cmd import re -def cmdString(cmdName, cmdArgs=None): +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']) \ - + str(cmdDict['command']) \ - + str(';'.join([cmdArgs[k] for k in cmdDict['args']]) if cmdArgs is not None else '') \ + + re.sub(r';+', ';', cmdDict['command'].format(argDict)) \ + str(cmd.DEFprefix['end'] if cmdDict['defPrefix'] else cmdDict['end'])