2021-01-15 10:12:21 +00:00
|
|
|
package gateway
|
2020-09-20 21:16:06 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"github.com/avast/retry-go"
|
|
|
|
"github.com/cyrilix/robocar-protobuf/go/events"
|
2021-01-15 10:12:21 +00:00
|
|
|
"github.com/cyrilix/robocar-simulator/pkg/simulator"
|
2020-09-20 21:16:06 +00:00
|
|
|
mqtt "github.com/eclipse/paho.mqtt.golang"
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
"github.com/golang/protobuf/ptypes/timestamp"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"io"
|
|
|
|
"net"
|
2021-01-15 10:31:37 +00:00
|
|
|
"sync"
|
2020-09-20 21:16:06 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func New(publisher Publisher, addressSimulator string) *Gateway {
|
2020-11-29 17:42:33 +00:00
|
|
|
l := log.WithField("simulator", addressSimulator)
|
2021-01-15 10:12:21 +00:00
|
|
|
l.Info("run gateway from simulator")
|
2020-09-20 21:16:06 +00:00
|
|
|
|
|
|
|
return &Gateway{
|
2020-11-29 17:42:33 +00:00
|
|
|
address: addressSimulator,
|
|
|
|
publisher: publisher,
|
|
|
|
log: l,
|
2020-09-20 21:16:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-15 10:12:21 +00:00
|
|
|
/* Simulator interface to publish gateway frames into mqtt topicFrame */
|
2020-09-20 21:16:06 +00:00
|
|
|
type Gateway struct {
|
2020-11-29 17:42:33 +00:00
|
|
|
cancel chan interface{}
|
2020-09-20 21:16:06 +00:00
|
|
|
|
|
|
|
address string
|
2021-01-15 10:31:37 +00:00
|
|
|
muConn sync.Mutex
|
|
|
|
conn io.ReadWriteCloser
|
|
|
|
|
|
|
|
muControl sync.Mutex
|
|
|
|
lastControl *simulator.ControlMsg
|
2020-09-20 21:16:06 +00:00
|
|
|
|
|
|
|
publisher Publisher
|
2020-11-29 17:42:33 +00:00
|
|
|
log *log.Entry
|
2020-09-20 21:16:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Gateway) Start() error {
|
2020-11-29 17:42:33 +00:00
|
|
|
p.log.Info("connect to simulator")
|
2020-09-20 21:16:06 +00:00
|
|
|
p.cancel = make(chan interface{})
|
2020-11-29 17:42:33 +00:00
|
|
|
msgChan := make(chan *simulator.TelemetryMsg)
|
2020-09-20 21:16:06 +00:00
|
|
|
|
|
|
|
go p.run(msgChan)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case msg := <-msgChan:
|
|
|
|
go p.publishFrame(msg)
|
2021-01-15 10:12:21 +00:00
|
|
|
go p.publishInputSteering(msg)
|
|
|
|
go p.publishInputThrottle(msg)
|
2020-09-20 21:16:06 +00:00
|
|
|
case <-p.cancel:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Gateway) Stop() {
|
2020-11-29 17:42:33 +00:00
|
|
|
p.log.Info("close simulator gateway")
|
2020-09-20 21:16:06 +00:00
|
|
|
close(p.cancel)
|
|
|
|
|
|
|
|
if err := p.Close(); err != nil {
|
2020-11-29 17:42:33 +00:00
|
|
|
p.log.Warnf("unexpected error while simulator connection is closed: %v", err)
|
2020-09-20 21:16:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Gateway) Close() error {
|
|
|
|
if p.conn == nil {
|
2020-11-29 17:42:33 +00:00
|
|
|
p.log.Warn("no connection to close")
|
2020-09-20 21:16:06 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err := p.conn.Close(); err != nil {
|
|
|
|
return fmt.Errorf("unable to close connection to simulator: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-29 17:42:33 +00:00
|
|
|
func (p *Gateway) run(msgChan chan<- *simulator.TelemetryMsg) {
|
2021-01-15 10:31:37 +00:00
|
|
|
err := p.connect()
|
2020-09-20 21:16:06 +00:00
|
|
|
if err != nil {
|
2020-11-29 17:42:33 +00:00
|
|
|
p.log.Panicf("unable to connect to simulator: %v", err)
|
2020-09-20 21:16:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
reader := bufio.NewReader(p.conn)
|
|
|
|
|
|
|
|
err = retry.Do(
|
|
|
|
func() error { return p.listen(msgChan, reader) },
|
|
|
|
)
|
|
|
|
if err != nil {
|
2020-11-29 17:42:33 +00:00
|
|
|
p.log.Errorf("unable to connect to server: %v", err)
|
2020-09-20 21:16:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-15 10:31:37 +00:00
|
|
|
func (p *Gateway) connect() error {
|
|
|
|
p.muConn.Lock()
|
|
|
|
defer p.muConn.Unlock()
|
|
|
|
|
|
|
|
if p.conn != nil {
|
|
|
|
// already connected
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
err := retry.Do(func() error {
|
|
|
|
p.log.Info("connect to simulator")
|
|
|
|
conn, err := connect(p.address)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to connect to simulator at %v", p.address)
|
|
|
|
}
|
|
|
|
p.conn = conn
|
|
|
|
p.log.Info("connection success")
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
retry.Delay(1*time.Second),
|
|
|
|
)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-29 17:42:33 +00:00
|
|
|
func (p *Gateway) listen(msgChan chan<- *simulator.TelemetryMsg, reader *bufio.Reader) error {
|
2020-09-20 21:16:06 +00:00
|
|
|
for {
|
|
|
|
rawLine, err := reader.ReadBytes('\n')
|
|
|
|
if err == io.EOF {
|
2020-11-29 17:42:33 +00:00
|
|
|
p.log.Info("Connection closed")
|
2020-09-20 21:16:06 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to read response: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-11-29 17:42:33 +00:00
|
|
|
var msg simulator.TelemetryMsg
|
2020-09-20 21:16:06 +00:00
|
|
|
err = json.Unmarshal(rawLine, &msg)
|
|
|
|
if err != nil {
|
2021-01-15 10:12:21 +00:00
|
|
|
p.log.Errorf("unable to unmarshal simulator msg '%v': %v", string(rawLine), err)
|
2020-09-20 21:16:06 +00:00
|
|
|
}
|
|
|
|
if "telemetry" != msg.MsgType {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
msgChan <- &msg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-29 17:42:33 +00:00
|
|
|
func (p *Gateway) publishFrame(msgSim *simulator.TelemetryMsg) {
|
2020-09-20 21:16:06 +00:00
|
|
|
now := time.Now()
|
|
|
|
msg := &events.FrameMessage{
|
|
|
|
Id: &events.FrameRef{
|
2021-01-15 10:12:21 +00:00
|
|
|
Name: "gateway",
|
2020-09-20 21:16:06 +00:00
|
|
|
Id: fmt.Sprintf("%d%03d", now.Unix(), now.Nanosecond()/1000/1000),
|
|
|
|
CreatedAt: ×tamp.Timestamp{
|
|
|
|
Seconds: now.Unix(),
|
|
|
|
Nanos: int32(now.Nanosecond()),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Frame: msgSim.Image,
|
|
|
|
}
|
|
|
|
|
2021-01-15 10:12:21 +00:00
|
|
|
log.Debugf("publish frame '%v/%v'", msg.Id.Name, msg.Id.Id)
|
2020-09-20 21:16:06 +00:00
|
|
|
payload, err := proto.Marshal(msg)
|
|
|
|
if err != nil {
|
2020-11-29 17:42:33 +00:00
|
|
|
p.log.Errorf("unable to marshal protobuf message: %v", err)
|
2020-09-20 21:16:06 +00:00
|
|
|
}
|
2021-01-15 10:12:21 +00:00
|
|
|
p.publisher.PublishFrame(payload)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Gateway) publishInputSteering(msgSim *simulator.TelemetryMsg) {
|
|
|
|
steering := &events.SteeringMessage{
|
2021-01-15 10:31:37 +00:00
|
|
|
Steering: float32(msgSim.SteeringAngle),
|
2021-01-15 10:12:21 +00:00
|
|
|
Confidence: 1.0,
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf("publish steering '%v'", steering.Steering)
|
|
|
|
payload, err := proto.Marshal(steering)
|
|
|
|
if err != nil {
|
|
|
|
p.log.Errorf("unable to marshal protobuf message: %v", err)
|
|
|
|
}
|
|
|
|
p.publisher.PublishSteering(payload)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Gateway) publishInputThrottle(msgSim *simulator.TelemetryMsg) {
|
|
|
|
steering := &events.ThrottleMessage{
|
2021-01-15 10:31:37 +00:00
|
|
|
Throttle: float32(msgSim.Throttle),
|
2021-01-15 10:12:21 +00:00
|
|
|
Confidence: 1.0,
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf("publish throttle '%v'", steering.Throttle)
|
|
|
|
payload, err := proto.Marshal(steering)
|
|
|
|
if err != nil {
|
|
|
|
p.log.Errorf("unable to marshal protobuf message: %v", err)
|
|
|
|
}
|
|
|
|
p.publisher.PublishThrottle(payload)
|
2020-09-20 21:16:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-01-15 10:31:37 +00:00
|
|
|
func (p *Gateway) WriteSteering(message *events.SteeringMessage) {
|
|
|
|
p.muControl.Lock()
|
|
|
|
defer p.muControl.Unlock()
|
|
|
|
p.initLastControlMsg()
|
|
|
|
|
|
|
|
p.lastControl.Steering = message.Steering
|
|
|
|
p.writeControlCommandToSimulator()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Gateway) writeControlCommandToSimulator() {
|
|
|
|
if err := p.connect(); err != nil {
|
|
|
|
p.log.Errorf("unable to connect to simulator to send control command: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w := bufio.NewWriter(p.conn)
|
|
|
|
content, err := json.Marshal(p.lastControl)
|
|
|
|
if err != nil {
|
|
|
|
p.log.Errorf("unable to marshall control msg \"%#v\": %v", p.lastControl, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = w.Write(append(content, '\n'))
|
|
|
|
if err != nil {
|
|
|
|
p.log.Errorf("unable to write control msg \"%#v\" to simulator: %v", p.lastControl, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = w.Flush()
|
|
|
|
if err != nil {
|
|
|
|
p.log.Errorf("unable to flush control msg \"%#v\" to simulator: %v", p.lastControl, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Gateway) WriteThrottle(message *events.ThrottleMessage) {
|
|
|
|
p.muControl.Lock()
|
|
|
|
defer p.muControl.Unlock()
|
|
|
|
p.initLastControlMsg()
|
|
|
|
|
|
|
|
if message.Throttle > 0 {
|
|
|
|
p.lastControl.Throttle = message.Throttle
|
|
|
|
p.lastControl.Brake = 0.
|
|
|
|
} else {
|
|
|
|
p.lastControl.Throttle = 0.
|
|
|
|
p.lastControl.Brake = -1 * message.Throttle
|
|
|
|
}
|
|
|
|
|
|
|
|
p.writeControlCommandToSimulator()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Gateway) initLastControlMsg() {
|
|
|
|
if p.lastControl != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p.lastControl = &simulator.ControlMsg{
|
|
|
|
MsgType: "control",
|
|
|
|
Steering: 0.,
|
|
|
|
Throttle: 0.,
|
|
|
|
Brake: 0.,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-20 21:16:06 +00:00
|
|
|
type Publisher interface {
|
2021-01-15 10:12:21 +00:00
|
|
|
PublishFrame(payload []byte)
|
|
|
|
PublishThrottle(payload []byte)
|
|
|
|
PublishSteering(payload []byte)
|
2020-09-20 21:16:06 +00:00
|
|
|
}
|
|
|
|
|
2021-01-15 10:12:21 +00:00
|
|
|
func NewMqttPublisher(client mqtt.Client, topicFrame, topicThrottle, topicSteering string) Publisher {
|
2020-09-20 21:16:06 +00:00
|
|
|
return &MqttPublisher{
|
2021-01-15 10:31:37 +00:00
|
|
|
client: client,
|
|
|
|
topicFrame: topicFrame,
|
2021-01-15 10:12:21 +00:00
|
|
|
topicSteering: topicSteering,
|
|
|
|
topicThrottle: topicThrottle,
|
2020-09-20 21:16:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type MqttPublisher struct {
|
2021-01-15 10:31:37 +00:00
|
|
|
client mqtt.Client
|
|
|
|
topicFrame string
|
2021-01-15 10:12:21 +00:00
|
|
|
topicSteering string
|
|
|
|
topicThrottle string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MqttPublisher) PublishThrottle(payload []byte) {
|
|
|
|
if m.topicThrottle == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err := m.publish(m.topicThrottle, payload)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("unable to publish throttle: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MqttPublisher) PublishSteering(payload []byte) {
|
|
|
|
if m.topicSteering == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err := m.publish(m.topicSteering, payload)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("unable to publish steering: %v", err)
|
|
|
|
}
|
2020-09-20 21:16:06 +00:00
|
|
|
}
|
|
|
|
|
2021-01-15 10:12:21 +00:00
|
|
|
func (m *MqttPublisher) PublishFrame(payload []byte) {
|
|
|
|
if m.topicFrame == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err := m.publish(m.topicFrame, payload)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("unable to publish frame: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MqttPublisher) publish(topic string, payload []byte) error {
|
|
|
|
token := m.client.Publish(topic, 0, false, payload)
|
2020-09-20 21:16:06 +00:00
|
|
|
token.WaitTimeout(10 * time.Millisecond)
|
|
|
|
if err := token.Error(); err != nil {
|
2021-01-15 10:12:21 +00:00
|
|
|
return fmt.Errorf("unable to publish to topic: %v", err)
|
2020-09-20 21:16:06 +00:00
|
|
|
}
|
2021-01-15 10:12:21 +00:00
|
|
|
return nil
|
2020-09-20 21:16:06 +00:00
|
|
|
}
|