This repository has been archived by the owner on May 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
frontend.py
388 lines (315 loc) · 10.7 KB
/
frontend.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
from backendChallenge import *
from tkinter import *
from tkinter import messagebox
def setUpHome():
"""Sets up a home with 5 devices via shell input, returns the home"""
newHome = SmartHome()
print("🏡 Setting up your Smart Home")
# allow the user to populate home with 5 devices
while len(newHome.getDevices()) < 5:
print(f"\n➕ Adding device {len(newHome.getDevices()) + 1}/5")
choice = input("Enter 'plug' for SmartPlug, 'doorbell' for SmartDoorbell: ")
choice = choice.lower().replace(" ", "")
if choice == "plug":
print("🔌 Adding a SmartPlug")
while True: # keep asking until valid
consumptionRate = input("Enter the consumption rate of the plug: ")
if consumptionRate.isdigit() and int(consumptionRate) >= 0 and int(consumptionRate) <= 150:
consumptionRate = int(consumptionRate)
break
print("⚠️ Consumption rate must be a number between 0 and 150!")
newHome.addDevice(SmartPlug(consumptionRate))
print(f"🔌 Added a SmartPlug, with consumption rate of {consumptionRate}")
elif choice == "doorbell":
# no questions to ask for a doorbell
newHome.addDevice(SmartDoorbell())
print("🔔 Added a SmartDoorbell")
else:
print("⚠️ You must enter 'plug' or 'doorbell'!")
return newHome
class SmartHomeSystem:
"""Represents the smart home system as whole, with a GUI frontend"""
def __init__(self, home):
if not isinstance(home, SmartHome):
raise ValueError("Home must be a SmartHome")
self.home = home
self.deviceWidgets = [] # list of widgets to be destroyed on refresh
self.win = Tk()
self.win.title("Smart Home System")
self.win.minsize(400, 0) # stops the window getting smaller when widgets are destroyed or changed
self.win.resizable(False, False)
# make section frames where our widgets will go
self.headerFrame = Frame(self.win)
self.headerFrame.grid(row=0, column=0, padx=10, pady=10)
self.devicesFrame = Frame(self.win)
self.devicesFrame.grid(row=1, column=0, padx=10, pady=10)
self.footerFrame = Frame(self.win)
self.footerFrame.grid(row=2, column=0, padx=10, pady=10)
def createStaticButtons(self):
"""Creates the buttons that will always be present in the GUI"""
# turn on/off all devices in the header
turnOnAllButt = Button(
self.headerFrame,
text="Turn on all",
command=self.turnOnAll,
padx=5,
pady=5
)
turnOnAllButt.grid(row=0, column=0, padx=10)
turnOffAllButt = Button(
self.headerFrame,
text="Turn off all",
command=self.turnOffAll,
padx=5,
pady=5
)
turnOffAllButt.grid(row=0, column=1, padx=10)
# add, import, and export devices in the footer
addButt = Button(
self.footerFrame,
text="Add",
command=self.addDeviceWindow,
padx=5,
pady=5
)
addButt.grid(row=0, column=0, padx=10)
def refreshDeviceList(self):
"""
Removes all widgets from the devicesFrame and re-creates
them with current devices/statuses
"""
for widget in self.deviceWidgets:
widget.destroy()
devices = self.home.getDevices()
numDevices = len(devices)
for i in range(numDevices):
device = devices[i]
self.createDeviceRow(self.deviceWidgets, self.devicesFrame, device, i)
if numDevices == 0:
noDevicesLabel = Label(self.devicesFrame, text="No devices")
noDevicesLabel.grid(row=0, column=0, columnspan=4)
self.deviceWidgets.append(noDevicesLabel)
def createDeviceRow(self, widgetList, parentFrame, device, i):
"""Creates a row of widgets for a device, and draws it to the given frame"""
if not isinstance(parentFrame, Frame):
raise ValueError("Widget must be a Frame")
if not isinstance(device, SmartDevice):
raise ValueError("Device must be a SmartDevice")
deviceStatus = "on" if device.getSwitchedOn() else "off"
if isinstance(device, SmartPlug):
consumptionRate = device.getConsumptionRate()
textLabel = f"Plug: {deviceStatus}, Consumption: {consumptionRate}"
elif isinstance(device, SmartDoorbell):
sleepMode = "enabled" if device.getSleep() else "disabled"
textLabel = f"Doorbell: {deviceStatus}, Sleep mode: {sleepMode}"
else:
textLabel = "Unknown device"
deviceLabel = Label(parentFrame, text=textLabel, width=28)
deviceLabel.grid(row=i, column=0, padx=10, pady=5)
widgetList.append(deviceLabel) # add to list to be destroyed later
toggleButt = Button(
parentFrame,
text="Toggle",
padx=5,
command=lambda: self.toggleDeviceAt(i)
)
toggleButt.grid(row=i, column=1, pady=5, padx=2.5)
widgetList.append(toggleButt)
editButt = Button(
parentFrame,
text="Edit",
padx=5,
command=lambda: self.editDeviceWindow(i)
)
editButt.grid(row=i, column=2, pady=5, padx=2.5)
widgetList.append(editButt)
removeButt = Button(
parentFrame,
text="Delete",
padx=5,
fg="red",
command=lambda: self.removeDeviceAt(i)
)
removeButt.grid(row=i, column=3, pady=5, padx=2.5)
widgetList.append(removeButt)
############################################
# Device manipulation functions
############################################
def toggleDeviceAt(self, index):
"""Toggles the device at the given index"""
device = self.home.getDeviceAt(index)
device.toggleSwitch()
self.refreshDeviceList()
def turnOnAll(self):
"""Turns on all devices"""
self.home.turnOnAll()
self.refreshDeviceList()
def turnOffAll(self):
"""Turns off all devices"""
self.home.turnOffAll()
self.refreshDeviceList()
def removeDeviceAt(self, index):
"""Removes the device at the given index, after confirmation"""
self.home.removeDeviceAt(index)
self.refreshDeviceList()
############################################
# Add window and its related functions
############################################
def addDeviceWindow(self):
"""Shows a window that allows a user to add a device to the home"""
addWin = Toplevel(self.win)
addWin.title("Add a device")
addWin.resizable(False, False)
consumptionText = Label(addWin, text="Plug consumption rate (0-150):")
consumptionText.grid(row=0, column=0, padx=10, pady=10)
consumptionVar = IntVar(value=0)
consumptionEntry = Spinbox(
addWin,
from_=0,
to=150,
width=4,
textvariable=consumptionVar,
wrap=True
)
consumptionEntry.grid(row=0, column=1, padx=10, pady=10)
addPlugButt = Button(
addWin,
text="Add a plug",
# we need to pass the tk variable here rather than its value
# so we can show a warning if it's invalid before adding the device
command=lambda: self.addPlug(addWin, consumptionVar)
)
addPlugButt.grid(row=1, column=0, columnspan=2,
padx=10, pady=10, sticky=EW)
orText = Label(addWin, text="or")
orText.grid(row=2, column=0, columnspan=2, padx=10, pady=10, sticky=EW)
addDoorbellButt = Button(
addWin,
text="Add a doorbell",
command=lambda: self.addDoorbell(addWin)
)
addDoorbellButt.grid(row=3, column=0, columnspan=2,
padx=10, pady=10, sticky=EW)
addWin.mainloop()
def addPlug(self, addWin, consumptionVar):
"""
From the add window, adds a plug to the home,
then destroys the window and refreshes the device list
"""
try:
consumption = consumptionVar.get()
except:
messagebox.showwarning(
title="Check your values!",
message="Consumption rate must be a number, and cannot be blank"
)
return
if consumption < 0 or consumption > 150:
messagebox.showwarning(
title="Check your values!",
message="Consumption rate must be between 0 and 150"
)
return
self.home.addDevice(SmartPlug(consumption))
addWin.destroy()
self.refreshDeviceList()
def addDoorbell(self, addWin):
"""
From the add window, adds a doorbell to the home,
then destroys the window and refreshes the device list
"""
self.home.addDevice(SmartDoorbell())
addWin.destroy()
self.refreshDeviceList()
############################################
# Edit window and its related functions
############################################
def editDeviceWindow(self, i):
"""
Shows a prompt to edit the properties of a device at the given index
"""
device = self.home.getDeviceAt(i)
deviceType = "plug" if isinstance(device, SmartPlug) else "doorbell"
editWin = Toplevel(self.win)
editWin.title(f"{deviceType.title()} at index {i}")
editWin.resizable(False, False)
label = Label(editWin, text=f"{deviceType.title()} at index {i}")
label.grid(row=0, column=0, padx=10, columnspan=2, pady=10)
if deviceType == "plug":
consumptionText = Label(editWin, text="Consumption rate:")
consumptionText.grid(row=1, column=0, padx=10)
consumptionVar = IntVar(value=device.getConsumptionRate())
consumptionEntry = Spinbox(
editWin,
from_=0,
to=150,
textvariable=consumptionVar,
wrap=True
)
consumptionEntry.grid(row=2, column=0, padx=10, pady=10)
editButt = Button(
editWin,
text="Save",
# as with adding a plug, we need to pass the variable here rather than its value
# so we can show a warning if it's invalid before editing the device
command=lambda: self.editPlugConsumptionRate(editWin, i, consumptionVar)
)
editButt.grid(row=2, column=1, padx=10, pady=10)
elif deviceType == "doorbell":
sleepModeVar = BooleanVar(value=device.getSleep())
sleepCheck = Checkbutton(
editWin,
text="Enable sleep mode",
variable=sleepModeVar
)
sleepCheck.grid(row=2, column=0, padx=10, pady=10)
editButt = Button(
editWin,
text="Save",
command=lambda: self.setDoorbellSleepMode(editWin, i, sleepModeVar.get())
)
editButt.grid(row=2, column=1, padx=10, pady=10)
editWin.mainloop()
def editPlugConsumptionRate(self, editWin, i, consumptionVar):
"""
From the edit window, sets the consumption rate of a plug at the given index,
then destroys the window and refreshes the device list
"""
try:
consumption = consumptionVar.get()
except:
messagebox.showwarning(
title="Check your values!",
message="Consumption rate must be a number, and cannot be blank"
)
return
if consumption < 0 or consumption > 150:
messagebox.showwarning(
title="Check your values!",
message="Consumption rate must be between 0 and 150"
)
return
self.home.getDeviceAt(i).setConsumptionRate(consumption)
editWin.destroy()
self.refreshDeviceList()
def setDoorbellSleepMode(self, editWin, i, sleepMode):
"""
From the edit window, sets the sleep mode of a doorbell at the given index,
then destroys the window and refreshes the device list
"""
self.home.getDeviceAt(i).setSleep(sleepMode)
editWin.destroy()
self.refreshDeviceList()
############################################
# Run the GUI
############################################
def run(self):
"""Runs the GUI and sets up everything"""
self.createStaticButtons()
self.refreshDeviceList()
self.win.mainloop()
def main():
home = setUpHome()
system = SmartHomeSystem(home)
system.run()
main()