-
Notifications
You must be signed in to change notification settings - Fork 353
Expand file tree
/
Copy pathgui-demo
More file actions
374 lines (302 loc) · 10.2 KB
/
Copy pathgui-demo
File metadata and controls
374 lines (302 loc) · 10.2 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
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
import tkinter as tk
import time
from opcua import Client
import mysql.connector
import datetime
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
# Enter Simulation Server URL
url = "";
# Database connectivity
mydb = mysql.connector.connect(
host="",
user="",
passwd="",
database=""
)
mycursor = mydb.cursor()
client = Client(url)
client.connect()
# GUI Code
root = tk.Tk()
root.configure(background='#00bbff')
clabel = tk.Label(root, text="Generate OPC UA Values")
clabel.pack()
clabel.configure(background='white')
clabel.place(x=500, y=10, height=100, width=500)
clabel1 = tk.Label(root, text="Counter Value : ",padx=10, pady=10)
clabel1.pack()
clabel1.place(x=500, y=200)
clabel1.configure(background='white')
label1 = tk.Label(root, fg="black")
label1.pack()
label1.configure(background='#00bbff')
label1.place(x=500, y=250)
hlimit1 = tk.Label(root)
hlimit1.pack()
hlimit1.place(x=500, y=300)
hlimit1.configure(background='#00bbff', fg='black')
r2 = tk.Label(root, text="> 0.5")
r2.pack()
r2.place(x=400, y=300)
r2.configure(background="white")
clabel2 = tk.Label(root, text="Sinusoidal Value : ", fg="white",padx=10, pady=10)
clabel2.pack()
clabel2.place(x=700, y=200)
clabel2.configure(background='green')
label2 = tk.Label(root, fg="black")
label2.pack()
label2.configure(background='#00bbff')
label2.place(x=700, y=250)
hlimit2 = tk.Label(root)
hlimit2.pack()
hlimit2.place(x=700, y=300)
hlimit2.configure(background='#00bbff', fg='black')
clabel3 = tk.Label(root, text="Random Value : ", fg="white",padx=10, pady=10)
clabel3.pack()
clabel3.place(x=900, y=200)
clabel3.configure(background='green')
label3 = tk.Label(root, fg="black")
label3.pack()
label3.configure(background='#00bbff')
label3.place(x=900, y=250)
hlimit3 = tk.Label(root)
hlimit3.pack()
hlimit3.place(x=900, y=300)
hlimit3.configure(background='#00bbff', fg='black')
clabel4 = tk.Label(root, text="Sawtooth Value : ", fg="white",padx=10, pady=10)
clabel4.pack()
clabel4.place(x=1100, y=200)
clabel4.configure(background='green')
label4 = tk.Label(root, fg="black")
label4.pack()
label4.configure(background='#00bbff')
label4.place(x=1100, y=250)
hlimit4 = tk.Label(root)
hlimit4.pack()
hlimit4.place(x=1100, y=300)
hlimit4.configure(background='#00bbff', fg='black')
clabel5 = tk.Label(root, text="Triangle Value : ", fg="white",padx=10, pady=10)
clabel5.pack()
clabel5.place(x=1300, y=200)
clabel5.configure(background='green')
label5 = tk.Label(root, fg="black")
label5.pack()
label5.configure(background='#00bbff')
label5.place(x=1300, y=250)
hlimit5 = tk.Label(root)
hlimit5.pack()
hlimit5.place(x=1300, y=300)
hlimit5.configure(background='#00bbff', fg='black')
c2 = 0
c3 = 0
c4 = 0
c5 = 0
counterArray = []
sineArray = []
randomArray = []
sawtoothArray = []
triangleArray = []
timeArray = []
sinet1 = ""
sinet2 = ""
randomt1 = ""
randomt2 = ""
sawtootht1 = ""
sawtootht2 = ""
trianglet1 = ""
trianglet2 = ""
temp1 = 1
temp2 = 1
temp3 = 1
temp4 = 1
countsine = 0
countrandom = 0
countsawtooth = 0
counttriangle = 0
def counter_label1():
global timeArray
global sineArray
global randomArray
global sawtoothArray
global triangleArray
global countsine
global countrandom
global countsawtooth
global counttriangle
named_tuple = time.localtime() # get struct_time
time_string = time.strftime("%H:%M:%S", named_tuple)
timeArray.append(time_string)
button1.configure(state='disabled')
Counter = client.get_node("ns=5;s=Counter1")
counter = Counter.get_value()
label1.config(text=str(counter))
counterArray.append(counter)
Sine = client.get_node("ns=5;s=Sinusoid1")
sine = Sine.get_value()
label2.config(text=str(sine))
sineArray.append(sine)
if sine > 0.5 :
if countsine == 0 :
global sinet1
sinet1 = datetime.datetime.now()
countsine += 1
clabel2.configure(background='red')
else :
global sinet2
global temp1
if countsine is not 0 :
sinet2 = datetime.datetime.now()
#timediff = sinet2-sinet1
formatted_datetime1 = sinet1.strftime('%Y-%m-%d %H:%M:%S')
formatted_datetime2 = sinet2.strftime('%Y-%m-%d %H:%M:%S')
sql1 = "INSERT INTO sinealert(Number,Generation,Checked,Downtime) VALUES (%d,'%s','%s','%s')"
val1 = (temp1,formatted_datetime1,formatted_datetime2,countsine)
mycursor.execute(sql1 %val1)
temp1 += 1
clabel2.configure(background='green')
countsine = 0
Random = client.get_node("ns=5;s=Random1")
random = Random.get_value()
label3.config(text=str(random))
randomArray.append(random)
if random > 0.5 :
if countrandom == 0 :
global randomt1
randomt1 = datetime.datetime.now()
countrandom += 1
clabel3.configure(background='red')
else :
global randomt2
global temp2
if countrandom is not 0 :
randomt2 = datetime.datetime.now()
#timediff = randomt2-randomt1
formatted_datetime1 = randomt1.strftime('%Y-%m-%d %H:%M:%S')
formatted_datetime2 = randomt2.strftime('%Y-%m-%d %H:%M:%S')
sql2 = "INSERT INTO randomalert(Number,Generation,Checked,Downtime) VALUES (%d,'%s','%s','%s')"
val2 = (temp2,formatted_datetime1,formatted_datetime2,countrandom)
mycursor.execute(sql2 %val2)
temp2 += 1
clabel3.configure(background='green')
countrandom = 0
Sawtooth = client.get_node("ns=5;s=Sawtooth1")
sawtooth = Sawtooth.get_value()
label4.config(text=str(sawtooth))
sawtoothArray.append(sawtooth)
if sawtooth > 0.5 :
if countsawtooth == 0 :
global sawtootht1
sawtootht1 = datetime.datetime.now()
countsawtooth += 1
clabel4.configure(background='red')
else :
global sawtootht2
global temp3
if countsawtooth is not 0 :
sawtootht2 = datetime.datetime.now()
#timediff = sawtootht2-sawtootht1
formatted_datetime1 = sawtootht1.strftime('%Y-%m-%d %H:%M:%S')
formatted_datetime2 = sawtootht2.strftime('%Y-%m-%d %H:%M:%S')
sql3 = "INSERT INTO sawtoothalert(Number,Generation,Checked,Downtime) VALUES (%d,'%s','%s','%s')"
val3 = (temp3,formatted_datetime1,formatted_datetime2,countsawtooth)
mycursor.execute(sql3 %val3)
temp3 += 1
clabel4.configure(background='green')
countsawtooth = 0
Triangle = client.get_node("ns=5;s=Triangle1")
triangle = Triangle.get_value()
label5.config(text=str(triangle))
triangleArray.append(triangle)
if triangle > 0.5 :
if counttriangle == 0 :
global trianglet1
trianglet1 = datetime.datetime.now()
counttriangle += 1
clabel5.configure(background='red')
else :
global trianglet2
global temp4
trianglet2 = datetime.datetime.now()
if counttriangle is not 0 :
#timediff = trianglet2-trianglet1
formatted_datetime1 = trianglet1.strftime('%Y-%m-%d %H:%M:%S')
formatted_datetime2 = trianglet2.strftime('%Y-%m-%d %H:%M:%S')
sql4 = "INSERT INTO trianglealert(Number,Generation,Checked,Downtime) VALUES (%d,'%s','%s','%s')"
val4 = (temp4,formatted_datetime1,formatted_datetime2,counttriangle)
mycursor.execute(sql4 %val4)
temp4 += 1
clabel5.configure(background='green')
counttriangle = 0
root.after(1000, counter_label1)
sql = "INSERT INTO logfile1 (Counter,Sinusoidal,Random,Sawtooth,Triangle) VALUES (%d,%2.6f,%2.6f,%2.6f,%2.6f)"
val = (counter,sine,random,sawtooth,triangle)
mycursor.execute(sql %val)
mydb.commit()
timeArray = timeArray[-5:]
sineArray = sineArray[-5:]
randomArray = randomArray[-5:]
sawtoothArray = sawtoothArray[-5:]
triangleArray = triangleArray[-5:]
a.plot(timeArray,sineArray,"b-*",timeArray,randomArray,"r-o")
a.set_xlabel("Time")
a.set_ylabel("Sinusoidal (Blue) Random (Red)")
a.grid()
canvas.draw()
b.plot(timeArray,randomArray,marker='o')
b.set_xlabel("Time")
b.set_ylabel("Random")
b.grid()
canvas2.draw()
c.plot(timeArray,sawtoothArray,"b-*",timeArray,triangleArray,"r-o")
c.set_xlabel("Time")
c.set_ylabel("Sawtooth (Blue) Triangle (Red)")
c.grid()
canvas3.draw()
a.cla()
b.cla()
c.cla()
def on_enter(event):
button1['background']="#06a2db"
button1['fg']="white"
def on_leave(event):
button1['background']="white"
button1['fg']="#06a2db"
def on_enter2(event):
button2['background']="#06a2db"
button2['fg']="white"
def on_leave2(event):
button2['background']="white"
button2['fg']="#06a2db"
root.title("OPC Generator")
button1 = tk.Button(root, text="Start", width=25, command=counter_label1, padx=6, pady=6, font='Helvetica 10 bold ')
button1.configure(background="white", fg="#06a2db")
button1.pack()
button1.place(x=50,y=200)
button1.bind("<Enter>",on_enter)
button1.bind("<Leave>",on_leave)
button2 = tk.Button(root, text="Stop", width=25, command=root.destroy, padx=6, pady=6, font='Helvetica 10 bold ')
button2.configure(background="white", fg="#06a2db")
button2.pack()
button2.place(x=50,y=300)
button2.bind("<Enter>",on_enter2)
button2.bind("<Leave>",on_leave2)
figure = Figure(figsize=(6,5),dpi=70)
figure.suptitle("Sinusoidal v/s Random Visualization")
figure2 = Figure(figsize=(6,5),dpi=70)
figure2.suptitle("Random Visualization")
figure3 = Figure(figsize=(6,5),dpi=70)
figure3.suptitle("Sawtooth v/s Triangle Visualization")
a = figure.add_subplot(111)
b = figure2.add_subplot(111)
c = figure3.add_subplot(111)
canvas = FigureCanvasTkAgg(figure, master=root)
canvas2 = FigureCanvasTkAgg(figure2, master=root)
canvas3 = FigureCanvasTkAgg(figure3, master=root)
canvas.get_tk_widget().pack()
canvas.get_tk_widget().place(x=10,y=450)
canvas2.get_tk_widget().pack()
canvas2.get_tk_widget().place(x=550,y=450)
canvas3.get_tk_widget().pack()
canvas3.get_tk_widget().place(x=1100,y=450)
root.mainloop()