-
Notifications
You must be signed in to change notification settings - Fork 0
/
anomaliesCheck.py
234 lines (185 loc) · 8.25 KB
/
anomaliesCheck.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
import MySQLdb as mdb
from Tkinter import *
import tkMessageBox
# array to hold input, output, tag name values from table anomalies
# value_input = []
# value_data_name = []
# value_output = []
# value_water_level = []
t = ""
# values = ""
# values1 = ""
# values2 = ""
# This function will display an alert window when anolmaly is detected
# def alert():
# # get the values from dictCursor module
# values = ""
# values1 = ""
# values2 = ""
# for i in range(len(value_spc)):
# values = values + str(value_spc[i]) + "\n"
# for j in range(len(value_data_reading)):
# values1 = values1 + str(value_data_reading[j]) + "\n"
# for k in range(len(value_data_name)):
# values2 = values2 + str(value_data_name[k]) + "\n"
# root = Tk()
# # this text display window shows tag name
# leftSide = Text(root, height=20, width=30) # create a text display
# scrollLeft = Scrollbar(root, command=leftSide.yview)
# leftSide.configure(yscrollcommand=scrollLeft.set)
# leftSide.insert(END, "Tag Name\n\n")
# leftSide.insert(END, values2)
# leftSide.pack(side=LEFT)
# scrollLeft.pack(side=LEFT, fill=Y)
# # this text display window shows the runtime tag value
# middle = Text(root, height=20, width=50)
# scrollMiddle = Scrollbar(root, command=middle.yview)
# middle.configure(yscrollcommand=scrollMiddle.set)
# middle.insert(END, 'RunTime Tag Value\n\n')
# middle.insert(END, values1)
# middle.pack(side = LEFT)
# scrollMiddle.pack(side=LEFT, fill=Y)
# # this text display window shows the SPC
# rightSide = Text(root, height=20, width=30) # create a text display
# scrollRight = Scrollbar(root, command=rightSide.yview)
# rightSide.configure(yscrollcommand=scrollRight.set)
# rightSide.insert(END, "Set Point Constant\n\n")
# rightSide.insert(END, values)
# rightSide.pack(side=LEFT)
# scrollRight.pack(side=RIGHT, fill=Y)
# root.mainloop()
def check():
value_input = []
value_data_name = []
value_output = []
value_water_level = []
values = ""
values1 = ""
values2 = ""
con = mdb.connect('localhost', 'root', 'swat', 'swatPLCdb') # connect to the database
cur = con.cursor(mdb.cursors.DictCursor) # give the data in python dict form
cur.execute("SELECT tag_value FROM anomalies WHERE tag_name=\'HMI_FIT101.Pv\';") # query HMI_FIT101.Pv reading from SPCchecker
rows = cur.fetchall()
cur0 = con.cursor(mdb.cursors.DictCursor) # give the data in python dict form
cur.execute("SELECT tag_value FROM anomalies WHERE tag_name=\'HMI_FIT201.Pv\';") # query HMI_FIT201.Pv reading from SPCchecker
rows0 = cur.fetchall()
cur1 = con.cursor(mdb.cursors.DictCursor) # give the data in python dict form
cur1.execute("SELECT tag_value FROM anomalies WHERE tag_name=\'HMI_LIT101.Pv\';") # query tag name
rows1 = cur1.fetchall()
cur2 = con.cursor(mdb.cursors.DictCursor) # give the data in python dict form
cur.execute("SELECT tag_name FROM anomalies;")
rows2 = cur.fetchall()
# store the input values in an array
for row in rows:
value_input.append(row["tag_value"])
print row["tag_value"]
# store the output value in an array
for row0 in rows0:
value_output.append(row0["tag_value"])
print row["tag_value"]
# # store water level value in an array
for row1 in rows1:
value_water_level.append(row1["tag_value"])
print row["tag_value"]
# store the tag name in an array
for row2 in rows2:
value_data_name.append(row2["tag_name"])
con.commit()
con.close()
# the following is the checker algorithm
# define a valid range for the difference of input and output
# if the value is within the range and water level is increasing,
# something is wrong and it will display a pop up window
if(len(value_water_level)>=2):
for i in range(2):
values = values + str(value_input[len(value_input)-(i+1)]) + '\n\n'
values1 = values1 + str(value_output[len(value_output)-(i+1)]) + '\n\n'
# values2 = values2 + str(float(absvalue_output[i] - float(value_input[i]))) + '\n\n'
# print values
# print values1
# I havent decided what values should be use, so I just use a random number
if(abs(float(value_water_level[len(value_water_level)-1]) - float(value_water_level[len(value_water_level)-2])) >= 3):
# if(abs((float(value_water_level[1] - float(value_water_level[0] >= 7))))):
values2 = values2 + str(abs(float(value_water_level[len(value_water_level)-1]) - float(value_water_level[len(value_water_level)-2]))) + '\n\n'
tkMessageBox.showinfo("Alert!", "anomalies detected")
root = Tk()
# this text display window shows tag name
leftSide = Text(root, height=30, width=30, font=("Helvetica",20)) # create a text display
scrollLeft = Scrollbar(root, command=leftSide.yview)
leftSide.configure(yscrollcommand=scrollLeft.set)
leftSide.insert(END, "Tag Name\n\n")
leftSide.insert(END, value_data_name[1])
leftSide.pack(side=LEFT)
scrollLeft.pack(side=LEFT, fill=Y)
# this text display window shows the runtime tag value
middle = Text(root, height=30, width=30, font=("Helvetica",20))
scrollMiddle = Scrollbar(root, command=middle.yview)
middle.configure(yscrollcommand=scrollMiddle.set)
middle.insert(END, 'Increase in Water Level Value\n\n')
middle.insert(END, values2)
middle.pack(side = LEFT)
scrollMiddle.pack(side=LEFT, fill=Y)
rightMiddle = Text(root, height=30, width=30,font=("Helvetica",20))
scrollMiddle = Scrollbar(root, command=middle.yview)
rightMiddle.configure(yscrollcommand=scrollMiddle.set)
rightMiddle.insert(END, 'Input Flow Rate\n\n')
rightMiddle.insert(END, values)
rightMiddle.pack(side = LEFT)
scrollMiddle.pack(side=LEFT, fill=Y)
# this text display window shows the SPC
rightSide = Text(root, height=30, width=30,font=("Helvetica",20)) # create a text display
scrollRight = Scrollbar(root, command=rightSide.yview)
rightSide.configure(yscrollcommand=scrollRight.set)
rightSide.insert(END, "Ouput Flow Rate\n\n")
rightSide.insert(END, values1)
rightSide.pack(side=LEFT)
scrollRight.pack(side=RIGHT, fill=Y)
root.mainloop()
# elif(abs(float(value_water_level[len(value_water_level)-1]) - float(value_water_level[len(value_water_level)-2])) == 0):
# # if(abs((float(value_water_level[1] - float(value_water_level[0] >= 7))))):
# values2 = values2 + str(abs(float(value_water_level[len(value_water_level)-1]) - float(value_water_level[len(value_water_level)-2]))) + '\n\n'
# tkMessageBox.showinfo("Alert!", "anomalies detected")
# root = Tk()
# # this text display window shows tag name
# leftSide = Text(root, height=30, width=30, font=("Helvetica",20)) # create a text display
# scrollLeft = Scrollbar(root, command=leftSide.yview)
# leftSide.configure(yscrollcommand=scrollLeft.set)
# leftSide.insert(END, "Tag Name\n\n")
# leftSide.insert(END, value_data_name[1])
# leftSide.pack(side=LEFT)
# scrollLeft.pack(side=LEFT, fill=Y)
# # this text display window shows the runtime tag value
# middle = Text(root, height=30, width=30, font=("Helvetica",20))
# scrollMiddle = Scrollbar(root, command=middle.yview)
# middle.configure(yscrollcommand=scrollMiddle.set)
# middle.insert(END, 'Increase in Water Level Value\n\n')
# middle.insert(END, values2)
# middle.pack(side = LEFT)
# scrollMiddle.pack(side=LEFT, fill=Y)
# rightMiddle = Text(root, height=30, width=30, font=("Helvetica",20))
# scrollMiddle = Scrollbar(root, command=middle.yview)
# rightMiddle.configure(yscrollcommand=scrollMiddle.set)
# rightMiddle.insert(END, 'Input Flow Rate\n\n')
# rightMiddle.insert(END, values)
# rightMiddle.pack(side = LEFT)
# scrollMiddle.pack(side=LEFT, fill=Y)
# # this text display window shows the SPC
# rightSide = Text(root, height=30, width=30, font=("Helvetica",20)) # create a text display
# scrollRight = Scrollbar(root, command=rightSide.yview)
# rightSide.configure(yscrollcommand=scrollRight.set)
# rightSide.insert(END, "Ouput Flow Rate\n\n")
# rightSide.insert(END, values1)
# rightSide.pack(side=LEFT)
# scrollRight.pack(side=RIGHT, fill=Y)
# root.mainloop()
# value_input = []
# value_data_name = []
# value_output = []
# value_water_level = []
# clear the array for next comparison
# value_input = []
# value_data_name = []
# value_output = []
# value_water_level = []
# print float(value_water_level[len(value_water_level)-1])
# print float(value_water_level[len(value_water_level)-2])