From 3e07ea65ce0d30c3448b5df220dd049d9ae7a971 Mon Sep 17 00:00:00 2001 From: Marco Zeisler Date: Mon, 24 May 2021 17:42:33 +0200 Subject: [PATCH] traffic light sends orchestrator its status on change; --- components/i_feed/traffic_light.py | 40 ++++++++++++++----- components/i_feed/vehicle.py | 11 +++-- components/orchestration/orchestrator.py | 29 ++++++++++---- components/shared/TrafficLightColor.py | 6 +++ components/{wrappers => shared}/daf.py | 0 .../message_broker_wrapper.py | 0 6 files changed, 65 insertions(+), 21 deletions(-) create mode 100644 components/shared/TrafficLightColor.py rename components/{wrappers => shared}/daf.py (100%) rename components/{wrappers => shared}/message_broker_wrapper.py (100%) diff --git a/components/i_feed/traffic_light.py b/components/i_feed/traffic_light.py index 16a2c58..66b75ce 100644 --- a/components/i_feed/traffic_light.py +++ b/components/i_feed/traffic_light.py @@ -1,21 +1,21 @@ +import pickle import threading import time from datetime import datetime -from enum import Enum from circuitbreaker import circuit # Default switching time in seconds +from shared import TrafficLightColor +from shared.TrafficLightColor import Color +from shared.message_broker_wrapper import MBWrapper + SWITCHING_TIME = 120 # Scale speed of switching by factor x SCALING = 100 class TrafficLight: - class Color(Enum): - GREEN = 0 - RED = 1 - # id of traffic light tlid: str # time between switches in seconds @@ -31,6 +31,9 @@ class TrafficLight: # stores runner thread _t: threading.Thread + # outgoing traffic light state MBWrapper + _tl_mb: MBWrapper + def __init__(self, tlid: str, switching_time: int = SWITCHING_TIME, @@ -39,16 +42,19 @@ class TrafficLight: self.switching_time = switching_time self._starting_color = starting_color + self._tl_mb = MBWrapper(exchange_name='traffic_light_state_{}'.format(self.tlid)) + self._tl_mb.setup_sender() + def start(self): self.running = True def switcher(): - num_colors = len(self.Color) + num_colors = len(TrafficLightColor.Color) # 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 = TrafficLight.Color((self._starting_color.value - 1) % num_colors) + self.current_color = Color((self._starting_color.value - 1) % num_colors) while self.running: - self.current_color = TrafficLight.Color((self.current_color.value + 1) % num_colors) + self.current_color = Color((self.current_color.value + 1) % num_colors) self.last_switch = datetime.now() self.send_status_update() time.sleep(self.switching_time / SCALING) @@ -61,10 +67,24 @@ class TrafficLight: @circuit(failure_threshold=10, expected_exception=ConnectionError) def send_status_update(self): - # TODO inform the message broker about the current status print(self.tlid, self.current_color, self.last_switch) + self._tl_mb.send(pickle.dumps({ + 'tlid': self.tlid, + 'color': self.current_color, + 'last_switch': self.last_switch + })) if __name__ == '__main__': - tl1 = TrafficLight(tlid='tl1') + # TODO fetch and use data from Entity Ident + + tl1 = TrafficLight(tlid='traffic-light-1', switching_time=120) tl1.start() + + time.sleep(1) + tl2 = TrafficLight(tlid='traffic-light-2', switching_time=240) + tl2.start() + + time.sleep(1) + tl3 = TrafficLight(tlid='traffic-light-3', switching_time=360) + tl3.start() diff --git a/components/i_feed/vehicle.py b/components/i_feed/vehicle.py index e854311..248aef2 100644 --- a/components/i_feed/vehicle.py +++ b/components/i_feed/vehicle.py @@ -7,9 +7,9 @@ from typing import Union import geopy import geopy.distance from circuitbreaker import circuit -from wrappers.daf import DAF +from shared.daf import DAF -from wrappers.message_broker_wrapper import MBWrapper +from shared.message_broker_wrapper import MBWrapper # Lat, Long STARTING_POINT = geopy.Point(48.853, 2.349) @@ -212,10 +212,15 @@ class Vehicle: if __name__ == "__main__": + # TODO fetch and use data from Entity Ident + v1 = Vehicle(vin='SB164ABN1PE082000') v1.start_driving() time.sleep(1) v2 = Vehicle(vin='SB999ABN1PE082111') v2.start_driving() - # stop manually + + time.sleep(0.5) + v3 = Vehicle(vin='SB555ABN1PE082555') + v3.start_driving() diff --git a/components/orchestration/orchestrator.py b/components/orchestration/orchestrator.py index d338e15..9c7a02c 100644 --- a/components/orchestration/orchestrator.py +++ b/components/orchestration/orchestrator.py @@ -2,15 +2,16 @@ import pickle import sys from random import randrange -from wrappers import daf -from wrappers.message_broker_wrapper import MBWrapper +from shared import daf +from shared.TrafficLightColor import Color +from shared.message_broker_wrapper import MBWrapper sys.modules['daf'] = daf class Orchestrator: vins = [] - tls = [] + tls = {} _daf_mbs = {} _velocity_mbs = {} @@ -19,11 +20,14 @@ class Orchestrator: # TODO fetch available car VINs from Entity Ident self.vins.append('SB164ABN1PE082000') self.vins.append('SB999ABN1PE082111') + self.vins.append('SB555ABN1PE082555') + print('vins', self.vins) # TODO fetch available TLs from Entity Ident - self.tls.append('traffic-light-1') - self.tls.append('traffic-light-2') - self.tls.append('traffic-light-3') + 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} + print('tls', self.tls) def setup_msg_queues(self): for vin in self.vins: @@ -35,11 +39,20 @@ class Orchestrator: velocity_channel.setup_sender() self._velocity_mbs[vin] = velocity_channel + 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) print(received_daf_object) - # TODO use the data from the traffic lights to transmit a new target velocity for this vehicle to achieve - # a green wave + # 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)) + + def handle_tl_state_receive(self, msg): + 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 new file mode 100644 index 0000000..784fd30 --- /dev/null +++ b/components/shared/TrafficLightColor.py @@ -0,0 +1,6 @@ +from enum import Enum + + +class Color(Enum): + GREEN = 0 + RED = 1 diff --git a/components/wrappers/daf.py b/components/shared/daf.py similarity index 100% rename from components/wrappers/daf.py rename to components/shared/daf.py diff --git a/components/wrappers/message_broker_wrapper.py b/components/shared/message_broker_wrapper.py similarity index 100% rename from components/wrappers/message_broker_wrapper.py rename to components/shared/message_broker_wrapper.py