2021-01-21 22:39:52 +00:00
|
|
|
package events
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/cyrilix/robocar-simulator/pkg/gateway"
|
|
|
|
mqtt "github.com/eclipse/paho.mqtt.golang"
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2021-10-12 20:33:13 +00:00
|
|
|
"go.uber.org/zap"
|
2021-01-21 22:39:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func NewMsgPublisher(srcEvents gateway.SimulatorSource, p Publisher, topicFrame, topicSteering, topicThrottle string) *MsgPublisher {
|
|
|
|
return &MsgPublisher{
|
|
|
|
p: p,
|
|
|
|
topicFrame: topicFrame,
|
|
|
|
topicSteering: topicSteering,
|
|
|
|
topicThrottle: topicThrottle,
|
|
|
|
srcEvents: srcEvents,
|
|
|
|
muCancel: sync.Mutex{},
|
|
|
|
cancel: nil,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type MsgPublisher struct {
|
|
|
|
p Publisher
|
|
|
|
topicFrame string
|
|
|
|
topicSteering string
|
|
|
|
topicThrottle string
|
|
|
|
|
|
|
|
srcEvents gateway.SimulatorSource
|
|
|
|
|
|
|
|
muCancel sync.Mutex
|
|
|
|
cancel chan interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MsgPublisher) Start() {
|
|
|
|
m.muCancel.Lock()
|
|
|
|
defer m.muCancel.Unlock()
|
|
|
|
|
|
|
|
m.cancel = make(chan interface{})
|
|
|
|
|
|
|
|
if m.topicThrottle != "" {
|
|
|
|
go m.listenThrottle()
|
|
|
|
}
|
|
|
|
if m.topicSteering != "" {
|
|
|
|
go m.listenSteering()
|
|
|
|
}
|
|
|
|
if m.topicFrame != "" {
|
|
|
|
go m.listenFrame()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MsgPublisher) Stop() {
|
|
|
|
m.muCancel.Lock()
|
|
|
|
defer m.muCancel.Unlock()
|
|
|
|
close(m.cancel)
|
|
|
|
m.cancel = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MsgPublisher) listenThrottle() {
|
2021-10-12 20:33:13 +00:00
|
|
|
logr := zap.S().With("msg_type", "throttleChan")
|
2021-01-21 22:39:52 +00:00
|
|
|
msgChan := m.srcEvents.SubscribeThrottle()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-m.cancel:
|
|
|
|
logr.Debug("exit listen throttleChan loop")
|
|
|
|
return
|
|
|
|
case msg := <-msgChan:
|
|
|
|
|
|
|
|
payload, err := proto.Marshal(msg)
|
|
|
|
if err != nil {
|
|
|
|
logr.Errorf("unable to marshal protobuf message: %v", err)
|
|
|
|
} else {
|
|
|
|
|
|
|
|
err = m.p.Publish(m.topicThrottle, payload)
|
|
|
|
if err != nil {
|
|
|
|
logr.Errorf("unable to publish events message: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MsgPublisher) listenSteering() {
|
2021-10-12 20:33:13 +00:00
|
|
|
logr := zap.S().With("msg_type", "steeringChan")
|
2021-01-21 22:39:52 +00:00
|
|
|
msgChan := m.srcEvents.SubscribeSteering()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
|
|
|
|
case msg := <-msgChan:
|
|
|
|
if m.topicSteering == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
payload, err := proto.Marshal(msg)
|
|
|
|
if err != nil {
|
|
|
|
logr.Errorf("unable to marshal protobuf message: %v", err)
|
|
|
|
} else {
|
|
|
|
err = m.p.Publish(m.topicSteering, payload)
|
|
|
|
if err != nil {
|
|
|
|
logr.Errorf("unable to publish events message: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MsgPublisher) listenFrame() {
|
2021-10-12 20:33:13 +00:00
|
|
|
logr := zap.S().With("msg_type", "frame")
|
2021-01-21 22:39:52 +00:00
|
|
|
msgChan := m.srcEvents.SubscribeFrame()
|
|
|
|
for {
|
|
|
|
msg := <-msgChan
|
2021-02-16 16:46:10 +00:00
|
|
|
if msg == nil {
|
|
|
|
// channel closed
|
|
|
|
break
|
|
|
|
}
|
2021-02-01 16:04:42 +00:00
|
|
|
logr.Debugf("new frame %v", msg.Id)
|
2021-01-21 22:39:52 +00:00
|
|
|
if m.topicFrame == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
payload, err := proto.Marshal(msg)
|
|
|
|
if err != nil {
|
|
|
|
logr.Errorf("unable to marshal protobuf message: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
err = m.p.Publish(m.topicFrame, payload)
|
|
|
|
if err != nil {
|
|
|
|
logr.Errorf("unable to publish events message: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Publisher interface {
|
|
|
|
Publish(topic string, payload []byte) error
|
|
|
|
}
|
|
|
|
|
2021-01-23 11:30:59 +00:00
|
|
|
func NewMqttPublisher(client mqtt.Client) *MqttPublisher {
|
|
|
|
return &MqttPublisher{client: client}
|
|
|
|
}
|
|
|
|
|
2021-01-21 22:39:52 +00:00
|
|
|
type MqttPublisher struct {
|
|
|
|
client mqtt.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MqttPublisher) Publish(topic string, payload []byte) error {
|
|
|
|
token := m.client.Publish(topic, 0, false, payload)
|
|
|
|
token.WaitTimeout(10 * time.Millisecond)
|
|
|
|
if err := token.Error(); err != nil {
|
|
|
|
return fmt.Errorf("unable to events to topic: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|