small refactorings;
added comments;
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user