Bugfix quasi terminato

master
Guido Longoni 2019-09-27 18:55:52 +02:00
parent 4df4253e61
commit 92bc86c48b
2 changed files with 75 additions and 66 deletions

View File

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

View File

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