Update hexapod.py

master
Zhengyu Peng 3 years ago
parent 0d8675658a
commit 1ac65f2d6c

@ -65,7 +65,6 @@ class MyApp(QtWidgets.QMainWindow):
super(MyApp, self).__init__() super(MyApp, self).__init__()
config_file = Path('config.json') config_file = Path('config.json')
if config_file.exists(): if config_file.exists():
self.config = json.load(open('config.json', 'r')) self.config = json.load(open('config.json', 'r'))
else: else:
@ -77,18 +76,18 @@ class MyApp(QtWidgets.QMainWindow):
ui_file = QFile(ui_file_name) ui_file = QFile(ui_file_name)
loader = QUiLoader() loader = QUiLoader()
self.ui = loader.load(ui_file) self.ui = loader.load(ui_file)
ui_file.close() ui_file.close()
self.init_ui() self.init_ui()
self.ui.comboBox_Interface.currentIndexChanged.connect( self.ui.comboBox_Interface.currentIndexChanged.connect(
self.on_interface_selection_change self.on_interface_selection_changed
) )
self.ui.button_Refresh.clicked.connect( self.ui.button_Refresh.clicked.connect(
self.on_refresh_button_clicked self.on_interface_refresh_button_clicked
) )
self.ui.button_TcpClient.clicked.connect( self.ui.buttonTcpConnect.clicked.connect(
self.on_tcp_client_connect_button_clicked self.on_tcp_client_connect_button_clicked
) )
@ -132,12 +131,12 @@ class MyApp(QtWidgets.QMainWindow):
self.on_turnright_button_clicked self.on_turnright_button_clicked
) )
self.ui.textBrowser_TcpClientMessage.installEventFilter(self) self.ui.textBrowserMessage.installEventFilter(self)
self.ui.show() self.ui.show()
def eventFilter(self, widget, event): def eventFilter(self, widget, event):
if self.ui.textBrowser_TcpClientMessage.isEnabled(): if self.ui.textBrowserMessage.isEnabled():
if (event.type() == QtCore.QEvent.KeyPress): if (event.type() == QtCore.QEvent.KeyPress):
key = event.key() key = event.key()
if key == QtCore.Qt.Key_Up: if key == QtCore.Qt.Key_Up:
@ -170,10 +169,10 @@ class MyApp(QtWidgets.QMainWindow):
def init_ui(self): def init_ui(self):
# Interface # Interface
self.update_network_interfaces() self.on_interface_refresh_button_clicked()
self.ui.groupBox_Control.setEnabled(False) self.ui.groupBox_Control.setEnabled(False)
self.ui.textBrowser_TcpClientMessage.setEnabled(False) self.ui.textBrowserMessage.setEnabled(False)
tcp_client_ip = self.config.get('TCP_Client_IP', '127.0.0.1') tcp_client_ip = self.config.get('TCP_Client_IP', '127.0.0.1')
tcp_client_port = self.config.get('TCP_Client_Port', '1234') tcp_client_port = self.config.get('TCP_Client_Port', '1234')
@ -236,7 +235,7 @@ class MyApp(QtWidgets.QMainWindow):
self.tcp_client.send('rightturn') self.tcp_client.send('rightturn')
self.append_message('rightturn') self.append_message('rightturn')
def update_network_interfaces(self): def on_interface_refresh_button_clicked(self):
self.net_if = psutil.net_if_addrs() self.net_if = psutil.net_if_addrs()
interface_idx = self.config.get('Interface', 0) interface_idx = self.config.get('Interface', 0)
@ -256,10 +255,7 @@ class MyApp(QtWidgets.QMainWindow):
else: else:
self.ui.comboBox_Interface.setCurrentIndex(interface_idx) self.ui.comboBox_Interface.setCurrentIndex(interface_idx)
current_interface = self.ui.comboBox_Interface.currentText() for snicaddr in self.net_if[self.ui.comboBox_Interface.currentText()]:
self.config['Interface'] = self.ui.comboBox_Interface.currentIndex()
for snicaddr in self.net_if[current_interface]:
if snicaddr.family == socket.AF_INET: if snicaddr.family == socket.AF_INET:
ipv4_add = snicaddr.address ipv4_add = snicaddr.address
break break
@ -268,9 +264,10 @@ class MyApp(QtWidgets.QMainWindow):
self.ui.label_LocalIP.setText(ipv4_add) self.ui.label_LocalIP.setText(ipv4_add)
self.config['Interface'] = self.ui.comboBox_Interface.currentIndex()
self.save_config() self.save_config()
def on_interface_selection_change(self): def on_interface_selection_changed(self):
current_interface = self.ui.comboBox_Interface.currentText() current_interface = self.ui.comboBox_Interface.currentText()
if current_interface in self.net_if: if current_interface in self.net_if:
@ -287,13 +284,10 @@ class MyApp(QtWidgets.QMainWindow):
self.config['Interface'] = self.ui.comboBox_Interface.currentIndex() self.config['Interface'] = self.ui.comboBox_Interface.currentIndex()
self.save_config() self.save_config()
def on_refresh_button_clicked(self):
self.update_network_interfaces()
# TCP Client # TCP Client
def on_tcp_client_connect_button_clicked(self): def on_tcp_client_connect_button_clicked(self):
if self.ui.button_TcpClient.text() == 'Connect': if self.ui.buttonTcpConnect.text() == 'Connect':
self.ui.button_TcpClient.setEnabled(False) self.ui.buttonTcpConnect.setEnabled(False)
self.ui.lineEdit_TcpClientTargetIP.setEnabled(False) self.ui.lineEdit_TcpClientTargetIP.setEnabled(False)
self.ui.lineEdit_TcpClientTargetPort.setEnabled(False) self.ui.lineEdit_TcpClientTargetPort.setEnabled(False)
@ -314,8 +308,8 @@ class MyApp(QtWidgets.QMainWindow):
self.config['TCP_Client_Port'] = self.ui.lineEdit_TcpClientTargetPort.text() self.config['TCP_Client_Port'] = self.ui.lineEdit_TcpClientTargetPort.text()
self.save_config() self.save_config()
elif self.ui.button_TcpClient.text() == 'Disconnect': elif self.ui.buttonTcpConnect.text() == 'Disconnect':
self.ui.button_TcpClient.setEnabled(False) self.ui.buttonTcpConnect.setEnabled(False)
self.tcp_client.close() self.tcp_client.close()
def on_tcp_client_status_update(self, status, addr): def on_tcp_client_status_update(self, status, addr):
@ -323,27 +317,27 @@ class MyApp(QtWidgets.QMainWindow):
self.tcp_client.status.disconnect() self.tcp_client.status.disconnect()
self.tcp_client.message.disconnect() self.tcp_client.message.disconnect()
self.ui.button_TcpClient.setText('Connect') self.ui.buttonTcpConnect.setText('Connect')
self.tcp_client_thread.quit() self.tcp_client_thread.quit()
self.ui.lineEdit_TcpClientTargetIP.setEnabled(True) self.ui.lineEdit_TcpClientTargetIP.setEnabled(True)
self.ui.lineEdit_TcpClientTargetPort.setEnabled(True) self.ui.lineEdit_TcpClientTargetPort.setEnabled(True)
self.ui.textBrowser_TcpClientMessage.setEnabled(False) self.ui.textBrowserMessage.setEnabled(False)
self.ui.groupBox_Control.setEnabled(False) self.ui.groupBox_Control.setEnabled(False)
self.ui.textBrowser_TcpClientMessage.setEnabled(False) self.ui.textBrowserMessage.setEnabled(False)
self.ui.status_bar.clearMessage() self.ui.status_bar.clearMessage()
self.ui.status_bar.setStyleSheet('color: green') self.ui.status_bar.setStyleSheet('color: green')
self.ui.status_bar.showMessage('● Idle') self.ui.status_bar.showMessage('● Idle')
elif status == TCPClient.CONNECTED: elif status == TCPClient.CONNECTED:
self.ui.button_TcpClient.setText('Disconnect') self.ui.buttonTcpConnect.setText('Disconnect')
self.ui.groupBox_Control.setEnabled(True) self.ui.groupBox_Control.setEnabled(True)
self.ui.textBrowser_TcpClientMessage.setEnabled(True) self.ui.textBrowserMessage.setEnabled(True)
self.ui.textBrowser_TcpClientMessage.setEnabled(True) self.ui.textBrowserMessage.setEnabled(True)
self.ui.textBrowser_TcpClientMessage.setFocus() self.ui.textBrowserMessage.setFocus()
self.ui.status_bar.clearMessage() self.ui.status_bar.clearMessage()
self.ui.status_bar.setStyleSheet('color: green') self.ui.status_bar.setStyleSheet('color: green')
@ -352,27 +346,27 @@ class MyApp(QtWidgets.QMainWindow):
self.ui.label_LocalIP.text() + self.ui.label_LocalIP.text() +
':'+self.ui.lineEdit_TcpClientTargetPort.text()) ':'+self.ui.lineEdit_TcpClientTargetPort.text())
self.ui.button_TcpClient.setEnabled(True) self.ui.buttonTcpConnect.setEnabled(True)
def on_tcp_client_message_ready(self, source, msg): def on_tcp_client_message_ready(self, source, msg):
self.ui.textBrowser_TcpClientMessage.append( self.ui.textBrowserMessage.append(
'<p style="text-align: center;"><span style="color: #2196F3;"><strong>----- ' + '<div style="color: #2196F3;"><strong>— ' +
source + source +
' -----</strong></span></p>') ' —</strong></div>')
self.ui.textBrowser_TcpClientMessage.append( self.ui.textBrowserMessage.append(
'<p style="text-align: center;"><span style="color: #2196F3;">' + '<div style="color: #2196F3;">' +
msg + msg +
'</span></p>') '<br></div>')
def append_message(self, message): def append_message(self, message):
self.ui.textBrowser_TcpClientMessage.append( self.ui.textBrowserMessage.append(
'<p style="text-align: center;"><strong>----- ' + '<div><strong>— ' +
'this' + 'localhost' +
' -----</strong></p>') ' —</strong></div>')
self.ui.textBrowser_TcpClientMessage.append( self.ui.textBrowserMessage.append(
'<p style="text-align: center;">' + '<div>' +
message + message +
'</p>') '<br></div>')
if __name__ == "__main__": if __name__ == "__main__":

Loading…
Cancel
Save