Beispiel #1
0
 def _handle_message(action: bytes, data: List[bytes]) -> bytes:
     from drivebuildclient.aiExchangeMessages_pb2 import Bool
     if action == b"isRunning":
         sid = SimulationID()
         sid.ParseFromString(data[0])
         result = Bool()
         result.value = _is_simulation_running(sid)
     elif action == b"vids":
         sid = SimulationID()
         sid.ParseFromString(data[0])
         result = _get_vids(sid)
     elif action == b"pollSensors":
         sid = SimulationID()
         sid.ParseFromString(data[0])
         result = _poll_sensors(sid)
     elif action == b"verify":
         sid = SimulationID()
         sid.ParseFromString(data[0])
         result = _verify(sid)
     elif action == b"requestAiFor":
         sid = SimulationID()
         sid.ParseFromString(data[0])
         vid = VehicleID()
         vid.ParseFromString(data[1])
         result = _request_ai_for(sid, vid)
     elif action == b"storeVerificationCycle":
         sid = SimulationID()
         sid.ParseFromString(data[0])
         started = Num()
         started.ParseFromString(data[1])
         finished = Num()
         finished.ParseFromString(data[2])
         result = _store_verification_cycle(
             sid, datetime.fromtimestamp(started.num),
             datetime.fromtimestamp(finished.num))
     elif action == b"steps":
         sid = SimulationID()
         sid.ParseFromString(data[0])
         steps = Num()
         steps.ParseFromString(data[1])
         result = Void()
         if _is_simulation_running(sid):
             _get_data(sid).scenario.bng.step(steps.num)
             result.message = "Simulated " + str(
                 steps.num) + " steps in simulation " + sid.sid + "."
         else:
             result.message = "Simulation " + sid.sid + " is not running anymore."
     elif action == b"stop":
         sid = SimulationID()
         sid.ParseFromString(data[0])
         test_result = TestResult()
         test_result.ParseFromString(data[1])
         _control_sim(sid, test_result.result, False)
         result = Void()
     else:
         message = "The action \"" + action.decode() + "\" is unknown."
         _logger.info(message)
         result = Void()
         result.message = message
     return result.SerializeToString()
 def control(self, sid: SimulationID, vid: VehicleID,
             commands: Control) -> Optional[Void]:
     """
     Control the simulation or a certain vehicle in the simulation.
     :param sid: The ID the simulation either to control or containing th vehicle to control.
     :param vid: The ID of the vehicle to possibly control.
     :param commands: The command either controlling a simulation or a vehicle in a simualtion. To define a command
     controlling a simulation you can use commands like:
     control = Control()
     control.simCommand.command = Control.SimCommand.Command.SUCCEED  # Force simulation to succeed
     control.simCommand.command = Control.SimCommand.Command.FAIL  # Force simulation to fail
     control.simCommand.command = Control.SimCommand.Command.CANCEL  # Force simulation to be cancelled/skipped
     For controlling a vehicle you have to define steering, acceleration and brake values:
     control = Control()
     control.avCommand.accelerate = <Acceleration intensity having a value between 0.0 and 1.0>
     control.avCommand.steer = <A steering value between -1.0 and 1.0 (Negative value steers left; a positive one
     steers right)>
     control.avCommand.brake = <Brake intensity having a value between 0.0 and 1.0>
     :return: A Void object possibly containing a info message.
     """
     from drivebuildclient.httpUtil import do_mixed_request
     response = do_mixed_request(self.host, self.port, "/ai/control", {
         "sid": sid.SerializeToString(),
         "vid": vid.SerializeToString()
     }, commands.SerializeToString())
     if response.status == 200:
         void = Void()
         void.ParseFromString(b"".join(response.readlines()))
         return void
     else:
         AIExchangeService._print_error(response)
Beispiel #3
0
 def _store_verification_cycle(sid: SimulationID, started: datetime,
                               finished: datetime) -> Void:
     vehicles = _get_data(sid).scenario.vehicles.keys()
     void = Void()
     if _is_simulation_running(sid):
         for vehicle in vehicles:
             vid = VehicleID()
             vid.vid = vehicle.vid
             request = DataRequest()
             request.request_ids.extend(vehicle.requests)
             data = _request_data(sid, vid, request)
             args = {
                 "sid": sid.sid,
                 "vid": vid.vid,
                 "tick": _get_data(sid).scenario.bng.current_tick,
                 "data": data.SerializeToString(),
                 "started": _time_to_string(started),
                 "finished": _time_to_string(finished)
             }
             _DB_CONNECTION.run_query(
                 """
             INSERT INTO verificationcycles VALUES
             (:sid, :vid, :tick, :data, :started, :finished);
             """, args)
         void.message = "Stored data of the current runtime verification cycle of simulation " + sid.sid + "."
     else:
         void.message = "Skipped storing the data of the current runtime verification cycle since simulation " \
                        + sid.sid + " does not run anymore."
     return void
Beispiel #4
0
 def _handle_message(action: bytes, data: List[bytes]) -> bytes:
     if action == b"generateSid":
         result = _generate_sid()
     else:
         message = "The action \"" + action.decode() + "\" is unknown."
         _logger.info(message)
         result = Void()
         result.message = message
     return result.SerializeToString()
