From 8df1930a966db5de79107843ef1278f6b0856384 Mon Sep 17 00:00:00 2001 From: Marco Zeisler Date: Mon, 24 May 2021 17:51:41 +0200 Subject: [PATCH] small refactorings; added comments; --- components/i_feed/traffic_light.py | 14 ++++---- components/orchestration/orchestrator.py | 45 ++++++++++++++++++------ components/shared/TrafficLightColor.py | 2 +- 3 files changed, 42 insertions(+), 19 deletions(-) diff --git a/components/i_feed/traffic_light.py b/components/i_feed/traffic_light.py index 66b75ce..44d47e2 100644 --- a/components/i_feed/traffic_light.py +++ b/components/i_feed/traffic_light.py @@ -7,7 +7,7 @@ from circuitbreaker import circuit # Default switching time in seconds from shared import TrafficLightColor -from shared.TrafficLightColor import Color +from shared.TrafficLightColor import TrafficLightColor from shared.message_broker_wrapper import MBWrapper SWITCHING_TIME = 120 @@ -23,11 +23,11 @@ class TrafficLight: # timestamp of last switch last_switch: datetime # currently lit color - current_color: Color + current_color: TrafficLightColor # marks if traffic light started running: bool = False # stores starting color - _starting_color: Color + _starting_color: TrafficLightColor # stores runner thread _t: threading.Thread @@ -37,7 +37,7 @@ class TrafficLight: def __init__(self, tlid: str, switching_time: int = SWITCHING_TIME, - starting_color: Color = Color.RED): + starting_color: TrafficLightColor = TrafficLightColor.RED): self.tlid = tlid self.switching_time = switching_time self._starting_color = starting_color @@ -49,12 +49,12 @@ class TrafficLight: self.running = True def switcher(): - num_colors = len(TrafficLightColor.Color) + num_colors = len(TrafficLightColor) # set current color to the one before starting color, because switch immediately happens in loop # therefore it sleeps on the end of the loop and first status is immediately sent to msg broker - self.current_color = Color((self._starting_color.value - 1) % num_colors) + self.current_color = TrafficLightColor((self._starting_color.value - 1) % num_colors) while self.running: - self.current_color = Color((self.current_color.value + 1) % num_colors) + self.current_color = TrafficLightColor((self.current_color.value + 1) % num_colors) self.last_switch = datetime.now() self.send_status_update() time.sleep(self.switching_time / SCALING) diff --git a/components/orchestration/orchestrator.py b/components/orchestration/orchestrator.py index 9c7a02c..825f127 100644 --- a/components/orchestration/orchestrator.py +++ b/components/orchestration/orchestrator.py @@ -1,20 +1,25 @@ import pickle import sys from random import randrange +from typing import List, Dict from shared import daf -from shared.TrafficLightColor import Color +from shared.TrafficLightColor import TrafficLightColor from shared.message_broker_wrapper import MBWrapper +# necessary to unpickle daf object sys.modules['daf'] = daf class Orchestrator: - vins = [] - tls = {} + # vehicle ids + vins: List[str] = [] + # traffic lights {tlid: {color: color, switching_time: in_seconds}} + tls: Dict[str, Dict] = {} - _daf_mbs = {} - _velocity_mbs = {} + # cache all active incoming daf connections + _daf_mbs: Dict[str, MBWrapper] = {} + _velocity_mbs: Dict[str, MBWrapper] = {} def __init__(self): # TODO fetch available car VINs from Entity Ident @@ -24,12 +29,13 @@ class Orchestrator: print('vins', self.vins) # TODO fetch available TLs from Entity Ident - self.tls['traffic-light-1'] = {'color': Color.RED, 'switching_time': 120} - self.tls['traffic-light-2'] = {'color': Color.GREEN, 'switching_time': 240} - self.tls['traffic-light-3'] = {'color': Color.RED, 'switching_time': 360} + self.tls['traffic-light-1'] = {'color': TrafficLightColor.RED, 'switching_time': 120} + self.tls['traffic-light-2'] = {'color': TrafficLightColor.GREEN, 'switching_time': 240} + self.tls['traffic-light-3'] = {'color': TrafficLightColor.RED, 'switching_time': 360} print('tls', self.tls) def setup_msg_queues(self): + # spawn the vehicle related message broker channels for vin in self.vins: daf_channel = MBWrapper(exchange_name='vehicle_daf_{}'.format(vin), callback=self.handle_daf_receive) daf_channel.setup_receiver() @@ -39,20 +45,37 @@ class Orchestrator: velocity_channel.setup_sender() self._velocity_mbs[vin] = velocity_channel + # spawn the traffic light related mb channels for tl in self.tls: tl_channel = MBWrapper(exchange_name='traffic_light_state_{}'.format(tl), callback=self.handle_tl_state_receive) tl_channel.setup_receiver() - def handle_daf_receive(self, msg): - received_daf_object = pickle.loads(msg) + def handle_daf_receive(self, pickle_binary): + """ + Gets the daf object's pickle binary dump. + Unpickle, calculate new target velocity based on daf and current tl data and + respond new target velicity for this vehicle. + + :param pickle_binary: daf object pickle binary dump + """ + received_daf_object = pickle.loads(pickle_binary) print(received_daf_object) # TODO use the data from the traffic lights (self.tls) # to transmit a new target velocity for this vehicle to achieve a green wave response_channel = self._velocity_mbs[received_daf_object.vehicle_identification_number] - response_channel.send(randrange(0, 130)) + response_channel.send(str(randrange(0, 130)).encode()) def handle_tl_state_receive(self, msg): + """ + Gets a dict full of traffic light state information: + { + 'tlid': str, + 'color': TrafficLightColor, + 'last_switch': datetime + } + Updates internal information about available TLs + """ tl_state = pickle.loads(msg) self.tls[tl_state['tlid']]['color'] = tl_state['color'] print(tl_state) diff --git a/components/shared/TrafficLightColor.py b/components/shared/TrafficLightColor.py index 784fd30..4e67603 100644 --- a/components/shared/TrafficLightColor.py +++ b/components/shared/TrafficLightColor.py @@ -1,6 +1,6 @@ from enum import Enum -class Color(Enum): +class TrafficLightColor(Enum): GREEN = 0 RED = 1