-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsocket.go
199 lines (170 loc) · 4.26 KB
/
websocket.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
package main
import (
"time"
"net/http"
"github.com/gorilla/websocket"
"log"
"github.com/jd3nn1s/lemonaid/telemetry"
"github.com/pkg/errors"
)
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
// allow all connections by default
return true
},
}
func ProcessMsg(msg []byte) error {
telemetryMsg, err := telemetry.Decode(msg)
if err != nil {
return errors.Wrap(err, "cannot read bytes from nerdobd2:")
}
tempTelemetry, ok := telemetryMsg.(telemetry.Telemetry)
var b []byte
if ok {
tempTelemetry.Speed = float32(int(tempTelemetry.Speed*0.6213712*10)) / 10
// add error/warning fields
var warnings []string
var errs []string
if tempTelemetry.FuelLevel < 5 {
errs = append(errs, []string{"FuelRemaining", "FuelLevel"}...)
} else if tempTelemetry.FuelLevel < 20 {
warnings = append(warnings, []string{"FuelRemaining", "FuelLevel"}...)
}
if tempTelemetry.CoolantTemp > 110 {
errs = append(errs, "CoolantTemp")
} else if tempTelemetry.CoolantTemp > 105 {
warnings = append(warnings, "CoolantTemp")
}
if tempTelemetry.BatteryVoltage < 11 {
errs = append(errs, "BatteryVoltage")
} else if tempTelemetry.BatteryVoltage < 12 {
warnings = append(warnings, "BatteryVoltage")
}
if tempTelemetry.OilTemp > 260 {
errs = append(errs, "OilTemp")
} else if tempTelemetry.OilTemp > 250 {
warnings = append(warnings, "OilTemp")
}
telemetryWithStatus := telemetry.TelemetryWithStatus{
&tempTelemetry,
warnings,
errs,
}
lock.Lock()
lastTelemetry = telemetryWithStatus
lock.Unlock()
b, err = telemetryWithStatus.JSONEncode()
if err != nil {
return errors.Wrap(err, "cannot json encode telemetry")
}
} else {
tempTiming, ok := telemetryMsg.(telemetry.Timing)
if ok {
b, err = tempTiming.JSONEncode()
if err != nil {
return errors.Wrap(err, "cannot JSON encode timing")
}
}
}
select {
case sendChannel <- b:
default:
}
select {
case videoSyncedSendChannel <- SyncedMessage{time.Now(), b}:
default:
// don't block if we can't send
}
return nil
}
// telemetry uplink handler. Decodes binary websocket message and forwards
// to broadcaster
func WebSocketIncomingHandler(w http.ResponseWriter, req *http.Request) {
conn, err := upgrader.Upgrade(w, req, nil)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
for {
t, msg, err := conn.ReadMessage()
if err != nil {
break
}
if t != websocket.BinaryMessage {
continue
}
if err = ProcessMsg(msg); err != nil {
log.Println("unable to process websocket msg:", err)
continue
}
}
}
// websocket clients that will be sent telemetry as it comes in
func WebSocketOutgoingHandler(w http.ResponseWriter, req *http.Request, c <-chan []byte) {
conn, err := upgrader.Upgrade(w, req, nil)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// read is required for proper websocket operation
go func() {
for {
if _, _, err := conn.NextReader(); err != nil {
conn.Close()
break
}
}
}()
first := true
for {
var msg []byte
if first {
// send last telemetry frame when a new client connects
lock.RLock()
msg, err = lastTelemetry.JSONEncode()
msgTiming, errTiming := lastTimingTelemetry.JSONEncode()
lock.RUnlock()
if errTiming != nil {
conn.WriteMessage(websocket.TextMessage, msgTiming)
}
first = false
if err != nil {
continue
}
} else {
msg = <-c
}
err = conn.WriteMessage(websocket.TextMessage, msg)
if err != nil {
log.Println("Outgoing websocket closed")
return
}
}
}
// websocket clients that will be sent telemetry delayed by the amount that the video is buffered by
func WebSocketVideoSyncOutgoingHandler(w http.ResponseWriter, req *http.Request, c <-chan []byte) {
conn, err := upgrader.Upgrade(w, req, nil)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// read is required for proper websocket operation
go func() {
for {
if _, _, err := conn.NextReader(); err != nil {
conn.Close()
break
}
}
}()
for {
msg := <-c
err = conn.WriteMessage(websocket.TextMessage, msg)
if err != nil {
log.Println("Outgoing websocket closed")
return
}
}
}