#!/usr/bin/env python from mod_utils import Choice from mod_utils import ChoiceFromDict from mod_utils import hex_VIN_plus_CRC from mod_utils import pyren_encode from mod_utils import clearScreen from mod_utils import ASCIITOHEX from mod_utils import StringToIntToHex from mod_ecu_service import * from mod_ecu_screen import * from mod_ecu_state import get_state from mod_ecu_parameter import get_parameter from mod_ecu_identification import get_identification from mod_ecu_scenario import playScenario from xml.dom.minidom import parse from xml.dom.minidom import parseString import xml.dom.minidom import os import sys import string def runCommand( command, ecu, elm, param = '', cmdt = 'HEX' ): isService = 0 isParam = 0 isInputList = 0 # check conditions if len(command.scenario): print("It is scenarium. I do not support them!!!\n") return if len(list(command.inputlist.keys())): isInputList = 1 for si in command.serviceID: isService = 1 service = ecu.Services[si] if len(service.params): isParam += len(service.params) if len(command.datarefs): print() print("#"*26," Current values ","#"*26) print("") strlst = [] elm.clear_cache() for dr in command.datarefs: datastr = dr.name; help = dr.type if dr.type=='State': datastr, help, csvd = get_state( ecu.States[dr.name], ecu.Mnemonics, ecu.Services, ecu.elm, ecu.calc ) if dr.type=='Parameter': datastr, help, csvd = get_parameter( ecu.Parameters[dr.name], ecu.Mnemonics, ecu.Services, ecu.elm, ecu.calc ) if dr.type=='Identification': datastr, help = get_identification( ecu.Identifications[dr.name], ecu.Mnemonics, ecu.Services, ecu.elm, ecu.calc ) print(pyren_encode( datastr )) print("") print("#"*70) print("") chosenParameter = '' if isInputList and param not in list(command.inputlist.keys()) : print("Not valid parameter. It isn't in inputList") return summary = '' for si in command.serviceID: service = ecu.Services[si] if len(service.params)==1 and chosenParameter=='': if len(service.params[0]['size']): parsize = int(service.params[0]['size']) else: parsize = 0 ch = param ch = ch.strip().upper() if cmdt=='HEX' and all(c in string.hexdigits for c in ch) and len(ch)%2==0: if parsize>0 and len(ch)!=parsize*2: continue chosenParameter = ch if cmdt=='VIN' and len(ch)==17 and ('I' not in ch) and ('O' not in ch): chosenParameter = hex_VIN_plus_CRC( ch ) if cmdt=='DEC' and all (c in string.digits for c in ch): chosenParameter = StringToIntToHex( ch ) #if parsize > 0 and len(chosenParameter) > parsize * 2: # print 'Too long value' # continue if parsize>0 and len(chosenParameter) 0 and len(chosenParameter) > parsize * 2: # print 'Too long value' # continue if parsize>0 and len(chosenParameter)" % (si, service.startReq))) # check conditions if len(command.scenario): #print "\nThere is scenarium. I do not support them!!!\n" #ch = raw_input('Press ENTER to exit ') #if 'show' in ch: playScenario( command, ecu, elm ) return cmdt = '' if (isParam-isInputList)!=0: print("\nThere are parameters. \n") cmdt = input('Press ENTER to exit or type [HEX, DEC, ASCII, VIN]: ') cmdt = cmdt.upper() if cmdt!='HEX' and cmdt!='DEC' and cmdt!='ASCII' and cmdt!='VIN': return if isService==0: print("\nNothing to send!!!\n") ch = input('Press ENTER to exit ') return # show datarefs if exist mnemo = '' if len(command.datarefs): print() print("#"*26," Current values ","#"*26) print("") strlst = [] elm.clear_cache() for dr in command.datarefs: datastr = dr.name; help = dr.type if dr.type=='State': datastr, help, csvd = get_state( ecu.States[dr.name], ecu.Mnemonics, ecu.Services, ecu.elm, ecu.calc ) mnemo = ecu.States[dr.name].mnemolist[0] if dr.type=='Parameter': datastr, help, csvd = get_parameter( ecu.Parameters[dr.name], ecu.Mnemonics, ecu.Services, ecu.elm, ecu.calc ) mnemo = ecu.Parameters[dr.name].mnemolist[0] if dr.type=='Identification': datastr, help = get_identification( ecu.Identifications[dr.name], ecu.Mnemonics, ecu.Services, ecu.elm, ecu.calc ) mnemo = ecu.Identifications[dr.name].mnemolist[0] print(pyren_encode( datastr )) print("") print("#"*70) print("") ch = input('Are you ready to execute the command? :') if ch.lower()!='yes': return chosenParameter = "" if isInputList: print() print("#"*25," Make your choice ","#"*25) print("") menu = {} menu = dict(command.inputlist) menu[""] = "Exit from command execution" choice = ChoiceFromDict(menu, "Choose :") if choice[0]=="": return chosenParameter = choice[0] print() ps = "##### Your choice is ("+chosenParameter+") "+command.inputlist[chosenParameter] print(pyren_encode( ps ), '#'*(70-len(ps))) print() if len(dataid) and dataid in list(ecu.DataIds.keys()) and len(mnemo) and mnemo in list(ecu.Mnemonics.keys()) and len(chosenParameter)>0: chosenParameter = packData( ecu, mnemo, dataid, data, chosenParameter ) for si in command.serviceID: service = ecu.Services[si] if len(service.params)==1: ostr = "cmd:"+service.startReq+chosenParameter print(pyren_encode("%-35s \n"%(ostr)), end=' ') elif len(service.params)==0: ostr = "cmd:"+service.startReq, print(pyren_encode("%-35s \n"%(ostr)), end=' ') ch = input('\nDo you agree? :') if ch.lower()!='yes': return print() print("#"*29," Execution ","#"*29) print() for si in command.serviceID: service = ecu.Services[si] if len(service.params)==1 and chosenParameter=='': if len(service.params[0]['size']): parsize = int(service.params[0]['size']) else: parsize = 0 print('Parametr type:',service.params[0]['type'], ' size:',service.params[0]['size']) while True: ch = input(cmdt+':') ch = ch.strip().upper() if cmdt=='HEX' and all(c in string.hexdigits for c in ch) and len(ch)%2==0: if parsize>0 and len(ch)!=parsize*2: continue chosenParameter = ch break if cmdt=='VIN' and len(ch)==17 and ('I' not in ch) and ('O' not in ch): chosenParameter = hex_VIN_plus_CRC( ch ) break if cmdt=='DEC' and all (c in string.digits for c in ch): chosenParameter = StringToIntToHex( ch ) if parsize > 0 and len(chosenParameter) > parsize * 2: print('Too long value') continue if parsize > 0 and len(chosenParameter) 0 and len(chosenParameter) > parsize * 2: print('Too long value') continue if parsize > 0 and len(chosenParameter)