-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
498 lines (425 loc) · 19.4 KB
/
main.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
import threading
from datetime import datetime
import matplotlib
import matplotlib.pyplot as plt
import serial
from PyQt5.QtCore import QTimer, pyqtSlot
from matplotlib.backends.backend_qt5 import NavigationToolbar2QT as NavigationToolbar
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from WriteIntoSQL import WriteIntoSQL
matplotlib.use("Qt5Agg")
from Real_time import Ui_MainWindow
import sys
import time
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import *
import dialog
from Myport import My_port
import serial.tools.list_ports
from neuralNetwork import BPNeuralNetwork
import math
now = datetime.now()
global outTimeNum, lastState, modeChoice
outTimeNum = 0
modeChoice = 1
lastState = 1
# warning Dialog
MESSAGE = "DANGER!TOO MANY NUMBER OUTNUMBER THE CRITICAL NUMBER!"
TITLE = "MESSAGE"
HEAD = " time\t\tnumber\t\ttemperature(℃)\thumidity(rh)\tconcentration(ml/立方米)\t\tlight(lx)\tstate\n"
SQL_Table_name = 'time' + now.strftime("%Y%m%d")
global j_1, warningTimes
warningTimes = 1
j_1 = len(HEAD)
NumberOfGroups = ['2', '3', '4', '5', '6', '7', '8'] # 要统计的数据数量
baudrate_comboBox = ['100', '300', '600', '1200', '2400', '4800', '9600', '14400', '19200', '38400', '56000', '57600',
'115200', '128000', '256000']
class MyplotOne(FigureCanvas):
def __init__(self, co="red", name="data", parent=None, width=10, height=6, dpi=100):
# normalized for 中文显示和负号
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
# new figure
self.fig = Figure(figsize=(width, height), dpi=dpi)
FigureCanvas.__init__(self, self.fig)
self.setParent(parent)
# 分区
self.axes1 = self.fig.add_subplot(231)
self.axes2 = self.fig.add_subplot(232)
self.axes3 = self.fig.add_subplot(233)
self.axes4 = self.fig.add_subplot(234)
self.axes5 = self.fig.add_subplot(235)
self.compute_initial_figure()
FigureCanvas.setSizePolicy(self,
QtWidgets.QSizePolicy.Expanding,
QtWidgets.QSizePolicy.Expanding)
FigureCanvas.updateGeometry(self)
def compute_initial_figure(self):
pass
# class Myplot for plotting with matplotlib
class MyplotTwo(FigureCanvas):
def __init__(self, co="red", name="data", parent=None, width=5, height=3, dpi=100):
# normalized for 中文显示和负号
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
# new figure
self.fig = Figure(figsize=(width, height), dpi=dpi)
FigureCanvas.__init__(self, self.fig)
self.setParent(parent)
# 分区
self.axes = self.fig.add_subplot(111)
self.compute_initial_figure()
FigureCanvas.setSizePolicy(self,
QtWidgets.QSizePolicy.Expanding,
QtWidgets.QSizePolicy.Expanding)
FigureCanvas.updateGeometry(self)
def compute_initial_figure(self):
pass
# class dynamic_Graph for dynamic graph
class DynamicFigOne(MyplotOne):
def __init__(self, *args, **kwargs):
MyplotOne.__init__(self, *args, **kwargs)
def compute_initial_figure(self):
# input data
timex = [1, 10]
temp1 = [0, 0]
self.axes1.plot(timex, temp1, '-ob', color='blue', label='temperature')
self.axes2.plot(timex, temp1, '-ob', color='red', label='humidity')
self.axes3.plot(timex, temp1, '-ob', color='green', label='MQ2')
self.axes4.plot(timex, temp1, '-ob', color='black', label='MQ4')
self.axes5.plot(timex, temp1, '-ob', color='orange', label='light')
self.axes1.set_xlabel("time", fontsize=15)
self.axes1.set_ylabel("temperature", fontsize=15)
self.axes1.legend(loc=0, ncol=1)
self.axes2.set_xlabel("time", fontsize=15)
self.axes2.set_ylabel("humidity", fontsize=15)
self.axes2.legend(loc=0, ncol=1)
self.axes3.set_xlabel("time", fontsize=15)
self.axes3.set_ylabel("MQ2", fontsize=15)
self.axes3.legend(loc=0, ncol=1)
self.axes4.set_xlabel("time", fontsize=15)
self.axes4.set_ylabel("MQ4", fontsize=15)
self.axes4.legend(loc=0, ncol=1)
self.axes5.set_xlabel("time", fontsize=15)
self.axes5.set_ylabel("light", fontsize=15)
self.axes5.legend(loc=0, ncol=1)
self.fig.tight_layout(pad=1, w_pad=0.1, h_pad=0)
class DynamicFigTwo(MyplotTwo):
def __init__(self, *args, **kwargs):
MyplotTwo.__init__(self, *args, **kwargs)
def compute_initial_figure(self):
# input data
timex = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
temp1 = [0, -1, -2, -1, 0, -3, -1, -1, -3, 0]
temp2 = [45, 46, 45, 46, 44, 46, 44, 45, 46, 44]
temp3 = [28, 30, 35, 28, 31, 32, 31, 35, 28, 32]
temp4 = [36, 50, 43, 39, 42, 32, 31, 45, 28, 48]
temp5 = [997, 996, 995, 997, 995, 167, 168, 175, 180, 167]
self.axes.plot(timex, temp1, '-ob', color="blue", label="temperature")
self.axes.plot(timex, temp2, '-ob', color="red", label="humidity")
self.axes.plot(timex, temp3, '-ob', color="green", label="MQ2")
self.axes.plot(timex, temp4, '-ob', color="black", label="MQ4")
self.axes.plot(timex, temp5, '-ob', color="orange", label="light")
self.axes.set_xlabel("time", fontsize=15)
self.axes.set_ylabel("data", fontsize=15)
self.axes.legend(loc=0, ncol=1)
self.fig.tight_layout(pad=1, w_pad=0.1, h_pad=0)
"""
warningDialog defined myself
"""
# noinspection PyAttributeOutsideInit
class WarningQDialog(QMessageBox):
def __init__(self, message):
super(WarningQDialog, self).__init__()
self.message = message
self.initUI()
def initUI(self):
self.warningDialog = QMessageBox.warning(self, 'ERROR', self.message, QMessageBox.Abort)
# noinspection PyArgumentList
class ModeOneWindow(QMainWindow):
def __init__(self, gridlayout):
super(ModeOneWindow, self).__init__(parent=None)
self.gridlayout = gridlayout
self.fig = DynamicFigOne(dpi=120)
self.fig_ntb = NavigationToolbar(self.fig, self)
self.gridlayout.addWidget(self.fig, 0, 0, 1, 1)
self.gridlayout.addWidget(self.fig_ntb, 1, 0, 1, 1)
class ModeTwoWindow(QMainWindow):
def __init__(self, gridlayout, parent=None):
super(ModeTwoWindow, self).__init__(parent)
self.gridlayout = gridlayout
self.fig = DynamicFigTwo(dpi=120)
self.fig_ntb = NavigationToolbar(self.fig, self)
self.gridlayout.addWidget(self.fig, 0, 0, 1, 1)
self.gridlayout.addWidget(self.fig_ntb, 1, 0, 1, 1)
# noinspection PyProtectedMember,PyAttributeOutsideInit,PyBroadException,PyUnresolvedReferences,PyShadowingNames,PyGlobalUndefined
class AppWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(AppWindow, self).__init__(parent)
global trainingSet
# creating nuetral network with 5 input nodes, 3 hiden nodes and 1 output node
self.neuralNetwork = BPNeuralNetwork()
# testing neutral network
self.neuralNetwork.test()
self.setupUi(self)
self.gridlayout = QGridLayout(self.Real_time_graph)
self.ModeOneClicked()
self.port_list.addItems(self.get_port_list())
self.NumberComboBox.addItems(NumberOfGroups)
self.Baudrate_comboBox.addItems(baudrate_comboBox)
self._timer = QTimer(self)
self._t = 1
self.temperature = []
self.humidity = []
self.third = []
self.forth = []
self.fifth = []
self.pass_time = []
self.infor = ''
self._Static_on = 0
self._update_on = 0
self.name = ''
self.allowed_times = 0
self.baudrate = 0
self.critical = 0
self.datas = []
self.beginOrEnd = 1
self.currentTime = ''
self.SQL_helper = WriteIntoSQL(SQL_Table_name)
self.state = 0
def drawing_mode1(self):
print("in 1")
self.modeOneFic = ModeOneWindow(self.gridlayout)
self.modeOneFic.fig.axes1.cla()
self.modeOneFic.fig.axes2.cla()
self.modeOneFic.fig.axes3.cla()
self.modeOneFic.fig.axes4.cla()
self.modeOneFic.fig.axes5.cla()
print("in 2")
self.modeOneFic.fig.axes1.plot(self.pass_time, self.temperature, '-ob', color='blue', label='temperature')
self.modeOneFic.fig.axes2.plot(self.pass_time, self.humidity, '-ob', color='red', label='humidity')
self.modeOneFic.fig.axes3.plot(self.pass_time, self.third, '-ob', color='green', label='MQ2')
self.modeOneFic.fig.axes4.plot(self.pass_time, self.forth, '-ob', color='black', label='MQ4')
self.modeOneFic.fig.axes5.plot(self.pass_time, self.fifth, '-ob', color='orange', label='light')
self.modeOneFic.fig.draw()
print("in 3")
def drawing_mode2(self):
self.modeTwoFic = ModeTwoWindow(self.gridlayout)
self.modeTwoFic.fig.axes.cla()
self.modeTwoFic.fig.axes.plot(self.pass_time, self.temperature, '-ob', color='blue', label="temperature")
self.modeTwoFic.fig.axes.plot(self.pass_time, self.humidity, '-ob', color='red', label="humidity")
self.modeTwoFic.fig.axes.plot(self.pass_time, self.third, '-ob', color='green', label="MQ2")
self.modeTwoFic.fig.axes.plot(self.pass_time, self.forth, '-ob', color='yellow', label="MQ4")
self.modeTwoFic.fig.axes.plot(self.pass_time, self.fifth, '-ob', color='black', label="light")
self.modeTwoFic.fig.axes.set_xlabel("times", fontsize=15)
self.modeTwoFic.fig.axes.set_ylabel("data", fontsize=15)
self.modeTwoFic.fig.axes.legend(ncol=1, loc='upper left')
self.modeTwoFic.fig.axes.axis('off')
self.modeTwoFic.fig.draw()
@pyqtSlot()
def on_START_clicked(self):
# 读取lineEdit中数据
threading.Thread(target=self.SQL_helper.create_table()).start()
try:
self.getFourCri()
# 打开串口
self.ser = My_port(self.name, self.baudrate, self.dataGroupsNum, self.SQL_helper)
self.ser.getport() # 打开串口
self.port = self.ser.ser # 得到串口
if self.port == 0:
WarningQDialog("CANNOT FIND SERIAL!")
else:
self.port.flushInput() # clear all data in buffer
print("----------clear all buffer success--------------")
self.portInfo = "串口:" + self.port.name + "\n串口波特率:" + str(self.port.baudrate)
self.textEdit.setPlainText(self.portInfo)
self.PAUSE.setEnabled(True)
self.START.setEnabled(False)
self._update_on = 1
try:
self._timer.timeout.connect(self.readPort_Warning)
self._timer.start(2000) # after delay
self.beginOrEnd = 1
except:
pass
except:
WarningQDialog("Please input right number!")
@pyqtSlot()
def readPort_Warning(self):
self.currentTime = str(time.strftime("%H:%M:%S", time.localtime()))
self.state = 1
if self.beginOrEnd == 1:
# 读取数据
global outTimeNum
readResult = self.ser.readport(self.currentTime)
if readResult == 0: # 读取超时
outTimeNum += 1
new_infor = self.currentTime + "\t\t" + "数据读取超时\n"
self.infor = HEAD + new_infor + self.infor[j_1:len(self.infor)]
self.textEdit_2.setPlainText(self.infor)
if outTimeNum >= 5:
WarningQDialog("PLEASE CHECK YOUR PORT")
elif readResult == 1: # 成功读取
if self.ser.dataInfor == 'this is GPS data':
# 是GPS信号
print("-----------GPS information-----------")
new_gps = "\n节点" + self.ser._data[0] + "\tGPS定位:" + self.ser._data[1] + ', ' + self.ser._data[2]
self.textEdit.append(new_gps)
try:
threading.Thread(target=self.SQL_helper.writeInto_GPStable(self.ser._data)).start()
except:
WarningQDialog("Can't write the table " + SQL_Table_name)
# self.beginOrEnd = 0
elif self.ser.dataInfor == 'this is sensor data':
# 测量值
if (len(self.ser._data[1]) and len(self.ser._data[2]) and len(self.ser._data[3]) and len(
self.ser._data[4]) and len(self.ser._data[5])):
self.temperature.append(self.ser._data[1])
self.temperature.sort()
self.humidity.append(self.ser._data[2])
self.humidity.sort()
self.third.append(self.ser._data[3])
self.third.sort()
self.forth.append(self.ser._data[4])
self.forth.sort()
self.fifth.append(self.ser._data[5])
self.fifth.sort()
print(len(self.temperature))
self._t += 1
self.pass_time.append(self._t)
if len(self.temperature) >= 11:
print("in if")
self.temperature.pop(0)
self.humidity.pop(0)
self.third.pop(0)
self.forth.pop(0)
self.fifth.pop(0)
self.pass_time.pop(0)
else:
pass
if modeChoice == 1:
print("modeOnePlot")
self.drawing_mode1()
print("modeOnePlotfinish")
else:
self.drawing_mode2()
if self.ifSafe() == 0:
self.state = 0
else:
self.state = 1
# 写入数据库
try:
threading.Thread(
target=self.SQL_helper.writeInto_datatable(self.currentTime, self.state,
self.ser._data)).start()
except:
WarningQDialog("Can't write the table " + SQL_Table_name)
new_infor = self.scrollOutputInfor(self.state)
self.infor = HEAD + new_infor + self.infor[j_1:len(self.infor)]
self.textEdit_2.setPlainText(self.infor)
# if self.ifSafe() == 0:
# self.di = QDialog()
# self.d = dialog.Ui_Dialog()
# self.d.setupUi(self.di, MESSAGE)
# self.di.show()
else:
try:
new_infor = self.ser.dataInfor
self.infor = HEAD + new_infor + self.infor[j_1:len(self.infor)]
self.textEdit_2.setPlainText(self.infor)
except:
new_infor = '无法输出的乱码'
self.infor = HEAD + new_infor + self.infor[j_1:len(self.infor)]
self.textEdit_2.setPlainText(self.infor)
elif readResult == -2:
# 可能丢包
print("乱码输出")
try:
self.infor = HEAD + self.currentTime + '\t\t' + self.ser.data + "\n" + self.infor[
j_1: len(self.infor)]
self.textEdit_2.setPlainText(self.infor)
except:
new_infor = '无法输出的乱码'
self.infor = HEAD + new_infor + self.infor[j_1:len(self.infor)]
self.textEdit_2.setPlainText(self.infor)
elif readResult == -1:
# 没有数据
pass
else:
pass
@pyqtSlot()
def on_PAUSE_clicked(self):
if self._update_on == 1:
self._update_on = 0
self.port.close()
self._timer.timeout.disconnect(self.readPort_Warning)
threading.Thread(target=self.SQL_helper.close_SQL()).start()
self.START.setEnabled(True)
print("------port close success------")
else:
pass
def ModeOneClicked(self):
self.modeOneFic = ModeOneWindow(self.gridlayout)
global modeChoice
modeChoice = 1
try:
self.gridlayout.removeWidget(self.modeTwoFic.fig)
self.gridlayout.removeWidget(self.modeTwoFic.fig_ntb)
self.modeTwoFic.fig.deleteLater()
self.modeTwoFic.fig_ntb.deleteLater()
except:
pass
self.Mode2.clicked.connect(self.ModeTwoClicked)
self.show()
def ModeTwoClicked(self):
self.modeTwoFic = ModeTwoWindow(self.gridlayout)
global modeChoice
modeChoice = 2
try:
self.gridlayout.removeWidget(self.modeOneFic.fig)
self.gridlayout.removeWidget(self.modeOneFic.fig_ntb)
self.modeOneFic.fig.deleteLater()
self.modeOneFic.fig_ntb.deleteLater()
except:
pass
self.Mode1.clicked.connect(self.ModeOneClicked)
self.show()
def getFourCri(self):
self.dataGroupsNum = int(self.NumberComboBox.currentText())
self.name = str(self.port_list.currentText())
self.baudrate = int(self.Baudrate_comboBox.currentText())
@staticmethod
def get_port_list():
# get all the COM port currently
com_list = []
port_list = serial.tools.list_ports.comports()
for port in port_list:
com_list.append(port[0] + ':USB-SERIAL')
return com_list
def ifSafe(self):
predictNum = self.ser._data[1:len(self.ser._data)]
predictNum = [int(predictNum) for predictNum in predictNum]
self.state = self.neuralNetwork.predict(predictNum)
print('*********predict value:' + str(self.state[0]))
if self.state[0] > 0.9: # safe
state = 1
else:
state = 0
return state
def scrollOutputInfor(self, flags):
new_infor0 = self.currentTime
i = 0
while i < self.dataGroupsNum + 1:
new_infor0 += '\t\t' + str(self.ser._data[i])
i += 1
if not flags:
new_infor = new_infor0 + "\t\tDANGER\n"
else:
new_infor = new_infor0 + "\t\tsafe\n"
return new_infor
if __name__ == "__main__":
app = QApplication(sys.argv)
win = AppWindow()
win.show()
sys.exit(app.exec_())