-
Notifications
You must be signed in to change notification settings - Fork 4
/
qqtmrt.py
executable file
·203 lines (145 loc) · 5.96 KB
/
qqtmrt.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
import json
from Qt import QtNetwork
from Qt import QtCore
from Qt.QtCore import Signal, Property
import xml2json
from qtmparser import QtmParser
from qtm.packet import QRTPacketType, QRTPacket, QRTEvent
from qtm.packet import RTheader, RTEvent
import qtm
from time import sleep
class QQtmRt(QtCore.QObject):
connectedChanged = Signal(bool)
streamingChanged = Signal(bool)
packetReceived = Signal(QRTPacket)
noDataReceived = Signal(QRTPacket)
eventReceived = Signal(int)
def __init__(self, parent=None):
super(QQtmRt, self).__init__(parent=parent)
self._connected = False
self._streaming = False
self._socket = QtNetwork.QTcpSocket(parent=self)
self._socket.disconnected.connect(self._disconnected)
self._handlers = {
QRTPacketType.PacketData: self._on_data,
QRTPacketType.PacketEvent: self._on_event,
QRTPacketType.PacketError: self._on_error,
QRTPacketType.PacketNoMoreData: self._on_no_data,
}
self._receiver = qtm.Receiver(self._handlers)
self.streamingChanged.connect(self._streaming_changed)
def _on_no_data(self, packet):
self.noDataReceived.emit(packet)
def _on_data(self, packet):
self.packetReceived.emit(packet)
def _on_xml(self, packet):
self.packetReceived.emit(packet)
def _on_error(self, packet):
self.packetReceived.emit(packet)
def _on_event(self, event):
self.eventReceived.emit(event)
def _disconnected(self):
self.streaming = False
self.connected = False
def _get_connected(self):
return self._connected
def _set_connected(self, connected):
if self._connected == connected:
return
self._connected = connected
self.connectedChanged.emit(connected)
connected = QtCore.Property(
bool, _get_connected, _set_connected, notify=connectedChanged
)
def _get_streaming(self):
return self._streaming
def _set_streaming(self, streaming):
if self._streaming == streaming:
return
self._streaming = streaming
self.streamingChanged.emit(streaming)
streaming = QtCore.Property(
bool, _get_streaming, _set_streaming, notify=streamingChanged
)
def _handshake(self):
response = self._wait_for_reply()
if response is not None and response != 'QTM RT Interface connected':
return False
self.requested_version = '1.24'
version = self.set_version(version=self.requested_version)
return version == 'Version set to {}'.format(self.requested_version)
def _wait_for_reply(self, event=False):
response = None
data = bytes()
if self._socket.waitForReadyRead():
while True:
data += self._socket.readAll().data()
size, type_ = QtmParser.parse_header(data)
while len(data) < size:
self._socket.waitForReadyRead()
data += self._socket.readAll().data()
response = QtmParser.parse_response(type_, data[RTheader.size : size])
data = data[size:]
if type_ == QRTPacketType.PacketEvent:
if event:
return response
else:
self.eventReceived.emit(response)
else:
return response
return response
def _send_command(self, command, command_type=QRTPacketType.PacketCommand):
command = QtmParser.create_command(command, command_type)
self._socket.write(command)
def _streaming_changed(self, streaming):
if streaming:
self._socket.readyRead.connect(self._data_received)
else:
self._socket.readyRead.disconnect(self._data_received)
def _delayed_stream_stop(self):
self.streaming = False
def get_settings(self, *args):
if args is ():
args = ['all']
self._send_command('getparameters {}'.format(' '.join(args)))
xml_text = self._wait_for_reply()
options = lambda: None
options.pretty = False
json_text = xml2json.xml2json(xml_text, options)
settings = json.loads(json_text)
settings = settings.pop('QTM_Parameters_Ver_' + self.requested_version)
return settings
def get_parameters(self, *args):
if args is ():
args = ['all']
self._send_command('getparameters {}'.format(' '.join(args)))
xml_text = self._wait_for_reply()
return xml_text
def get_latest_event(self):
self._send_command('getstate')
return self._wait_for_reply(event=True)
def set_version(self, version='1.21'):
self._send_command('version {}'.format(version))
return self._wait_for_reply()
def _data_received(self):
self._receiver.data_received(self._socket.readAll().data())
def stream(self, *args):
if args is ():
args = ['all']
self._send_command('streamframes allframes {}'.format(' '.join(args)))
self.streaming = True
def stop_stream(self):
self._send_command('streamframes stop')
# Hackish so that any packets already on the way will be delivered and parsed correctly.
QtCore.QTimer.singleShot(500, self._delayed_stream_stop)
def connect_to_qtm(self, host='127.0.0.1', timeout=3000):
if self._connected:
return False
self._socket.connectToHost(host, 22223)
if self._socket.waitForConnected(timeout):
self.connected = self._handshake()
return self.connected
def disconnect(self):
if not self._connected:
return
self._socket.disconnectFromHost()