small refactorings;

added comments;
This commit is contained in:
Marco Zeisler
2021-05-24 17:51:41 +02:00
parent 3e07ea65ce
commit 8df1930a96
3 changed files with 42 additions and 19 deletions
+7 -7
View File
@@ -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)