robocar-led/pkg/part/part.go

107 lines
2.5 KiB
Go
Raw Normal View History

2019-12-14 10:56:22 +00:00
package part
import (
"fmt"
2019-12-27 15:51:39 +00:00
"github.com/cyrilix/robocar-base/service"
"github.com/cyrilix/robocar-led/pkg/led"
2020-01-01 18:30:34 +00:00
"github.com/cyrilix/robocar-protobuf/go/events"
2019-12-14 10:56:22 +00:00
mqtt "github.com/eclipse/paho.mqtt.golang"
2020-01-01 18:30:34 +00:00
"github.com/golang/protobuf/proto"
2021-10-12 17:00:46 +00:00
"go.uber.org/zap"
2019-12-14 10:56:22 +00:00
"sync"
"time"
)
func NewPart(client mqtt.Client, driveModeTopic, recordTopic string) *LedPart {
return &LedPart{
led: led.New(),
client: client,
onDriveModeTopic: driveModeTopic,
onRecordTopic: recordTopic,
muDriveMode: sync.Mutex{},
2020-01-01 18:30:34 +00:00
m: events.DriveMode_INVALID,
2019-12-14 10:56:22 +00:00
muRecord: sync.Mutex{},
recordEnabled: false,
}
}
type LedPart struct {
2021-12-29 17:16:33 +00:00
led led.ColoredLed
client mqtt.Client
2019-12-14 10:56:22 +00:00
onDriveModeTopic string
onRecordTopic string
muDriveMode sync.Mutex
2020-01-01 18:30:34 +00:00
m events.DriveMode
2019-12-14 10:56:22 +00:00
muRecord sync.Mutex
recordEnabled bool
}
func (p *LedPart) Start() error {
if err := p.registerCallbacks(); err != nil {
return fmt.Errorf("unable to start service: %v", err)
}
for {
time.Sleep(1 * time.Hour)
}
}
func (p *LedPart) Stop() {
2019-12-17 23:28:15 +00:00
defer p.led.SetBlink(0)
2021-12-29 17:16:33 +00:00
defer p.led.SetColor(led.ColorBlack)
2019-12-27 15:51:39 +00:00
service.StopService("led", p.client, p.onDriveModeTopic, p.onRecordTopic)
2019-12-14 10:56:22 +00:00
}
func (p *LedPart) onDriveMode(_ mqtt.Client, message mqtt.Message) {
2020-01-01 18:30:34 +00:00
var driveModeMessage events.DriveModeMessage
err := proto.Unmarshal(message.Payload(), &driveModeMessage)
if err != nil {
2021-10-12 17:00:46 +00:00
zap.S().Errorf("unable to unmarshal %T message: %v", driveModeMessage, err)
2020-01-01 18:30:34 +00:00
return
}
switch driveModeMessage.GetDriveMode() {
case events.DriveMode_USER:
2021-12-29 17:16:33 +00:00
p.led.SetColor(led.ColorGreen)
2020-01-01 18:30:34 +00:00
case events.DriveMode_PILOT:
2021-12-29 17:16:33 +00:00
p.led.SetColor(led.ColorBlue)
2019-12-14 10:56:22 +00:00
}
}
func (p *LedPart) onRecord(client mqtt.Client, message mqtt.Message) {
2020-01-01 18:30:34 +00:00
var switchRecord events.SwitchRecordMessage
err := proto.Unmarshal(message.Payload(), &switchRecord)
2019-12-14 10:56:22 +00:00
if err != nil {
2021-10-12 17:00:46 +00:00
zap.S().Errorf("unable to unmarchal %T message: %v", switchRecord, err)
2019-12-14 10:56:22 +00:00
return
}
2020-02-03 18:31:09 +00:00
p.muRecord.Lock()
defer p.muRecord.Unlock()
if p.recordEnabled == switchRecord.GetEnabled() {
return
}
p.recordEnabled = switchRecord.GetEnabled()
2021-12-29 17:16:33 +00:00
2020-01-01 18:30:34 +00:00
if switchRecord.GetEnabled() {
2021-10-12 17:00:46 +00:00
zap.S().Info("record mode enabled")
2021-12-29 17:31:53 +00:00
p.led.SetBlink(0.5)
2019-12-14 10:56:22 +00:00
} else {
2021-10-12 17:00:46 +00:00
zap.S().Info("record mode disabled")
2019-12-14 10:56:22 +00:00
p.led.SetBlink(0)
}
}
func (p *LedPart) registerCallbacks() error {
2019-12-27 15:51:39 +00:00
err := service.RegisterCallback(p.client, p.onDriveModeTopic, p.onDriveMode)
2019-12-14 10:56:22 +00:00
if err != nil {
return err
}
2019-12-27 15:51:39 +00:00
err = service.RegisterCallback(p.client, p.onRecordTopic, p.onRecord)
2019-12-14 10:56:22 +00:00
if err != nil {
return err
}
return nil
}