robocar-led/part/part.go

103 lines
2.4 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"
2019-12-14 10:56:22 +00:00
"github.com/cyrilix/robocar-led/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"
log "github.com/sirupsen/logrus"
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 {
led led.ColoredLed
client mqtt.Client
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)
defer p.led.SetGreen(0)
defer p.led.SetBlue(0)
defer p.led.SetRed(0)
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 {
log.Errorf("unable to unmarchal %T message: %v", driveModeMessage, err)
return
}
switch driveModeMessage.GetDriveMode() {
case events.DriveMode_USER:
2019-12-14 10:56:22 +00:00
p.led.SetRed(0)
p.led.SetGreen(255)
p.led.SetBlue(0)
2020-01-01 18:30:34 +00:00
case events.DriveMode_PILOT:
2019-12-14 10:56:22 +00:00
p.led.SetRed(0)
p.led.SetGreen(0)
p.led.SetBlue(255)
}
}
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 {
2020-01-01 18:30:34 +00:00
log.Errorf("unable to unmarchal %T message: %v", switchRecord, err)
2019-12-14 10:56:22 +00:00
return
}
2020-01-01 18:30:34 +00:00
if switchRecord.GetEnabled() {
2019-12-14 10:56:22 +00:00
p.led.SetBlink(2)
} else {
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
}