-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtraffic_light_api.py
More file actions
44 lines (34 loc) · 1.12 KB
/
traffic_light_api.py
File metadata and controls
44 lines (34 loc) · 1.12 KB
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
import threading
from api.client import ApiClient
from hardware.traffic_light import TrafficLight
class TrafficLightApi:
def __init__(self, light: TrafficLight, sheets: ApiClient):
self._light = light
self._sheets = sheets
@property
def connected(self) -> bool:
return self._light.connected
def drive(self, color: str) -> None:
"""Directly set the physical traffic light without posting to the API."""
if color == "red":
self._light.set_red()
elif color == "green":
self._light.set_green()
elif color == "yellow":
self._light.set_yellow()
else:
self._light.set_off()
def _post(self, color: str) -> None:
threading.Thread(
target=self._sheets.set_traffic_light,
args=(color,),
daemon=True,
).start()
def request_red(self) -> None:
self._post("red")
def request_green(self) -> None:
self._post("green")
def request_yellow(self) -> None:
self._post("yellow")
def request_off(self) -> None:
self._post("off")