-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhubUI.py
More file actions
257 lines (209 loc) · 11.4 KB
/
hubUI.py
File metadata and controls
257 lines (209 loc) · 11.4 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
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
from tkinter import Tk, Label, Entry, Button, Text, Scrollbar, Toplevel, END, messagebox, ttk, StringVar
from threading import Thread
from hub import Hub
from data.config import *
class HubUI:
def __init__(self, hub):
self.hub = hub
self.root = Tk()
self.root.title("IoT Hub")
self.root.geometry("600x500") # Set a consistent window size
self.setup_ui()
def setup_ui(self):
# Device Registration Frame
reg_frame = ttk.LabelFrame(self.root, text="Device Registration")
reg_frame.grid(row=0, column=0, padx=10, pady=10, sticky="ew", columnspan=2)
reg_frame.columnconfigure(1, weight=1)
Label(reg_frame, text="Device ID:").grid(row=0, column=0, sticky="w", padx=10, pady=5)
self.device_id_entry = Entry(reg_frame)
self.device_id_entry.grid(row=0, column=1, padx=10, pady=5, sticky="ew")
Label(reg_frame, text="Device IP:").grid(row=1, column=0, sticky="w", padx=10, pady=5)
self.device_ip_entry = Entry(reg_frame)
self.device_ip_entry.grid(row=1, column=1, padx=10, pady=5, sticky="ew")
Label(reg_frame, text="Device Port:").grid(row=2, column=0, sticky="w", padx=10, pady=5)
self.device_port_entry = Entry(reg_frame)
self.device_port_entry.grid(row=2, column=1, padx=10, pady=5, sticky="ew")
Label(reg_frame, text="Location:").grid(row=3, column=0, sticky="w", padx=10, pady=5)
self.device_location_entry = Entry(reg_frame)
self.device_location_entry.grid(row=3, column=1, padx=10, pady=5, sticky="ew")
# Buttons
button_frame = ttk.Frame(self.root)
button_frame.grid(row=1, column=0, padx=10, pady=10, sticky="ew", columnspan=2)
button_frame.columnconfigure((0, 1, 2, 3), weight=1)
self.start_consensus_button = Button(button_frame, text="Start Consensus", command=self.hub.start_consensus_protocol)
self.start_consensus_button.grid(row=0, column=0, padx=5, pady=5)
self.add_device_button = Button(button_frame, text="Add Device", command=self.add_device)
self.add_device_button.grid(row=0, column=1, padx=5, pady=5)
self.send_button = Button(button_frame, text="Send Message", command=self.open_send_message_popup)
self.send_button.grid(row=0, column=2, padx=5, pady=5)
self.view_blockchain_button = Button(button_frame, text="View Blockchain", command=self.view_blockchain)
self.view_blockchain_button.grid(row=0, column=3, padx=5, pady=5)
# Device List Display
list_frame = ttk.Frame(self.root)
list_frame.grid(row=2, column=0, columnspan=2, padx=10, pady=10, sticky="nsew")
list_frame.columnconfigure(0, weight=1)
list_frame.rowconfigure(0, weight=1)
self.device_list_text = Text(list_frame, height=10, wrap="word")
self.device_list_text.grid(row=0, column=0, sticky="nsew")
self.device_list_text.config(state="disabled")
scrollbar_device_list = Scrollbar(list_frame, command=self.device_list_text.yview)
scrollbar_device_list.grid(row=0, column=1, sticky="ns")
self.device_list_text['yscrollcommand'] = scrollbar_device_list.set
# Device selection for blockchain data display
self.device_var = StringVar()
dropdown_frame = ttk.Frame(self.root)
dropdown_frame.grid(row=3, column=0, columnspan=2, pady=10, sticky="ew")
dropdown_frame.columnconfigure(0, weight=1)
self.blockchain_device_dropdown = ttk.Combobox(dropdown_frame, textvariable=self.device_var)
self.blockchain_device_dropdown.grid(row=0, column=0, padx=10, pady=5, sticky="ew")
# Start message receiving thread
receive_thread = Thread(target=self.receive_messages)
receive_thread.daemon = True
receive_thread.start()
self.root.protocol("WM_DELETE_WINDOW", self.on_close)
self.root.mainloop()
def add_device(self):
device_id = self.device_id_entry.get()
device_ip = self.device_ip_entry.get()
device_port = self.device_port_entry.get()
location = self.device_location_entry.get()
if device_id and device_ip and device_port:
try:
self.hub.register_device(device_id, device_ip, device_port, location)
messagebox.showinfo("Device Added",
f"Device {device_id} added successfully.\n"
f"Location: {location}")
self.update_device_list()
# Clear entry fields
self.device_id_entry.delete(0, END)
self.device_ip_entry.delete(0, END)
self.device_port_entry.delete(0, END)
self.device_location_entry.delete(0, END)
except Exception as e:
messagebox.showerror("Error", f"Failed to add device: {str(e)}")
else:
messagebox.showerror("Error", "All fields must be filled.")
def open_send_message_popup(self):
popup = Toplevel(self.root)
popup.title("Send Message")
popup.geometry("600x400")
# Get list of registered devices
devices = list(self.hub._authenticated_devices.keys())
# Device selection
Label(popup, text="Select Device:").grid(row=0, column=0, padx=5, pady=5)
device_var = ttk.Combobox(popup, values=devices)
device_var.grid(row=0, column=1, padx=5, pady=5)
# Command selection
Label(popup, text="Command:").grid(row=1, column=0, padx=5, pady=10)
command_entry = Entry(popup)
command_entry.grid(row=1, column=1, padx=5, pady=5)
# Parameter input
Label(popup, text="Parameter (optional):").grid(row=2, column=0, padx=5, pady=5)
param_entry = Entry(popup)
param_entry.grid(row=2, column=1, padx=5, pady=5)
# Help text for common commands
help_text = Text(popup, height=8, wrap="word")
help_text.grid(row=3, column=0, columnspan=2, padx=10, pady=5)
help_text.insert(END, "Common Commands:\n"
"Cameras: get_status, set_status, get_location\n"
"DoorLocks: get_state, set_state, get_status, set_status\n"
"Thermostats: get_temperature, set_temperature, get_status")
help_text.config(state="disabled")
# Send button
send_button = Button(popup, text="Send",
command=lambda: self.send_message_popup(
device_var.get(),
f"{command_entry.get()};{param_entry.get()}" if param_entry.get()
else command_entry.get(),
popup))
send_button.grid(row=4, column=0, columnspan=2, pady=10)
def send_message_popup(self, device_id, message, popup):
popup.destroy()
if device_id and message:
try:
recipient_ip, recipient_port = self.hub._authenticated_devices[device_id]
self.hub.send(message, (recipient_ip, int(recipient_port)))
messagebox.showinfo("Message Sent", f"Message sent successfully to {device_id}.")
except KeyError:
messagebox.showerror("Error", f"Device {device_id} not found.")
except Exception as e:
messagebox.showerror("Error", f"Failed to send message: {str(e)}")
else:
messagebox.showerror("Error", "Device ID and message must be provided.")
def receive_messages(self):
while True:
try:
message, sender_addr = self.hub.receive()
self.display_received_message(message)
except Exception as e:
print(f"Error in receive_messages: {e}")
continue
def display_received_message(self, message):
try:
print(f"Received message: {message}")
except Exception as e:
print(f"Error displaying message: {e}")
def update_device_list(self):
self.device_list_text.config(state="normal")
self.device_list_text.delete(1.0, END)
for device_id, (device_ip, device_port) in self.hub._authenticated_devices.items():
location = self.hub.get_device_location(device_id)
device_info = f"{device_id} - IP: {device_ip}, Port: {device_port}, Location: {location}\n"
self.device_list_text.insert(END, device_info)
self.device_list_text.config(state="disabled")
# Update device dropdown for blockchain view with registered devices
device_ids = list(self.hub._authenticated_devices.keys())
self.blockchain_device_dropdown['values'] = device_ids
if device_ids:
self.blockchain_device_dropdown.set(device_ids[0])
# Shows updating blockchain of selected registered device
def view_blockchain(self):
selected_device = self.device_var.get()
if not selected_device: # Must select device via dropdown
messagebox.showerror("Error", "Please select a device.")
return
blockchain_window = Toplevel(self.root)
blockchain_window.title(f"Blockchain View - {selected_device}")
blockchain_window.geometry("800x600")
blockchain_text = Text(blockchain_window, wrap="word", height=30, width=90)
scrollbar = Scrollbar(blockchain_window, command=blockchain_text.yview)
blockchain_text.configure(yscrollcommand=scrollbar.set)
blockchain_text.pack(side="left", fill="both", expand=True)
scrollbar.pack(side="right", fill="y")
# Shows the current blockchain for device selected
def update_blockchain_view():
try:
current_device = self.device_var.get()
blockchain_data = self.hub.get_blockchain_data(current_device) # Fetches device blockchain data
blockchain_text.config(state="normal")
blockchain_text.delete(1.0, END)
if not blockchain_data:
blockchain_text.insert(END, "No blockchain data found")
else: # Iterates through blockchain and displays each block and details
for block in blockchain_data:
blockchain_text.insert(END, f"\nBlock {block['index']}:\n")
blockchain_text.insert(END, f"Timestamp: {block['timestamp']}\n")
blockchain_text.insert(END, f"Previous Hash: {block['previous_hash']}\n")
blockchain_text.insert(END, f"Interactions: {block['interactions']}\n")
blockchain_text.config(state="disabled")
except Exception as e:
blockchain_text.config(state="normal")
blockchain_text.delete(1.0, END)
blockchain_text.insert(END, f"Error retrieving blockchain data: {str(e)}\n")
blockchain_text.config(state="disabled")
if blockchain_window.winfo_exists():
blockchain_window.after(5000, update_blockchain_view)
# Refreshes window every 5 seconds
update_blockchain_view()
def on_close(self):
self.root.destroy()
exit()
def main():
print("Setting up a new Hub..")
input_ip = HUB_IP
input_port = HUB_PORT
hub = Hub("HUB", input_ip, input_port)
hub.setEncryption(KEY, upperCaseAll=False, removeSpace=False)
hub_ui = HubUI(hub)
if __name__ == "__main__":
main()