-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathport_instantiation_helper.py
471 lines (402 loc) · 16.9 KB
/
port_instantiation_helper.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
import json
import tkinter as tk
from tkinter import messagebox, ttk
"""
Helper tool to generate JSON for port instantiation (Serial, Relay, Ethernet)
Place this file in the root of your projects directory
Run this on a workstation, not on the processor
"""
class PortInstantiationApp:
def __init__(self, root):
self.host_classes = ["ProcessorDevice", "eBUSDevice"]
self.host_options = self.find_existing_hosts()
self.json_cache = []
self.processor_address = ""
self.processor_password = ""
self.sftp_port = 22022
self.sftp_export_available = False
try:
import paramiko
self.paramiko = paramiko
self.sftp_export_available = True
except ImportError as e:
print(e)
self.root = root
self.root.title("Port Instantiation Helper")
self.notebook = ttk.Notebook(root)
self.notebook.pack(pady=10, expand=True)
self.serial_frame = ttk.Frame(self.notebook, width=400, height=280)
self.ethernet_frame = ttk.Frame(self.notebook, width=400, height=280)
self.relay_frame = ttk.Frame(self.notebook, width=400, height=280)
self.serial_frame.pack(fill="both", expand=True)
self.ethernet_frame.pack(fill="both", expand=True)
self.relay_frame.pack(fill="both", expand=True)
self.notebook.add(self.serial_frame, text="Serial Interface")
self.notebook.add(self.ethernet_frame, text="Ethernet Client Interface")
self.notebook.add(self.relay_frame, text="Relay Interface")
self.create_serial_interface()
self.create_ethernet_interface()
self.create_relay_interface()
def find_existing_hosts(self):
"""Scans the room config JSON to try and pre-fill 'Host' fields"""
existing_hosts = []
try:
with open("src/hardware/hardware.py", "r") as hardware_file:
lines = hardware_file.readlines()
except Exception as e:
print(e)
return []
for line in lines:
line = line.strip()
if "import" in line or line.startswith("#"):
continue
for host_class in self.host_classes:
if host_class in line and "(" in line and ")" in line:
host_name = line.split("(")[1].split(")")[0].strip('"').strip("'")
existing_hosts.append(host_name)
return existing_hosts
def create_serial_interface(self):
struct = {
"Alias": "",
"Host": "",
"Port": "",
"Baud": 9600,
"Data": 8,
"Stop": 1,
"CharDelay": 0,
}
hosts_option_len = len(self.host_options)
if hosts_option_len == 1:
struct["Host"] = self.host_options[0]
elif hosts_option_len > 1:
button_frame = ttk.Frame(self.serial_frame)
button_frame.pack(pady=10)
for host in self.host_options:
button = ttk.Button(
button_frame,
text=host,
command=lambda h=host: self.serial_entries["Host"].delete(0, tk.END)
or self.serial_entries["Host"].insert(0, h),
)
button.pack(side="left")
self.serial_entries = {}
for field, default in struct.items():
label = ttk.Label(self.serial_frame, text=field)
label.pack()
entry = ttk.Entry(self.serial_frame)
entry.insert(0, default)
entry.pack()
self.serial_entries[field] = entry
# Parity
parity_label = ttk.Label(self.serial_frame, text="Parity")
parity_label.pack()
self.parity_var = tk.StringVar(value="None")
parity_frame = ttk.Frame(self.serial_frame)
parity_frame.pack()
for parity in ["None", "Odd", "Even"]:
radio = ttk.Radiobutton(
parity_frame, text=parity, variable=self.parity_var, value=parity
)
radio.pack(side="left")
# FlowControl
flowcontrol_label = ttk.Label(self.serial_frame, text="FlowControl")
flowcontrol_label.pack()
self.flowcontrol_var = tk.StringVar(value="Off")
flowcontrol_frame = ttk.Frame(self.serial_frame)
flowcontrol_frame.pack()
for flowcontrol in ["Off", "HW", "SW"]:
radio = ttk.Radiobutton(
flowcontrol_frame,
text=flowcontrol,
variable=self.flowcontrol_var,
value=flowcontrol,
)
radio.pack(side="left")
# Mode
mode_label = ttk.Label(self.serial_frame, text="Mode")
mode_label.pack()
self.mode_var = tk.StringVar(value="RS232")
mode_frame = ttk.Frame(self.serial_frame)
mode_frame.pack()
for mode in ["RS232", "RS422", "RS485"]:
radio = ttk.Radiobutton(
mode_frame, text=mode, variable=self.mode_var, value=mode
)
radio.pack(side="left")
generate_button = ttk.Button(
self.serial_frame,
text="Generate and Add",
command=self.generate_serial_json,
)
generate_button.pack(pady=10)
preview_button = ttk.Button(
self.serial_frame, text="Preview Export", command=self.show_preview
)
preview_button.pack(pady=10)
export_button = ttk.Button(
self.serial_frame, text="Export over SFTP", command=self.export_prompt
)
export_button.pack(pady=10)
def create_ethernet_interface(self):
struct = {
"Alias": "",
"Hostname": "",
"IPPort": "",
"Protocol": "TCP",
"Username": "",
"Password": "",
"ServicePort": 0,
"bufferSize": 4096,
}
self.ethernet_entries = {}
self.protocol_var = tk.StringVar(value="TCP")
for field, default in struct.items():
if field == "Protocol":
label = ttk.Label(self.ethernet_frame, text=field)
label.pack()
frame = ttk.Frame(self.ethernet_frame)
frame.pack()
for protocol in ["TCP", "UDP", "SSH"]:
radio = ttk.Radiobutton(
frame,
text=protocol,
variable=self.protocol_var,
value=protocol,
command=self.update_ethernet_fields,
)
radio.pack(side="left")
else:
label = ttk.Label(self.ethernet_frame, text=field)
label.pack()
entry = ttk.Entry(self.ethernet_frame)
entry.insert(0, default)
entry.pack()
self.ethernet_entries[field] = entry
self.update_ethernet_fields()
generate_button = ttk.Button(
self.ethernet_frame,
text="Generate and Add",
command=self.generate_ethernet_json,
)
generate_button.pack(pady=10)
preview_button = ttk.Button(
self.ethernet_frame, text="Preview Export", command=self.show_preview
)
preview_button.pack(pady=10)
export_button = ttk.Button(
self.ethernet_frame, text="Export over SFTP", command=self.export_prompt
)
export_button.pack(pady=10)
def update_ethernet_fields(self):
protocol = self.protocol_var.get()
if protocol == "TCP":
self.ethernet_entries["IPPort"].delete(0, tk.END)
self.ethernet_entries["IPPort"].insert(0, "23")
self.ethernet_entries["ServicePort"].config(state="disabled")
self.ethernet_entries["Username"].config(state="disabled")
self.ethernet_entries["Password"].config(state="disabled")
self.ethernet_entries["bufferSize"].config(state="disabled")
elif protocol == "UDP":
self.ethernet_entries["IPPort"].delete(0, tk.END)
self.ethernet_entries["ServicePort"].config(state="normal")
self.ethernet_entries["Username"].config(state="disabled")
self.ethernet_entries["Password"].config(state="disabled")
self.ethernet_entries["bufferSize"].config(state="normal")
elif protocol == "SSH":
self.ethernet_entries["IPPort"].delete(0, tk.END)
self.ethernet_entries["IPPort"].insert(0, "22023")
self.ethernet_entries["ServicePort"].config(state="disabled")
self.ethernet_entries["Username"].config(state="normal")
self.ethernet_entries["Password"].config(state="normal")
self.ethernet_entries["bufferSize"].config(state="disabled")
def create_relay_interface(self):
struct = {"Alias": "", "Host": "", "Port": ""}
hosts_option_len = len(self.host_options)
if hosts_option_len == 1:
struct["Host"] = self.host_options[0]
elif hosts_option_len > 1:
button_frame = ttk.Frame(self.relay_frame)
button_frame.pack(pady=10)
for host in self.host_options:
button = ttk.Button(
button_frame,
text=host,
command=lambda h=host: self.relay_entries["Host"].delete(0, tk.END)
or self.relay_entries["Host"].insert(0, h),
)
button.pack(side="left")
self.relay_entries = {}
for field, default in struct.items():
label = ttk.Label(self.relay_frame, text=field)
label.pack()
entry = ttk.Entry(self.relay_frame)
entry.insert(0, default)
entry.pack()
self.relay_entries[field] = entry
generate_button = ttk.Button(
self.relay_frame,
text="Generate and Add",
command=self.generate_relay_json,
)
generate_button.pack(pady=10)
preview_button = ttk.Button(
self.relay_frame, text="Preview Export", command=self.show_preview
)
preview_button.pack(pady=10)
export_button = ttk.Button(
self.relay_frame, text="Export over SFTP", command=self.export_prompt
)
export_button.pack(pady=10)
def generate_serial_json(self):
data = {field: entry.get() for field, entry in self.serial_entries.items()}
if not data["Port"].startswith("COM"):
messagebox.showerror(
"Error", "Malformed Serial Port: must start with 'COM'."
)
return
data["Class"] = "SerialInterfaceEx"
data["Parity"] = self.parity_var.get()
data["FlowControl"] = self.flowcontrol_var.get()
data["Mode"] = self.mode_var.get()
self.json_cache.append(data)
self.serial_entries["Port"].delete(0, tk.END)
self.serial_entries["Alias"].delete(0, tk.END)
def generate_ethernet_json(self):
data = {field: entry.get() for field, entry in self.ethernet_entries.items()}
if data["Hostname"] == "":
messagebox.showerror("Error", "Hostname cannot be empty.")
return
data["Class"] = "EthernetClientInterfaceEx"
protocol = self.protocol_var.get()
data["Protocol"] = protocol
tcp_pop = ["Username", "Password", "ServicePort", "bufferSize"]
udp_pop = ["Username", "Password"]
ssh_pop = ["ServicePort", "bufferSize"]
if protocol == "TCP":
pop_list = tcp_pop
elif protocol == "UDP":
pop_list = udp_pop
elif protocol == "SSH":
pop_list = ssh_pop
if data["Username"] == "" or data["Password"] == "":
messagebox.showerror("Error", "Missing Credentials for SSH")
return
for pop_item in pop_list:
data.pop(pop_item)
self.json_cache.append(data)
self.ethernet_entries["Hostname"].delete(0, tk.END)
self.ethernet_entries["Alias"].delete(0, tk.END)
def generate_relay_json(self):
data = {field: entry.get() for field, entry in self.relay_entries.items()}
if not data["Port"].startswith("RLY"):
messagebox.showerror(
"Error", "Malformed Relay Port: must start with 'RLY'."
)
return
data["Class"] = "RelayInterfaceEx"
self.json_cache.append(data)
self.relay_entries["Port"].delete(0, tk.END)
self.relay_entries["Alias"].delete(0, tk.END)
def show_preview(self):
preview_window = tk.Toplevel(self.root)
preview_window.title("JSON Cache Preview")
text = tk.Text(preview_window, wrap="word")
json_data = json.dumps(self.json_cache, indent=4)
text.insert("1.0", json_data)
text.pack(expand=True, fill="both")
text.config(state="disabled")
def export(self):
json_data = json.dumps(self.json_cache, indent=4)
hostname = self.processor_address
port = self.sftp_port
username = "admin"
password = self.processor_password
remote_file_path = "/ports.json"
try:
transport = self.paramiko.Transport((hostname, port))
transport.connect(username=username, password=password)
sftp = self.paramiko.SFTPClient.from_transport(transport)
# Check if the file already exists
try:
sftp.stat(remote_file_path)
file_exists = True
except FileNotFoundError:
file_exists = False
if not file_exists:
with sftp.file(remote_file_path, "w") as remote_file:
remote_file.write(json_data)
if file_exists:
action = messagebox.askyesnocancel(
"File Exists",
f"The file {remote_file_path} already exists. Do you want to overwrite it? (Yes to overwrite, No to append, Cancel to abort)",
)
if action is None:
transport.close()
return
elif action:
# Overwrite the file
with sftp.file(remote_file_path, "w") as remote_file:
remote_file.write(json_data)
else:
# Append to the file
with sftp.file(remote_file_path, "r") as remote_file:
existing_data = json.load(remote_file)
# Merge the existing data with the new data
if isinstance(existing_data, list):
existing_data.extend(self.json_cache)
elif isinstance(existing_data, dict):
existing_data.update(self.json_cache)
else:
raise ValueError("Unsupported JSON format for appending")
with sftp.file(remote_file_path, "w") as remote_file:
remote_file.write(json.dumps(existing_data, indent=4))
print("Export successful")
transport.close()
exit(0)
except Exception as e:
print("Export failed!")
messagebox.showerror(
"Error",
f"""Export failed: {e}
You can still copy-paste your data from the "Show Preview" button
""",
)
finally:
transport.close()
def export_prompt(self):
if not self.sftp_export_available:
messagebox.showerror(
"No SFTP Client",
"""Missing required library "paramiko".
SFTP Export is diabled for this session.
Please copy and paste manually from the "Preview Export" button
To fix this error, please `pip install paramiko`
""",
)
return
if self.json_cache == []:
messagebox.showerror("Error", "No data to export.")
return
processor_info_window = tk.Toplevel(self.root)
processor_info_window.title("Enter Processor Information")
address_label = ttk.Label(processor_info_window, text="Processor Address:")
address_label.pack(pady=10)
address_entry = ttk.Entry(processor_info_window)
address_entry.pack(pady=10)
password_label = ttk.Label(processor_info_window, text="Admin Password:")
password_label.pack(pady=10)
password_entry = ttk.Entry(processor_info_window, show="*")
password_entry.pack(pady=10)
def on_submit():
self.processor_address = address_entry.get()
self.processor_password = password_entry.get()
processor_info_window.destroy()
self.export()
submit_button = ttk.Button(
processor_info_window, text="Submit", command=on_submit
)
submit_button.pack(pady=10)
if __name__ == "__main__":
root = tk.Tk()
app = PortInstantiationApp(root)
root.mainloop()