-
Notifications
You must be signed in to change notification settings - Fork 58
/
main.go
331 lines (278 loc) · 9.76 KB
/
main.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
package main
//go:generate go-bindata -pkg main -o bindata.go -modtime 1 -prefix html html
// build: GOOS=windows GOARCH=amd64 go build -o vfrmap.exe github.com/lian/msfs2020-go/vfrmap
import (
"encoding/json"
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"path/filepath"
"syscall"
"time"
"unsafe"
"github.com/lian/msfs2020-go/simconnect"
"github.com/lian/msfs2020-go/vfrmap/html/leafletjs"
"github.com/lian/msfs2020-go/vfrmap/websockets"
)
type Report struct {
simconnect.RecvSimobjectDataByType
Title [256]byte `name:"TITLE"`
Altitude float64 `name:"INDICATED ALTITUDE" unit:"feet"` // PLANE ALTITUDE or PLANE ALT ABOVE GROUND
Latitude float64 `name:"PLANE LATITUDE" unit:"degrees"`
Longitude float64 `name:"PLANE LONGITUDE" unit:"degrees"`
Heading float64 `name:"PLANE HEADING DEGREES TRUE" unit:"degrees"`
Airspeed float64 `name:"AIRSPEED INDICATED" unit:"knot"`
AirspeedTrue float64 `name:"AIRSPEED TRUE" unit:"knot"`
VerticalSpeed float64 `name:"VERTICAL SPEED" unit:"ft/min"`
Flaps float64 `name:"TRAILING EDGE FLAPS LEFT ANGLE" unit:"degrees"`
Trim float64 `name:"ELEVATOR TRIM PCT" unit:"percent"`
RudderTrim float64 `name:"RUDDER TRIM PCT" unit:"percent"`
}
func (r *Report) RequestData(s *simconnect.SimConnect) {
defineID := s.GetDefineID(r)
requestID := defineID
s.RequestDataOnSimObjectType(requestID, defineID, 0, simconnect.SIMOBJECT_TYPE_USER)
}
type TrafficReport struct {
simconnect.RecvSimobjectDataByType
AtcID [64]byte `name:"ATC ID"`
AtcFlightNumber [8]byte `name:"ATC FLIGHT NUMBER"`
Altitude float64 `name:"PLANE ALTITUDE" unit:"feet"`
Latitude float64 `name:"PLANE LATITUDE" unit:"degrees"`
Longitude float64 `name:"PLANE LONGITUDE" unit:"degrees"`
Heading float64 `name:"PLANE HEADING DEGREES TRUE" unit:"degrees"`
}
func (r *TrafficReport) RequestData(s *simconnect.SimConnect) {
defineID := s.GetDefineID(r)
requestID := defineID
s.RequestDataOnSimObjectType(requestID, defineID, 0, simconnect.SIMOBJECT_TYPE_AIRCRAFT)
}
func (r *TrafficReport) Inspect() string {
return fmt.Sprintf(
"%s GPS %.6f %.6f @ %.0f feet %.0f°",
r.AtcID,
r.Latitude,
r.Longitude,
r.Altitude,
r.Heading,
)
}
type TeleportRequest struct {
simconnect.RecvSimobjectDataByType
Latitude float64 `name:"PLANE LATITUDE" unit:"degrees"`
Longitude float64 `name:"PLANE LONGITUDE" unit:"degrees"`
Altitude float64 `name:"PLANE ALTITUDE" unit:"feet"`
}
func (r *TeleportRequest) SetData(s *simconnect.SimConnect) {
defineID := s.GetDefineID(r)
buf := [3]float64{
r.Latitude,
r.Longitude,
r.Altitude,
}
size := simconnect.DWORD(3 * 8) // 2 * 8 bytes
s.SetDataOnSimObject(defineID, simconnect.OBJECT_ID_USER, 0, 0, size, unsafe.Pointer(&buf[0]))
}
var buildVersion string
var buildTime string
var disableTeleport bool
var verbose bool
var httpListen string
func main() {
flag.BoolVar(&verbose, "verbose", false, "verbose output")
flag.StringVar(&httpListen, "listen", "0.0.0.0:9000", "http listen")
flag.BoolVar(&disableTeleport, "disable-teleport", false, "disable teleport")
flag.Parse()
fmt.Printf("\nmsfs2020-go/vfrmap\n readme: https://github.com/lian/msfs2020-go/blob/master/vfrmap/README.md\n issues: https://github.com/lian/msfs2020-go/issues\n version: %s (%s)\n\n", buildVersion, buildTime)
exitSignal := make(chan os.Signal, 1)
signal.Notify(exitSignal, os.Interrupt, syscall.SIGTERM)
exePath, _ := os.Executable()
ws := websockets.New()
s, err := simconnect.New("msfs2020-go/vfrmap")
if err != nil {
panic(err)
}
fmt.Println("connected to flight simulator!")
report := &Report{}
err = s.RegisterDataDefinition(report)
if err != nil {
panic(err)
}
trafficReport := &TrafficReport{}
err = s.RegisterDataDefinition(trafficReport)
if err != nil {
panic(err)
}
teleportReport := &TeleportRequest{}
err = s.RegisterDataDefinition(teleportReport)
if err != nil {
panic(err)
}
eventSimStartID := s.GetEventID()
//s.SubscribeToSystemEvent(eventSimStartID, "SimStart")
//s.SubscribeToFacilities(simconnect.FACILITY_LIST_TYPE_AIRPORT, s.GetDefineID(&simconnect.DataFacilityAirport{}))
//s.SubscribeToFacilities(simconnect.FACILITY_LIST_TYPE_WAYPOINT, s.GetDefineID(&simconnect.DataFacilityWaypoint{}))
startupTextEventID := s.GetEventID()
s.ShowText(simconnect.TEXT_TYPE_PRINT_WHITE, 15, startupTextEventID, "msfs2020-go/vfrmap connected")
go func() {
app := func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
w.Header().Set("Pragma", "no-cache")
w.Header().Set("Expires", "0")
w.Header().Set("Content-Type", "text/html")
filePath := filepath.Join(filepath.Dir(exePath), "index.html")
if _, err = os.Stat(filePath); os.IsNotExist(err) {
w.Write(MustAsset(filepath.Base(filePath)))
} else {
fmt.Println("use local", filePath)
http.ServeFile(w, r, filePath)
}
}
http.HandleFunc("/ws", ws.Serve)
http.Handle("/leafletjs/", http.StripPrefix("/leafletjs/", leafletjs.FS{}))
http.HandleFunc("/", app)
//http.Handle("/", http.FileServer(http.Dir(".")))
err := http.ListenAndServe(httpListen, nil)
if err != nil {
panic(err)
}
}()
simconnectTick := time.NewTicker(100 * time.Millisecond)
planePositionTick := time.NewTicker(200 * time.Millisecond)
trafficPositionTick := time.NewTicker(10000 * time.Millisecond)
for {
select {
case <-planePositionTick.C:
report.RequestData(s)
case <-trafficPositionTick.C:
//fmt.Println("--------------------------------- REQUEST TRAFFIC --------------")
//trafficReport.RequestData(s)
//s.RequestFacilitiesList(simconnect.FACILITY_LIST_TYPE_AIRPORT, airportRequestID)
//s.RequestFacilitiesList(simconnect.FACILITY_LIST_TYPE_WAYPOINT, waypointRequestID)
case <-simconnectTick.C:
ppData, r1, err := s.GetNextDispatch()
if r1 < 0 {
if uint32(r1) == simconnect.E_FAIL {
// skip error, means no new messages?
continue
} else {
panic(fmt.Errorf("GetNextDispatch error: %d %s", r1, err))
}
}
recvInfo := *(*simconnect.Recv)(ppData)
switch recvInfo.ID {
case simconnect.RECV_ID_EXCEPTION:
recvErr := *(*simconnect.RecvException)(ppData)
fmt.Printf("SIMCONNECT_RECV_ID_EXCEPTION %#v\n", recvErr)
case simconnect.RECV_ID_OPEN:
recvOpen := *(*simconnect.RecvOpen)(ppData)
fmt.Printf(
"\nflight simulator info:\n codename: %s\n version: %d.%d (%d.%d)\n simconnect: %d.%d (%d.%d)\n\n",
recvOpen.ApplicationName,
recvOpen.ApplicationVersionMajor,
recvOpen.ApplicationVersionMinor,
recvOpen.ApplicationBuildMajor,
recvOpen.ApplicationBuildMinor,
recvOpen.SimConnectVersionMajor,
recvOpen.SimConnectVersionMinor,
recvOpen.SimConnectBuildMajor,
recvOpen.SimConnectBuildMinor,
)
case simconnect.RECV_ID_EVENT:
recvEvent := *(*simconnect.RecvEvent)(ppData)
switch recvEvent.EventID {
case eventSimStartID:
fmt.Println("EVENT: SimStart")
case startupTextEventID:
// ignore
default:
fmt.Println("unknown SIMCONNECT_RECV_ID_EVENT", recvEvent.EventID)
}
case simconnect.RECV_ID_WAYPOINT_LIST:
waypointList := *(*simconnect.RecvFacilityWaypointList)(ppData)
fmt.Printf("SIMCONNECT_RECV_ID_WAYPOINT_LIST %#v\n", waypointList)
case simconnect.RECV_ID_AIRPORT_LIST:
airportList := *(*simconnect.RecvFacilityAirportList)(ppData)
fmt.Printf("SIMCONNECT_RECV_ID_AIRPORT_LIST %#v\n", airportList)
case simconnect.RECV_ID_SIMOBJECT_DATA_BYTYPE:
recvData := *(*simconnect.RecvSimobjectDataByType)(ppData)
switch recvData.RequestID {
case s.DefineMap["Report"]:
report = (*Report)(ppData)
if verbose {
fmt.Printf("REPORT: %#v\n", report)
}
ws.Broadcast(map[string]interface{}{
"type": "plane",
"latitude": report.Latitude,
"longitude": report.Longitude,
"altitude": fmt.Sprintf("%.0f", report.Altitude),
"heading": int(report.Heading),
"airspeed": fmt.Sprintf("%.0f", report.Airspeed),
"airspeed_true": fmt.Sprintf("%.0f", report.AirspeedTrue),
"vertical_speed": fmt.Sprintf("%.0f", report.VerticalSpeed),
"flaps": fmt.Sprintf("%.0f", report.Flaps),
"trim": fmt.Sprintf("%.1f", report.Trim),
"rudder_trim": fmt.Sprintf("%.1f", report.RudderTrim),
})
case s.DefineMap["TrafficReport"]:
trafficReport = (*TrafficReport)(ppData)
fmt.Printf("TRAFFIC REPORT: %s\n", trafficReport.Inspect())
}
default:
fmt.Println("recvInfo.ID unknown", recvInfo.ID)
}
case <-exitSignal:
fmt.Println("exiting..")
if err = s.Close(); err != nil {
panic(err)
}
os.Exit(0)
case _ = <-ws.NewConnection:
// drain and skip
case m := <-ws.ReceiveMessages:
handleClientMessage(m, s)
}
}
}
func handleClientMessage(m websockets.ReceiveMessage, s *simconnect.SimConnect) {
var pkt map[string]interface{}
if err := json.Unmarshal(m.Message, &pkt); err != nil {
fmt.Println("invalid websocket packet", err)
} else {
pktType, ok := pkt["type"].(string)
if !ok {
fmt.Println("invalid websocket packet", pkt)
return
}
switch pktType {
case "teleport":
if disableTeleport {
fmt.Println("teleport disabled", pkt)
return
}
// validate user input
lat, ok := pkt["lat"].(float64)
if !ok {
fmt.Println("invalid websocket packet", pkt)
return
}
lng, ok := pkt["lng"].(float64)
if !ok {
fmt.Println("invalid websocket packet", pkt)
return
}
altitude, ok := pkt["altitude"].(float64)
if !ok {
fmt.Println("invalid websocket packet", pkt)
return
}
// teleport
r := &TeleportRequest{Latitude: lat, Longitude: lng, Altitude: altitude}
r.SetData(s)
}
}
}