robocar-led/pkg/part/part.go

163 lines
3.9 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"
2021-10-12 17:00:46 +00:00
"go.uber.org/zap"
2022-01-03 10:07:28 +00:00
"google.golang.org/protobuf/proto"
2019-12-14 10:56:22 +00:00
"sync"
"time"
)
2023-03-09 18:42:47 +00:00
func NewPart(client mqtt.Client, driveModeTopic, recordTopic, speedZoneTopic string) *LedPart {
2019-12-14 10:56:22 +00:00
return &LedPart{
led: led.New(),
client: client,
onDriveModeTopic: driveModeTopic,
onRecordTopic: recordTopic,
2023-03-09 18:42:47 +00:00
onSpeedZoneTopic: speedZoneTopic,
2019-12-14 10:56:22 +00:00
muDriveMode: sync.Mutex{},
2023-03-09 18:42:47 +00:00
driveMode: events.DriveMode_INVALID,
2019-12-14 10:56:22 +00:00
muRecord: sync.Mutex{},
recordEnabled: false,
2023-03-09 18:42:47 +00:00
muSpeedZone: sync.Mutex{},
speedZone: events.SpeedZone_UNKNOWN,
2019-12-14 10:56:22 +00:00
}
}
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
2023-03-09 18:42:47 +00:00
onSpeedZoneTopic string
2019-12-14 10:56:22 +00:00
muDriveMode sync.Mutex
2023-03-09 18:42:47 +00:00
driveMode events.DriveMode
2019-12-14 10:56:22 +00:00
muRecord sync.Mutex
recordEnabled bool
2023-03-09 18:42:47 +00:00
muSpeedZone sync.Mutex
speedZone events.SpeedZone
2019-12-14 10:56:22 +00:00
}
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)
2023-03-09 18:42:47 +00:00
service.StopService("led", p.client, p.onDriveModeTopic, p.onRecordTopic, p.onSpeedZoneTopic)
}
func (p *LedPart) setDriveMode(m events.DriveMode) {
p.muDriveMode.Lock()
defer p.muDriveMode.Unlock()
p.driveMode = m
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
}
2023-03-09 18:42:47 +00:00
p.setDriveMode(driveModeMessage.GetDriveMode())
p.updateColor()
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")
p.led.SetBlink(2)
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)
}
}
2023-03-09 18:42:47 +00:00
func (p *LedPart) setSpeedZone(sz events.SpeedZone) {
p.muSpeedZone.Lock()
defer p.muSpeedZone.Unlock()
p.speedZone = sz
}
func (p *LedPart) onSpeedZone(_ mqtt.Client, message mqtt.Message) {
var speedZoneMessage events.SpeedZoneMessage
err := proto.Unmarshal(message.Payload(), &speedZoneMessage)
if err != nil {
zap.S().Errorf("unable to unmarshal %T message: %v", speedZoneMessage, err)
return
}
p.setSpeedZone(speedZoneMessage.GetSpeedZone())
p.updateColor()
}
func (p *LedPart) updateColor() {
p.muSpeedZone.Lock()
defer p.muSpeedZone.Unlock()
p.muDriveMode.Lock()
defer p.muDriveMode.Unlock()
switch p.driveMode {
case events.DriveMode_USER:
p.led.SetColor(led.ColorGreen)
case events.DriveMode_PILOT:
switch p.speedZone {
case events.SpeedZone_UNKNOWN:
p.led.SetColor(led.ColorWhite)
case events.SpeedZone_SLOW:
p.led.SetColor(led.ColorRed)
case events.SpeedZone_NORMAL:
p.led.SetColor(led.ColorYellow)
case events.SpeedZone_FAST:
p.led.SetColor(led.ColorBlue)
}
}
}
2019-12-14 10:56:22 +00:00
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
}
2023-03-09 18:42:47 +00:00
err = service.RegisterCallback(p.client, p.onSpeedZoneTopic, p.onSpeedZone)
if err != nil {
return err
}
2019-12-14 10:56:22 +00:00
return nil
}