-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistener.go
142 lines (123 loc) · 2.41 KB
/
listener.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
package main
import (
"bytes"
"encoding/binary"
"encoding/json"
"log"
"net"
)
type lfsPacket struct {
dataType string
data interface{}
}
type outGaugeData struct {
Time uint32
Car [4]byte // In C that would be a char[4]
Flags uint16 // In C that would be a WORD (2 bytes of data)
Gear byte
PLID byte
Speed float32
RPM float32
Turbo float32
EngTemp float32
Fuel float32
OilPressure float32
OilTemp float32
DashLights uint32
ShowLights uint32
Throttle float32
Brake float32
Clutch float32
Display1 [16]byte
Display2 [16]byte
ID int32
}
type outSimData struct {
Time uint32
AngVelX float32
AngVelY float32
AngVelZ float32
Heading float32
Pitch float32
Roll float32
AccelX float32
AccelY float32
AccelZ float32
VelX float32
VelY float32
VelZ float32
PosX int32
PosY int32
PosZ int32
ID int32
}
func (lp *lfsPacket) toJSON() ([]byte, error) {
var s []byte
var err error
switch d := lp.data.(type) {
case outGaugeData:
s, err = json.Marshal(d)
case outSimData:
s, err = json.Marshal(d)
}
if err != nil {
return nil, err
}
return s, nil
}
func outGaugeListener(address string, c chan lfsPacket) {
addr, err := net.ResolveUDPAddr("udp", address)
if err != nil {
log.Fatal(err)
}
conn, err := net.ListenUDP("udp", addr)
if err != nil {
log.Fatal(err)
}
var decoded outGaugeData
buffer := make([]byte, binary.Size(&decoded))
for {
if _, err := conn.Read(buffer); err != nil {
log.Fatal(err)
}
if err := binary.Read(bytes.NewReader(buffer), binary.LittleEndian, &decoded); err != nil {
log.Fatal(err)
}
packet := lfsPacket{
dataType: "outGauge",
data: decoded,
}
select {
case c <- packet:
default:
}
}
}
func outSimListener(address string, c chan lfsPacket) {
addr, err := net.ResolveUDPAddr("udp", address)
if err != nil {
log.Fatal(err)
}
conn, err := net.ListenUDP("udp", addr)
if err != nil {
log.Fatal(err)
}
var decoded outSimData
buffer := make([]byte, binary.Size(&decoded))
for {
if _, err := conn.Read(buffer); err != nil {
log.Fatal(err)
}
if err := binary.Read(bytes.NewReader(buffer), binary.LittleEndian, &decoded); err != nil {
log.Fatal(err)
}
packet := lfsPacket{
dataType: "outSim",
data: decoded,
}
select {
case c <- packet:
default:
}
}
}