-
Notifications
You must be signed in to change notification settings - Fork 5
/
tools.go
190 lines (179 loc) · 4.06 KB
/
tools.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
package simconnect
import (
"errors"
"reflect"
"strconv"
"strings"
"unsafe"
"github.com/sirupsen/logrus"
)
func convStrToGoString(buf []byte) string {
var index int
var value byte
for index, value = range buf {
if value == 0x00 {
break
}
}
return string(buf[:index])
}
func convGoStringtoBytes(str string) []byte {
str = str + "\x00"
return []byte(str)
}
func convCBytesToGoBytes(ptr unsafe.Pointer, size int) ([]byte, error) {
if size > 1<<30 {
return nil, errors.New("Dispatch return to big size array data")
}
buf := make([]byte, size)
copy(buf, (*[1 << 30]byte)(ptr)[:size:size])
return buf, nil
}
func getByVarName(name string, listSimVar []SimVar) *SimVar {
if strings.Contains(name, ":") {
name = strings.Split(name, ":")[0]
}
for _, simVar := range listSimVar {
if name == simVar.Name {
return &simVar
}
}
return nil
}
func getUnitForType(t string) SimVarUnit {
switch t {
case "string":
return UnitString
case "*SIMCONNECT_DATA_XYZ":
return UnitSimconnectDataXyz
case "*SIMCONNECT_DATA_LATLONALT":
return UnitSimconnectDataLatlonalt
case "*SIMCONNECT_DATA_WAYPOINT":
return UnitSimconnectDataWaypoint
default:
return ""
}
}
func SimVarGenerator(iFace interface{}) ([]SimVar, error) {
rt := reflect.TypeOf(iFace)
if rt.Kind() != reflect.Struct {
return nil, errors.New("Interface error : " + rt.Name())
}
simVars := []SimVar{}
var err error
for i := 0; i < rt.NumField(); i++ {
f := rt.Field(i)
tag := f.Tag.Get("sim")
if tag == "" {
continue
}
index := 0
if strings.Contains(tag, ":") {
v := strings.Split(tag, ":")
tag = v[0]
index, err = strconv.Atoi(v[1])
if err != nil {
return nil, err
}
}
unit := SimVarUnit(f.Tag.Get("simUnit"))
if unit == "" {
unit = getUnitForType(f.Type.Name())
}
simVar := SimVar{
Name: tag,
Unit: unit,
Index: index,
}
simVars = append(simVars, simVar)
}
return simVars, nil
}
func InterfaceAssignSimVar(listSimVar []SimVar, iFace interface{}) {
rt := reflect.ValueOf(iFace)
for i := 0; i < rt.NumField(); i++ {
field := rt.Field(i)
switch field.Type().Name() {
case "float64":
listSimVar[i].SetFloat64(field.Float())
}
}
}
func SimVarAssignInterface(iFace interface{}, listSimVar []SimVar) interface{} {
rt := reflect.TypeOf(iFace)
if rt.Kind() != reflect.Struct {
logrus.Warn("Interface error in SimVarAssignInterface:", rt.Name())
return nil
}
reflectElem := reflect.New(rt).Elem()
for i := 0; i < rt.NumField(); i++ {
f := rt.Field(i)
tag := f.Tag.Get("sim")
if tag == "" {
continue
}
logWarm := func(i int64, err error) {
logrus.Warn("#"+strconv.FormatInt(i, 10), "ignored field in AssignInterface", f.Name, " tag:", tag, "error:", err)
}
simVar := getByVarName(tag, listSimVar)
if simVar == nil {
logWarm(8, errors.New("SimVar not found"))
continue
}
reflectValue := reflectElem.FieldByName(f.Name)
if reflectValue.Kind() == 0 {
logWarm(1, nil)
continue
}
typeValue := reflectValue.Type().String()
switch typeValue {
case "string":
reflectValue.SetString(simVar.GetString())
case "float64":
f, err := simVar.GetFloat64()
if err != nil {
logWarm(2, err)
continue
}
reflectValue.SetFloat(f)
case "bool":
b, err := simVar.GetBool()
if err != nil {
logWarm(3, err)
continue
}
reflectValue.SetBool(b)
case "int":
i, err := simVar.GetInt()
if err != nil {
logWarm(4, err)
continue
}
reflectValue.SetInt(int64(i))
case "*SIMCONNECT_DATA_XYZ":
data, err := simVar.GetDataXYZ()
if err != nil {
logWarm(5, err)
continue
}
reflectValue.Set(reflect.ValueOf(data))
case "*SIMCONNECT_DATA_LATLONALT":
data, err := simVar.GetDataLatLonAlt()
if err != nil {
logWarm(6, err)
continue
}
reflectValue.Set(reflect.ValueOf(data))
case "*SIMCONNECT_DATA_WAYPOINT":
data, err := simVar.GetDataWaypoint()
if err != nil {
logWarm(7, err)
continue
}
reflectValue.Set(reflect.ValueOf(data))
default:
logrus.Infoln("Type :", reflectValue.Type(), "?")
}
}
return reflectElem.Interface()
}