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"
|
2021-10-12 17:05:34 +00:00
|
|
|
"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-05-10 17:52:44 +00:00
|
|
|
const (
|
|
|
|
LedModeBrake LedMode = iota
|
|
|
|
LedModeSpeedZone
|
|
|
|
)
|
|
|
|
|
|
|
|
type LedMode int
|
|
|
|
|
|
|
|
func NewPart(client mqtt.Client, driveModeTopic, recordTopic, speedZoneTopic, throttleTopic string, ledMode LedMode) *LedPart {
|
2019-12-14 10:56:22 +00:00
|
|
|
return &LedPart{
|
|
|
|
led: led.New(),
|
2023-05-10 17:52:44 +00:00
|
|
|
mode: ledMode,
|
2019-12-14 10:56:22 +00:00
|
|
|
client: client,
|
|
|
|
onDriveModeTopic: driveModeTopic,
|
|
|
|
onRecordTopic: recordTopic,
|
2023-03-09 18:42:47 +00:00
|
|
|
onSpeedZoneTopic: speedZoneTopic,
|
2023-05-10 17:52:44 +00:00
|
|
|
onThrottleTopic: throttleTopic,
|
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,
|
2023-05-10 17:52:44 +00:00
|
|
|
muThrottle: sync.Mutex{},
|
2019-12-14 10:56:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
type LedPart struct {
|
2021-12-29 17:16:33 +00:00
|
|
|
led led.ColoredLed
|
2023-05-10 17:52:44 +00:00
|
|
|
mode LedMode
|
2021-12-29 17:16:33 +00:00
|
|
|
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
|
2023-05-10 17:52:44 +00:00
|
|
|
onThrottleTopic 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
|
2023-05-10 17:52:44 +00:00
|
|
|
|
|
|
|
muThrottle sync.Mutex
|
|
|
|
throttle float32
|
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-05-10 17:52:44 +00:00
|
|
|
service.StopService("led", p.client, p.onDriveModeTopic, p.onRecordTopic, p.onSpeedZoneTopic, p.onThrottleTopic)
|
2023-03-09 18:42:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
2021-12-29 18:29:32 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2023-05-10 17:52:44 +00:00
|
|
|
func (p *LedPart) setThrottle(throttle float32) {
|
|
|
|
p.muThrottle.Lock()
|
|
|
|
defer p.muThrottle.Unlock()
|
|
|
|
p.throttle = throttle
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *LedPart) onThrottle(_ mqtt.Client, message mqtt.Message) {
|
|
|
|
var throttleMessage events.ThrottleMessage
|
|
|
|
err := proto.Unmarshal(message.Payload(), &throttleMessage)
|
|
|
|
if err != nil {
|
|
|
|
zap.S().Errorf("unable to unmarshal %T message: %v", throttleMessage, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p.setThrottle(throttleMessage.GetThrottle())
|
|
|
|
p.updateColor()
|
|
|
|
}
|
|
|
|
|
2023-03-09 18:42:47 +00:00
|
|
|
func (p *LedPart) updateColor() {
|
|
|
|
p.muSpeedZone.Lock()
|
|
|
|
defer p.muSpeedZone.Unlock()
|
|
|
|
p.muDriveMode.Lock()
|
|
|
|
defer p.muDriveMode.Unlock()
|
2023-05-10 17:52:44 +00:00
|
|
|
p.muThrottle.Lock()
|
|
|
|
defer p.muThrottle.Unlock()
|
|
|
|
|
|
|
|
if p.throttle <= -0.05 {
|
|
|
|
p.led.SetColor(led.Color{Red: int(p.throttle * -255)})
|
|
|
|
return
|
|
|
|
}
|
2023-03-09 18:42:47 +00:00
|
|
|
|
2023-05-10 17:52:44 +00:00
|
|
|
switch p.mode {
|
|
|
|
case LedModeBrake:
|
|
|
|
p.updateBrakeColor()
|
|
|
|
case LedModeSpeedZone:
|
|
|
|
p.updateSpeedZoneColor()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *LedPart) updateSpeedZoneColor() {
|
2023-03-09 18:42:47 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-10 17:52:44 +00:00
|
|
|
func (p *LedPart) updateBrakeColor() {
|
|
|
|
|
|
|
|
switch p.driveMode {
|
|
|
|
case events.DriveMode_USER:
|
|
|
|
p.led.SetColor(led.ColorGreen)
|
|
|
|
case events.DriveMode_PILOT:
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-05-10 17:52:44 +00:00
|
|
|
err = service.RegisterCallback(p.client, p.onThrottleTopic, p.onThrottle)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-14 10:56:22 +00:00
|
|
|
return nil
|
|
|
|
}
|