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