1import vhal_consts_2_0 as c 2from vhal_emulator import Vhal 3 4import argparse 5import json 6import sys 7import array 8 9vhal_types = c.vhal_types_2_0 10 11def propType(con): 12 return getattr(c,con) 13 14def parseVal(val, valType): 15 if valType in vhal_types.TYPE_STRING: 16 return str(val) 17 elif valType in vhal_types.TYPE_INT32: 18 return int(val) 19 elif valType in vhal_types.TYPE_INT32S: 20 return map(int, val.split(',')) 21 elif valType in vhal_types.TYPE_INT64: 22 return long(val) 23 elif valType in vhal_types.TYPE_INT64S: 24 return map(long, val.split(',')) 25 elif valType in vhal_types.TYPE_FLOAT: 26 return float(val) 27 elif valType in vhal_types.TYPE_FLOATS: 28 return map(float, val.split(',')) 29 elif valType in vhal_types.TYPE_MIXED: 30 print val 31 return json.loads(val) 32 else: 33 raise ValueError('Property value type not recognized:', valType) 34 return 35 36def main(): 37 parser = argparse.ArgumentParser( 38 description='Execute vehicle simulation to simulate actual car sceanrios.') 39 parser.add_argument( 40 '-s', 41 type=str, 42 action='store', 43 dest='device', 44 default=None, 45 help='Device serial number. Optional') 46 parser.add_argument( 47 '--property', 48 type=propType, 49 default=c.VEHICLEPROPERTY_EV_CHARGE_PORT_OPEN, 50 help='Property name from vhal_consts_2_0.py, e.g. VEHICLEPROPERTY_EV_CHARGE_PORT_OPEN') 51 parser.add_argument( 52 '--area', 53 default=0, 54 type=int, 55 help='Area id for the property, "0" for global') 56 parser.add_argument( 57 '--value', 58 type=str, 59 help='Property value. If the value is MIXED type, you should provide the JSON string \ 60 of the value, e.g. \'{"int32_values": [0, 291504647], "int64_values": [1000000], \ 61 "float_values": [0.0, 30, 0.1]}\' which is for fake data generation controlling \ 62 property in default VHAL. If the value is array, use comma to split values') 63 args = parser.parse_args() 64 if not args.property: 65 print 'Project is required. Use --help to see options.' 66 sys.exit(1) 67 68 executeCommand(args); 69 70def executeCommand(args): 71 # Create an instance of vhal class. Need to pass the vhal_types constants. 72 v = Vhal(c.vhal_types_2_0, args.device); 73 74 # Get the property config (if desired) 75 # property = args.property; 76 print args.property 77 #i = c.VEHICLEPROPERTY_EV_CHARGE_PORT_OPEN 78 v.getConfig(args.property); 79 80 # Get the response message to getConfig() 81 reply = v.rxMsg(); 82 print(reply); 83 84 value = parseVal(args.value, reply.config[0].value_type) 85 v.setProperty(args.property, args.area, value); 86 87 # Get the response message to setProperty() 88 reply = v.rxMsg(); 89 print(reply); 90 91if __name__=="__main__": 92 main()