forked from quimcalpe/iracing-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
variables.go
224 lines (201 loc) · 5 KB
/
variables.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
package irsdk
import (
"fmt"
"log"
"sync"
"time"
)
type VarBuffer struct {
TickCount int // used to detect changes in data
bufOffset int // offset from header
}
type VarType int
const (
VarTypeChar VarType = 0
VarTypeBool VarType = 1
VarTypeInt VarType = 2
VarTypeBitField VarType = 3
VarTypeFloat VarType = 4
VarTypeDouble VarType = 5
VarTypeETCount VarType = 6
)
type Variable struct {
VarType VarType // irsdk_VarType
offset int // offset fron start of buffer row
Count int // number of entrys (array) so length in bytes would be irsdk_VarTypeBytes[type] * count
countAsTime bool
Name string
Desc string
Unit string
Value interface{}
Values interface{}
RawBytes []byte
}
func (v Variable) String() string {
var ret string
switch v.VarType {
case 0:
ret = fmt.Sprintf("%c", v.Value)
case 1:
ret = fmt.Sprintf("%v", v.Value)
case 2:
ret = fmt.Sprintf("%d", v.Value)
case 3:
ret = fmt.Sprintf("%s", v.Value)
case 4:
ret = fmt.Sprintf("%f", v.Value)
case 5:
ret = fmt.Sprintf("%f", v.Value)
default:
ret = fmt.Sprintf("Unknown (%d)", v.VarType)
}
return ret
}
// TelemetryVars holds all variables we can read from telemetry live
type TelemetryVars struct {
lastVersion int
vars map[string]Variable
mux sync.Mutex
}
func findLatestBuffer(r reader, h *header) VarBuffer {
var vb VarBuffer
foundTickCount := 0
for i := 0; i < h.numBuf; i++ {
rbuf := make([]byte, 16)
_, err := r.ReadAt(rbuf, int64(48+i*16))
if err != nil {
log.Fatal(err)
}
currentVb := VarBuffer{
byte4ToInt(rbuf[0:4]),
byte4ToInt(rbuf[4:8]),
}
//fmt.Printf("BUFF?: %+v\n", currentVb)
if foundTickCount < currentVb.TickCount {
foundTickCount = currentVb.TickCount
vb = currentVb
}
}
//fmt.Printf("BUFF: %+v\n", vb)
return vb
}
func readVariableHeaders(r reader, h *header) *TelemetryVars {
vars := TelemetryVars{vars: make(map[string]Variable, h.numVars)}
for i := 0; i < h.numVars; i++ {
rbuf := make([]byte, 144)
_, err := r.ReadAt(rbuf, int64(h.headerOffset+i*144))
if err != nil {
log.Fatal(err)
}
v := Variable{
VarType(byte4ToInt(rbuf[0:4])),
byte4ToInt(rbuf[4:8]),
byte4ToInt(rbuf[8:12]),
int(rbuf[12]) > 0,
bytesToString(rbuf[16:48]),
bytesToString(rbuf[48:112]),
bytesToString(rbuf[112:144]),
nil,
nil,
nil,
}
vars.vars[v.Name] = v
}
return &vars
}
func readVariableValues(sdk *IRSDK) bool {
newData := false
if sessionStatusOK(sdk.h.status) {
// find latest buffer for variables
vb := findLatestBuffer(sdk.r, sdk.h)
sdk.tVars.mux.Lock()
if sdk.tVars.lastVersion < vb.TickCount {
newData = true
sdk.tVars.lastVersion = vb.TickCount
sdk.lastValidData = time.Now().Unix()
for varName, v := range sdk.tVars.vars {
var rbuf []byte
switch v.VarType {
case VarTypeChar:
values := make([]string, v.Count)
for i := 0; i < v.Count; i++ {
rbuf = make([]byte, 1)
_, err := sdk.r.ReadAt(rbuf, int64(vb.bufOffset+v.offset+(1*i)))
if err != nil {
log.Fatal(err)
}
values[i] = string(rbuf[0])
}
v.Value = values[0]
v.Values = values
case VarTypeBool:
values := make([]bool, v.Count)
for i := 0; i < v.Count; i++ {
rbuf = make([]byte, 1)
_, err := sdk.r.ReadAt(rbuf, int64(vb.bufOffset+v.offset+(1*i)))
if err != nil {
log.Fatal(err)
}
values[i] = int(rbuf[0]) > 0
}
v.Value = values[0]
v.Values = values
case VarTypeInt:
values := make([]int, v.Count)
for i := 0; i < v.Count; i++ {
rbuf = make([]byte, 4)
_, err := sdk.r.ReadAt(rbuf, int64(vb.bufOffset+v.offset+(4*i)))
if err != nil {
log.Fatal(err)
}
values[i] = byte4ToInt(rbuf)
}
v.Value = values[0]
v.Values = values
case VarTypeBitField:
values := make([]int, v.Count)
for i := 0; i < v.Count; i++ {
rbuf = make([]byte, 4)
_, err := sdk.r.ReadAt(rbuf, int64(vb.bufOffset+v.offset+(4*i)))
if err != nil {
log.Fatal(err)
}
values[i] = byte4ToInt(rbuf)
}
v.Value = values[0]
v.Values = values
case VarTypeFloat:
values := make([]float32, v.Count)
for i := 0; i < v.Count; i++ {
rbuf = make([]byte, 4)
_, err := sdk.r.ReadAt(rbuf, int64(vb.bufOffset+v.offset+(4*i)))
if err != nil {
log.Fatal(err)
}
values[i] = byte4ToFloat(rbuf)
}
v.Value = values[0]
v.Values = values
case VarTypeDouble:
values := make([]float64, v.Count)
for i := 0; i < v.Count; i++ {
rbuf = make([]byte, 8)
_, err := sdk.r.ReadAt(rbuf, int64(vb.bufOffset+v.offset+(8*i)))
if err != nil {
log.Fatal(err)
}
values[i] = byte8ToFloat(rbuf)
}
v.Value = values[0]
v.Values = values
default:
log.Printf("unknown var type: %d", v.VarType)
}
v.RawBytes = rbuf
sdk.tVars.vars[varName] = v
}
}
sdk.tVars.mux.Unlock()
}
return newData
}