forked from ashishps1/awesome-low-level-design
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraffic_controller.py
51 lines (43 loc) · 1.68 KB
/
traffic_controller.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import threading
import time
from signal import Signal
from road import Road
from traffic_light import TrafficLight
class TrafficController:
_instance = None
_lock = threading.Lock()
def __new__(cls):
with cls._lock:
if cls._instance is None:
cls._instance = super().__new__(cls)
cls._instance.roads = {}
return cls._instance
@classmethod
def get_instance(cls):
return cls()
def add_road(self, road: Road):
self.roads[road.id] = road
def remove_road(self, road_id: str):
self.roads.pop(road_id, None)
def start_traffic_control(self):
for road in self.roads.values():
traffic_light = road.get_traffic_light()
threading.Thread(target=self._control_traffic_light, args=(traffic_light,), daemon=True).start()
def _control_traffic_light(self, traffic_light: TrafficLight):
while True:
try:
time.sleep(traffic_light.red_duration / 1000) # Convert to seconds
traffic_light.change_signal(Signal.GREEN)
time.sleep(traffic_light.green_duration / 1000)
traffic_light.change_signal(Signal.YELLOW)
time.sleep(traffic_light.yellow_duration / 1000)
traffic_light.change_signal(Signal.RED)
except Exception as e:
print(f"Error in traffic light control: {e}")
def handle_emergency(self, road_id: str):
road = self.roads.get(road_id)
if road:
traffic_light = road.get_traffic_light()
traffic_light.change_signal(Signal.GREEN)
# Perform emergency handling logic
# ...