-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.go
335 lines (300 loc) · 9.43 KB
/
commands.go
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
package qualisys
import (
"encoding/binary"
"fmt"
"strconv"
"strings"
)
type senderType func(string) error
func (rt *Protocol) sendCommand(cmd string) error {
if !rt.IsConnected() {
return fmt.Errorf("sendcommand: not connected")
}
const packetHeaderSize = 8
dataSize := len(cmd) + packetHeaderSize + 1
data := make([]byte, dataSize)
binary.LittleEndian.PutUint32(data, uint32(dataSize))
binary.LittleEndian.PutUint32(data[4:8], uint32(PacketTypeCommand))
copy(data[8:dataSize], cmd+"\x00")
if _, err := rt.conn.Write(data); err != nil {
return fmt.Errorf("sendcommand: write failed: %w", err)
}
return nil
}
func (rt *Protocol) sendXML(cmd string) error {
if !rt.IsConnected() {
return fmt.Errorf("sendxml: not connected")
}
const packetHeaderSize = 8
dataSize := len(cmd) + packetHeaderSize + 1
data := make([]byte, dataSize)
binary.LittleEndian.PutUint32(data, uint32(dataSize))
binary.LittleEndian.PutUint32(data[4:8], uint32(PacketTypeXML))
copy(data[8:dataSize], cmd+"\x00")
if _, err := rt.conn.Write(data); err != nil {
return fmt.Errorf("sendxml: write failed: %w", err)
}
return nil
}
func (rt *Protocol) SetVersion(major, minor int) error {
ver := strconv.Itoa(major) + "." + strconv.Itoa(minor)
cmd := "Version " + ver
qtmResponses := []string{"Version set to " + ver}
if err := rt.sendAndWaitForResponse(rt.sendCommand, cmd, qtmResponses); err != nil {
return fmt.Errorf("start: %w", err)
}
return nil
}
// GetState makes QTM send the current state as an event.
func (rt *Protocol) GetState() error {
cmd := "GetState"
if err := rt.sendCommand(cmd); err != nil {
return fmt.Errorf("getstate: %w", err)
}
return nil
}
//go:generate stringer -type StreamRateType -trimprefix StreamRateType
type StreamRateType int
const (
StreamRateTypeAllFrames StreamRateType = iota
StreamRateTypeFrequency
StreamRateTypeFrequencyDivisor
)
func (rt Protocol) getComponentString(c ComponentType) string {
componentsToString := map[ComponentType]string{
ComponentType3D: "3D",
ComponentType3DNoLabels: "3DNoLabels",
ComponentTypeAnalog: "Analog",
ComponentTypeForce: "Force",
ComponentType6D: "6D",
ComponentType6DEuler: "6DEuler",
ComponentType2D: "2D",
ComponentType2DLinearized: "2DLin",
ComponentType3DResidual: "3DRes",
ComponentType3DNoLabelsResidual: "3dNoLabelsResidual",
ComponentType6DResidual: "6DRes",
ComponentType6DEulerResidual: "6DEulerRes",
ComponentTypeAnalogSingle: "AnalogSingle",
ComponentTypeImage: "Image",
ComponentTypeForceSingle: "ForceSingle",
ComponentTypeGazeVector: "GazeVector",
ComponentTypeTimecode: "Timecode",
ComponentTypeSkeleton: "Skeleton",
ComponentTypeEyeTracker: "EyeTracker",
}
return componentsToString[c]
}
func (rt *Protocol) GetCurrentFrame(components ...ComponentType) error {
cmd := "GetCurrentFrame"
for _, c := range components {
cmd += " " + rt.getComponentString(c)
}
if err := rt.sendCommand(cmd); err != nil {
return fmt.Errorf("getcurrentframe: %w", err)
}
return nil
}
func (rt *Protocol) StreamFramesAll(components ...ComponentType) error {
if err := rt.StreamFrames(StreamRateTypeAllFrames, 0, components...); err != nil {
return fmt.Errorf("streamframesall: %w", err)
}
return nil
}
func (rt *Protocol) StreamFrames(rate StreamRateType, value int, components ...ComponentType) error {
cmd := "StreamFrames"
switch rate {
case StreamRateTypeAllFrames:
cmd += " allframes"
case StreamRateTypeFrequency:
cmd += " frequency:" + strconv.Itoa(value)
case StreamRateTypeFrequencyDivisor:
cmd += " frequencydivisor:" + strconv.Itoa(value)
}
for _, c := range components {
cmd += " " + rt.getComponentString(c)
}
if err := rt.sendCommand(cmd); err != nil {
return fmt.Errorf("streamframes: %w", err)
}
return nil
}
func (rt *Protocol) StreamFramesStop() error {
cmd := "StreamFrames stop"
if err := rt.sendCommand(cmd); err != nil {
return fmt.Errorf("streamframesstop: %w", err)
}
return nil
}
func (rt *Protocol) TakeControl(password string) error {
cmd := "TakeControl " + password
qtmResponses := []string{"You are now master", "You are already master"}
if err := rt.sendAndWaitForResponse(rt.sendCommand, cmd, qtmResponses); err != nil {
return fmt.Errorf("takecontrol: %w", err)
}
return nil
}
func (rt *Protocol) ReleaseControl() error {
cmd := "ReleaseControl"
qtmResponses := []string{"You are now a regular client", "You are already a regular client"}
if err := rt.sendAndWaitForResponse(rt.sendCommand, cmd, qtmResponses); err != nil {
return fmt.Errorf("releasecontrol: %w", err)
}
return nil
}
func (rt *Protocol) New() error {
cmd := "New"
qtmResponses := []string{"Creating new connection"}
if err := rt.sendAndWaitForResponse(rt.sendCommand, cmd, qtmResponses); err != nil {
return fmt.Errorf("new: %w", err)
}
return nil
}
func (rt *Protocol) Close() error {
cmd := "Close"
qtmResponses := []string{"Closing connection", "Closing file"}
if err := rt.sendAndWaitForResponse(rt.sendCommand, cmd, qtmResponses); err != nil {
return fmt.Errorf("start: %w", err)
}
return nil
}
func (rt *Protocol) Start(rtFromFile bool) error {
cmd := "Start"
if rtFromFile {
cmd += " RTFromFile"
}
qtmResponses := []string{"Starting measurement", "Starting RT from file"}
if err := rt.sendAndWaitForResponse(rt.sendCommand, cmd, qtmResponses); err != nil {
return fmt.Errorf("start: %w", err)
}
return nil
}
func (rt *Protocol) Stop() error {
cmd := "Stop"
qtmResponses := []string{"Stopping measurement"}
if err := rt.sendAndWaitForResponse(rt.sendCommand, cmd, qtmResponses); err != nil {
return fmt.Errorf("stop: %w", err)
}
return nil
}
func (rt *Protocol) Load(filename string) error {
cmd := "Load " + filename
qtmResponses := []string{"Measurement loaded"}
if err := rt.sendAndWaitForResponse(rt.sendCommand, cmd, qtmResponses); err != nil {
return fmt.Errorf("load: %w", err)
}
return nil
}
func (rt *Protocol) Save(filename string, overwrite bool) error {
cmd := "Save " + filename
if overwrite {
cmd += " Overwrite"
}
qtmResponses := []string{"Measurement saved", "Measurement saved as " + filename}
if err := rt.sendAndWaitForResponse(rt.sendCommand, cmd, qtmResponses); err != nil {
return fmt.Errorf("save: %w", err)
}
return nil
}
func (rt *Protocol) LoadProject(path string) error {
cmd := "LoadProject " + path
qtmResponses := []string{"Project loaded"}
if err := rt.sendAndWaitForResponse(rt.sendCommand, cmd, qtmResponses); err != nil {
return fmt.Errorf("loadproject: %w", err)
}
return nil
}
func (rt *Protocol) GetCaptureC3D() error {
cmd := "GetCaptureC3D"
qtmResponses := []string{"Sending capture"}
if err := rt.sendAndWaitForResponse(rt.sendCommand, cmd, qtmResponses); err != nil {
return fmt.Errorf("getcapturec3d: %w", err)
}
return nil
}
func (rt *Protocol) GetCaptureQTM() error {
cmd := "GetCaptureQTM"
qtmResponses := []string{"Sending capture"}
if err := rt.sendAndWaitForResponse(rt.sendCommand, cmd, qtmResponses); err != nil {
return fmt.Errorf("getcaptureqtm: %w", err)
}
return nil
}
func (rt *Protocol) Trig() error {
cmd := "Trig"
qtmResponses := []string{"Trig ok"}
if err := rt.sendAndWaitForResponse(rt.sendCommand, cmd, qtmResponses); err != nil {
return fmt.Errorf("trig: %w", err)
}
return nil
}
func (rt *Protocol) SetQTMEvent(label string) error {
cmd := "SetQTMEvent " + label
qtmResponses := []string{"Event set"}
if err := rt.sendAndWaitForResponse(rt.sendCommand, cmd, qtmResponses); err != nil {
return fmt.Errorf("setqtmevent: %w", err)
}
return nil
}
func (rt *Protocol) Reprocess() error {
cmd := "Reprocess"
qtmResponses := []string{"Reprocessing file"}
if err := rt.sendAndWaitForResponse(rt.sendCommand, cmd, qtmResponses); err != nil {
return fmt.Errorf("reprocess: %w", err)
}
return nil
}
func (rt *Protocol) Calibrate(refine bool) error {
cmd := "Calibrate"
if refine {
cmd += " Refine"
}
qtmResponses := []string{"Starting calibration"}
if err := rt.sendAndWaitForResponse(rt.sendCommand, cmd, qtmResponses); err != nil {
return fmt.Errorf("calibrate: %w", err)
}
return nil
}
//go:generate stringer -type LedMode -trimprefix LedMode
type LedMode uint8
const (
LedModeOn LedMode = iota
LedModeOff
LedModePulsing
)
//go:generate stringer -type LedColor -trimprefix LedColor
type LedColor uint8
const (
LedColorAmber LedColor = iota
LedColorGreen
LedColorAll
)
func (rt *Protocol) Led(cameraNumber int, mode LedMode, color LedColor) error {
cmd := "Led " + strconv.Itoa(cameraNumber) + " " + mode.String() + " " + color.String()
if err := rt.sendCommand(cmd); err != nil {
return fmt.Errorf("stop: %w", err)
}
return nil
}
func (rt *Protocol) Quit() error {
cmd := "Quit"
qtmResponses := []string{"Bye bye"}
if err := rt.sendAndWaitForResponse(rt.sendCommand, cmd, qtmResponses); err != nil {
return fmt.Errorf("stop: %w", err)
}
return nil
}
func (rt *Protocol) sendAndWaitForResponse(sender senderType, s string, expectedResponses []string) error {
if err := sender(s); err != nil {
return fmt.Errorf("sendcommandandwaitforresponse: sender: %w", err)
}
p, err := rt.Receive()
if err != nil {
return fmt.Errorf("sendcommandandwaitforresponse: receive: %w", err)
}
for _, r := range expectedResponses {
if strings.EqualFold(p.CommandResponse, r) {
return nil
}
}
return fmt.Errorf("sendcommandandwaitforresponse: response (%s)", p.CommandResponse)
}