2020-01-28 22:18:35 +00:00
|
|
|
package record
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"github.com/cyrilix/robocar-base/service"
|
|
|
|
"github.com/cyrilix/robocar-protobuf/go/events"
|
|
|
|
mqtt "github.com/eclipse/paho.mqtt.golang"
|
|
|
|
"github.com/golang/protobuf/proto"
|
2022-06-09 10:15:09 +00:00
|
|
|
"go.uber.org/zap"
|
2020-01-28 22:18:35 +00:00
|
|
|
"io/ioutil"
|
2020-02-02 22:44:23 +00:00
|
|
|
"os"
|
2020-01-28 22:18:35 +00:00
|
|
|
)
|
|
|
|
|
2020-02-16 18:14:38 +00:00
|
|
|
func New(client mqtt.Client, recordsDir, recordTopic string) (*Recorder, error) {
|
|
|
|
err := os.MkdirAll(recordsDir, os.FileMode(0755))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to create %v directory: %v", recordsDir, err)
|
|
|
|
}
|
2020-01-28 22:18:35 +00:00
|
|
|
return &Recorder{
|
|
|
|
client: client,
|
2020-02-16 18:14:38 +00:00
|
|
|
recordsDir: recordsDir,
|
2020-01-28 22:18:35 +00:00
|
|
|
recordTopic: recordTopic,
|
|
|
|
cancel: make(chan interface{}),
|
2020-02-16 18:14:38 +00:00
|
|
|
}, nil
|
2020-01-28 22:18:35 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
type Recorder struct {
|
2020-02-16 18:14:38 +00:00
|
|
|
client mqtt.Client
|
|
|
|
recordsDir string
|
|
|
|
recordTopic string
|
|
|
|
cancel chan interface{}
|
2020-01-28 22:18:35 +00:00
|
|
|
}
|
|
|
|
|
2020-02-16 18:14:38 +00:00
|
|
|
var RecorNameFormat = "record_%s.json"
|
|
|
|
|
2020-01-28 22:18:35 +00:00
|
|
|
func (r *Recorder) Start() error {
|
|
|
|
err := service.RegisterCallback(r.client, r.recordTopic, r.onRecordMsg)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to start recorder part: %v", err)
|
|
|
|
}
|
|
|
|
<-r.cancel
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Recorder) Stop() {
|
|
|
|
service.StopService("record", r.client, r.recordTopic)
|
|
|
|
close(r.cancel)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Recorder) onRecordMsg(_ mqtt.Client, message mqtt.Message) {
|
2022-06-09 10:15:09 +00:00
|
|
|
l := zap.S()
|
2020-01-28 22:18:35 +00:00
|
|
|
var msg events.RecordMessage
|
|
|
|
err := proto.Unmarshal(message.Payload(), &msg)
|
|
|
|
if err != nil {
|
2022-06-09 10:15:09 +00:00
|
|
|
zap.S().Errorf("unable to unmarshal protobuf %T: %v", msg, err)
|
2020-01-28 22:18:35 +00:00
|
|
|
return
|
|
|
|
}
|
2020-02-16 18:14:38 +00:00
|
|
|
fmt.Printf("record %s: %s\r", msg.GetRecordSet(), msg.GetFrame().GetId().GetId())
|
|
|
|
|
|
|
|
recordDir := fmt.Sprintf("%s/%s", r.recordsDir, msg.GetRecordSet())
|
2020-01-28 22:18:35 +00:00
|
|
|
|
2020-02-16 18:14:38 +00:00
|
|
|
imgDir := fmt.Sprintf("%s/cam", recordDir)
|
|
|
|
imgName := fmt.Sprintf("%s/cam-image_array_%s.jpg", imgDir, msg.GetFrame().GetId().GetId())
|
|
|
|
err = os.MkdirAll(imgDir, os.FileMode(0755))
|
|
|
|
if err != nil {
|
2022-06-09 10:15:09 +00:00
|
|
|
l.Errorf("unable to create %v directory: %v", imgDir, err)
|
2020-02-16 18:14:38 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
err = ioutil.WriteFile(imgName, msg.GetFrame().GetFrame(), os.FileMode(0755))
|
2020-01-28 22:18:35 +00:00
|
|
|
if err != nil {
|
2022-06-09 10:15:09 +00:00
|
|
|
l.Errorf("unable to write img file %v: %v", imgName, err)
|
2020-01-28 22:18:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-16 18:14:38 +00:00
|
|
|
jsonDir := fmt.Sprintf("%s/", recordDir)
|
|
|
|
recordName := fmt.Sprintf("%s/%s", jsonDir, fmt.Sprintf(RecorNameFormat, msg.GetFrame().GetId().GetId()))
|
|
|
|
err = os.MkdirAll(jsonDir, os.FileMode(0755))
|
|
|
|
if err != nil {
|
2022-06-09 10:15:09 +00:00
|
|
|
l.Errorf("unable to create %v directory: %v", jsonDir, err)
|
2020-02-16 18:14:38 +00:00
|
|
|
return
|
|
|
|
}
|
2020-01-28 22:18:35 +00:00
|
|
|
record := Record{
|
|
|
|
UserAngle: msg.GetSteering().GetSteering(),
|
|
|
|
CamImageArray: imgName,
|
|
|
|
}
|
|
|
|
jsonBytes, err := json.Marshal(&record)
|
|
|
|
if err != nil {
|
2022-06-09 10:15:09 +00:00
|
|
|
l.Errorf("unable to marshal json content: %v", err)
|
2020-01-28 22:18:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
err = ioutil.WriteFile(recordName, jsonBytes, 0755)
|
|
|
|
if err != nil {
|
2022-06-09 10:15:09 +00:00
|
|
|
l.Errorf("unable to write json file %v: %v", recordName, err)
|
2020-01-28 22:18:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
type Record struct {
|
|
|
|
UserAngle float32 `json:"user/angle,"`
|
|
|
|
CamImageArray string `json:"cam/image_array,"`
|
|
|
|
}
|