diff --git a/components/orchestration/orchestration_server.py b/components/orchestration/orchestration_server.py index 7c0a87d..ab78121 100644 --- a/components/orchestration/orchestration_server.py +++ b/components/orchestration/orchestration_server.py @@ -23,17 +23,6 @@ def api2(): if __name__ == '__main__': - ENTITY_IDENT_URL = 'http://entityident:5002/api/v1/resources/' - try: - response = requests.get(ENTITY_IDENT_URL + 'cars') - cars = response.json() - - response = requests.get(ENTITY_IDENT_URL + 'traffic_lights') - traffic_lights = response.json() - except requests.exceptions.ConnectionError as e: - print("Is the entity_ident_server running and reachable?") - raise e - - orc = Orchestrator(cars, traffic_lights) + orc = Orchestrator() threading.Thread(target=orc.setup_msg_queues).start() threading.Thread(target=app.run, args=('0.0.0.0', 5003)).start() diff --git a/components/orchestration/orchestrator.py b/components/orchestration/orchestrator.py index 35910a2..fa14b62 100644 --- a/components/orchestration/orchestrator.py +++ b/components/orchestration/orchestrator.py @@ -4,11 +4,14 @@ import sys from random import randrange from typing import List, Dict +import requests from dse_shared_libs import daf, traffic_light_state, traffic_light_color, target_velocity +from dse_shared_libs.daf import DAF from dse_shared_libs.message_broker_wrapper import MBWrapper # necessary to unpickle daf object from dse_shared_libs.target_velocity import TargetVelocity from dse_shared_libs.traffic_light_state import TrafficLightState +from requests import Session sys.modules['daf'] = daf sys.modules['traffic_light_state'] = traffic_light_state @@ -16,6 +19,9 @@ sys.modules['traffic_light_color'] = traffic_light_color sys.modules['target_velocity'] = target_velocity +ENTITY_IDENT_URL = 'http://entityident:5002/api/v1/resources/' + + class Orchestrator: # vehicle ids vins: List[str] = [] @@ -26,7 +32,22 @@ class Orchestrator: _daf_mbs: Dict[str, MBWrapper] = {} _velocity_mbs: Dict[str, MBWrapper] = {} - def __init__(self, cars, traffic_lights): + _session: Session + + def __init__(self): + # re-use http session, is faster then request.ing everytime + self._session = requests.Session() + + try: + response = self._session.get(ENTITY_IDENT_URL + 'cars') + cars = response.json() + + response = self._session.get(ENTITY_IDENT_URL + 'traffic_lights') + traffic_lights = response.json() + except requests.exceptions.ConnectionError as e: + print("Is the entity_ident_server running and reachable?") + raise e + for car in cars['cursor']: self.vins.append(car['vin']) @@ -59,10 +80,20 @@ class Orchestrator: :param pickle_binary: daf object pickle binary dump """ - received_daf_object = pickle.loads(pickle_binary) + received_daf_object: DAF = pickle.loads(pickle_binary) + loc = received_daf_object.gps_location print(received_daf_object) # TODO ask entity ident if tl in range?! + response = self._session.get(ENTITY_IDENT_URL + 'traffic_lights_geo', + params={'lat': loc.latitude, 'lon': loc.longitude}) + traffic_lights_geo = response.json() + print('TLGeo', traffic_lights_geo) + # TODO in the best case we get a traffic light ID and some sort of visibility range here? + # we need to calculate new speed regarding current speed and position and visibility of TL to ensure a + # green wave + # keep NCE in mind!? + # TODO use the data from the traffic lights (self.tls) # to transmit a new target velocity for this vehicle to achieve a green wave