2020-01-26 18:57:32 +00:00
|
|
|
package part
|
|
|
|
|
|
|
|
import (
|
2020-02-02 22:42:43 +00:00
|
|
|
"fmt"
|
2020-01-26 18:57:32 +00:00
|
|
|
"github.com/cyrilix/robocar-base/service"
|
|
|
|
"github.com/cyrilix/robocar-protobuf/go/events"
|
|
|
|
mqtt "github.com/eclipse/paho.mqtt.golang"
|
2021-10-12 20:08:07 +00:00
|
|
|
"go.uber.org/zap"
|
2022-01-03 16:01:34 +00:00
|
|
|
"google.golang.org/protobuf/proto"
|
2020-01-26 18:57:32 +00:00
|
|
|
"sync"
|
2020-02-02 22:42:43 +00:00
|
|
|
"time"
|
2020-01-26 18:57:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func NewRecorder(client mqtt.Client, recordTopic, cameraTopic, steeringTopic, switchRecordTopic string) *Recorder {
|
|
|
|
return &Recorder{
|
2020-02-02 22:42:43 +00:00
|
|
|
client: client,
|
|
|
|
recordTopic: recordTopic,
|
|
|
|
cameraTopic: cameraTopic,
|
|
|
|
steeringTopic: steeringTopic,
|
2020-01-26 18:57:32 +00:00
|
|
|
switchRecordTopic: switchRecordTopic,
|
2020-02-02 22:42:43 +00:00
|
|
|
enabled: false,
|
|
|
|
idGenerator: NewDateBasedGenerator(),
|
|
|
|
recordSet: "",
|
|
|
|
cancel: make(chan interface{}),
|
2020-01-26 18:57:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Recorder struct {
|
2020-02-02 22:42:43 +00:00
|
|
|
client mqtt.Client
|
|
|
|
recordTopic string
|
2020-01-26 18:57:32 +00:00
|
|
|
cameraTopic, steeringTopic, switchRecordTopic string
|
|
|
|
|
|
|
|
muSteeringMsg sync.Mutex
|
|
|
|
currentSteering *events.SteeringMessage
|
|
|
|
|
|
|
|
muEnabled sync.RWMutex
|
|
|
|
enabled bool
|
|
|
|
|
2020-02-02 22:42:43 +00:00
|
|
|
idGenerator IdGenerator
|
|
|
|
recordSet string
|
|
|
|
|
2020-01-26 18:57:32 +00:00
|
|
|
cancel chan interface {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Recorder) Start() error {
|
|
|
|
registerCallBacks(r)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-r.cancel:
|
2021-10-12 20:08:07 +00:00
|
|
|
zap.S().Info("Stop service")
|
2020-01-26 18:57:32 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Recorder) Stop() {
|
|
|
|
close(r.cancel)
|
|
|
|
service.StopService("record", r.client, r.cameraTopic, r.steeringTopic)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Recorder) onSwitchRecord(_ mqtt.Client, message mqtt.Message) {
|
|
|
|
var msg events.SwitchRecordMessage
|
|
|
|
err := proto.Unmarshal(message.Payload(), &msg)
|
|
|
|
if err != nil {
|
2021-10-12 20:08:07 +00:00
|
|
|
zap.S().Errorf("unable to unmarshal protobuf %T: %v", msg, err)
|
2020-01-26 18:57:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
r.muEnabled.Lock()
|
|
|
|
defer r.muEnabled.Unlock()
|
2020-02-02 22:42:43 +00:00
|
|
|
|
2021-02-22 17:35:15 +00:00
|
|
|
if !r.enabled && msg.GetEnabled() {
|
2020-02-02 22:42:43 +00:00
|
|
|
r.recordSet = r.idGenerator.Next()
|
|
|
|
}
|
|
|
|
|
2020-01-26 18:57:32 +00:00
|
|
|
r.enabled = msg.GetEnabled()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Recorder) onSteering(_ mqtt.Client, message mqtt.Message) {
|
|
|
|
var msg events.SteeringMessage
|
|
|
|
err := proto.Unmarshal(message.Payload(), &msg)
|
|
|
|
if err != nil {
|
2021-10-12 20:08:07 +00:00
|
|
|
zap.S().Errorf("unable to unmarshal protobuf %T: %v", msg, err)
|
2020-01-26 18:57:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
r.muSteeringMsg.Lock()
|
|
|
|
defer r.muSteeringMsg.Unlock()
|
|
|
|
r.currentSteering = &msg
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Recorder) onFrame(_ mqtt.Client, message mqtt.Message) {
|
|
|
|
if !r.Enabled() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var msg events.FrameMessage
|
|
|
|
err := proto.Unmarshal(message.Payload(), &msg)
|
|
|
|
if err != nil {
|
2021-10-12 20:08:07 +00:00
|
|
|
zap.S().Errorf("unable to unmarshal protobuf FrameMessage: %v", err)
|
2020-01-26 18:57:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
steering := r.CurrentSteering()
|
|
|
|
if steering == nil {
|
2021-10-12 20:08:07 +00:00
|
|
|
zap.S().Warnf("no current steeringMsg, skip frameMsg %v", msg.GetId().Id)
|
2020-01-26 18:57:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
record := events.RecordMessage{
|
2021-02-22 17:35:15 +00:00
|
|
|
Frame: &msg,
|
|
|
|
Steering: steering,
|
|
|
|
RecordSet: r.recordSet,
|
2020-01-26 18:57:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
payload, err := proto.Marshal(&record)
|
|
|
|
if err != nil {
|
2021-10-12 20:08:07 +00:00
|
|
|
zap.S().Errorf("unable to marshal message %v: %v", record, err)
|
2020-01-26 18:57:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
publish(r.client, r.recordTopic, &payload)
|
|
|
|
}
|
|
|
|
|
|
|
|
var publish = func(client mqtt.Client, topic string, payload *[]byte) {
|
|
|
|
client.Publish(topic, 0, false, *payload)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Recorder) CurrentSteering() *events.SteeringMessage {
|
|
|
|
r.muSteeringMsg.Lock()
|
|
|
|
defer r.muSteeringMsg.Unlock()
|
|
|
|
steering := r.currentSteering
|
|
|
|
return steering
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Recorder) Enabled() bool {
|
|
|
|
r.muEnabled.RLock()
|
|
|
|
defer r.muEnabled.RUnlock()
|
|
|
|
return r.enabled
|
|
|
|
}
|
|
|
|
|
|
|
|
var registerCallBacks = func(r *Recorder) {
|
|
|
|
err := service.RegisterCallback(r.client, r.cameraTopic, r.onFrame)
|
|
|
|
if err != nil {
|
2022-01-02 15:49:04 +00:00
|
|
|
zap.S().Panicf("unable to register callback to %v:%v", r.cameraTopic, err)
|
2020-01-26 18:57:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = service.RegisterCallback(r.client, r.steeringTopic, r.onSteering)
|
|
|
|
if err != nil {
|
2022-01-02 15:49:04 +00:00
|
|
|
zap.S().Panicf("unable to register callback to %v:%v", r.steeringTopic, err)
|
2020-01-26 18:57:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = service.RegisterCallback(r.client, r.switchRecordTopic, r.onSwitchRecord)
|
|
|
|
if err != nil {
|
2022-01-02 15:49:04 +00:00
|
|
|
zap.S().Panicf("unable to register callback to %v:%v", r.switchRecordTopic, err)
|
2020-01-26 18:57:32 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-02 22:42:43 +00:00
|
|
|
|
|
|
|
type IdGenerator interface {
|
|
|
|
Next() string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDateBasedGenerator() *DateBasedGenerator {
|
|
|
|
return &DateBasedGenerator{
|
|
|
|
muCpt: sync.Mutex{},
|
|
|
|
cpt: 0,
|
2022-01-02 15:32:00 +00:00
|
|
|
idTemplate: "%s_%d",
|
|
|
|
start: time.Now().Format("2006-01-02T15-04"),
|
2020-02-02 22:42:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type DateBasedGenerator struct {
|
|
|
|
muCpt sync.Mutex
|
|
|
|
cpt int
|
|
|
|
idTemplate string
|
|
|
|
start string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DateBasedGenerator) Next() string {
|
|
|
|
d.muCpt.Lock()
|
|
|
|
defer d.muCpt.Unlock()
|
|
|
|
d.cpt += 1
|
|
|
|
return fmt.Sprintf(d.idTemplate, d.start, d.cpt)
|
|
|
|
}
|