-
Notifications
You must be signed in to change notification settings - Fork 2
/
simmate.go
382 lines (324 loc) · 9.24 KB
/
simmate.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
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
374
375
376
377
378
379
380
381
382
package simconnect
import (
"fmt"
"strings"
"sync"
"time"
"unsafe"
log "github.com/sirupsen/logrus"
)
const (
simVarRequestTimeout int64 = 10000
)
type OnOpenFunc func(applName, applVersion, applBuild, simConnectVersion, simConnectBuild string)
type OnQuitFunc func()
type OnSimObjectDataFunc func(data *RecvSimObjectData)
type OnSimObjectDataByTypeFunc func(data *RecvSimObjectDataByType)
type OnDataReadyFunc func()
type OnEventIDFunc func(eventID DWord)
type OnExceptionFunc func(exceptionCode DWord)
type EventListener struct {
OnOpen OnOpenFunc
OnQuit OnQuitFunc
OnSimObjectData OnSimObjectDataFunc
OnSimObjectDataByType OnSimObjectDataByTypeFunc
OnDataReady OnDataReadyFunc
OnEventID OnEventIDFunc
OnException OnExceptionFunc
}
type SimMate struct {
SimConnect
simVarManager *SimVarManager
mutex sync.Mutex
dirty bool
}
func NewSimMate() *SimMate {
if !initialized {
Initialize("")
}
mate := &SimMate{
simVarManager: NewSimVarManager(),
}
return mate
}
func (mate *SimMate) AddSimVar(name, unit string, dataType DWord) DWord {
defineID := mate.simVarManager.Add(name, unit, dataType)
mate.dirty = true
return defineID
}
func (mate *SimMate) RemoveSimVar(defineID DWord) bool {
if ok := mate.simVarManager.Remove(defineID); !ok {
return false
}
return true
}
func (mate *SimMate) SimVarValueAndDataType(defineID DWord) (interface{}, DWord, bool) {
mate.mutex.Lock()
defer mate.mutex.Unlock()
simVar, ok := mate.simVarManager.GetSimVar(defineID)
if !ok {
return nil, DataTypeInvalid, false
}
return simVar.Value, simVar.DataType, true
}
func (mate *SimMate) SimVar(defineID DWord) (SimVar, bool) {
mate.mutex.Lock()
defer mate.mutex.Unlock()
simVar, ok := mate.simVarManager.GetSimVar(defineID)
if !ok {
return SimVar{}, false
}
return *simVar, true
}
func (mate *SimMate) SimVarDump(indent string) []string {
mate.mutex.Lock()
defer mate.mutex.Unlock()
return mate.simVarManager.SimVarDump(indent)
}
func (mate *SimMate) SetSimObjectData(name, unit string, value interface{}, dataType DWord) error {
defineID := NewDefineID()
if err := mate.AddToDataDefinition(defineID, name, unit, DataTypeFloat64); err != nil {
return err
}
switch dataType {
case DataTypeFloat64:
buffer := [1]float64{
ValueToFloat64(value),
}
size := DWord(unsafe.Sizeof(buffer))
mate.SetDataOnSimObject(defineID, ObjectIDUser, 0, 0, size, unsafe.Pointer(&buffer[0]))
default:
log.Tracef("SimConnect.SetSimObjectData: datatype not implemented: %v", dataType)
}
return nil
}
func (mate *SimMate) HandleEvents(requestDataInterval time.Duration, receiveDataInterval time.Duration, stop chan interface{}, listener *EventListener) {
reqDataTicker := time.NewTicker(requestDataInterval)
defer reqDataTicker.Stop()
recvDataTicker := time.NewTicker(receiveDataInterval)
defer recvDataTicker.Stop()
requestCount := 0
updateCount := 0
for {
select {
case <-stop:
return
case <-reqDataTicker.C:
if updateCount > 0 {
if listener != nil && listener.OnDataReady != nil {
listener.OnDataReady()
}
}
mate.requestSimObjectData()
requestCount++
case <-recvDataTicker.C:
ppData, r1, err := mate.GetNextDispatch()
if r1 < 0 {
if uint32(r1) != EFail {
log.Tracef("GetNextDispatch error: %s (%d)", err.Error(), r1)
return
}
if ppData == nil {
break
}
}
recv := *(*Recv)(ppData)
switch recv.ID {
case RecvIDException:
recvException := *(*RecvException)(ppData)
if listener != nil && listener.OnException != nil {
listener.OnException(recvException.Exception)
}
case RecvIDOpen:
recvOpen := *(*RecvOpen)(ppData)
applName := strings.Trim(string(recvOpen.ApplicationName[:256]), "\x00")
applVersion := fmt.Sprintf("%d.%d", recvOpen.ApplicationVersionMajor, recvOpen.ApplicationVersionMinor)
applBuild := fmt.Sprintf("%d.%d", recvOpen.ApplicationBuildMajor, recvOpen.ApplicationBuildMinor)
simConnectVersion := fmt.Sprintf("%d.%d", recvOpen.SimConnectVersionMajor, recvOpen.SimConnectVersionMinor)
simConnectBuild := fmt.Sprintf("%d.%d", recvOpen.SimConnectBuildMajor, recvOpen.SimConnectBuildMinor)
if listener != nil && listener.OnOpen != nil {
listener.OnOpen(applName, applVersion, applBuild, simConnectVersion, simConnectBuild)
}
case RecvIDQuit:
if listener != nil && listener.OnQuit != nil {
listener.OnQuit()
}
case RecvIDEvent:
recvEvent := *(*RecvEvent)(ppData)
if listener != nil && listener.OnEventID != nil {
listener.OnEventID(recvEvent.EventID)
}
// case RecvIDEventObjectAddRemove:
// case RecvIDEventFilename:
// case RecvIDEventFrame:
case RecvIDSimobjectData:
recvData := *(*RecvSimObjectData)(ppData)
if listener != nil && listener.OnSimObjectData != nil {
listener.OnSimObjectData(&recvData)
}
case RecvIDSimObjectDataByType:
recvData := *(*RecvSimObjectDataByType)(ppData)
simVar, exists := mate.simVarManager.GetSimVar(recvData.DefineID)
if !exists {
continue
}
var value interface{}
switch simVar.DataType {
case DataTypeInt32:
value = (*SimObjectData_int32)(ppData).Value
case DataTypeInt64:
value = (*SimObjectData_int64)(ppData).Value
case DataTypeFloat32:
value = (*SimObjectData_float32)(ppData).Value
case DataTypeFloat64:
value = (*SimObjectData_float64)(ppData).Value
case DataTypeString8:
value = (*SimObjectData_string8)(ppData).Value
case DataTypeString32:
value = (*SimObjectData_string32)(ppData).Value
case DataTypeString64:
value = (*SimObjectData_string64)(ppData).Value
case DataTypeString128:
value = (*SimObjectData_string128)(ppData).Value
case DataTypeString256:
value = (*SimObjectData_string256)(ppData).Value
case DataTypeString260:
value = (*SimObjectData_string260)(ppData).Value
case DataTypeStringV:
value = (*SimObjectData_stringv)(ppData).Value
// case DataTypeInitPosition:
// case DataTypeStringMarkerState:
// case DataTypeWaypoint:
// case DataTypeStringLatLonAlt:
// case DataTypeStringXYZ:
default:
}
if value != nil {
mate.updateSimObjectData(recvData.RequestID, recvData.DefineID, value)
updateCount++
}
if listener != nil && listener.OnSimObjectDataByType != nil {
listener.OnSimObjectDataByType(&recvData)
}
// case RecvIDWeatherObservation:
// case RecvIDCloudState:
// case RecvIDAssignedObjectID:
// case RecvIDReservedKey:
// case RecvIDCustomAction:
// case RecvIDSystemState:
// case RecvIDClientData:
// case RecvIDEventWeatherMode:
// case RecvIDAirportList:
// case RecvIDVORList:
// case RecvIDNDBList:
// case RecvIDWaypointList:
// case RecvIDEventMultiplayerServerStarted:
// case RecvIDEventMultiplayerClientStarted:
// case RecvIDEventMultiplayerSessionEnded:
// case RecvIDEventRaceEnd:
// case RecvIDEventRaceLap:
// case RecvIDPick:
default:
log.Tracef("Unknown recvInfo ID: %d", recv.ID)
}
}
}
}
func (mate *SimMate) registerSimVars() (int, error) {
count := 0
for _, simVar := range mate.simVarManager.Vars {
if !simVar.Registered {
err := mate.AddToDataDefinition(simVar.DefineID, simVar.Name, simVar.Unit, simVar.DataType)
if err != nil {
return count, err
} else {
simVar.Registered = true
count++
}
}
}
return count, nil
}
func (mate *SimMate) requestSimObjectData() (bool, error) {
mate.mutex.Lock()
defer mate.mutex.Unlock()
if mate.dirty {
count, err := mate.registerSimVars()
if err != nil {
return false, err
}
if count > 0 {
log.Tracef("Registered %d simvars", count)
}
mate.dirty = false
}
timestamp := time.Now().UnixNano() / int64(time.Millisecond)
const radiusMeters = 0
simObjectType := SimObjectTypeUser
for _, simVar := range mate.simVarManager.Vars {
if !simVar.Pending {
simVar.RequestID = NewRequestID()
} else {
if timestamp-simVar.Timestamp < simVarRequestTimeout {
continue
}
}
mate.RequestDataOnSimObjectType(simVar.RequestID, simVar.DefineID, radiusMeters, simObjectType)
simVar.Timestamp = timestamp
simVar.Pending = true
}
return true, nil
}
func (mate *SimMate) updateSimObjectData(requestID, defineID DWord, value interface{}) {
if simVar, updated := mate.simVarManager.Update(requestID, defineID, value); updated {
simVar.Pending = false
}
}
// Generics. Needed. Badly. Ugh.
type SimObjectData struct {
RecvSimObjectDataByType
}
type SimObjectData_int32 struct {
SimObjectData
Value int32
}
type SimObjectData_int64 struct {
SimObjectData
Value int64
}
type SimObjectData_float32 struct {
SimObjectData
Value float32
}
type SimObjectData_float64 struct {
SimObjectData
Value float64
}
type SimObjectData_string8 struct {
SimObjectData
Value [8]byte
}
type SimObjectData_string32 struct {
SimObjectData
Value [32]byte
}
type SimObjectData_string64 struct {
SimObjectData
Value [64]byte
}
type SimObjectData_string128 struct {
SimObjectData
Value [128]byte
}
type SimObjectData_string256 struct {
SimObjectData
Value [256]byte
}
type SimObjectData_string260 struct {
SimObjectData
Value [260]byte
}
type SimObjectData_stringv struct {
SimObjectData
Value string // TODO: Not sure if this is correct
}