From 5a386de4ab3a4f2ec0b4491e8787675af3a5ae24 Mon Sep 17 00:00:00 2001 From: Cyrille Nofficial Date: Sat, 20 Aug 2022 21:33:45 +0200 Subject: [PATCH] refactor: rename part to controller --- cmd/rc-steering/rc-steering.go | 4 ++-- pkg/{part/part.go => steering/controller.go} | 20 +++++++++---------- .../controller_test.go} | 6 +++--- 3 files changed, 15 insertions(+), 15 deletions(-) rename pkg/{part/part.go => steering/controller.go} (81%) rename pkg/{part/part_test.go => steering/controller_test.go} (95%) diff --git a/cmd/rc-steering/rc-steering.go b/cmd/rc-steering/rc-steering.go index 55a2d82..3af67ae 100644 --- a/cmd/rc-steering/rc-steering.go +++ b/cmd/rc-steering/rc-steering.go @@ -3,7 +3,7 @@ package main import ( "flag" "github.com/cyrilix/robocar-base/cli" - "github.com/cyrilix/robocar-steering/pkg/part" + "github.com/cyrilix/robocar-steering/pkg/steering" "go.uber.org/zap" "log" "os" @@ -57,7 +57,7 @@ func main() { } defer client.Disconnect(50) - p := part.NewPart(client, steeringTopic, driveModeTopic, rcSteeringTopic, tfSteeringTopic, debug) + p := steering.NewController(client, steeringTopic, driveModeTopic, rcSteeringTopic, tfSteeringTopic, debug) defer p.Stop() cli.HandleExit(p) diff --git a/pkg/part/part.go b/pkg/steering/controller.go similarity index 81% rename from pkg/part/part.go rename to pkg/steering/controller.go index 7f8996d..548920e 100644 --- a/pkg/part/part.go +++ b/pkg/steering/controller.go @@ -1,4 +1,4 @@ -package part +package steering import ( "github.com/cyrilix/robocar-base/service" @@ -9,8 +9,8 @@ import ( "sync" ) -func NewPart(client mqtt.Client, steeringTopic, driveModeTopic, rcSteeringTopic, tfSteeringTopic string, debug bool) *SteeringPart { - return &SteeringPart{ +func NewController(client mqtt.Client, steeringTopic, driveModeTopic, rcSteeringTopic, tfSteeringTopic string, debug bool) *SteeringController { + return &SteeringController{ client: client, steeringTopic: steeringTopic, driveModeTopic: driveModeTopic, @@ -21,7 +21,7 @@ func NewPart(client mqtt.Client, steeringTopic, driveModeTopic, rcSteeringTopic, } -type SteeringPart struct { +type SteeringController struct { client mqtt.Client steeringTopic string @@ -34,7 +34,7 @@ type SteeringPart struct { debug bool } -func (p *SteeringPart) Start() error { +func (p *SteeringController) Start() error { if err := registerCallbacks(p); err != nil { zap.S().Errorf("unable to rgeister callbacks: %v", err) return err @@ -45,12 +45,12 @@ func (p *SteeringPart) Start() error { return nil } -func (p *SteeringPart) Stop() { +func (p *SteeringController) Stop() { close(p.cancel) service.StopService("throttle", p.client, p.driveModeTopic, p.rcSteeringTopic, p.tfSteeringTopic) } -func (p *SteeringPart) onDriveMode(_ mqtt.Client, message mqtt.Message) { +func (p *SteeringController) onDriveMode(_ mqtt.Client, message mqtt.Message) { var msg events.DriveModeMessage err := proto.Unmarshal(message.Payload(), &msg) if err != nil { @@ -63,7 +63,7 @@ func (p *SteeringPart) onDriveMode(_ mqtt.Client, message mqtt.Message) { p.driveMode = msg.GetDriveMode() } -func (p *SteeringPart) onRCSteering(_ mqtt.Client, message mqtt.Message) { +func (p *SteeringController) onRCSteering(_ mqtt.Client, message mqtt.Message) { p.muDriveMode.RLock() defer p.muDriveMode.RUnlock() if p.debug { @@ -81,7 +81,7 @@ func (p *SteeringPart) onRCSteering(_ mqtt.Client, message mqtt.Message) { publish(p.client, p.steeringTopic, &payload) } } -func (p *SteeringPart) onTFSteering(_ mqtt.Client, message mqtt.Message) { +func (p *SteeringController) onTFSteering(_ mqtt.Client, message mqtt.Message) { p.muDriveMode.RLock() defer p.muDriveMode.RUnlock() if p.debug { @@ -100,7 +100,7 @@ func (p *SteeringPart) onTFSteering(_ mqtt.Client, message mqtt.Message) { } } -var registerCallbacks = func(p *SteeringPart) error { +var registerCallbacks = func(p *SteeringController) error { err := service.RegisterCallback(p.client, p.driveModeTopic, p.onDriveMode) if err != nil { return err diff --git a/pkg/part/part_test.go b/pkg/steering/controller_test.go similarity index 95% rename from pkg/part/part_test.go rename to pkg/steering/controller_test.go index 531010f..8f11cca 100644 --- a/pkg/part/part_test.go +++ b/pkg/steering/controller_test.go @@ -1,4 +1,4 @@ -package part +package steering import ( "github.com/cyrilix/robocar-base/testtools" @@ -17,7 +17,7 @@ func TestDefaultSteering(t *testing.T) { registerCallbacks = oldRegister publish = oldPublish }() - registerCallbacks = func(p *SteeringPart) error { + registerCallbacks = func(p *SteeringController) error { return nil } @@ -34,7 +34,7 @@ func TestDefaultSteering(t *testing.T) { rcSteeringTopic := "topic/rcSteering" tfSteeringTopic := "topic/tfSteering" - p := NewPart(nil, steeringTopic, driveModeTopic, rcSteeringTopic, tfSteeringTopic, true) + p := NewController(nil, steeringTopic, driveModeTopic, rcSteeringTopic, tfSteeringTopic, true) cases := []struct { driveMode events.DriveModeMessage