1#!/usr/bin/env python
2#
3# Copyright (C) 2017 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18import argparse
19import time
20
21from threading import Thread
22
23import driving_info_generator
24import user_action_generator
25import vhal_consts_2_0 as c
26
27from vhal_emulator import Vhal
28
29DEFAULT_TIMEOUT_SEC = 60 * 60 # 1 hour
30
31class VhalPropSimulator(object):
32    """
33        A class simulates vhal properties by calling each generator in a separate thread. It is
34        itself a listener passed to each generator to handle vhal event
35    """
36
37    def __init__(self, device, gpxFile,):
38        self.vhal = Vhal(c.vhal_types_2_0, device)
39        self.gpxFile = gpxFile
40
41    def handle(self, prop, area_id, value, desc=None):
42        """
43            handle generated VHAL property by injecting through vhal emulator.
44        """
45        print('Generated property %s with value: %s' % (desc, value))
46        self.vhal.setProperty(prop, area_id, value)
47
48    def _startGeneratorThread(self, generator):
49        thread = Thread(target=generator.generate, args=(self,))
50        thread.daemon = True
51        thread.start()
52
53    def run(self, timeout):
54        userActionGenerator = user_action_generator.UserActionGenerator(self.vhal)
55        drivingInfoGenerator = driving_info_generator.DrivingInfoGenerator(self.gpxFile, self.vhal)
56        self._startGeneratorThread(userActionGenerator)
57        self._startGeneratorThread(drivingInfoGenerator)
58        time.sleep(float(timeout))
59
60
61if __name__ == '__main__':
62    parser = argparse.ArgumentParser(description='Vhal Property Simulator')
63    parser.add_argument('-s', action='store', dest='deviceid', default=None)
64    parser.add_argument('--timeout', action='store', dest='timeout', default=DEFAULT_TIMEOUT_SEC)
65    parser.add_argument('--gpx', action='store', dest='gpxFile', default=None)
66    args = parser.parse_args()
67
68    simulator = VhalPropSimulator(device=args.deviceid, gpxFile=args.gpxFile)
69    simulator.run(args.timeout)
70