-
Notifications
You must be signed in to change notification settings - Fork 0
/
gateway.go
575 lines (496 loc) · 14.9 KB
/
gateway.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
package gateway
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"github.com/avast/retry-go"
"github.com/cyrilix/robocar-protobuf/go/events"
"github.com/cyrilix/robocar-simulator/pkg/simulator"
"github.com/disintegration/imaging"
"github.com/golang/protobuf/ptypes/timestamp"
"go.uber.org/zap"
"image"
"image/jpeg"
"io"
"net"
"strconv"
"sync"
"time"
)
type SimulatorSource interface {
FrameSource
SteeringSource
ThrottleSource
}
type FrameSource interface {
SubscribeFrame() <-chan *events.FrameMessage
}
type SteeringSource interface {
SubscribeSteering() <-chan *events.SteeringMessage
}
type ThrottleSource interface {
SubscribeThrottle() <-chan *events.ThrottleMessage
}
func New(addressSimulator string, car *simulator.CarConfigMsg, racer *simulator.RacerBioMsg, camera *simulator.CamConfigMsg) (*Gateway, error) {
l := zap.S().With("simulator", addressSimulator)
l.Info("run gateway from simulator")
imgHeight, err := strconv.Atoi(camera.ImgH)
if err != nil {
return nil, fmt.Errorf("invalid camera configuration, height should be a number: %w", err)
}
imgWidth, err := strconv.Atoi(camera.ImgW)
if err != nil {
return nil, fmt.Errorf("invalid camera configuration, width should be a number: %w", err)
}
return &Gateway{
address: addressSimulator,
log: l,
frameSubscribers: make(map[chan<- *events.FrameMessage]interface{}),
steeringSubscribers: make(map[chan<- *events.SteeringMessage]interface{}),
throttleSubscribers: make(map[chan<- *events.ThrottleMessage]interface{}),
telemetrySubscribers: make(map[chan *simulator.TelemetryMsg]interface{}),
carSubscribers: make(map[chan *simulator.Msg]interface{}),
racerSubscribers: make(map[chan *simulator.Msg]interface{}),
cameraSubscribers: make(map[chan *simulator.Msg]interface{}),
carConfig: car,
imgWidth: imgWidth,
imgHeight: imgHeight,
racer: racer,
cameraConfig: camera,
}, nil
}
// Gateway is Simulator interface to events gateway frames into events topicFrame
type Gateway struct {
cancel chan interface{}
address string
muCommand sync.Mutex
muConn sync.Mutex
conn io.ReadWriteCloser
muControl sync.Mutex
lastControl *simulator.ControlMsg
log *zap.SugaredLogger
frameSubscribers map[chan<- *events.FrameMessage]interface{}
steeringSubscribers map[chan<- *events.SteeringMessage]interface{}
throttleSubscribers map[chan<- *events.ThrottleMessage]interface{}
telemetrySubscribers map[chan *simulator.TelemetryMsg]interface{}
carSubscribers map[chan *simulator.Msg]interface{}
racerSubscribers map[chan *simulator.Msg]interface{}
cameraSubscribers map[chan *simulator.Msg]interface{}
carConfig *simulator.CarConfigMsg
imgHeight int
imgWidth int
racer *simulator.RacerBioMsg
cameraConfig *simulator.CamConfigMsg
}
func (g *Gateway) Start() error {
g.log.Info("connect to simulator")
g.cancel = make(chan interface{})
go g.run()
err := g.writeRacerConfig()
if err != nil {
return fmt.Errorf("unable to configure racer to server: %v", err)
}
err = g.writeCameraConfig()
if err != nil {
return fmt.Errorf("unable to configure camera to server: %v", err)
}
err = g.writeCarConfig()
if err != nil {
return fmt.Errorf("unable to configure car to server: %v", err)
}
msgChan := g.subscribeTelemetryEvents()
for {
select {
case msg := <-msgChan:
g.log.Debug("try to publish frame")
fr, err := g.publishFrame(msg)
if err != nil {
zap.S().Errorf("unable to publish frame, ignore event %v: %v", msg, err)
}
g.log.Debugf("frame published: %v", fr)
go g.publishInputSteering(msg, fr)
go g.publishInputThrottle(msg, fr)
case <-g.cancel:
return nil
}
}
}
func (g *Gateway) Stop() {
g.log.Info("close simulator gateway")
close(g.cancel)
if err := g.Close(); err != nil {
g.log.Warnf("unexpected error while simulator connection is closed: %v", err)
}
}
func (g *Gateway) Close() error {
for c := range g.frameSubscribers {
close(c)
}
for c := range g.steeringSubscribers {
close(c)
}
for c := range g.throttleSubscribers {
close(c)
}
if g.conn == nil {
g.log.Warn("no connection to close")
return nil
}
if err := g.conn.Close(); err != nil {
return fmt.Errorf("unable to close connection to simulator: %v", err)
}
return nil
}
func (g *Gateway) run() {
err := g.connect()
if err != nil {
g.log.Panicf("unable to connect to simulator: %v", err)
}
err = retry.Do(
func() error { return g.listen() },
)
if err != nil {
g.log.Errorf("unable to connect to server: %v", err)
}
}
func (g *Gateway) connect() error {
g.muConn.Lock()
defer g.muConn.Unlock()
if g.conn != nil {
// already connected
return nil
}
err := retry.Do(func() error {
g.log.Info("connect to simulator")
conn, err := connect(g.address)
if err != nil {
return fmt.Errorf("unable to connect to simulator at %v", g.address)
}
g.conn = conn
g.log.Info("connection success")
return nil
},
retry.Delay(1*time.Second),
)
return err
}
func (g *Gateway) listen() error {
reader := bufio.NewReader(g.conn)
for {
rawLine, err := reader.ReadBytes('\n')
if err == io.EOF {
g.log.Info("Connection closed")
return err
}
if err != nil {
return fmt.Errorf("unable to read response: %v", err)
}
var msg simulator.Msg
err = json.Unmarshal(rawLine, &msg)
if err != nil {
g.log.Errorf("unable to unmarshal simulator msg '%v': %v", string(rawLine), err)
continue
}
g.log.Debugf("new message of type %v", string(msg.MsgType))
switch msg.MsgType {
case "":
g.log.Debug("ping")
case simulator.MsgTypeTelemetry:
g.broadcastTelemetryMsg(rawLine)
case simulator.MsgTypeCarLoaded:
g.log.Debugf("car loaded: %v",string(rawLine))
if err := g.writeCarConfig(); err != nil{
zap.S().Errorf("unable to send car config: %v", err)
}
g.broadcastCarMsg(rawLine)
case simulator.MsgTypeRacerInfo:
g.broadcastRacerMsg(rawLine)
default:
g.log.Warnf("unmanaged simulator message: %v", string(rawLine))
}
}
}
func (g *Gateway) broadcastTelemetryMsg(rawLine []byte) {
var tMsg simulator.TelemetryMsg
err := json.Unmarshal(rawLine, &tMsg)
if err != nil {
g.log.Errorf("unable to unmarshal telemetry simulator msg '%v': %v", string(rawLine), err)
return
}
g.log.Debugf("broadcast %d telemetry'", len(g.telemetrySubscribers))
for c := range g.telemetrySubscribers {
g.log.Debugf("broadcast telemetry ")
c <- &tMsg
}
}
func (g *Gateway) broadcastCarMsg(rawLine []byte) {
var tMsg simulator.Msg
err := json.Unmarshal(rawLine, &tMsg)
if err != nil {
g.log.Errorf("unable to unmarshal car simulator msg '%v': %v", string(rawLine), err)
return
}
for c := range g.carSubscribers {
c <- &tMsg
}
}
func (g *Gateway) broadcastRacerMsg(rawLine []byte) {
var tMsg simulator.Msg
err := json.Unmarshal(rawLine, &tMsg)
if err != nil {
g.log.Errorf("unable to unmarshal racer simulator msg '%v': %v", string(rawLine), err)
return
}
for c := range g.racerSubscribers {
c <- &tMsg
}
}
func (g *Gateway) broadcastCameraMsg(rawLine []byte) {
var tMsg simulator.Msg
err := json.Unmarshal(rawLine, &tMsg)
if err != nil {
g.log.Errorf("unable to unmarshal camera simulator msg '%v': %v", string(rawLine), err)
return
}
for c := range g.cameraSubscribers {
c <- &tMsg
}
}
func (g *Gateway) publishFrame(msgSim *simulator.TelemetryMsg) (*events.FrameRef, error) {
now := time.Now()
frameRef := &events.FrameRef{
Name: "gateway",
Id: fmt.Sprintf("%d%03d", now.Unix(), now.Nanosecond()/1000/1000),
CreatedAt: ×tamp.Timestamp{
Seconds: now.Unix(),
Nanos: int32(now.Nanosecond()),
},
}
msg := &events.FrameMessage{
Id: frameRef,
Frame: msgSim.Image,
}
img, _, err := image.Decode(bytes.NewReader(msgSim.Image))
if err != nil {
return nil, fmt.Errorf("bad image, skip simulator event: %w", err)
}
if img.Bounds().Dy() != g.imgHeight || img.Bounds().Dx() != g.imgWidth {
zap.S().Debugf("resize simulator image from (%vx%v) -> (%dx%d)", img.Bounds().Dx(), img.Bounds().Dy(), g.imgWidth, g.imgHeight)
img = imaging.Resize(img, g.imgWidth, g.imgHeight, imaging.NearestNeighbor)
var imgBuf bytes.Buffer
err = jpeg.Encode(&imgBuf, img, nil)
if err != nil {
return nil, fmt.Errorf("unable to encode to peg resized image: %w", err)
}
msg.Frame = imgBuf.Bytes()
}
g.log.Debugf("events frame '%v/%v'", msg.Id.Name, msg.Id.Id)
for fs := range g.frameSubscribers {
fs <- msg
}
return frameRef, nil
}
func (g *Gateway) publishInputSteering(msgSim *simulator.TelemetryMsg, frameRef *events.FrameRef) {
steering := &events.SteeringMessage{
FrameRef: frameRef,
Steering: float32(msgSim.SteeringAngle),
Confidence: 1.0,
}
g.log.Debugf("events steering '%v'", steering.Steering)
for ss := range g.steeringSubscribers {
ss <- steering
}
}
func (g *Gateway) publishInputThrottle(msgSim *simulator.TelemetryMsg, frameRef *events.FrameRef) {
msg := &events.ThrottleMessage{
FrameRef: frameRef,
Throttle: float32(msgSim.Throttle),
Confidence: 1.0,
}
g.log.Debugf("events throttle '%v'", msg.Throttle)
for ts := range g.throttleSubscribers {
ts <- msg
}
}
func (g *Gateway) SubscribeFrame() <-chan *events.FrameMessage {
frameChan := make(chan *events.FrameMessage)
g.frameSubscribers[frameChan] = struct{}{}
return frameChan
}
func (g *Gateway) SubscribeSteering() <-chan *events.SteeringMessage {
steeringChan := make(chan *events.SteeringMessage)
g.steeringSubscribers[steeringChan] = struct{}{}
return steeringChan
}
func (g *Gateway) SubscribeThrottle() <-chan *events.ThrottleMessage {
throttleChan := make(chan *events.ThrottleMessage)
g.throttleSubscribers[throttleChan] = struct{}{}
return throttleChan
}
func (g *Gateway) subscribeTelemetryEvents() chan *simulator.TelemetryMsg {
telemetryChan := make(chan *simulator.TelemetryMsg)
g.telemetrySubscribers[telemetryChan] = struct{}{}
return telemetryChan
}
func (g *Gateway) unsubscribeTelemetryEvents(telemetryChan chan *simulator.TelemetryMsg) {
delete(g.telemetrySubscribers, telemetryChan)
close(telemetryChan)
}
func (g *Gateway) subscribeCarEvents() chan *simulator.Msg {
carChan := make(chan *simulator.Msg)
g.carSubscribers[carChan] = struct{}{}
return carChan
}
func (g *Gateway) unsubscribeCarEvents(carChan chan *simulator.Msg) {
delete(g.carSubscribers, carChan)
close(carChan)
}
func (g *Gateway) subscribeRacerEvents() chan *simulator.Msg {
racerChan := make(chan *simulator.Msg)
g.racerSubscribers[racerChan] = struct{}{}
return racerChan
}
func (g *Gateway) unsubscribeRacerEvents(racerChan chan *simulator.Msg) {
delete(g.racerSubscribers, racerChan)
close(racerChan)
}
func (g *Gateway) subscribeCameraEvents() chan *simulator.Msg {
cameraChan := make(chan *simulator.Msg)
g.cameraSubscribers[cameraChan] = struct{}{}
return cameraChan
}
func (g *Gateway) unsubscribeCameraEvents(cameraChan chan *simulator.Msg) {
delete(g.cameraSubscribers, cameraChan)
close(cameraChan)
}
var connect = func(address string) (io.ReadWriteCloser, error) {
conn, err := net.Dial("tcp", address)
if err != nil {
return nil, fmt.Errorf("unable to connect to %v", address)
}
return conn, nil
}
func (g *Gateway) WriteSteering(message *events.SteeringMessage) {
g.muControl.Lock()
defer g.muControl.Unlock()
g.initLastControlMsg()
g.lastControl.Steering = fmt.Sprintf("%.2f", message.Steering)
zap.S().Debugf("steering updated: %v", g.lastControl.Steering)
g.writeControlCommandToSimulator()
}
func (g *Gateway) writeControlCommandToSimulator() {
content, err := json.Marshal(g.lastControl)
if err != nil {
g.log.Errorf("unable to marshall control msg \"%#v\": %v", g.lastControl, err)
return
}
err = g.writeCommand(content)
if err != nil {
g.log.Errorf("unable to send control command to simulator: %v", err)
}
}
func (g *Gateway) writeCommand(content []byte) error {
g.muCommand.Lock()
defer g.muCommand.Unlock()
if err := g.connect(); err != nil {
g.log.Errorf("unable to connect to simulator to send control command: %v", err)
return nil
}
g.log.Debugf("write command to simulator: %v", string(content))
w := bufio.NewWriter(g.conn)
_, err := w.Write(content)
if err != nil {
return fmt.Errorf("unable to write control msg \"%#v\" to simulator: %v", g.lastControl, err)
}
err = w.Flush()
if err != nil {
return fmt.Errorf("unable to flush control msg \"%#v\" to simulator: %v", g.lastControl, err)
}
return nil
}
func (g *Gateway) WriteThrottle(message *events.ThrottleMessage) {
g.muControl.Lock()
defer g.muControl.Unlock()
g.initLastControlMsg()
if message.Throttle > 0 {
g.lastControl.Throttle = fmt.Sprintf("%.2f", message.Throttle)
g.lastControl.Brake = "0.0"
} else {
g.lastControl.Throttle = "0.0"
g.lastControl.Brake = fmt.Sprintf("%.2f", -1*message.Throttle)
}
zap.S().Debugf("throttle updated: %v, brake: %v", g.lastControl.Throttle, g.lastControl.Brake)
g.writeControlCommandToSimulator()
}
func (g *Gateway) initLastControlMsg() {
if g.lastControl != nil {
return
}
g.lastControl = &simulator.ControlMsg{
MsgType: simulator.MsgTypeControl,
Steering: "0.0",
Throttle: "0.0",
Brake: "0.0",
}
}
func (g *Gateway) writeCarConfig() error {
carChan := g.subscribeCarEvents()
defer g.unsubscribeCarEvents(carChan)
g.log.Info("Send car configuration")
content, err := json.Marshal(g.carConfig)
if err != nil {
return fmt.Errorf("unable to marshall car config msg \"%#v\": %v", g.lastControl, err)
}
err = g.writeCommand(content)
if err != nil {
return fmt.Errorf("unable to send car config to simulator: %v", err)
}
select {
case msg := <-carChan:
g.log.Infof("Car loaded: %v", msg)
case <-time.Tick(250 * time.Millisecond):
g.log.Info("no response after car loading")
}
return nil
}
func (g *Gateway) writeRacerConfig() error {
racerChan := g.subscribeRacerEvents()
defer g.unsubscribeRacerEvents(racerChan)
g.log.Info("Send racer configuration")
content, err := json.Marshal(g.racer)
if err != nil {
return fmt.Errorf("unable to marshall racer config msg \"%#v\": %v", g.lastControl, err)
}
err = g.writeCommand(content)
if err != nil {
return fmt.Errorf("unable to send racer config to simulator: %v", err)
}
select {
case msg := <-racerChan:
g.log.Infof("Racer loaded: %v", msg)
case <-time.Tick(250 * time.Millisecond):
g.log.Info("no response after racer loading")
}
return nil
}
func (g *Gateway) writeCameraConfig() error {
cameraChan := g.subscribeCameraEvents()
defer g.unsubscribeCameraEvents(cameraChan)
g.log.Info("Send camera configuration")
content, err := json.Marshal(g.cameraConfig)
if err != nil {
return fmt.Errorf("unable to marshall camera config msg \"%#v\": %v", g.lastControl, err)
}
err = g.writeCommand(content)
if err != nil {
return fmt.Errorf("unable to send camera config to simulator: %v", err)
}
select {
case msg := <-cameraChan:
g.log.Infof("Camera configured: %v", msg)
case <-time.Tick(250 * time.Millisecond):
g.log.Info("no response after camera loading")
}
return nil
}