Beispiel #5
0
 def _poll_sensors(sid: SimulationID) -> Void:
     vehicles = _get_data(sid).scenario.vehicles.keys()
     void = Void()
     if _is_simulation_running(sid):
         for vehicle in vehicles:
             _get_data(sid).scenario.bng.poll_sensors(vehicle)
         void.message = "Polled all registered sensors of simulation " + sid.sid + "."
     else:
         void.message = "Skipped polling sensors since simulation " + sid.sid + " is not running anymore."
     return void
 def control_sim(self, sid: SimulationID,
                 result: TestResult) -> Optional[Void]:
     """
     Force a simulation to end having the given result.
     :param sid: The simulation to control.
     :param result: The test result to be set to the simulation.
     :return: A Void object possibly containing a info message.
     """
     from drivebuildclient.httpUtil import do_get_request
     response = do_get_request(self.host, self.port, "/sim/stop", {
         "sid": sid.SerializeToString(),
         "result": result.SerializeToString()
     })
     if response.status == 200:
         void = Void()
         void.ParseFromString(b"".join(response.readlines()))
         return void
     else:
         AIExchangeService._print_error(response)
Beispiel #7
0
 def _control(sid: SimulationID, vid: VehicleID, control: Control) -> Void:
     _logger.info("ai_control: enter for " + vid.vid)
     result = Void()
     if _is_simulation_running(sid):
         command_type = control.WhichOneof("command")
         if command_type == "simCommand":
             _control_sim(sid, control.simCommand.command, True)
         elif command_type == "avCommand":
             sim = _get_simulation(sid)
             if sim and sim.get_current_movement_mode(
                     vid.vid) is MovementMode.AUTONOMOUS:
                 _control_av(sid, vid, control.avCommand)
         else:
             raise NotImplementedError("Interpreting commands of type " +
                                       command_type +
                                       " is not implemented, yet.")
         result.message = "Controlled vehicle " + vid.vid + " in simulation " + sid.sid + "."
     else:
         result.message = "Simulation " + sid.sid + " does not run."
     _logger.info("ai_control: leave for " + vid.vid)
     return result
Beispiel #8
0
 def _request_ai_for(sid: SimulationID, vid: VehicleID) -> Void:
     from time import sleep
     _logger.debug("sim_request_ai_for: enter for " + sid.sid + ":" +
                   vid.vid)
     _registered_ais_lock.acquire()
     _init_registered_ais(sid, vid)
     num_sim_ready, num_ai_ready = _registered_ais[sid.sid][vid.vid]
     _registered_ais[sid.sid][vid.vid] = (num_sim_ready + 1, num_ai_ready)
     _logger.debug(sid.sid + ":" + vid.vid + " after raf: " +
                   str(_registered_ais[sid.sid][vid.vid]))
     _registered_ais_lock.release()
     while _registered_ais[sid.sid][vid.vid][1] < _registered_ais[sid.sid][vid.vid][0] \
             and _is_simulation_running(sid):
         _logger.debug(sid.sid + ":" + vid.vid + " wait for ai ready")
         sleep(5)
         pass  # Wait for all being ready
     _logger.debug("sim_request_ai_for: leave for " + sid.sid + ":" +
                   vid.vid)
     void = Void()
     void.message = "Simulation " + sid.sid + " finished requesting vehicle " + vid.vid + "."
     return void
Beispiel #9
0
 def _handle_main_app_message(action: bytes, data: List[bytes]) -> bytes:
     from google.protobuf.message import DecodeError
     if action == b"runTests":
         user = User()
         try:
             user.ParseFromString(data[1])
             result = _run_tests(data[0], user)
         except DecodeError:
             _logger.exception("Running a test failed since \"" +
                               str(data[1]) +
                               "\" can not be parsed to an User")
             result = SubmissionResult()
             result.message.message = "The user parameter could not be parsed."
     elif action == b"waitForSimulatorRequest":
         sid = SimulationID()
         sid.ParseFromString(data[0])
         vid = VehicleID()
         vid.ParseFromString(data[1])
         result = _wait_for_simulator_request(sid, vid)
     elif action == b"control":
         sid = SimulationID()
         sid.ParseFromString(data[0])
         vid = VehicleID()
         vid.ParseFromString(data[1])
         control = Control()
         control.ParseFromString(data[2])
         result = _control(sid, vid, control)
     elif action == b"requestData":
         sid = SimulationID()
         sid.ParseFromString(data[0])
         vid = VehicleID()
         vid.ParseFromString(data[1])
         request = DataRequest()
         request.ParseFromString(data[2])
         result = _request_data(sid, vid, request)
     elif action == b"requestSocket":
         client = create_client(MAIN_APP_HOST, MAIN_APP_PORT)
         client_thread = Thread(target=process_requests,
                                args=(client, _handle_main_app_message))
         client_thread.daemon = True
         _logger.info("_handle_main_app_message --> " +
                      str(client.getsockname()))
         client_thread.start()
         result = Void()
         result.message = "Connected another client socket to the main app."
     elif action == b"runningTests":
         user = User()
         user.ParseFromString(data[0])
         result = _get_running_tests(user)
     elif action == b"stop":
         sid = SimulationID()
         sid.ParseFromString(data[0])
         test_result = TestResult()
         test_result.ParseFromString(data[1])
         _control_sim(sid, test_result.result, False)
         result = Void()
         result.message = "Stopped simulation " + sid.sid + "."
     else:
         message = "The action \"" + action.decode() + "\" is unknown."
         _logger.info(message)
         result = Void()
         result.message = message
     return result.SerializeToString()