2020-09-20 21:16:06 +00:00
|
|
|
package camera
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"github.com/avast/retry-go"
|
|
|
|
"github.com/cyrilix/robocar-protobuf/go/events"
|
|
|
|
"github.com/cyrilix/robocar-simulator/simulator"
|
|
|
|
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"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func New(publisher Publisher, addressSimulator string) *Gateway {
|
2020-11-29 17:42:33 +00:00
|
|
|
l := log.WithField("simulator", addressSimulator)
|
|
|
|
l.Info("run camera 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Simulator interface to publish camera frames into mqtt topic */
|
|
|
|
type Gateway struct {
|
2020-11-29 17:42:33 +00:00
|
|
|
cancel chan interface{}
|
2020-09-20 21:16:06 +00:00
|
|
|
|
|
|
|
address string
|
|
|
|
conn io.ReadCloser
|
|
|
|
|
|
|
|
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)
|
|
|
|
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) {
|
2020-09-20 21:16:06 +00:00
|
|
|
err := retry.Do(func() error {
|
2020-11-29 17:42:33 +00:00
|
|
|
p.log.Info("connect to simulator")
|
2020-09-20 21:16:06 +00:00
|
|
|
conn, err := connect(p.address)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to connect to simulator at %v", p.address)
|
|
|
|
}
|
|
|
|
p.conn = conn
|
2020-11-29 17:42:33 +00:00
|
|
|
p.log.Info("connection success")
|
2020-09-20 21:16:06 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
retry.Delay(1*time.Second),
|
|
|
|
)
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2020-11-29 17:42:33 +00:00
|
|
|
p.log.Errorf("unable to unmarshal simulator msg: %v", 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{
|
|
|
|
Name: "camera",
|
|
|
|
Id: fmt.Sprintf("%d%03d", now.Unix(), now.Nanosecond()/1000/1000),
|
|
|
|
CreatedAt: ×tamp.Timestamp{
|
|
|
|
Seconds: now.Unix(),
|
|
|
|
Nanos: int32(now.Nanosecond()),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Frame: msgSim.Image,
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2020-11-29 17:42:33 +00:00
|
|
|
p.publisher.Publish(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
|
|
|
|
}
|
|
|
|
|
|
|
|
type Publisher interface {
|
2020-11-29 17:42:33 +00:00
|
|
|
Publish(payload []byte)
|
2020-09-20 21:16:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewMqttPublisher(client mqtt.Client, topic string) Publisher {
|
|
|
|
return &MqttPublisher{
|
|
|
|
client: client,
|
2020-11-29 17:42:33 +00:00
|
|
|
topic: topic,
|
2020-09-20 21:16:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type MqttPublisher struct {
|
|
|
|
client mqtt.Client
|
2020-11-29 17:42:33 +00:00
|
|
|
topic string
|
2020-09-20 21:16:06 +00:00
|
|
|
}
|
|
|
|
|
2020-11-29 17:42:33 +00:00
|
|
|
func (m *MqttPublisher) Publish(payload []byte) {
|
|
|
|
token := m.client.Publish(m.topic, 0, false, payload)
|
2020-09-20 21:16:06 +00:00
|
|
|
token.WaitTimeout(10 * time.Millisecond)
|
|
|
|
if err := token.Error(); err != nil {
|
|
|
|
log.Errorf("unable to publish frame: %v", err)
|
|
|
|
}
|
|
|
|
}
|