refactor: rename part package
This commit is contained in:
parent
517e558081
commit
c724c1170a
@ -3,7 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"github.com/cyrilix/robocar-base/cli"
|
"github.com/cyrilix/robocar-base/cli"
|
||||||
"github.com/cyrilix/robocar-throttle/pkg/part"
|
"github.com/cyrilix/robocar-throttle/pkg/throttle"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
@ -65,7 +65,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
defer client.Disconnect(50)
|
defer client.Disconnect(50)
|
||||||
|
|
||||||
p := part.NewPart(client, throttleTopic, driveModeTopic, rcThrottleTopic, float32(minThrottle), float32(maxThrottle), 2)
|
p := throttle.New(client, throttleTopic, driveModeTopic, rcThrottleTopic, float32(minThrottle), float32(maxThrottle), 2)
|
||||||
defer p.Stop()
|
defer p.Stop()
|
||||||
|
|
||||||
cli.HandleExit(p)
|
cli.HandleExit(p)
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package part
|
package throttle
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/cyrilix/robocar-base/service"
|
"github.com/cyrilix/robocar-base/service"
|
||||||
@ -10,8 +10,8 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewPart(client mqtt.Client, throttleTopic, driveModeTopic, rcThrottleTopic string, minValue, maxValue float32, publishPilotFrequency int) *ThrottlePart {
|
func New(client mqtt.Client, throttleTopic, driveModeTopic, rcThrottleTopic string, minValue, maxValue float32, publishPilotFrequency int) *Controller {
|
||||||
return &ThrottlePart{
|
return &Controller{
|
||||||
client: client,
|
client: client,
|
||||||
throttleTopic: throttleTopic,
|
throttleTopic: throttleTopic,
|
||||||
driveModeTopic: driveModeTopic,
|
driveModeTopic: driveModeTopic,
|
||||||
@ -24,7 +24,7 @@ func NewPart(client mqtt.Client, throttleTopic, driveModeTopic, rcThrottleTopic
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type ThrottlePart struct {
|
type Controller struct {
|
||||||
client mqtt.Client
|
client mqtt.Client
|
||||||
throttleTopic string
|
throttleTopic string
|
||||||
minThrottle, maxThrottle float32
|
minThrottle, maxThrottle float32
|
||||||
@ -37,34 +37,34 @@ type ThrottlePart struct {
|
|||||||
driveModeTopic, rcThrottleTopic string
|
driveModeTopic, rcThrottleTopic string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *ThrottlePart) Start() error {
|
func (c *Controller) Start() error {
|
||||||
if err := registerCallbacks(p); err != nil {
|
if err := registerCallbacks(c); err != nil {
|
||||||
zap.S().Errorf("unable to register callbacks: %v", err)
|
zap.S().Errorf("unable to register callbacks: %v", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
p.cancel = make(chan interface{})
|
c.cancel = make(chan interface{})
|
||||||
ticker := time.NewTicker(1 * time.Second / time.Duration(p.publishPilotFrequency))
|
ticker := time.NewTicker(1 * time.Second / time.Duration(c.publishPilotFrequency))
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
p.onPublishPilotValue()
|
c.onPublishPilotValue()
|
||||||
case <-p.cancel:
|
case <-c.cancel:
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *ThrottlePart) onPublishPilotValue() {
|
func (c *Controller) onPublishPilotValue() {
|
||||||
p.muDriveMode.RLock()
|
c.muDriveMode.RLock()
|
||||||
defer p.muDriveMode.RUnlock()
|
defer c.muDriveMode.RUnlock()
|
||||||
|
|
||||||
if p.driveMode != events.DriveMode_PILOT {
|
if c.driveMode != events.DriveMode_PILOT {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
throttleMsg := events.ThrottleMessage{
|
throttleMsg := events.ThrottleMessage{
|
||||||
Throttle: p.minThrottle,
|
Throttle: c.minThrottle,
|
||||||
Confidence: 1.0,
|
Confidence: 1.0,
|
||||||
}
|
}
|
||||||
payload, err := proto.Marshal(&throttleMsg)
|
payload, err := proto.Marshal(&throttleMsg)
|
||||||
@ -73,15 +73,15 @@ func (p *ThrottlePart) onPublishPilotValue() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
publish(p.client, p.throttleTopic, &payload)
|
publish(c.client, c.throttleTopic, &payload)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *ThrottlePart) Stop() {
|
func (c *Controller) Stop() {
|
||||||
close(p.cancel)
|
close(c.cancel)
|
||||||
service.StopService("throttle", p.client, p.driveModeTopic, p.rcThrottleTopic)
|
service.StopService("throttle", c.client, c.driveModeTopic, c.rcThrottleTopic)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *ThrottlePart) onDriveMode(_ mqtt.Client, message mqtt.Message) {
|
func (c *Controller) onDriveMode(_ mqtt.Client, message mqtt.Message) {
|
||||||
var msg events.DriveModeMessage
|
var msg events.DriveModeMessage
|
||||||
err := proto.Unmarshal(message.Payload(), &msg)
|
err := proto.Unmarshal(message.Payload(), &msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -89,15 +89,15 @@ func (p *ThrottlePart) onDriveMode(_ mqtt.Client, message mqtt.Message) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
p.muDriveMode.Lock()
|
c.muDriveMode.Lock()
|
||||||
defer p.muDriveMode.Unlock()
|
defer c.muDriveMode.Unlock()
|
||||||
p.driveMode = msg.GetDriveMode()
|
c.driveMode = msg.GetDriveMode()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *ThrottlePart) onRCThrottle(_ mqtt.Client, message mqtt.Message) {
|
func (c *Controller) onRCThrottle(_ mqtt.Client, message mqtt.Message) {
|
||||||
p.muDriveMode.RLock()
|
c.muDriveMode.RLock()
|
||||||
defer p.muDriveMode.RUnlock()
|
defer c.muDriveMode.RUnlock()
|
||||||
if p.driveMode == events.DriveMode_USER {
|
if c.driveMode == events.DriveMode_USER {
|
||||||
// Republish same content
|
// Republish same content
|
||||||
payload := message.Payload()
|
payload := message.Payload()
|
||||||
var throttleMsg events.ThrottleMessage
|
var throttleMsg events.ThrottleMessage
|
||||||
@ -107,22 +107,22 @@ func (p *ThrottlePart) onRCThrottle(_ mqtt.Client, message mqtt.Message) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
zap.S().Debugf("publish new throttle value from rc: %v", throttleMsg.GetThrottle())
|
zap.S().Debugf("publish new throttle value from rc: %v", throttleMsg.GetThrottle())
|
||||||
if throttleMsg.GetThrottle() > p.maxThrottle {
|
if throttleMsg.GetThrottle() > c.maxThrottle {
|
||||||
zap.S().Debugf("throttle upper that max value allowed, patch value from %v to %v", throttleMsg.GetThrottle(), p.maxThrottle)
|
zap.S().Debugf("throttle upper that max value allowed, patch value from %v to %v", throttleMsg.GetThrottle(), c.maxThrottle)
|
||||||
throttleMsg.Throttle = p.maxThrottle
|
throttleMsg.Throttle = c.maxThrottle
|
||||||
payloadPatched, err := proto.Marshal(&throttleMsg)
|
payloadPatched, err := proto.Marshal(&throttleMsg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
zap.S().Errorf("unable to marshall throttle msg: %v", err)
|
zap.S().Errorf("unable to marshall throttle msg: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
publish(p.client, p.throttleTopic, &payloadPatched)
|
publish(c.client, c.throttleTopic, &payloadPatched)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
publish(p.client, p.throttleTopic, &payload)
|
publish(c.client, c.throttleTopic, &payload)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var registerCallbacks = func(p *ThrottlePart) error {
|
var registerCallbacks = func(p *Controller) error {
|
||||||
err := service.RegisterCallback(p.client, p.driveModeTopic, p.onDriveMode)
|
err := service.RegisterCallback(p.client, p.driveModeTopic, p.onDriveMode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
@ -1,4 +1,4 @@
|
|||||||
package part
|
package throttle
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/cyrilix/robocar-base/testtools"
|
"github.com/cyrilix/robocar-base/testtools"
|
||||||
@ -17,7 +17,7 @@ func TestDefaultThrottle(t *testing.T) {
|
|||||||
registerCallbacks = oldRegister
|
registerCallbacks = oldRegister
|
||||||
publish = oldPublish
|
publish = oldPublish
|
||||||
}()
|
}()
|
||||||
registerCallbacks = func(p *ThrottlePart) error {
|
registerCallbacks = func(p *Controller) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,7 +35,7 @@ func TestDefaultThrottle(t *testing.T) {
|
|||||||
|
|
||||||
minValue := float32(0.56)
|
minValue := float32(0.56)
|
||||||
|
|
||||||
p := NewPart(nil, throttleTopic, driveModeTopic, rcThrottleTopic, minValue, 1., 200)
|
p := New(nil, throttleTopic, driveModeTopic, rcThrottleTopic, minValue, 1., 200)
|
||||||
|
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
name string
|
name string
|
Loading…
x
Reference in New Issue
Block a user