forked from quimcalpe/iracing-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathirsdk.go
224 lines (187 loc) · 4.53 KB
/
irsdk.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"
"github.com/go-yaml/yaml"
"github.com/hfoxy/iracing-sdk/iryaml"
"io/ioutil"
"log"
"strings"
"time"
"github.com/hfoxy/iracing-sdk/lib/winevents"
"github.com/hidez8891/shm"
)
type SDK interface {
RefreshSession()
WaitForData(timeout time.Duration) bool
GetVars() ([]Variable, error)
GetVar(name string) (Variable, error)
GetVarValue(name string) (interface{}, error)
GetVarValues(name string) (interface{}, error)
GetSession() iryaml.IRSession
GetLastVersion() int
IsConnected() bool
ExportIbtTo(fileName string)
ExportSessionTo(fileName string)
GetYaml() string
BroadcastMsg(msg Msg)
Close()
}
// IRSDK is the main SDK object clients must use
type IRSDK struct {
SDK
r reader
h *header
session iryaml.IRSession
s []string
tVars *TelemetryVars
lastValidData int64
}
func (sdk *IRSDK) RefreshSession() {
if sessionStatusOK(sdk.h.status) {
sRaw := readSessionData(sdk.r, sdk.h)
err := yaml.Unmarshal([]byte(sRaw), &sdk.session)
if err != nil {
log.Println(err)
}
sdk.s = strings.Split(sRaw, "\n")
}
}
func (sdk *IRSDK) WaitForData(timeout time.Duration) bool {
if !sdk.IsConnected() {
initIRSDK(sdk)
}
if winevents.WaitForSingleObject(timeout) {
sdk.RefreshSession()
return readVariableValues(sdk)
}
return false
}
func (sdk *IRSDK) GetVars() ([]Variable, error) {
if !sessionStatusOK(sdk.h.status) {
return make([]Variable, 0), fmt.Errorf("session is not active")
}
results := make([]Variable, len(sdk.tVars.vars))
sdk.tVars.mux.Lock()
defer sdk.tVars.mux.Unlock()
idx := 0
for _, variable := range sdk.tVars.vars {
results[idx] = variable
idx++
}
return results, nil
}
func (sdk *IRSDK) GetVar(name string) (Variable, error) {
if !sessionStatusOK(sdk.h.status) {
return Variable{}, fmt.Errorf("session is not active")
}
sdk.tVars.mux.Lock()
defer sdk.tVars.mux.Unlock()
if v, ok := sdk.tVars.vars[name]; ok {
return v, nil
}
return Variable{}, fmt.Errorf("Telemetry variable %q not found", name)
}
func (sdk *IRSDK) GetVarValue(name string) (interface{}, error) {
var r Variable
var err error
if r, err = sdk.GetVar(name); err == nil {
return r.Value, nil
}
return r, err
}
func (sdk *IRSDK) GetVarValues(name string) (interface{}, error) {
var r Variable
var err error
if r, err = sdk.GetVar(name); err == nil {
return r.Values, nil
}
return r, err
}
func (sdk *IRSDK) GetSession() iryaml.IRSession {
return sdk.session
}
func (sdk *IRSDK) GetLastVersion() int {
if !sessionStatusOK(sdk.h.status) {
return -1
}
sdk.tVars.mux.Lock()
defer sdk.tVars.mux.Unlock()
last := sdk.tVars.lastVersion
return last
}
func (sdk *IRSDK) GetSessionData(path string) (string, error) {
if !sessionStatusOK(sdk.h.status) {
return "", fmt.Errorf("session not connected")
}
return getSessionDataPath(sdk.s, path)
}
func (sdk *IRSDK) IsConnected() bool {
if sdk.h != nil {
if sessionStatusOK(sdk.h.status) && (sdk.lastValidData+connTimeout > time.Now().Unix()) {
return true
}
}
return false
}
// ExportIbtTo exports current memory data to a file
func (sdk *IRSDK) ExportIbtTo(fileName string) {
rbuf := make([]byte, fileMapSize)
_, err := sdk.r.ReadAt(rbuf, 0)
if err != nil {
log.Fatal(err)
}
ioutil.WriteFile(fileName, rbuf, 0644)
}
// ExportSessionTo exports current session yaml data to a file
func (sdk *IRSDK) ExportSessionTo(fileName string) {
y := strings.Join(sdk.s, "\n")
ioutil.WriteFile(fileName, []byte(y), 0644)
}
func (sdk *IRSDK) GetYaml() string {
return strings.Join(sdk.s, "\n")
}
func (sdk *IRSDK) BroadcastMsg(msg Msg) {
if msg.P2 == nil {
msg.P2 = 0
}
winevents.BroadcastMsg(broadcastMsgName, msg.Cmd, msg.P1, msg.P2, msg.P3)
}
// Close clean up sdk resources
func (sdk *IRSDK) Close() {
sdk.r.Close()
}
// Init creates a SDK instance to operate with
func Init(r reader) SDK {
if r == nil {
var err error
r, err = shm.Open(fileMapName, fileMapSize)
if err != nil {
log.Fatal(err)
}
}
sdk := &IRSDK{r: r, lastValidData: 0}
winevents.OpenEvent(dataValidEventName)
initIRSDK(sdk)
return sdk
}
func initIRSDK(sdk *IRSDK) {
h := readHeader(sdk.r)
sdk.h = &h
sdk.s = nil
if sdk.tVars != nil {
sdk.tVars.vars = nil
}
if sessionStatusOK(h.status) {
sRaw := readSessionData(sdk.r, &h)
err := yaml.Unmarshal([]byte(sRaw), &sdk.session)
if err != nil {
log.Println(err)
}
sdk.s = strings.Split(sRaw, "\n")
sdk.tVars = readVariableHeaders(sdk.r, &h)
readVariableValues(sdk)
}
}
func sessionStatusOK(status int) bool {
return (status & stConnected) > 0
}