-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
109 lines (89 loc) · 3.65 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
"""
A simple PyQT5 tool for Universal Robot Dashboard Server
Author: Creed Zagrzebski ([email protected])
"""
import sys
import socket
from datetime import datetime
from PyQt5.QtWidgets import QApplication, QMainWindow, QTextEdit, QTextBrowser, QPushButton, QMessageBox, QLabel
from PyQt5 import uic, QtCore
import threading
import time
class URDiagnosticsUI(QMainWindow):
heartbeat_signal = QtCore.pyqtSignal(str)
def __init__(self):
super(URDiagnosticsUI, self).__init__()
uic.loadUi("ui/main.ui", self)
self.setWindowTitle("UR Diagnostics")
self.__init_socket()
# bind button events
self.connectBtn = self.findChild(QPushButton, 'connectBtn')
self.connectBtn.clicked.connect(self.__connect)
self.disconnectBtn = self.findChild(QPushButton, 'disconnectBtn')
self.disconnectBtn.clicked.connect(self.__disconnect)
self.sendBtn = self.findChild(QPushButton, 'sendBtn')
self.sendBtn.clicked.connect(self.__send_command)
self.connectionLabel = self.findChild(QLabel, "connectionStatus")
self.heartbeat_signal.connect(self.connectionLabel.setText)
# TODO: Setup heartbeat to detect connection loss
# set heartbeat timer
# heartbeat_thread = threading.Thread(target=self.__heartbeat)
# heartbeat_thread.start()
self.setFixedSize(770, 550)
self.show()
def __display_error(self, message):
msg = QMessageBox()
msg.setWindowTitle("Error")
msg.setText(message)
msg.setIcon(QMessageBox.Critical)
msg.exec_()
def __init_socket(self):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.settimeout(3000)
def __connect(self):
# grab address/port from UI input
ipPort = self.findChild(
QTextEdit, 'addrTextEdit').toPlainText().split(":")
self.__init_socket()
try:
self.sock.connect((ipPort[0], int(ipPort[1])))
self.__append_message(self.sock.recv(1024).decode())
self.connectionLabel.setText("Connected")
except ConnectionError:
self.__display_error("Connection Refused")
def __disconnect(self):
try:
self.sock.close()
self.__append_message("Disconnected: Universal Robots Dashboard Server")
self.connectionLabel.setText("Disconnected")
except ConnectionError:
self.__display_error("Connection Refused")
def __send_command(self):
input_box = self.findChild(QTextEdit, 'inputTextEdit')
command = input_box.toPlainText()
command += "\n"
input_box.clear()
try:
self.sock.send(command.encode())
data = self.sock.recv(1024)
self.__append_message(data.decode())
except ConnectionError:
self.__display_error("Not connected to server")
def __heartbeat(self):
while True:
print("Checking heartbeat")
time.sleep(5000)
try:
self.sock.send("version".encode())
data = self.sock.recv(1024)
self.__append_message(data.decode())
self.heartbeat_signal.emit("Connected")
except ConnectionError:
self.heartbeat_signal.emit("Disconnected")
def __append_message(self, message):
input_box = self.findChild(QTextBrowser, 'outputConsole')
input_box.append(f'<span><b>{datetime.now()}</b>: {message}</span>')
if __name__ == "__main__":
app = QApplication(sys.argv)
ui = URDiagnosticsUI()
app.exec_()