-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.py
258 lines (223 loc) · 9.8 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
# -*- coding: utf-8 -*-
'''
Created on 2014年9月16日
@author: Ying
'''
import sys
from PyQt4 import QtGui
from PyQt4 import QtCore
from PyQt4 import Qt
from PyQt4.QtCore import pyqtSignal
from MainWindow_ui import Ui_MainWindow
import requests
import json
import serial
import threading
class BusyBar(QtGui.QProgressDialog):
def __init__(self, text=""):
super(BusyBar, self).__init__()
self.text = text
self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
self.setRange(0, 0)
self.setLabelText(self.text)
self.setValue(0)
self.setFixedSize(500, 100)
self.setAutoClose(False)
self.setWindowModality(Qt.Qt.WindowModal)
class SerialThread(threading.Thread):
def __init__(self, com):
threading.Thread.__init__(self)
self.thread_stop = False
self.ser = serial.Serial(com)
self.ser.baudrate = 115200
print(self.ser.isOpen())
self.id = 0
self.myCommunicator = MyCommunicator()
def run(self):
while not self.thread_stop:
try:
data = self.ser.readline()
self.id = data
# print(self.id)
# print("before sendsignal")
self.myCommunicator.sendSignal()
# print("after sendsignal")
except:
print("readline fail")
def stop(self):
if self.ser.isOpen():
self.ser.close()
print(self.ser.isOpen())
self.thread_stop = True
class MyCommunicator(QtCore.QObject):
trigger = pyqtSignal(name='SerialDataReceived')
def __init__(self, parent=None):
super(MyCommunicator, self).__init__()
def sendSignal(self):
self.trigger.emit()
class MyMainWindow(Ui_MainWindow):
def __init__(self, parent=None):
super(MyMainWindow, self).__init__()
# QtGui.QMainWindow.__init__(self,parent)
self.ui = Ui_MainWindow(self)
self.ui.setupUi(self)
self.setupConnect()
self.isOpenCom = False
self.serverURL = 'http://166.111.53.137:8080/makerPlus/servlet/'
# self.serverURL = 'http://59.66.81.161:8080/makerPlus/servlet/'
# self.serverURL = 'http://172.27.35.42:8080/makerPlus/servlet/'
# self.busyBar = BusyBar("Connecting to Server...")
# self.busyBar.setLabelText("Connecting")
# self.busyBar.show()
try:
self.eventList = requests.get(self.serverURL + 'Event').json()
self.updateEventList(self.eventList)
self.eventSelected()
# self.busyBar.close()
except:
# self.busyBar.close()
QtGui.QMessageBox.warning(self, "Error", "Can't connect to server",
QtGui.QMessageBox.Cancel, QtGui.QMessageBox.NoButton,
QtGui.QMessageBox.NoButton)
strFileType = "TXT File (*.txt)"
self.localFilePath = QtGui.QFileDialog.getSaveFileName(self,
"Save MSpectrum",
"/home",
strFileType)
def __del__(self):
print("in del")
try:
self.mySerialThread.stop()
except:
pass
def setupConnect(self):
self.ui.pushButton.clicked.connect(self.upLoad)
# self.ui.comboBox.currentIndexChanged.connect(self.eventSelected)
self.ui.comboBox.activated.connect(self.eventSelected)
self.ui.pushButton_openCom.clicked.connect(self.onPressCom)
def upLoad(self):
uploadParameters = dict()
for i in range(len(self.currentEventParameters)):
if i == 0:
uploadParameters[self.currentEventParameters[i]] = self.ui.lineEdit_1.text()
elif i == 1:
uploadParameters[self.currentEventParameters[i]] = self.ui.lineEdit_2.text()
elif i == 2:
uploadParameters[self.currentEventParameters[i]] = self.ui.lineEdit_3.text()
elif i == 3:
uploadParameters[self.currentEventParameters[i]] = self.ui.lineEdit_4.text()
elif i == 4:
uploadParameters[self.currentEventParameters[i]] = self.ui.lineEdit_5.text()
elif i == 5:
uploadParameters[self.currentEventParameters[i]] = self.ui.lineEdit_6.text()
elif i == 6:
uploadParameters[self.currentEventParameters[i]] = self.ui.lineEdit_7.text()
elif i == 7:
uploadParameters[self.currentEventParameters[i]] = self.ui.lineEdit_8.text()
r = requests.post(self.serverURL + "Event?EventName=" + self.currentEventName, data={'data': str(uploadParameters)})
# r = requests.post(self.serverURL + "Event?EventName=" + self.currentEventName, data={'data': u"{'some':'毕滢'}"})
print(r.text)
if r.status_code != 200:
QtGui.QMessageBox.warning(self, "Error",
"Upload Error: " + str(r.status_code),
QtGui.QMessageBox.Cancel, QtGui.QMessageBox.NoButton,
QtGui.QMessageBox.NoButton)
else:
isShowDialog = False
if self.currentEventName == "RegisterEvent":
dialogShowStr = r.text
isShowDialog = True
elif self.currentEventName == "CheckEvent":
memberInfo = r.json()
phoneNum = memberInfo["电话"]
showPhoneNum = "手机尾号:" + phoneNum[len(phoneNum) - 4:len(phoneNum)]
self.ui.label_return_info_1.setText(memberInfo["姓名"])
self.ui.label_return_info_2.setText(showPhoneNum)
isShowDialog = False
elif self.currentEventName == "LotteryEvent":
memberInfo = r.json()
phoneNum = memberInfo["电话"]
showPhoneNum = "手机尾号:" + phoneNum[len(phoneNum) - 4:len(phoneNum)]
self.ui.label_return_info_1.setText(memberInfo["姓名"])
self.ui.label_return_info_2.setText(showPhoneNum)
dialogShowStr = "获得15点经验值!!"
isShowDialog = True
if isShowDialog:
QtGui.QMessageBox.warning(self, "Done!",
dialogShowStr,
QtGui.QMessageBox.Information, QtGui.QMessageBox.NoButton,
QtGui.QMessageBox.NoButton)
self.f = open(self.localFilePath, 'a')
self.f.write(str(uploadParameters))
self.f.write(" ")
self.f.write(r.text)
# self.f.write("\n")
self.f.close()
# copy id to clipboard
clipboard = QtGui.QApplication.clipboard()
mimeData = QtCore.QMimeData()
# mimeData.setText(r.text)
mimeData.setText(r.text[0:len(r.text) - 2])
clipboard.setMimeData(mimeData)
def updateEventList(self, eventList):
self.ui.comboBox.clear()
for i in range(len(eventList)):
self.ui.comboBox.addItem(eventList[i])
def eventSelected(self):
self.currentEventName = self.eventList[self.ui.comboBox.currentIndex()]
self.currentEventParameters = requests.get(self.serverURL + 'Event?EventName=' + self.currentEventName).json()
# self.currentEventParameters = requests.get(self.serverURL + 'Tester').json()
# clean label text
self.ui.label_1.setText("")
self.ui.label_2.setText("")
self.ui.label_3.setText("")
self.ui.label_4.setText("")
self.ui.label_5.setText("")
self.ui.label_6.setText("")
self.ui.label_7.setText("")
self.ui.label_8.setText("")
for i in range(len(self.currentEventParameters)):
if i == 0:
self.ui.label_1.setText(self.currentEventParameters[i])
elif i == 1:
self.ui.label_2.setText(self.currentEventParameters[i])
elif i == 2:
self.ui.label_3.setText(self.currentEventParameters[i])
elif i == 3:
self.ui.label_4.setText(self.currentEventParameters[i])
elif i == 4:
self.ui.label_5.setText(self.currentEventParameters[i])
elif i == 5:
self.ui.label_6.setText(self.currentEventParameters[i])
elif i == 6:
self.ui.label_7.setText(self.currentEventParameters[i])
elif i == 7:
self.ui.label_8.setText(self.currentEventParameters[i])
def onPressCom(self):
if not self.isOpenCom:
try:
self.mySerialThread = SerialThread(int(self.ui.lineEdit_com.text()) - 1)
self.mySerialThread.myCommunicator.trigger.connect(self.updateIDfromSerial)
self.mySerialThread.start()
self.isOpenCom = True
self.ui.pushButton_openCom.setText("关闭串口")
except:
self.mySerialThread.stop()
QtGui.QMessageBox.warning(self, "Error",
"Can't open Com",
QtGui.QMessageBox.Cancel, QtGui.QMessageBox.NoButton,
QtGui.QMessageBox.NoButton)
else:
self.mySerialThread.stop()
self.isOpenCom = False
self.ui.pushButton_openCom.setText("开启串口")
def updateIDfromSerial(self):
self.id = self.mySerialThread.id
if not self.currentEventName == "RegisterEvent":
idStr = str(self.id)
self.ui.lineEdit_1.setText(idStr[2:len(idStr) - 5])
print(self.id)
app = QtGui.QApplication(sys.argv)
widget = MyMainWindow()
widget.show()
sys.exit(app.exec_())