-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.py
87 lines (69 loc) · 3.14 KB
/
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
from device import Device, Light, Thermostat
class Controller:
def __init__(self, controller_id, name):
self.controller_id = controller_id
self.name = name
self.devices = []
def add_device(self, device: Device):
self.devices.append(device)
def get_device(self, device_id):
for device in self.devices:
if device.device_id == device_id:
return device
return None
def control_device(self, device_id, action):
device = self.get_device(device_id)
if device:
if action == "on":
return device.turn_on()
elif action == "off":
return device.turn_off()
else:
print(f"Unknown action '{action}' for device '{device.name}'")
else:
print(f"No device found with ID {device_id}")
def monitor_devices(self):
for device in self.devices:
print(device)
class LightingController(Controller):
colors = ['warm white', 'neutral white', 'cool white']
def __init__(self, controller_id, name):
super().__init__(controller_id, name)
def adjust_lighting(self, brightness, color):
for device in self.devices:
if isinstance(device, Light):
device.brightness = brightness
if color in self.colors:
device.color = color
print(f"{self.name}: Adjusted {device.name} to brightness {brightness} and color {color}")
else:
print(f"Unknown color '{color}' for device '{device.name}'")
class TemperatureController(Controller):
def __init__(self, controller_id, name):
super().__init__(controller_id, name)
self.target_temperature = None
def maintain_temperature(self):
for device in self.devices:
if isinstance(device, Thermostat):
current_temp = device.current_temp
target_temp = device.target_temp
if target_temp is not None:
if current_temp < target_temp - 1:
print(f"{device.name} start to warming temperature to {target_temp}")
elif current_temp > target_temp:
print(f"{device.name} start cooling down to {target_temp}")
else:
print(f"{device.name}'s temperature is maintained at {device.current_temp}°C")
else:
print(f"{device.name} has no target temperature set")
else:
print(f"{device.name} is not a thermostat")
def set_target_temperature(self, thermostat_id, target_temp):
thermostat = self.get_device(thermostat_id)
if isinstance(thermostat, Thermostat):
thermostat.target_temp = target_temp
print(f"{self.name}: Set {thermostat.name}'s target temperature to {target_temp}°C")
else:
print(f"{self.name}: No thermostat found with ID {thermostat_id}")
def get_current_temperature(self, device: Thermostat):
print(f"{device.name} current temperature is {device.current_temp}°C")