-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGUI.py
564 lines (387 loc) · 15.6 KB
/
GUI.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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
import PySimpleGUI as sg
from opcua.common import events
import opcua.common
import string
import os
import time
import json
from opcua import ua
from opcua import Client
import sys
sg.ChangeLookAndFeel('Dark')
sg.SetOptions(element_padding=(5,10))
queue=list()
def no_config():
sg.PopupOK('No previous configuration settings found. Please enter the required credentials.')
def config_error():
sg.PopupError('Error - could not load configuration settings.')
def opc_pop():
sg.PopupOK('Connected to OPC UA Server')
def opc_error():
sg.PopupError('Error - could not connect to OPC UA Server')
def sql_pop1():
sg.PopupOK('Connected to MySQL Server Database')
def sql_pop2():
sg.PopupOK('New database created. Connected to MySQL Server Database')
def sql_error():
sg.PopupError('Error - could not connect to MySQL Server')
def server_empty():
sg.PopupError('Error - the server is empty')
def option_error():
sg.PopupError('Error - Please choose an option')
def no_children():
sg.PopupOK('Node has no children')
def traversal_done():
sg.PopupOK('Node traversal successful')
def no_push():
sg.PopupError('Error - no nodes selected to push to MQTT')
def flux_pop():
sg.PopupOK('Connected to Mainflux')
def flux_error():
sg.PopupError('Error - could not connect to Mainflux')
def table_empty():
sg.PopupOK('No records present in DATA_CONFIG_TABLE')
def device_deleted():
sg.PopupOK('Device/s deleted')
def para_deleted():
sg.PopupOK('Parameter/s deleted')
def no_device():
sg.PopupError('Error - no devices selected')
def no_parameter():
sg.PopupError('Error - no parameters selected')
def exit():
exitlayout=[
[sg.Text('Are you sure you want to close the application?')],
[sg.Button(button_text="Yes"),sg.Button(button_text="No")]]
exitwindow=sg.Window('OPC UA Client for MQTT',exitlayout).Finalize()
button,values=exitwindow.Read()
exitwindow.Close()
if(button=='Yes'):
sys.exit(0)
def config_option():
layout=[
[sg.Text('Previous configuration settings will be loaded in'),sg.Text('',key='text')],
[sg.Text('Click on the button below to setup new configuration settings')],
[sg.Button(button_text="New setup")]]
window=sg.Window('Configuration Settings',layout).Finalize()
current_time=0
paused=False
start_time=int(round(time.time()*100))
while True:
button,values=window.Read(timeout=10)
current_time=int(round(time.time()*100))-start_time
if(((current_time//100)%60)==5):
window.Close()
return False
if(button=="New setup"):
window.Close()
return True
if(button==None):
window.Close()
sys.exit(0)
window.FindElement('text').Update('{}'.format(5-(current_time//100)%60))
def opc_initialise():
opclayout= [
[sg.Text('Please enter OPC UA Server details')],
[sg.Text('Server Name ', size=(25, 1)), sg.InputText()],
[sg.Text('Endpoint URL (opc.tcp URL)', size=(25, 1)), sg.InputText()],
[sg.Submit()]]
opcwindow= sg.Window('OPC UA Server',opclayout).Finalize()
button, values = opcwindow.Read()
opcwindow.Close()
if(button==None):
sys.exit(0)
else:
return values
def sql_initialise():
sqllayout=[
[sg.Text('Please enter MySQL Server details')],
[sg.Text('Host',size=(25,1)),sg.InputText()],
[sg.Text('Username',size=(25,1)),sg.InputText()],
[sg.Text('Password',size=(25,1)),sg.InputText()],
[sg.Text('Database name',size=(25,1)),sg.Input()],
[sg.Submit()]]
sqlwindow=sg.Window('MySQL Server',sqllayout).Finalize()
button, values = sqlwindow.Read()
sqlwindow.Close()
if(button==None):
sys.exit(0)
else:
return values
def flux_initialise():
fluxlayout=[
[sg.Text('Please enter Mainflux details')],
[sg.Text('IP address',size=(25,1)),sg.InputText()],
[sg.Text('Port',size=(25,1)),sg.InputText()],
[sg.Text('Email',size=(25,1)),sg.InputText()],
[sg.Text('Password',size=(25,1)),sg.InputText()],
[sg.Submit()]]
fluxwindow=sg.Window('Mainflux',fluxlayout).Finalize()
button,values=fluxwindow.Read()
fluxwindow.Close()
if(button==None):
sys.exit(0)
else:
return values
def main_menu():
mainlayout=[[sg.Text('Choose option')],
[sg.Radio(' Display Current Node','mainmenu')],
[sg.Radio(' Traverse / Push to MQTT','mainmenu')],
[sg.Radio('Traverse entire tree','mainmenu')],
[sg.Radio('Delete record','mainmenu')],
[sg.Radio(' Exit','mainmenu')],
[sg.Submit()]]
mainwindow=sg.Window('Main Menu',mainlayout,size=(400,400)).Finalize()
button,values=mainwindow.Read()
mainwindow.Close()
if(button==None):
sys.exit(0)
else:
return values
def current_node(node_id,ntype,disp_name,nidx,value,dtype):
if(value==None):
val="N/A"
else:
val=value
if(dtype==None):
datatype="N/A"
else:
datatype=dtype
currentlayout=[
[sg.Text('Details: ')],
[sg.Text('Node ID: {}'.format(node_id))],
[sg.Text('Node ID Type: {}'.format(ntype))],
[sg.Text('Display Name: {}'.format(disp_name))],
[sg.Text('Namespace Index: {}'.format(nidx))],
[sg.Text('Value: {}'.format(val))],
[sg.Text('Datatype: {}'.format(datatype))],
[sg.OK()]]
currentwindow=sg.Window('Current Node Details',currentlayout,size=(400,400)).Finalize()
button,values=currentwindow.Read()
currentwindow.Close()
def trav_or_push():
layout=[
[sg.Text('Choose operation to perform')],
[sg.Radio('Traverse','operation')],
[sg.Radio('Push to MQTT','operation')],
[sg.Submit()]]
window=sg.Window('Traverse / Push to MQTT',layout)
button,values=window.Read()
window.Close()
return (button,values)
def path_node(z):
i=0
a=list()
a=z.get_path()
path_str="("
while(i<len(a)):
z=a[i]
output=str(z.get_display_name())
inter=str(output.split("Text:",1)[1])
disp_name=str(inter.split(")",1)[0])
if(i==len(a)-1):
path_str=path_str+disp_name
path_str=str(path_str)
else:
path_str=path_str+disp_name+'\\'
i=i+1
path_str=path_str+")"
# print("Path string: ",path_str)
return path_str
# returns node ID and display name of a node
def extra(z):
path=str(path_node(z))
output2=str(z.get_display_name()) # Display Name string
inter=str(output2.split("Text:",1)[1])
disp_name=str(inter.split(")",1)[0])
disp_name=str(disp_name)
return (disp_name,path)
def traversal_only(c,n,opclist):
col=[]
i=0
x=opclist[0] # important/opclist contains client object/needed to obtain other nodes
output=str(c.get_display_name())
inter=str(output.split("Text:",1)[1])
node_name=str(inter.split(")",1)[0])
while(i<n):
temp=x.get_node(c.get_children()[i])
name,path=extra(temp)
string1=name+" "+path
col.append([sg.Radio(string1,'traverse')])
i=i+1
layout=[
[sg.Text('Which node do you want to traverse? ')],
[sg.Text('Children of '+str(node_name))],
[sg.Column(col,size=(500,500),scrollable=True)],
[sg.Submit()]]
window=sg.Window('Node Traversal',layout).Finalize()
button,values=window.Read()
window.Close()
return (button,values)
inter=list()
def push_screen(chunk,next_page):
i=0
col=[]
while(i<len(chunk)):
temp=chunk[i]
name,path=extra(temp)
string1=name+" "+path
col.append([sg.Checkbox(string1,key=i)])
i=i+1
if(next_page==True):
layout=[
[sg.Text('Please select the nodes to send to the MQTT Broker')],
[sg.Button('Select All'),sg.Button('Deselect All')],
*col,
[sg.Button(button_text='Submit'),sg.Button(button_text='Next Page'),sg.Button(button_text='Confirm')]]
elif(next_page==False):
layout=[
[sg.Text('Please select the nodes to send to the MQTT Broker')],
[sg.Button('Select All'),sg.Button('Deselect All')],
*col,
[sg.Button(button_text='Submit'),sg.Button(button_text='Confirm')]]
window=sg.Window('Push to MQTT',layout).Finalize()
while True:
button,values=window.Read()
if(button=='Select All'):
for x in range(0,len(chunk)):
window.FindElement(x).Update(True)
elif(button=='Deselect All'):
for x in range(0,len(chunk)):
window.FindElement(x).Update(False)
elif(button=='Submit'):
check=0
for num,val in values.items():
if(val==True):
op=num
inter.append(chunk[op])
elif(val==False):
check=check+1
if(check==len(chunk)):
window.Hide()
option_error()
window.UnHide()
elif(button=='Next Page'):
window.Close()
return True
elif(button=='Confirm'):
window.Close()
pushlist=list(dict.fromkeys(inter)) # to remove duplicates
inter.clear() # clear the list
return pushlist
elif(button==None):
break
def device_or_para():
layout=[
[sg.Text('Choose operation to perform')],
[sg.Radio('Delete device/s','operation')],
[sg.Radio('Delete parameter/s','operation')],
[sg.Submit()]]
window=sg.Window('Delete records',layout)
button,values=window.Read()
window.Close()
return (button,values)
def delete_device_screen(chunk,next_page):
i=0
col=[]
while(i<len(chunk)):
temp=chunk[i]
name,path=extra(temp)
string1=name+" "+path
col.append([sg.Checkbox(string1,key=i)])
i=i+1
if(next_page==True):
layout=[
[sg.Text('Please select the devices to be deleted')],
[sg.Button('Select All'),sg.Button('Deselect All')],
*col,
[sg.Button(button_text='Submit'),sg.Button(button_text='Next Page'),sg.Button(button_text='Confirm')]]
elif(next_page==False):
layout=[
[sg.Text('Please select the devices to be deleted')],
[sg.Button('Select All'),sg.Button('Deselect All')],
*col,
[sg.Button(button_text='Submit'),sg.Button(button_text='Confirm')]]
window=sg.Window('Delete devices',layout).Finalize()
while True:
button,values=window.Read()
if(button=='Select All'):
for x in range(0,len(chunk)):
window.FindElement(x).Update(True)
elif(button=='Deselect All'):
for x in range(0,len(chunk)):
window.FindElement(x).Update(False)
elif(button=='Submit'):
check=0
for num,val in values.items():
if(val==True):
op=num
inter.append(chunk[op])
elif(val==False):
check=check+1
if(check==len(chunk)):
window.Hide()
option_error()
window.UnHide()
elif(button=='Next Page'):
window.Close()
return True
elif(button=='Confirm'):
window.Close()
pushlist=list(dict.fromkeys(inter)) # to remove duplicates
inter.clear() # clear the list
return pushlist
elif(button==None):
break
def delete_parameter_screen(chunk,next_page):
i=0
col=[]
while(i<len(chunk)):
temp=chunk[i]
name,path=extra(temp)
string1=name+" "+path
col.append([sg.Checkbox(string1,key=i)])
i=i+1
if(next_page==True):
layout=[
[sg.Text('Please select the parameters to be deleted')],
[sg.Button('Select All'),sg.Button('Deselect All')],
*col,
[sg.Button(button_text='Submit'),sg.Button(button_text='Next Page'),sg.Button(button_text='Confirm')]]
elif(next_page==False):
layout=[
[sg.Text('Please select the parameters to be deleted')],
[sg.Button('Select All'),sg.Button('Deselect All')],
*col,
[sg.Button(button_text='Submit'),sg.Button(button_text='Confirm')]]
window=sg.Window('Delete parameters',layout).Finalize()
while True:
button,values=window.Read()
if(button=='Select All'):
for x in range(0,len(chunk)):
window.FindElement(x).Update(True)
elif(button=='Deselect All'):
for x in range(0,len(chunk)):
window.FindElement(x).Update(False)
elif(button=='Submit'):
check=0
for num,val in values.items():
if(val==True):
op=num
inter.append(chunk[op])
elif(val==False):
check=check+1
if(check==len(chunk)):
window.Hide()
option_error()
window.UnHide()
elif(button=='Next Page'):
window.Close()
return True
elif(button=='Confirm'):
window.Close()
pushlist=list(dict.fromkeys(inter)) # to remove duplicates
inter.clear() # clear the list
return pushlist
elif(button==None):
break