-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclient_ui.py
More file actions
373 lines (312 loc) · 14.4 KB
/
client_ui.py
File metadata and controls
373 lines (312 loc) · 14.4 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
# coding=utf-8
import os
import misc
import time
import configurationWindow
from PyQt4 import QtGui, QtCore
from twisted.python import log
from gs_interface import GroundStationInterface
# FIXME import sentence no optimized
import client_amp
from threads import Threads
"""
Copyright 2016 Samuel Góngora García
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
:Author:
Samuel Góngora García (s.gongoragarcia@gmail.com)
"""
__author__ = 's.gongoragarcia@gmail.com'
class SatNetUI(QtGui.QWidget):
def __init__(self, argumentsdict, parent=None):
QtGui.QWidget.__init__(self, parent)
QtGui.QToolTip.setFont(QtGui.QFont('SansSerif', 18))
self.enviromentDesktop = os.environ.get('DESKTOP_SESSION')
self.connection = ''
self.settingsfile = '.settings'
self.setArguments(argumentsdict)
self.initUI()
self.initButtons()
self.initFields()
self.setParameters()
self.initLogo()
self.initConfiguration()
self.initConsole()
self.gsi = GroundStationInterface(self.CONNECTION_INFO, "Vigo",
client_amp.ClientProtocol)
self.threads = Threads(self.CONNECTION_INFO, self.gsi)
# Initialize the reactor parameters needed for the pyqt enviroment
client_amp.Client(self.CONNECTION_INFO, self.gsi, self.threads).createconnection(test=False)
# Create a new connection by loading the connection parameters
# from the interface window
def NewConnection(self, test=False):
self.CONNECTION_INFO['username'] = str(self.LabelUsername.text())
self.CONNECTION_INFO['password'] = str(self.LabelPassword.text())
self.CONNECTION_INFO['connection'] =\
str(self.LabelConnection.currentText())
if self.AutomaticReconnection.isChecked():
self.CONNECTION_INFO['reconnection'] = 'yes'
else:
self.CONNECTION_INFO['reconnection'] = 'no'
self.openInterface()
return client_amp.Client(self.CONNECTION_INFO, self.gsi,
self.threads).setconnection(test=False)
def initUI(self):
"""
self.CONNECTION_INFO = misc.get_data_local_file(
settingsFile='.settings')
"""
self.CONNECTION_INFO = misc.get_data_local_file(
settingsFile=self.settingsfile)
QtGui.QToolTip.setFont(QtGui.QFont('SansSerif', 10))
self.setFixedSize(1300, 800)
self.setWindowTitle("SatNet client - %s" %
(self.CONNECTION_INFO['name']))
if self.CONNECTION_INFO['parameters'] == 'yes':
self.CONNECTION_INFO = {}
self.UpdateFields()
elif self.CONNECTION_INFO['parameters'] == 'no':
pass
else:
log.msg("No parameters configuration found." +
" Using default parameter - Yes")
def initButtons(self):
buttons = QtGui.QGroupBox(self)
grid = QtGui.QGridLayout(buttons)
buttons.setLayout(grid)
self.ButtonNew = QtGui.QPushButton("Connection")
self.ButtonNew.setToolTip("Start a new connection using " +
" the selected connection")
self.ButtonNew.setFixedWidth(145)
self.ButtonNew.clicked.connect(self.NewConnection)
self.ButtonCancel = QtGui.QPushButton("Disconnection")
self.ButtonCancel.setToolTip("End current connection")
self.ButtonCancel.setFixedWidth(145)
self.ButtonCancel.clicked.connect(self.CloseConnection)
self.ButtonCancel.setEnabled(False)
self.ButtonLoad = QtGui.QPushButton("Load parameters from file")
self.ButtonLoad.setToolTip("Load parameters from <i>.settings</i> file")
self.ButtonLoad.setFixedWidth(296)
self.ButtonLoad.clicked.connect(self.UpdateFields)
self.ButtonConfiguration = QtGui.QPushButton("Configuration")
self.ButtonConfiguration.setToolTip("Open configuration window")
self.ButtonConfiguration.setFixedWidth(145)
self.ButtonConfiguration.clicked.connect(self.SetConfiguration)
ButtonHelp = QtGui.QPushButton("Help")
ButtonHelp.setToolTip("Click for help")
ButtonHelp.setFixedWidth(145)
ButtonHelp.clicked.connect(self.usage)
grid.addWidget(self.ButtonNew, 0, 0, 1, 1)
grid.addWidget(self.ButtonCancel, 0, 1, 1, 1)
grid.addWidget(self.ButtonLoad, 1, 0, 1, 2)
grid.addWidget(self.ButtonConfiguration, 2, 0, 1, 1)
grid.addWidget(ButtonHelp, 2, 1, 1, 1)
buttons.setTitle("Connection")
buttons.move(10, 10)
self.dialogTextBrowser = configurationWindow.ConfigurationWindow(self)
def initFields(self):
# Connection parameters group
connectionParameters = QtGui.QGroupBox(self)
gridConnection = QtGui.QFormLayout()
connectionParameters.setLayout(gridConnection)
LabelAttemps = QtGui.QLabel("Reconnection tries:")
LabelAttemps.setFixedWidth(145)
self.FieldLabelAttemps = QtGui.QLineEdit()
self.FieldLabelAttemps.setFixedWidth(145)
gridConnection.addRow(LabelAttemps, self.FieldLabelAttemps)
connectionParameters.setTitle("Connection parameters")
connectionParameters.move(10, 140)
# Configuration group.
configuration = QtGui.QGroupBox(self)
configurationLayout = QtGui.QVBoxLayout()
configuration.setLayout(configurationLayout)
self.LoadDefaultSettings =\
QtGui.QCheckBox("Automatically load settings from file")
configurationLayout.addWidget(self.LoadDefaultSettings)
self.AutomaticReconnection =\
QtGui.QCheckBox("Reconnect after a failure")
configurationLayout.addWidget(self.AutomaticReconnection)
configuration.move(10, 180)
# User parameters group
parameters = QtGui.QGroupBox(self)
self.layout = QtGui.QFormLayout()
parameters.setLayout(self.layout)
self.LabelUsername = QtGui.QLineEdit()
self.LabelUsername.setFixedWidth(190)
self.layout.addRow(QtGui.QLabel("Username: "),
self.LabelUsername)
self.LabelPassword = QtGui.QLineEdit()
self.LabelPassword.setFixedWidth(190)
self.LabelPassword.setEchoMode(QtGui.QLineEdit.Password)
self.layout.addRow(QtGui.QLabel("Password: "),
self.LabelPassword)
self.LabelConnection = QtGui.QComboBox()
self.LabelConnection.setFixedWidth(190)
self.LabelConnection.addItems(['serial', 'udp', 'tcp', 'none'])
self.LabelConnection.activated.connect(self.openInterface)
self.layout.addRow(QtGui.QLabel("Interface: "),
self.LabelConnection)
parameters.setTitle("User data")
parameters.move(10, 265)
# User interface group
interfaceControl = QtGui.QGroupBox(self)
gridControl = QtGui.QGridLayout(interfaceControl)
interfaceControl.setLayout(gridControl)
self.stopInterfaceButton = QtGui.QPushButton("Stop interface")
self.stopInterfaceButton.setToolTip("Stop the actual interface")
self.stopInterfaceButton.setFixedWidth(145)
self.stopInterfaceButton.clicked.connect(self.stopInterface)
self.stopInterfaceButton.setEnabled(False)
gridControl.addWidget(self.stopInterfaceButton, 0, 0, 1, 1)
interfaceControl.move(155, 380)
def initLogo(self):
LabelLogo = QtGui.QLabel(self)
LabelLogo.move(20, 450)
pic = QtGui.QPixmap(os.getcwd() + "/logo.png")
pic = pic.scaledToWidth(300)
# pic = pic.scaled(400, 400, QtCore.Qt.KeepAspectRatio)
LabelLogo.setPixmap(pic)
LabelLogo.show()
def initConfiguration(self):
if self.CONNECTION_INFO['reconnection'] == 'yes':
self.AutomaticReconnection.setChecked(True)
elif self.CONNECTION_INFO['reconnection'] == 'no':
self.AutomaticReconnection.setChecked(False)
if self.CONNECTION_INFO['parameters'] == 'yes':
self.LoadDefaultSettings.setChecked(True)
elif self.CONNECTION_INFO['parameters'] == 'no':
self.LoadDefaultSettings.setChecked(False)
def initConsole(self):
self.console = QtGui.QTextBrowser(self)
self.console.move(340, 10)
self.console.resize(950, 780)
self.console.setFont(QtGui.QFont('SansSerif', 11))
# Set parameters form arguments list.
def setArguments(self, argumentsdict):
try:
if argumentsdict['username'] != "":
self.LabelUsername.setText(argumentsdict['username'])
if argumentsdict['connection'] != "":
index = self.LabelConnection.findText(argumentsdict['connection'])
self.LabelConnection.setCurrentIndex(index)
except KeyError:
self.settingsfile = argumentsdict['file']
# Set parameters from CONNECTION_INFO dict.
def setParameters(self):
self.FieldLabelAttemps.setText(self.CONNECTION_INFO['attempts'])
self.LabelUsername.setText(self.CONNECTION_INFO['username'])
try:
index = self.LabelConnection.findText(
self.CONNECTION_INFO['connection'])
self.LabelConnection.setCurrentIndex(index)
except Exception as e:
log.err(e)
def CloseConnection(self):
self.gsi.clear_slots()
self.ButtonNew.setEnabled(True)
self.ButtonCancel.setEnabled(False)
def UpdateFields(self):
self.CONNECTION_INFO = misc.get_data_local_file(
settingsFile=self.settingsfile)
log.msg("Parameters loaded from .setting file.")
@QtCore.pyqtSlot()
def SetConfiguration(self):
self.dialogTextBrowser.exec_()
self.UpdateFields()
def openInterface(self):
if str(self.LabelConnection.currentText()) == 'udp':
self.threads.runUDPThreadReceive()
self.threads.runUDPThreadSend()
self.connection = 'udp'
self.LabelConnection.setEnabled(False)
self.stopInterfaceButton.setEnabled(True)
elif str(self.LabelConnection.currentText()) == 'serial':
self.threads.runKISSThreadReceive()
self.connection = 'serial'
self.LabelConnection.setEnabled(False)
self.stopInterfaceButton.setEnabled(True)
def stopInterface(self):
if self.connection == 'udp':
self.threads.stopUDPThreadReceive()
self.LabelConnection.setEnabled(True)
self.stopInterfaceButton.setEnabled(False)
elif self.connection == 'serial':
self.threads.stopKISSThread()
self.LabelConnection.setEnabled(True)
self.stopInterfaceButton.setEnabled(False)
def usage(self):
log.msg("USAGE of client_amp.py")
log.msg("")
log.msg("python client_amp.py")
log.msg(" [-n <username>] # Set SATNET username to login")
log.msg(" [-p <password>] # Set SATNET user password to login")
log.msg(" [-c <connection>] # Set the type of interface with "
"the GS (serial, udp or tcp)")
log.msg(" [-s <serialport>] # Set serial port")
log.msg(" [-b <baudrate>] # Set serial port baudrate")
log.msg(" [-i <ip>] # Set ip direction")
log.msg(" [-u <udpport>] # Set port address")
log.msg("")
log.msg("Example for serial config:")
log.msg("python client_amp.py -g -n crespo -p cre.spo -t 2 -c serial"
"-s /dev/ttyS1 -b 115200")
log.msg("Example for udp config:")
log.msg("python client_amp.py -g -n crespo -p cre.spo -t 2 -c udp -i"
"127.0.0.1 -u 5001")
log.msg("")
log.msg("[User]")
log.msg("username: test-sc-user")
log.msg("password: password")
log.msg("connection: udp")
log.msg("[Serial]")
log.msg("serialport: /dev/ttyUSB0")
log.msg("baudrate: 500000")
log.msg("[UDP]")
log.msg("ip: 127.0.0.1")
log.msg("udpport: 5005")
def center(self):
frameGm = self.frameGeometry()
screen_pos = QtGui.QApplication.desktop().cursor().pos()
screen = QtGui.QApplication.desktop().screenNumber(screen_pos)
centerPoint = QtGui.QApplication.desktop().screenGeometry(
screen).center()
frameGm.moveCenter(centerPoint)
self.move(frameGm.topLeft())
# Functions designed to output information
@QtCore.pyqtSlot(str)
def append_text(self, text):
self.console.moveCursor(QtGui.QTextCursor.End)
self.console.insertPlainText(text)
if "Connection lost" in str(text):
self.ButtonNew.setEnabled(True)
self.ButtonCancel.setEnabled(False)
if "Connection failed" in str(text):
self.ButtonNew.setEnabled(True)
self.ButtonCancel.setEnabled(False)
if "Connection sucessful" in str(text):
self.ButtonNew.setEnabled(False)
self.ButtonCancel.setEnabled(True)
filename = ("log-" + self.CONNECTION_INFO['name'] +
"-" + time.strftime("%Y.%m.%d") + ".csv")
with open(filename, "a+") as f:
f.write(text)
def closeEvent(self, event):
self.reply = QtGui.QMessageBox.question(self, 'Exit confirmation',
"Are you sure to quit?",
QtGui.QMessageBox.Yes |
QtGui.QMessageBox.No,
QtGui.QMessageBox.No)
# Non asynchronous way. Need to re implement this. TO-DO
if self.reply == QtGui.QMessageBox.Yes:
self.stopInterface()
self.gsi.clear_slots()
client_amp.Client(self.CONNECTION_INFO, self.gsi, self.threads).destroyconnection()
elif self.reply == QtGui.QMessageBox.No:
event.ignore()