diff --git a/components/arm/arm.go b/components/arm/arm.go index b8e3f35229c..38008b3ac12 100644 --- a/components/arm/arm.go +++ b/components/arm/arm.go @@ -190,9 +190,14 @@ func CreateStatus(ctx context.Context, resource interface{}) (*pb.Status, error) type reconfigurableArm struct { mu sync.RWMutex + name resource.Name actual Arm } +func (r *reconfigurableArm) Name() resource.Name { + return r.name +} + func (r *reconfigurableArm) DoCommand(ctx context.Context, cmd map[string]interface{}) (map[string]interface{}, error) { r.mu.RLock() defer r.mu.RUnlock() @@ -321,7 +326,7 @@ func (r *reconfigurableLocalArm) Reconfigure(ctx context.Context, newArm resourc // WrapWithReconfigurable converts a regular Arm implementation to a reconfigurableArm // and a localArm into a reconfigurableLocalArm // If arm is already a Reconfigurable, then nothing is done. -func WrapWithReconfigurable(r interface{}) (resource.Reconfigurable, error) { +func WrapWithReconfigurable(r interface{}, name resource.Name) (resource.Reconfigurable, error) { arm, ok := r.(Arm) if !ok { return nil, NewUnimplementedInterfaceError(r) @@ -331,7 +336,7 @@ func WrapWithReconfigurable(r interface{}) (resource.Reconfigurable, error) { return reconfigurable, nil } - rArm := &reconfigurableArm{actual: arm} + rArm := &reconfigurableArm{name: name, actual: arm} localArm, ok := r.(LocalArm) if !ok { // is an arm but is not a local arm diff --git a/components/arm/arm_test.go b/components/arm/arm_test.go index 21722069c99..69172a6d00a 100644 --- a/components/arm/arm_test.go +++ b/components/arm/arm_test.go @@ -248,21 +248,21 @@ func TestArmName(t *testing.T) { func TestWrapWithReconfigurable(t *testing.T) { var actualArm1 arm.Arm = &mock{Name: testArmName} - reconfArm1, err := arm.WrapWithReconfigurable(actualArm1) + reconfArm1, err := arm.WrapWithReconfigurable(actualArm1, resource.Name{}) test.That(t, err, test.ShouldBeNil) - _, err = arm.WrapWithReconfigurable(nil) + _, err = arm.WrapWithReconfigurable(nil, resource.Name{}) test.That(t, err, test.ShouldBeError, arm.NewUnimplementedInterfaceError(nil)) - reconfArm2, err := arm.WrapWithReconfigurable(reconfArm1) + reconfArm2, err := arm.WrapWithReconfigurable(reconfArm1, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfArm2, test.ShouldEqual, reconfArm1) var actualArm2 arm.LocalArm = &mockLocal{Name: testArmName} - reconfArm3, err := arm.WrapWithReconfigurable(actualArm2) + reconfArm3, err := arm.WrapWithReconfigurable(actualArm2, resource.Name{}) test.That(t, err, test.ShouldBeNil) - reconfArm4, err := arm.WrapWithReconfigurable(reconfArm3) + reconfArm4, err := arm.WrapWithReconfigurable(reconfArm3, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfArm4, test.ShouldResemble, reconfArm3) @@ -272,12 +272,12 @@ func TestWrapWithReconfigurable(t *testing.T) { func TestReconfigurableArm(t *testing.T) { actualArm1 := &mockLocal{Name: testArmName} - reconfArm1, err := arm.WrapWithReconfigurable(actualArm1) + reconfArm1, err := arm.WrapWithReconfigurable(actualArm1, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfArm1, test.ShouldNotBeNil) actualArm2 := &mockLocal{Name: testArmName2} - reconfArm2, err := arm.WrapWithReconfigurable(actualArm2) + reconfArm2, err := arm.WrapWithReconfigurable(actualArm2, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfArm2, test.ShouldNotBeNil) test.That(t, actualArm1.reconfCount, test.ShouldEqual, 0) @@ -300,7 +300,7 @@ func TestReconfigurableArm(t *testing.T) { test.That(t, err, test.ShouldBeError, rutils.NewUnexpectedTypeError(reconfArm1, nil)) actualArm3 := &mock{Name: failArmName} - reconfArm3, err := arm.WrapWithReconfigurable(actualArm3) + reconfArm3, err := arm.WrapWithReconfigurable(actualArm3, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfArm3, test.ShouldNotBeNil) @@ -314,7 +314,7 @@ func TestReconfigurableArm(t *testing.T) { test.That(t, err, test.ShouldBeError, rutils.NewUnexpectedTypeError(reconfArm3, reconfArm1)) actualArm4 := &mock{Name: testArmName2} - reconfArm4, err := arm.WrapWithReconfigurable(actualArm4) + reconfArm4, err := arm.WrapWithReconfigurable(actualArm4, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfArm4, test.ShouldNotBeNil) @@ -325,7 +325,7 @@ func TestReconfigurableArm(t *testing.T) { func TestStop(t *testing.T) { actualArm1 := &mockLocal{Name: testArmName} - reconfArm1, err := arm.WrapWithReconfigurable(actualArm1) + reconfArm1, err := arm.WrapWithReconfigurable(actualArm1, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, actualArm1.stopCount, test.ShouldEqual, 0) @@ -335,7 +335,7 @@ func TestStop(t *testing.T) { func TestClose(t *testing.T) { actualArm1 := &mockLocal{Name: testArmName} - reconfArm1, err := arm.WrapWithReconfigurable(actualArm1) + reconfArm1, err := arm.WrapWithReconfigurable(actualArm1, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, actualArm1.reconfCount, test.ShouldEqual, 0) @@ -345,7 +345,7 @@ func TestClose(t *testing.T) { func TestExtraOptions(t *testing.T) { actualArm1 := &mockLocal{Name: testArmName} - reconfArm1, err := arm.WrapWithReconfigurable(actualArm1) + reconfArm1, err := arm.WrapWithReconfigurable(actualArm1, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, actualArm1.extra, test.ShouldEqual, nil) diff --git a/components/audioinput/audio_input.go b/components/audioinput/audio_input.go index 2210bab61b5..16be2f2854d 100644 --- a/components/audioinput/audio_input.go +++ b/components/audioinput/audio_input.go @@ -76,7 +76,7 @@ func DependencyTypeError(name, actual interface{}) error { } // WrapWithReconfigurable wraps an audio input with a reconfigurable and locking interface. -func WrapWithReconfigurable(r interface{}) (resource.Reconfigurable, error) { +func WrapWithReconfigurable(r interface{}, name resource.Name) (resource.Reconfigurable, error) { i, ok := r.(AudioInput) if !ok { return nil, NewUnimplementedInterfaceError(r) @@ -86,6 +86,7 @@ func WrapWithReconfigurable(r interface{}) (resource.Reconfigurable, error) { } cancelCtx, cancel := context.WithCancel(context.Background()) return &reconfigurableAudioInput{ + name: name, actual: i, cancelCtx: cancelCtx, cancel: cancel, @@ -194,11 +195,16 @@ func (as *audioSource) Close(ctx context.Context) error { type reconfigurableAudioInput struct { mu sync.RWMutex + name resource.Name actual AudioInput cancelCtx context.Context cancel func() } +func (i *reconfigurableAudioInput) Name() resource.Name { + return i.name +} + func (i *reconfigurableAudioInput) ProxyFor() interface{} { i.mu.RLock() defer i.mu.RUnlock() diff --git a/components/audioinput/audioinput_test.go b/components/audioinput/audioinput_test.go index f7d90544d5a..faadc5ac974 100644 --- a/components/audioinput/audioinput_test.go +++ b/components/audioinput/audioinput_test.go @@ -156,24 +156,24 @@ func TestAudioInputName(t *testing.T) { func TestWrapWithReconfigurable(t *testing.T) { var actualaudioInput1 audioinput.AudioInput = &mock{Name: testAudioInputName} - reconfaudioInput1, err := audioinput.WrapWithReconfigurable(actualaudioInput1) + reconfaudioInput1, err := audioinput.WrapWithReconfigurable(actualaudioInput1, resource.Name{}) test.That(t, err, test.ShouldBeNil) - _, err = audioinput.WrapWithReconfigurable(nil) + _, err = audioinput.WrapWithReconfigurable(nil, resource.Name{}) test.That(t, err, test.ShouldBeError, audioinput.NewUnimplementedInterfaceError(nil)) - reconfAudioInput2, err := audioinput.WrapWithReconfigurable(reconfaudioInput1) + reconfAudioInput2, err := audioinput.WrapWithReconfigurable(reconfaudioInput1, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfAudioInput2, test.ShouldEqual, reconfaudioInput1) } func TestReconfigurableAudioInput(t *testing.T) { actualaudioInput1 := &mock{Name: testAudioInputName} - reconfaudioInput1, err := audioinput.WrapWithReconfigurable(actualaudioInput1) + reconfaudioInput1, err := audioinput.WrapWithReconfigurable(actualaudioInput1, resource.Name{}) test.That(t, err, test.ShouldBeNil) actualAudioInput2 := &mock{Name: testAudioInputName2} - reconfAudioInput2, err := audioinput.WrapWithReconfigurable(actualAudioInput2) + reconfAudioInput2, err := audioinput.WrapWithReconfigurable(actualAudioInput2, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, actualaudioInput1.reconfCount, test.ShouldEqual, 0) @@ -210,7 +210,7 @@ func TestReconfigurableAudioInput(t *testing.T) { func TestClose(t *testing.T) { actualaudioInput1 := &mock{Name: testAudioInputName} - reconfaudioInput1, err := audioinput.WrapWithReconfigurable(actualaudioInput1) + reconfaudioInput1, err := audioinput.WrapWithReconfigurable(actualaudioInput1, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, actualaudioInput1.reconfCount, test.ShouldEqual, 0) @@ -295,7 +295,7 @@ func TestNewAudioInput(t *testing.T) { test.That(t, audioData, test.ShouldResemble, audioData1) // audioInput1 wrapped with reconfigurable - _, err = audioinput.WrapWithReconfigurable(audioInput1) + _, err = audioinput.WrapWithReconfigurable(audioInput1, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, audioInput1.Close(context.Background()), test.ShouldBeNil) } diff --git a/components/base/base.go b/components/base/base.go index 28c05d72ce6..280a2f2b2bb 100644 --- a/components/base/base.go +++ b/components/base/base.go @@ -159,9 +159,14 @@ func CreateStatus(ctx context.Context, resource interface{}) (*commonpb.Actuator type reconfigurableBase struct { mu sync.RWMutex + name resource.Name actual Base } +func (r *reconfigurableBase) Name() resource.Name { + return r.name +} + func (r *reconfigurableBase) ProxyFor() interface{} { r.mu.RLock() defer r.mu.RUnlock() @@ -272,7 +277,7 @@ func (r *reconfigurableLocalBase) Reconfigure(ctx context.Context, newBase resou // WrapWithReconfigurable converts a regular LocalBase implementation to a reconfigurableBase. // If base is already a reconfigurableBase, then nothing is done. -func WrapWithReconfigurable(r interface{}) (resource.Reconfigurable, error) { +func WrapWithReconfigurable(r interface{}, name resource.Name) (resource.Reconfigurable, error) { base, ok := r.(Base) if !ok { return nil, NewUnimplementedInterfaceError(r) @@ -281,7 +286,7 @@ func WrapWithReconfigurable(r interface{}) (resource.Reconfigurable, error) { return reconfigurable, nil } - rBase := &reconfigurableBase{actual: base} + rBase := &reconfigurableBase{name: name, actual: base} localBase, ok := r.(LocalBase) if !ok { return rBase, nil diff --git a/components/base/base_test.go b/components/base/base_test.go index a887f6d3f2f..1e509f1c5a1 100644 --- a/components/base/base_test.go +++ b/components/base/base_test.go @@ -184,21 +184,21 @@ func TestCreateStatus(t *testing.T) { func TestWrapWithReconfigurable(t *testing.T) { var actualBase1 base.Base = &mock{Name: testBaseName} - reconfBase1, err := base.WrapWithReconfigurable(actualBase1) + reconfBase1, err := base.WrapWithReconfigurable(actualBase1, resource.Name{}) test.That(t, err, test.ShouldBeNil) - _, err = base.WrapWithReconfigurable(nil) + _, err = base.WrapWithReconfigurable(nil, resource.Name{}) test.That(t, err, test.ShouldBeError, base.NewUnimplementedInterfaceError(nil)) - reconfBase2, err := base.WrapWithReconfigurable(reconfBase1) + reconfBase2, err := base.WrapWithReconfigurable(reconfBase1, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfBase2, test.ShouldEqual, reconfBase1) var actualBase2 base.LocalBase = &mockLocal{Name: testBaseName} - reconfBase3, err := base.WrapWithReconfigurable(actualBase2) + reconfBase3, err := base.WrapWithReconfigurable(actualBase2, resource.Name{}) test.That(t, err, test.ShouldBeNil) - reconfBase4, err := base.WrapWithReconfigurable(reconfBase3) + reconfBase4, err := base.WrapWithReconfigurable(reconfBase3, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfBase4, test.ShouldResemble, reconfBase3) @@ -208,11 +208,11 @@ func TestWrapWithReconfigurable(t *testing.T) { func TestReconfigurableBase(t *testing.T) { actualBase1 := &mockLocal{Name: testBaseName} - reconfBase1, err := base.WrapWithReconfigurable(actualBase1) + reconfBase1, err := base.WrapWithReconfigurable(actualBase1, resource.Name{}) test.That(t, err, test.ShouldBeNil) actualBase2 := &mockLocal{Name: testBaseName2} - reconfBase2, err := base.WrapWithReconfigurable(actualBase2) + reconfBase2, err := base.WrapWithReconfigurable(actualBase2, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, actualBase1.reconfCount, test.ShouldEqual, 0) @@ -234,7 +234,7 @@ func TestReconfigurableBase(t *testing.T) { test.That(t, err, test.ShouldBeError, rutils.NewUnexpectedTypeError(reconfBase1, nil)) actualBase3 := &mock{Name: failBaseName} - reconfBase3, err := base.WrapWithReconfigurable(actualBase3) + reconfBase3, err := base.WrapWithReconfigurable(actualBase3, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfBase3, test.ShouldNotBeNil) @@ -247,7 +247,7 @@ func TestReconfigurableBase(t *testing.T) { test.That(t, err, test.ShouldBeError, rutils.NewUnexpectedTypeError(reconfBase3, reconfBase1)) actualBase4 := &mock{Name: testBaseName2} - reconfBase4, err := base.WrapWithReconfigurable(actualBase4) + reconfBase4, err := base.WrapWithReconfigurable(actualBase4, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfBase4, test.ShouldNotBeNil) @@ -258,7 +258,7 @@ func TestReconfigurableBase(t *testing.T) { func TestClose(t *testing.T) { actualBase1 := &mockLocal{Name: testBaseName} - reconfBase1, _ := base.WrapWithReconfigurable(actualBase1) + reconfBase1, _ := base.WrapWithReconfigurable(actualBase1, resource.Name{}) test.That(t, actualBase1.reconfCount, test.ShouldEqual, 0) test.That(t, utils.TryClose(context.Background(), reconfBase1), test.ShouldBeNil) diff --git a/components/board/board.go b/components/board/board.go index f786ef5c95d..edbeb6eb7b7 100644 --- a/components/board/board.go +++ b/components/board/board.go @@ -208,11 +208,16 @@ func NamesFromRobot(r robot.Robot) []string { type reconfigurableBoard struct { mu sync.RWMutex + name resource.Name actual Board analogs map[string]*reconfigurableAnalogReader digitals map[string]*reconfigurableDigitalInterrupt } +func (r *reconfigurableBoard) Name() resource.Name { + return r.name +} + func (r *reconfigurableBoard) ProxyFor() interface{} { r.mu.RLock() defer r.mu.RUnlock() @@ -458,7 +463,7 @@ func (r *reconfigurableLocalBoard) Reconfigure(ctx context.Context, newBoard res // WrapWithReconfigurable converts a regular Board implementation to a reconfigurableBoard. // If board is already a reconfigurableBoard, then nothing is done. -func WrapWithReconfigurable(r interface{}) (resource.Reconfigurable, error) { +func WrapWithReconfigurable(r interface{}, name resource.Name) (resource.Reconfigurable, error) { board, ok := r.(Board) if !ok { return nil, NewUnimplementedInterfaceError(r) @@ -469,6 +474,7 @@ func WrapWithReconfigurable(r interface{}) (resource.Reconfigurable, error) { } rb := reconfigurableBoard{ + name: name, actual: board, analogs: map[string]*reconfigurableAnalogReader{}, digitals: map[string]*reconfigurableDigitalInterrupt{}, diff --git a/components/board/board_test.go b/components/board/board_test.go index 0f704f5ddf4..78799cac5c6 100644 --- a/components/board/board_test.go +++ b/components/board/board_test.go @@ -160,21 +160,21 @@ var ( func TestWrapWithReconfigurable(t *testing.T) { var actualBoard board.Board = newLocalBoard(testBoardName) - reconfBoard1, err := board.WrapWithReconfigurable(actualBoard) + reconfBoard1, err := board.WrapWithReconfigurable(actualBoard, resource.Name{}) test.That(t, err, test.ShouldBeNil) - _, err = board.WrapWithReconfigurable(nil) + _, err = board.WrapWithReconfigurable(nil, resource.Name{}) test.That(t, err, test.ShouldBeError, board.NewUnimplementedInterfaceError(nil)) - reconfBoard2, err := board.WrapWithReconfigurable(reconfBoard1) + reconfBoard2, err := board.WrapWithReconfigurable(reconfBoard1, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfBoard2, test.ShouldEqual, reconfBoard1) var actualBoard2 board.LocalBoard = &mockLocal{Name: testBoardName} - reconfBoard3, err := board.WrapWithReconfigurable(actualBoard2) + reconfBoard3, err := board.WrapWithReconfigurable(actualBoard2, resource.Name{}) test.That(t, err, test.ShouldBeNil) - reconfBoard4, err := board.WrapWithReconfigurable(reconfBoard3) + reconfBoard4, err := board.WrapWithReconfigurable(reconfBoard3, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfBoard4, test.ShouldResemble, reconfBoard3) @@ -189,11 +189,11 @@ func TestReconfigurableBoard(t *testing.T) { } for _, actualBoard1 := range actualBoards { - reconfBoard1, err := board.WrapWithReconfigurable(actualBoard1) + reconfBoard1, err := board.WrapWithReconfigurable(actualBoard1, resource.Name{}) test.That(t, err, test.ShouldBeNil) actualBoard2 := newBoard(testBoardName2) - reconfBoard2, err := board.WrapWithReconfigurable(actualBoard2) + reconfBoard2, err := board.WrapWithReconfigurable(actualBoard2, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, actualBoard1.reconfCount, test.ShouldEqual, 0) @@ -203,7 +203,7 @@ func TestReconfigurableBoard(t *testing.T) { test.That(t, actualBoard1.reconfCount, test.ShouldEqual, 1) actualBoard3 := newLocalBoard(testBoardName2) - reconfBoard3, err := board.WrapWithReconfigurable(actualBoard3) + reconfBoard3, err := board.WrapWithReconfigurable(actualBoard3, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, actualBoard3.reconfCount, test.ShouldEqual, 0) @@ -212,13 +212,13 @@ func TestReconfigurableBoard(t *testing.T) { test.That(t, err, test.ShouldBeError, rutils.NewUnexpectedTypeError(reconfBoard3, reconfBoard1)) actualBoard4 := &mock{Name: testBoardName2} - reconfBoard4, err := board.WrapWithReconfigurable(actualBoard4) + reconfBoard4, err := board.WrapWithReconfigurable(actualBoard4, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfBoard4, test.ShouldNotBeNil) } actualBoard1 := &mock{Name: testBoardName} - reconfBoard1, err := board.WrapWithReconfigurable(actualBoard1) + reconfBoard1, err := board.WrapWithReconfigurable(actualBoard1, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfBoard1, test.ShouldNotBeNil) } @@ -230,11 +230,11 @@ func TestReconfigurableLocalBoard(t *testing.T) { } for _, actualBoard1 := range actualBoards { - reconfBoard1, err := board.WrapWithReconfigurable(actualBoard1) + reconfBoard1, err := board.WrapWithReconfigurable(actualBoard1, resource.Name{}) test.That(t, err, test.ShouldBeNil) actualBoard2 := newLocalBoard(testBoardName2) - reconfBoard2, err := board.WrapWithReconfigurable(actualBoard2) + reconfBoard2, err := board.WrapWithReconfigurable(actualBoard2, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, actualBoard1.reconfCount, test.ShouldEqual, 0) @@ -262,7 +262,7 @@ func TestReconfigurableLocalBoard(t *testing.T) { func TestSetGPIO(t *testing.T) { actualBoard := newLocalBoard(testBoardName) - reconfBoard, _ := board.WrapWithReconfigurable(actualBoard) + reconfBoard, _ := board.WrapWithReconfigurable(actualBoard, resource.Name{}) test.That(t, actualBoard.gpioPin.setCount, test.ShouldEqual, 0) test.That(t, actualBoard.gpioPin.extra, test.ShouldBeNil) @@ -277,7 +277,7 @@ func TestSetGPIO(t *testing.T) { func TestGetGPIO(t *testing.T) { actualBoard := newLocalBoard(testBoardName) - reconfBoard, _ := board.WrapWithReconfigurable(actualBoard) + reconfBoard, _ := board.WrapWithReconfigurable(actualBoard, resource.Name{}) test.That(t, actualBoard.gpioPin.getCount, test.ShouldEqual, 0) test.That(t, actualBoard.gpioPin.extra, test.ShouldBeNil) @@ -291,9 +291,10 @@ func TestGetGPIO(t *testing.T) { test.That(t, actualBoard.gpioPin.extra, test.ShouldResemble, extra) } +//nolint:dupl func TestSetPWM(t *testing.T) { actualBoard := newLocalBoard(testBoardName) - reconfBoard, _ := board.WrapWithReconfigurable(actualBoard) + reconfBoard, _ := board.WrapWithReconfigurable(actualBoard, resource.Name{}) test.That(t, actualBoard.gpioPin.setPWMCount, test.ShouldEqual, 0) test.That(t, actualBoard.gpioPin.extra, test.ShouldBeNil) @@ -306,9 +307,10 @@ func TestSetPWM(t *testing.T) { test.That(t, actualBoard.gpioPin.extra, test.ShouldResemble, extra) } +//nolint:dupl func TestSetPWMFreq(t *testing.T) { actualBoard := newLocalBoard(testBoardName) - reconfBoard, _ := board.WrapWithReconfigurable(actualBoard) + reconfBoard, _ := board.WrapWithReconfigurable(actualBoard, resource.Name{}) test.That(t, actualBoard.gpioPin.setPWMFreqCount, test.ShouldEqual, 0) test.That(t, actualBoard.gpioPin.extra, test.ShouldBeNil) @@ -323,7 +325,7 @@ func TestSetPWMFreq(t *testing.T) { func TestStatus(t *testing.T) { actualBoard := newLocalBoard(testBoardName) - reconfBoard, _ := board.WrapWithReconfigurable(actualBoard) + reconfBoard, _ := board.WrapWithReconfigurable(actualBoard, resource.Name{}) test.That(t, actualBoard.statusCount, test.ShouldEqual, 0) test.That(t, actualBoard.extra, test.ShouldBeNil) @@ -337,7 +339,7 @@ func TestStatus(t *testing.T) { func TestSPIs(t *testing.T) { actualBoard := newLocalBoard(testBoardName) - reconfBoard, _ := board.WrapWithReconfigurable(actualBoard) + reconfBoard, _ := board.WrapWithReconfigurable(actualBoard, resource.Name{}) reconfSPINames := reconfBoard.(board.LocalBoard).SPINames() test.That(t, reconfSPINames, test.ShouldResemble, []string{"spi1"}) @@ -351,7 +353,7 @@ func TestSPIs(t *testing.T) { func TestI2Cs(t *testing.T) { actualBoard := newLocalBoard(testBoardName) - reconfBoard, _ := board.WrapWithReconfigurable(actualBoard) + reconfBoard, _ := board.WrapWithReconfigurable(actualBoard, resource.Name{}) reconfI2CNames := reconfBoard.(board.LocalBoard).I2CNames() test.That(t, reconfI2CNames, test.ShouldResemble, []string{"i2c1"}) @@ -366,7 +368,7 @@ func TestI2Cs(t *testing.T) { //nolint:dupl func TestAnalogReaders(t *testing.T) { actualBoard := newLocalBoard(testBoardName) - reconfBoard, _ := board.WrapWithReconfigurable(actualBoard) + reconfBoard, _ := board.WrapWithReconfigurable(actualBoard, resource.Name{}) reconfAnalogReaderNames := reconfBoard.(board.LocalBoard).AnalogReaderNames() test.That(t, reconfAnalogReaderNames, test.ShouldResemble, []string{"analog1"}) @@ -384,7 +386,7 @@ func TestAnalogReaders(t *testing.T) { //nolint:dupl func TestDigitalInterrupts(t *testing.T) { actualBoard := newLocalBoard(testBoardName) - reconfBoard, _ := board.WrapWithReconfigurable(actualBoard) + reconfBoard, _ := board.WrapWithReconfigurable(actualBoard, resource.Name{}) reconfDigitalInterruptNames := reconfBoard.(board.LocalBoard).DigitalInterruptNames() test.That(t, reconfDigitalInterruptNames, test.ShouldResemble, []string{"digital1"}) @@ -401,7 +403,7 @@ func TestDigitalInterrupts(t *testing.T) { func TestClose(t *testing.T) { actualBoard1 := &mockLocal{Name: testBoardName} - reconfBoard1, _ := board.WrapWithReconfigurable(actualBoard1) + reconfBoard1, _ := board.WrapWithReconfigurable(actualBoard1, resource.Name{}) test.That(t, actualBoard1.reconfCount, test.ShouldEqual, 0) test.That(t, utils.TryClose(context.Background(), reconfBoard1), test.ShouldBeNil) diff --git a/components/camera/camera.go b/components/camera/camera.go index 4fcfef5012b..d708c9ad38e 100644 --- a/components/camera/camera.go +++ b/components/camera/camera.go @@ -271,7 +271,7 @@ func DependencyTypeError(name, actual interface{}) error { } // WrapWithReconfigurable wraps a camera with a reconfigurable and locking interface. -func WrapWithReconfigurable(r interface{}) (resource.Reconfigurable, error) { +func WrapWithReconfigurable(r interface{}, name resource.Name) (resource.Reconfigurable, error) { c, ok := r.(Camera) if !ok { return nil, NewUnimplementedInterfaceError(r) @@ -281,6 +281,7 @@ func WrapWithReconfigurable(r interface{}) (resource.Reconfigurable, error) { } cancelCtx, cancel := context.WithCancel(context.Background()) return &reconfigurableCamera{ + name: name, actual: c, cancelCtx: cancelCtx, cancel: cancel, @@ -327,11 +328,16 @@ func NamesFromRobot(r robot.Robot) []string { type reconfigurableCamera struct { mu sync.RWMutex + name resource.Name actual Camera cancelCtx context.Context cancel func() } +func (c *reconfigurableCamera) Name() resource.Name { + return c.name +} + func (c *reconfigurableCamera) ProxyFor() interface{} { c.mu.RLock() defer c.mu.RUnlock() diff --git a/components/camera/camera_test.go b/components/camera/camera_test.go index 48d161400ae..80bff4edd64 100644 --- a/components/camera/camera_test.go +++ b/components/camera/camera_test.go @@ -165,24 +165,24 @@ func TestCameraName(t *testing.T) { func TestWrapWithReconfigurable(t *testing.T) { var actualCamera1 camera.Camera = &mock{Name: testCameraName} - reconfCamera1, err := camera.WrapWithReconfigurable(actualCamera1) + reconfCamera1, err := camera.WrapWithReconfigurable(actualCamera1, resource.Name{}) test.That(t, err, test.ShouldBeNil) - _, err = camera.WrapWithReconfigurable(nil) + _, err = camera.WrapWithReconfigurable(nil, resource.Name{}) test.That(t, err, test.ShouldBeError, camera.NewUnimplementedInterfaceError(nil)) - reconfCamera2, err := camera.WrapWithReconfigurable(reconfCamera1) + reconfCamera2, err := camera.WrapWithReconfigurable(reconfCamera1, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfCamera2, test.ShouldEqual, reconfCamera1) } func TestReconfigurableCamera(t *testing.T) { actualCamera1 := &mock{Name: testCameraName} - reconfCamera1, err := camera.WrapWithReconfigurable(actualCamera1) + reconfCamera1, err := camera.WrapWithReconfigurable(actualCamera1, resource.Name{}) test.That(t, err, test.ShouldBeNil) actualCamera2 := &mock{Name: testCameraName2} - reconfCamera2, err := camera.WrapWithReconfigurable(actualCamera2) + reconfCamera2, err := camera.WrapWithReconfigurable(actualCamera2, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, actualCamera1.reconfCount, test.ShouldEqual, 0) @@ -221,7 +221,7 @@ func TestReconfigurableCamera(t *testing.T) { func TestClose(t *testing.T) { actualCamera1 := &mock{Name: testCameraName} - reconfCamera1, err := camera.WrapWithReconfigurable(actualCamera1) + reconfCamera1, err := camera.WrapWithReconfigurable(actualCamera1, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, actualCamera1.reconfCount, test.ShouldEqual, 0) @@ -366,7 +366,7 @@ func TestNewCamera(t *testing.T) { test.That(t, *(cam4props.IntrinsicParams), test.ShouldNotResemble, *(cam2props.IntrinsicParams)) // cam4 wrapped with reconfigurable - reconfig, err := camera.WrapWithReconfigurable(cam4) + reconfig, err := camera.WrapWithReconfigurable(cam4, resource.Name{}) test.That(t, err, test.ShouldBeNil) fakeCamera := reconfig.(camera.Camera) props, _ = fakeCamera.Properties(context.Background()) diff --git a/components/encoder/encoder.go b/components/encoder/encoder.go index 1cea70dcf11..80356463ae1 100644 --- a/components/encoder/encoder.go +++ b/components/encoder/encoder.go @@ -103,9 +103,14 @@ func NamesFromRobot(r robot.Robot) []string { type reconfigurableEncoder struct { mu sync.RWMutex + name resource.Name actual Encoder } +func (r *reconfigurableEncoder) Name() resource.Name { + return r.name +} + func (r *reconfigurableEncoder) ProxyFor() interface{} { r.mu.RLock() defer r.mu.RUnlock() @@ -156,7 +161,7 @@ func (r *reconfigurableEncoder) reconfigure(ctx context.Context, newEncoder reso // WrapWithReconfigurable converts a regular Encoder implementation to a reconfigurableEncoder. // If encoder is already a reconfigurableEncoder, then nothing is done. -func WrapWithReconfigurable(r interface{}) (resource.Reconfigurable, error) { +func WrapWithReconfigurable(r interface{}, name resource.Name) (resource.Reconfigurable, error) { m, ok := r.(Encoder) if !ok { return nil, NewUnimplementedInterfaceError(r) @@ -164,5 +169,5 @@ func WrapWithReconfigurable(r interface{}) (resource.Reconfigurable, error) { if reconfigurable, ok := m.(*reconfigurableEncoder); ok { return reconfigurable, nil } - return &reconfigurableEncoder{actual: m}, nil + return &reconfigurableEncoder{name: name, actual: m}, nil } diff --git a/components/gantry/gantry.go b/components/gantry/gantry.go index 7884fea94fe..ffa42f0686b 100644 --- a/components/gantry/gantry.go +++ b/components/gantry/gantry.go @@ -163,7 +163,7 @@ func CreateStatus(ctx context.Context, resource interface{}) (*pb.Status, error) // WrapWithReconfigurable wraps a gantry or localGantry with a reconfigurable // and locking interface. -func WrapWithReconfigurable(r interface{}) (resource.Reconfigurable, error) { +func WrapWithReconfigurable(r interface{}, name resource.Name) (resource.Reconfigurable, error) { g, ok := r.(Gantry) if !ok { return nil, NewUnimplementedInterfaceError(r) @@ -172,7 +172,7 @@ func WrapWithReconfigurable(r interface{}) (resource.Reconfigurable, error) { return reconfigurable, nil } - rGantry := &reconfigurableGantry{actual: g} + rGantry := &reconfigurableGantry{name: name, actual: g} gLocal, ok := r.(LocalGantry) if !ok { return rGantry, nil @@ -193,9 +193,14 @@ var ( type reconfigurableGantry struct { mu sync.RWMutex + name resource.Name actual Gantry } +func (g *reconfigurableGantry) Name() resource.Name { + return g.name +} + func (g *reconfigurableGantry) ProxyFor() interface{} { g.mu.RLock() defer g.mu.RUnlock() diff --git a/components/gantry/gantry_test.go b/components/gantry/gantry_test.go index 0a62620459c..62d3ba75056 100644 --- a/components/gantry/gantry_test.go +++ b/components/gantry/gantry_test.go @@ -246,21 +246,21 @@ func TestGantryName(t *testing.T) { func TestWrapWithReconfigurable(t *testing.T) { var actualGantry1 gantry.Gantry = &mock{Name: testGantryName} - reconfGantry1, err := gantry.WrapWithReconfigurable(actualGantry1) + reconfGantry1, err := gantry.WrapWithReconfigurable(actualGantry1, resource.Name{}) test.That(t, err, test.ShouldBeNil) - _, err = gantry.WrapWithReconfigurable(nil) + _, err = gantry.WrapWithReconfigurable(nil, resource.Name{}) test.That(t, err, test.ShouldBeError, gantry.NewUnimplementedInterfaceError(nil)) - reconfGantry2, err := gantry.WrapWithReconfigurable(reconfGantry1) + reconfGantry2, err := gantry.WrapWithReconfigurable(reconfGantry1, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfGantry2, test.ShouldEqual, reconfGantry1) var actualGantry2 gantry.LocalGantry = &mockLocal{Name: testGantryName} - reconfGantry3, err := gantry.WrapWithReconfigurable(actualGantry2) + reconfGantry3, err := gantry.WrapWithReconfigurable(actualGantry2, resource.Name{}) test.That(t, err, test.ShouldBeNil) - reconfGantry4, err := gantry.WrapWithReconfigurable(reconfGantry3) + reconfGantry4, err := gantry.WrapWithReconfigurable(reconfGantry3, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfGantry4, test.ShouldResemble, reconfGantry3) @@ -270,11 +270,11 @@ func TestWrapWithReconfigurable(t *testing.T) { func TestReconfigurableGantry(t *testing.T) { actualGantry1 := &mockLocal{Name: testGantryName} - reconfGantry1, err := gantry.WrapWithReconfigurable(actualGantry1) + reconfGantry1, err := gantry.WrapWithReconfigurable(actualGantry1, resource.Name{}) test.That(t, err, test.ShouldBeNil) actualGantry2 := &mockLocal{Name: testGantryName2} - reconfGantry2, err := gantry.WrapWithReconfigurable(actualGantry2) + reconfGantry2, err := gantry.WrapWithReconfigurable(actualGantry2, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, actualGantry1.reconfCount, test.ShouldEqual, 0) @@ -296,7 +296,7 @@ func TestReconfigurableGantry(t *testing.T) { test.That(t, err, test.ShouldBeError, rutils.NewUnexpectedTypeError(reconfGantry1, nil)) actualGantry3 := &mock{Name: testGantryName} - reconfGantry3, err := gantry.WrapWithReconfigurable(actualGantry3) + reconfGantry3, err := gantry.WrapWithReconfigurable(actualGantry3, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfGantry3, test.ShouldNotBeNil) @@ -310,7 +310,7 @@ func TestReconfigurableGantry(t *testing.T) { test.That(t, err, test.ShouldBeError, rutils.NewUnexpectedTypeError(reconfGantry3, reconfGantry1)) actualGantry4 := &mock{Name: testGantryName2} - reconfGantry4, err := gantry.WrapWithReconfigurable(actualGantry4) + reconfGantry4, err := gantry.WrapWithReconfigurable(actualGantry4, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfGantry4, test.ShouldNotBeNil) @@ -321,7 +321,7 @@ func TestReconfigurableGantry(t *testing.T) { func TestStop(t *testing.T) { actualGantry1 := &mockLocal{Name: testGantryName} - reconfGantry1, err := gantry.WrapWithReconfigurable(actualGantry1) + reconfGantry1, err := gantry.WrapWithReconfigurable(actualGantry1, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, actualGantry1.stopCount, test.ShouldEqual, 0) @@ -332,7 +332,7 @@ func TestStop(t *testing.T) { func TestClose(t *testing.T) { actualGantry1 := &mockLocal{Name: testGantryName} - reconfGantry1, err := gantry.WrapWithReconfigurable(actualGantry1) + reconfGantry1, err := gantry.WrapWithReconfigurable(actualGantry1, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, actualGantry1.reconfCount, test.ShouldEqual, 0) diff --git a/components/generic/generic.go b/components/generic/generic.go index 607580f2960..c97d35364b6 100644 --- a/components/generic/generic.go +++ b/components/generic/generic.go @@ -117,9 +117,14 @@ func NamesFromRobot(r robot.Robot) []string { type reconfigurableGeneric struct { mu sync.RWMutex + name resource.Name actual Generic } +func (r *reconfigurableGeneric) Name() resource.Name { + return r.name +} + func (r *reconfigurableGeneric) Close(ctx context.Context) error { r.mu.RLock() defer r.mu.RUnlock() @@ -154,7 +159,7 @@ func (r *reconfigurableGeneric) Reconfigure(ctx context.Context, newGeneric reso // WrapWithReconfigurable converts a regular Generic implementation to a reconfigurableGeneric. // If Generic is already a reconfigurableGeneric, then nothing is done. -func WrapWithReconfigurable(r interface{}) (resource.Reconfigurable, error) { +func WrapWithReconfigurable(r interface{}, name resource.Name) (resource.Reconfigurable, error) { Generic, ok := r.(Generic) if !ok { return nil, NewUnimplementedInterfaceError(r) @@ -162,7 +167,7 @@ func WrapWithReconfigurable(r interface{}) (resource.Reconfigurable, error) { if reconfigurable, ok := Generic.(*reconfigurableGeneric); ok { return reconfigurable, nil } - return &reconfigurableGeneric{actual: Generic}, nil + return &reconfigurableGeneric{name: name, actual: Generic}, nil } // RegisterService is a helper for testing in other components. diff --git a/components/generic/generic_test.go b/components/generic/generic_test.go index 720db9de204..88c3794e21e 100644 --- a/components/generic/generic_test.go +++ b/components/generic/generic_test.go @@ -106,24 +106,24 @@ func TestGenericName(t *testing.T) { func TestWrapWithReconfigurable(t *testing.T) { var actualGeneric1 generic.Generic = &mock{Name: testGenericName} - reconfGeneric1, err := generic.WrapWithReconfigurable(actualGeneric1) + reconfGeneric1, err := generic.WrapWithReconfigurable(actualGeneric1, resource.Name{}) test.That(t, err, test.ShouldBeNil) - _, err = generic.WrapWithReconfigurable(nil) + _, err = generic.WrapWithReconfigurable(nil, resource.Name{}) test.That(t, err, test.ShouldBeError, generic.NewUnimplementedInterfaceError(nil)) - reconfGeneric2, err := generic.WrapWithReconfigurable(reconfGeneric1) + reconfGeneric2, err := generic.WrapWithReconfigurable(reconfGeneric1, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfGeneric2, test.ShouldEqual, reconfGeneric1) } func TestReconfigurableGeneric(t *testing.T) { actualGeneric1 := &mock{Name: testGenericName} - reconfGeneric1, err := generic.WrapWithReconfigurable(actualGeneric1) + reconfGeneric1, err := generic.WrapWithReconfigurable(actualGeneric1, resource.Name{}) test.That(t, err, test.ShouldBeNil) actualGeneric2 := &mock{Name: testGenericName2} - reconfGeneric2, err := generic.WrapWithReconfigurable(actualGeneric2) + reconfGeneric2, err := generic.WrapWithReconfigurable(actualGeneric2, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, actualGeneric1.reconfCount, test.ShouldEqual, 0) @@ -147,7 +147,7 @@ func TestReconfigurableGeneric(t *testing.T) { func TestClose(t *testing.T) { actualGeneric1 := &mock{Name: testGenericName} - reconfGeneric1, err := generic.WrapWithReconfigurable(actualGeneric1) + reconfGeneric1, err := generic.WrapWithReconfigurable(actualGeneric1, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, actualGeneric1.reconfCount, test.ShouldEqual, 0) diff --git a/components/gripper/gripper.go b/components/gripper/gripper.go index 138840b4269..5de6f7efde3 100644 --- a/components/gripper/gripper.go +++ b/components/gripper/gripper.go @@ -93,7 +93,7 @@ func NewUnimplementedLocalInterfaceError(actual interface{}) error { } // WrapWithReconfigurable wraps a gripper with a reconfigurable and locking interface. -func WrapWithReconfigurable(r interface{}) (resource.Reconfigurable, error) { +func WrapWithReconfigurable(r interface{}, name resource.Name) (resource.Reconfigurable, error) { g, ok := r.(Gripper) if !ok { return nil, NewUnimplementedInterfaceError(r) @@ -101,7 +101,7 @@ func WrapWithReconfigurable(r interface{}) (resource.Reconfigurable, error) { if reconfigurable, ok := g.(*reconfigurableGripper); ok { return reconfigurable, nil } - rGripper := &reconfigurableGripper{actual: g} + rGripper := &reconfigurableGripper{name: name, actual: g} gLocal, ok := r.(LocalGripper) if !ok { return rGripper, nil @@ -156,9 +156,14 @@ func CreateStatus(ctx context.Context, resource interface{}) (*commonpb.Actuator type reconfigurableGripper struct { mu sync.RWMutex + name resource.Name actual Gripper } +func (g *reconfigurableGripper) Name() resource.Name { + return g.name +} + func (g *reconfigurableGripper) ProxyFor() interface{} { g.mu.RLock() defer g.mu.RUnlock() diff --git a/components/gripper/gripper_test.go b/components/gripper/gripper_test.go index 6f366a39c2a..a8b97df84bd 100644 --- a/components/gripper/gripper_test.go +++ b/components/gripper/gripper_test.go @@ -183,21 +183,21 @@ func TestGripperName(t *testing.T) { func TestWrapWithReconfigurable(t *testing.T) { var actualGripper1 gripper.Gripper = &mockLocal{Name: testGripperName} - reconfGripper1, err := gripper.WrapWithReconfigurable(actualGripper1) + reconfGripper1, err := gripper.WrapWithReconfigurable(actualGripper1, resource.Name{}) test.That(t, err, test.ShouldBeNil) - _, err = gripper.WrapWithReconfigurable(nil) + _, err = gripper.WrapWithReconfigurable(nil, resource.Name{}) test.That(t, err, test.ShouldBeError, gripper.NewUnimplementedInterfaceError(nil)) - reconfGripper2, err := gripper.WrapWithReconfigurable(reconfGripper1) + reconfGripper2, err := gripper.WrapWithReconfigurable(reconfGripper1, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfGripper2, test.ShouldEqual, reconfGripper1) var actualGripper2 gripper.LocalGripper = &mockLocal{Name: testGripperName} - reconfGripper3, err := gripper.WrapWithReconfigurable(actualGripper2) + reconfGripper3, err := gripper.WrapWithReconfigurable(actualGripper2, resource.Name{}) test.That(t, err, test.ShouldBeNil) - reconfGripper4, err := gripper.WrapWithReconfigurable(reconfGripper3) + reconfGripper4, err := gripper.WrapWithReconfigurable(reconfGripper3, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfGripper4, test.ShouldResemble, reconfGripper3) @@ -207,11 +207,11 @@ func TestWrapWithReconfigurable(t *testing.T) { func TestReconfigurableGripper(t *testing.T) { actualGripper1 := &mockLocal{Name: testGripperName} - reconfGripper1, err := gripper.WrapWithReconfigurable(actualGripper1) + reconfGripper1, err := gripper.WrapWithReconfigurable(actualGripper1, resource.Name{}) test.That(t, err, test.ShouldBeNil) actualGripper2 := &mockLocal{Name: testGripperName} - reconfGripper2, err := gripper.WrapWithReconfigurable(actualGripper2) + reconfGripper2, err := gripper.WrapWithReconfigurable(actualGripper2, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, actualGripper1.reconfCount, test.ShouldEqual, 0) @@ -233,7 +233,7 @@ func TestReconfigurableGripper(t *testing.T) { test.That(t, err, test.ShouldBeError, rutils.NewUnexpectedTypeError(reconfGripper1, nil)) actualGripper3 := &mock{Name: failGripperName} - reconfGripper3, err := gripper.WrapWithReconfigurable(actualGripper3) + reconfGripper3, err := gripper.WrapWithReconfigurable(actualGripper3, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfGripper3, test.ShouldNotBeNil) @@ -247,7 +247,7 @@ func TestReconfigurableGripper(t *testing.T) { test.That(t, err, test.ShouldBeError, rutils.NewUnexpectedTypeError(reconfGripper3, reconfGripper1)) actualGripper4 := &mock{Name: testGripperName2} - reconfGripper4, err := gripper.WrapWithReconfigurable(actualGripper4) + reconfGripper4, err := gripper.WrapWithReconfigurable(actualGripper4, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfGripper4, test.ShouldNotBeNil) @@ -258,7 +258,7 @@ func TestReconfigurableGripper(t *testing.T) { func TestStop(t *testing.T) { actualGripper1 := &mockLocal{Name: testGripperName} - reconfGripper1, err := gripper.WrapWithReconfigurable(actualGripper1) + reconfGripper1, err := gripper.WrapWithReconfigurable(actualGripper1, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, actualGripper1.stopCount, test.ShouldEqual, 0) diff --git a/components/input/input.go b/components/input/input.go index bd5d26c737c..c286b29dd43 100644 --- a/components/input/input.go +++ b/components/input/input.go @@ -66,7 +66,9 @@ type Controller interface { // Events returns most recent Event for each input (which should be the current state) Events(ctx context.Context, extra map[string]interface{}) (map[Control]Event, error) - // RegisterCallback registers a callback that will fire on given EventTypes for a given Control + // RegisterCallback registers a callback that will fire on given EventTypes for a given Control. + // The callback is called on the same goroutine as the firer and if any long operation is to occur, + // the callback should start a goroutine. RegisterControlCallback( ctx context.Context, control Control, @@ -169,7 +171,7 @@ func DependencyTypeError(name, actual interface{}) error { } // WrapWithReconfigurable wraps a Controller with a reconfigurable and locking interface. -func WrapWithReconfigurable(r interface{}) (resource.Reconfigurable, error) { +func WrapWithReconfigurable(r interface{}, name resource.Name) (resource.Reconfigurable, error) { c, ok := r.(Controller) if !ok { return nil, NewUnimplementedInterfaceError(r) @@ -177,7 +179,7 @@ func WrapWithReconfigurable(r interface{}) (resource.Reconfigurable, error) { if reconfigurable, ok := c.(*reconfigurableInputController); ok { return reconfigurable, nil } - return &reconfigurableInputController{actual: c}, nil + return &reconfigurableInputController{name: name, actual: c}, nil } var ( @@ -243,9 +245,14 @@ func CreateStatus(ctx context.Context, resource interface{}) (*pb.Status, error) type reconfigurableInputController struct { mu sync.RWMutex + name resource.Name actual Controller } +func (c *reconfigurableInputController) Name() resource.Name { + return c.name +} + func (c *reconfigurableInputController) ProxyFor() interface{} { c.mu.RLock() defer c.mu.RUnlock() diff --git a/components/input/input_test.go b/components/input/input_test.go index 2e25ce099ed..c53586640a1 100644 --- a/components/input/input_test.go +++ b/components/input/input_test.go @@ -220,24 +220,24 @@ func TestInputControllerName(t *testing.T) { func TestWrapWithReconfigurable(t *testing.T) { var actualInput1 input.Controller = &mock{Name: testInputControllerName} - reconfInput1, err := input.WrapWithReconfigurable(actualInput1) + reconfInput1, err := input.WrapWithReconfigurable(actualInput1, resource.Name{}) test.That(t, err, test.ShouldBeNil) - _, err = input.WrapWithReconfigurable(nil) + _, err = input.WrapWithReconfigurable(nil, resource.Name{}) test.That(t, err, test.ShouldBeError, input.NewUnimplementedInterfaceError(nil)) - reconfInput2, err := input.WrapWithReconfigurable(reconfInput1) + reconfInput2, err := input.WrapWithReconfigurable(reconfInput1, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfInput2, test.ShouldEqual, reconfInput1) } func TestReconfigurableInputController(t *testing.T) { actualInput1 := &mock{Name: testInputControllerName} - reconfInput1, err := input.WrapWithReconfigurable(actualInput1) + reconfInput1, err := input.WrapWithReconfigurable(actualInput1, resource.Name{}) test.That(t, err, test.ShouldBeNil) actualInput2 := &mock{Name: testInputControllerName2} - reconfInput2, err := input.WrapWithReconfigurable(actualInput2) + reconfInput2, err := input.WrapWithReconfigurable(actualInput2, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, actualInput1.reconfCount, test.ShouldEqual, 0) @@ -261,7 +261,7 @@ func TestReconfigurableInputController(t *testing.T) { func TestClose(t *testing.T) { actualInput1 := &mock{Name: testInputControllerName} - reconfInput1, err := input.WrapWithReconfigurable(actualInput1) + reconfInput1, err := input.WrapWithReconfigurable(actualInput1, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, actualInput1.reconfCount, test.ShouldEqual, 0) @@ -271,7 +271,7 @@ func TestClose(t *testing.T) { func TestExtra(t *testing.T) { actualInput1 := &mock{Name: testInputControllerName} - reconfInput1, err := input.WrapWithReconfigurable((actualInput1)) + reconfInput1, err := input.WrapWithReconfigurable((actualInput1), resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, actualInput1.extra, test.ShouldBeNil) diff --git a/components/input/webgamepad/webgamepad.go b/components/input/webgamepad/webgamepad.go index 716483d31b6..2adca1c8638 100644 --- a/components/input/webgamepad/webgamepad.go +++ b/components/input/webgamepad/webgamepad.go @@ -6,7 +6,6 @@ import ( "sync" "github.com/edaniels/golog" - "go.viam.com/utils" "go.viam.com/rdk/components/generic" "go.viam.com/rdk/components/input" @@ -29,9 +28,6 @@ func NewController(ctx context.Context, _ registry.Dependencies, config config.C var w webGamepad w.callbacks = make(map[input.Control]map[input.EventType]input.ControlFunction) w.lastEvents = make(map[input.Control]input.Event) - ctxWithCancel, cancel := context.WithCancel(ctx) - w.cancelFunc = cancel - w.ctxWithCancel = ctxWithCancel w.controls = []input.Control{ input.AbsoluteX, input.AbsoluteY, input.AbsoluteRX, input.AbsoluteRY, input.AbsoluteZ, input.AbsoluteRZ, input.AbsoluteHat0X, input.AbsoluteHat0Y, @@ -46,17 +42,14 @@ var _ = input.Controller(&webGamepad{}) // webGamepad is an input.Controller. type webGamepad struct { - controls []input.Control - lastEvents map[input.Control]input.Event - mu sync.RWMutex - activeBackgroundWorkers sync.WaitGroup - ctxWithCancel context.Context - cancelFunc func() - callbacks map[input.Control]map[input.EventType]input.ControlFunction + controls []input.Control + lastEvents map[input.Control]input.Event + mu sync.RWMutex + callbacks map[input.Control]map[input.EventType]input.ControlFunction generic.Unimplemented } -func (w *webGamepad) makeCallbacks(eventOut input.Event) { +func (w *webGamepad) makeCallbacks(ctx context.Context, eventOut input.Event) { w.mu.Lock() w.lastEvents[eventOut.Control] = eventOut w.mu.Unlock() @@ -74,29 +67,15 @@ func (w *webGamepad) makeCallbacks(eventOut input.Event) { ctrlFunc, ok := w.callbacks[eventOut.Control][eventOut.Event] if ok && ctrlFunc != nil { - w.activeBackgroundWorkers.Add(1) - utils.PanicCapturingGo(func() { - defer w.activeBackgroundWorkers.Done() - ctrlFunc(w.ctxWithCancel, eventOut) - }) + ctrlFunc(ctx, eventOut) } ctrlFuncAll, ok := w.callbacks[eventOut.Control][input.AllEvents] if ok && ctrlFuncAll != nil { - w.activeBackgroundWorkers.Add(1) - utils.PanicCapturingGo(func() { - defer w.activeBackgroundWorkers.Done() - ctrlFuncAll(w.ctxWithCancel, eventOut) - }) + ctrlFuncAll(ctx, eventOut) } } -// Close terminates background worker threads. -func (w *webGamepad) Close() { - w.cancelFunc() - w.activeBackgroundWorkers.Wait() -} - // Controls lists the inputs of the gamepad. func (w *webGamepad) Controls(ctx context.Context, extra map[string]interface{}) ([]input.Control, error) { out := append([]input.Control(nil), w.controls...) @@ -141,6 +120,6 @@ func (w *webGamepad) RegisterControlCallback( // TriggerEvent allows directly sending an Event (such as a button press) from external code. func (w *webGamepad) TriggerEvent(ctx context.Context, event input.Event, extra map[string]interface{}) error { - w.makeCallbacks(event) + w.makeCallbacks(ctx, event) return nil } diff --git a/components/motor/motor.go b/components/motor/motor.go index 8e32b53de19..675d00967c6 100644 --- a/components/motor/motor.go +++ b/components/motor/motor.go @@ -206,9 +206,14 @@ func CreateStatus(ctx context.Context, resource interface{}) (*pb.Status, error) type reconfigurableMotor struct { mu sync.RWMutex + name resource.Name actual Motor } +func (r *reconfigurableMotor) Name() resource.Name { + return r.name +} + func (r *reconfigurableMotor) ProxyFor() interface{} { r.mu.RLock() defer r.mu.RUnlock() @@ -330,7 +335,7 @@ func (r *reconfigurableLocalMotor) IsMoving(ctx context.Context) (bool, error) { // WrapWithReconfigurable converts a regular Motor implementation to a reconfigurableMotor. // If motor is already a reconfigurableMotor, then nothing is done. -func WrapWithReconfigurable(r interface{}) (resource.Reconfigurable, error) { +func WrapWithReconfigurable(r interface{}, name resource.Name) (resource.Reconfigurable, error) { m, ok := r.(Motor) if !ok { return nil, NewUnimplementedInterfaceError(r) @@ -338,7 +343,7 @@ func WrapWithReconfigurable(r interface{}) (resource.Reconfigurable, error) { if reconfigurable, ok := m.(*reconfigurableMotor); ok { return reconfigurable, nil } - rMotor := &reconfigurableMotor{actual: m} + rMotor := &reconfigurableMotor{name: name, actual: m} mLocal, ok := r.(LocalMotor) if !ok { return rMotor, nil diff --git a/components/motor/motor_test.go b/components/motor/motor_test.go index c25c7217d16..d6156f1f2a6 100644 --- a/components/motor/motor_test.go +++ b/components/motor/motor_test.go @@ -264,21 +264,21 @@ func TestMotorName(t *testing.T) { func TestWrapWithReconfigurable(t *testing.T) { var actualMotor1 motor.Motor = &mock{Name: testMotorName} - reconfMotor1, err := motor.WrapWithReconfigurable(actualMotor1) + reconfMotor1, err := motor.WrapWithReconfigurable(actualMotor1, resource.Name{}) test.That(t, err, test.ShouldBeNil) - _, err = motor.WrapWithReconfigurable(nil) + _, err = motor.WrapWithReconfigurable(nil, resource.Name{}) test.That(t, err, test.ShouldBeError, motor.NewUnimplementedInterfaceError(nil)) - reconfMotor2, err := motor.WrapWithReconfigurable(reconfMotor1) + reconfMotor2, err := motor.WrapWithReconfigurable(reconfMotor1, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfMotor2, test.ShouldEqual, reconfMotor1) var actualMotor2 motor.LocalMotor = &mockLocal{Name: testMotorName} - reconfMotor3, err := motor.WrapWithReconfigurable(actualMotor2) + reconfMotor3, err := motor.WrapWithReconfigurable(actualMotor2, resource.Name{}) test.That(t, err, test.ShouldBeNil) - reconfMotor4, err := motor.WrapWithReconfigurable(reconfMotor3) + reconfMotor4, err := motor.WrapWithReconfigurable(reconfMotor3, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfMotor4, test.ShouldResemble, reconfMotor3) @@ -288,11 +288,11 @@ func TestWrapWithReconfigurable(t *testing.T) { func TestReconfigurableMotor(t *testing.T) { actualMotor1 := &mockLocal{Name: testMotorName} - reconfMotor1, err := motor.WrapWithReconfigurable(actualMotor1) + reconfMotor1, err := motor.WrapWithReconfigurable(actualMotor1, resource.Name{}) test.That(t, err, test.ShouldBeNil) actualMotor2 := &mockLocal{Name: testMotorName2} - reconfMotor2, err := motor.WrapWithReconfigurable(actualMotor2) + reconfMotor2, err := motor.WrapWithReconfigurable(actualMotor2, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, actualMotor1.reconfCount, test.ShouldEqual, 0) @@ -314,7 +314,7 @@ func TestReconfigurableMotor(t *testing.T) { test.That(t, err, test.ShouldBeError, rutils.NewUnexpectedTypeError(reconfMotor1, nil)) actualMotor3 := &mock{Name: failMotorName} - reconfMotor3, err := motor.WrapWithReconfigurable(actualMotor3) + reconfMotor3, err := motor.WrapWithReconfigurable(actualMotor3, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfMotor3, test.ShouldNotBeNil) @@ -328,7 +328,7 @@ func TestReconfigurableMotor(t *testing.T) { test.That(t, err, test.ShouldBeError, rutils.NewUnexpectedTypeError(reconfMotor3, reconfMotor1)) actualMotor4 := &mock{Name: testMotorName2} - reconfMotor4, err := motor.WrapWithReconfigurable(actualMotor4) + reconfMotor4, err := motor.WrapWithReconfigurable(actualMotor4, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfMotor4, test.ShouldNotBeNil) @@ -339,7 +339,7 @@ func TestReconfigurableMotor(t *testing.T) { func TestSetPower(t *testing.T) { actualMotor1 := &mock{Name: testMotorName} - reconfMotor1, err := motor.WrapWithReconfigurable(actualMotor1) + reconfMotor1, err := motor.WrapWithReconfigurable(actualMotor1, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, actualMotor1.powerCount, test.ShouldEqual, 0) @@ -350,7 +350,7 @@ func TestSetPower(t *testing.T) { func TestGoFor(t *testing.T) { actualMotor1 := &mock{Name: testMotorName} - reconfMotor1, err := motor.WrapWithReconfigurable(actualMotor1) + reconfMotor1, err := motor.WrapWithReconfigurable(actualMotor1, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, actualMotor1.goForCount, test.ShouldEqual, 0) @@ -361,7 +361,7 @@ func TestGoFor(t *testing.T) { func TestGoTo(t *testing.T) { actualMotor1 := &mock{Name: testMotorName} - reconfMotor1, err := motor.WrapWithReconfigurable(actualMotor1) + reconfMotor1, err := motor.WrapWithReconfigurable(actualMotor1, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, actualMotor1.goToCount, test.ShouldEqual, 0) @@ -372,7 +372,7 @@ func TestGoTo(t *testing.T) { func TestResetZeroPosition(t *testing.T) { actualMotor1 := &mock{Name: testMotorName} - reconfMotor1, err := motor.WrapWithReconfigurable(actualMotor1) + reconfMotor1, err := motor.WrapWithReconfigurable(actualMotor1, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, actualMotor1.zeroCount, test.ShouldEqual, 0) @@ -383,7 +383,7 @@ func TestResetZeroPosition(t *testing.T) { func TestPosition(t *testing.T) { actualMotor1 := &mock{Name: testMotorName} - reconfMotor1, err := motor.WrapWithReconfigurable(actualMotor1) + reconfMotor1, err := motor.WrapWithReconfigurable(actualMotor1, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, actualMotor1.posCount, test.ShouldEqual, 0) @@ -395,7 +395,7 @@ func TestPosition(t *testing.T) { func TestProperties(t *testing.T) { actualMotor1 := &mock{Name: testMotorName} - reconfMotor1, err := motor.WrapWithReconfigurable(actualMotor1) + reconfMotor1, err := motor.WrapWithReconfigurable(actualMotor1, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, actualMotor1.featuresCount, test.ShouldEqual, 0) @@ -407,7 +407,7 @@ func TestProperties(t *testing.T) { func TestStop(t *testing.T) { actualMotor1 := &mock{Name: testMotorName} - reconfMotor1, err := motor.WrapWithReconfigurable(actualMotor1) + reconfMotor1, err := motor.WrapWithReconfigurable(actualMotor1, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, actualMotor1.stopCount, test.ShouldEqual, 0) @@ -418,7 +418,7 @@ func TestStop(t *testing.T) { func TestIsPowered(t *testing.T) { actualMotor1 := &mock{Name: testMotorName} - reconfMotor1, err := motor.WrapWithReconfigurable(actualMotor1) + reconfMotor1, err := motor.WrapWithReconfigurable(actualMotor1, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, actualMotor1.poweredCount, test.ShouldEqual, 0) @@ -430,7 +430,7 @@ func TestIsPowered(t *testing.T) { func TestGoTillStop(t *testing.T) { actualMotor := &mockLocal{Name: testMotorName} - reconfMotor, err := motor.WrapWithReconfigurable(actualMotor) + reconfMotor, err := motor.WrapWithReconfigurable(actualMotor, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, actualMotor.goTillStopCount, test.ShouldEqual, 0) @@ -441,7 +441,7 @@ func TestGoTillStop(t *testing.T) { func TestClose(t *testing.T) { actualMotor1 := &mock{Name: testMotorName} - reconfMotor1, err := motor.WrapWithReconfigurable(actualMotor1) + reconfMotor1, err := motor.WrapWithReconfigurable(actualMotor1, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, actualMotor1.reconfCount, test.ShouldEqual, 0) @@ -451,7 +451,7 @@ func TestClose(t *testing.T) { func TestExtra(t *testing.T) { actualMotor1 := &mock{Name: testMotorName} - reconfMotor1, err := motor.WrapWithReconfigurable(actualMotor1) + reconfMotor1, err := motor.WrapWithReconfigurable(actualMotor1, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, actualMotor1.extra, test.ShouldEqual, nil) diff --git a/components/movementsensor/movementsensor.go b/components/movementsensor/movementsensor.go index e2d1761db78..7683fe46a2a 100644 --- a/components/movementsensor/movementsensor.go +++ b/components/movementsensor/movementsensor.go @@ -185,9 +185,14 @@ func Readings(ctx context.Context, g MovementSensor, extra map[string]interface{ type reconfigurableMovementSensor struct { mu sync.RWMutex + name resource.Name actual MovementSensor } +func (r *reconfigurableMovementSensor) Name() resource.Name { + return r.name +} + func (r *reconfigurableMovementSensor) Close(ctx context.Context) error { r.mu.RLock() defer r.mu.RUnlock() @@ -277,7 +282,7 @@ func (r *reconfigurableMovementSensor) reconfigure(ctx context.Context, newMovem // WrapWithReconfigurable - if MovementSensor is already a reconfigurableMovementSensor, then nothing is done. // Otherwise wraps in a Reconfigurable. -func WrapWithReconfigurable(r interface{}) (resource.Reconfigurable, error) { +func WrapWithReconfigurable(r interface{}, name resource.Name) (resource.Reconfigurable, error) { ms, ok := r.(MovementSensor) if !ok { return nil, NewUnimplementedInterfaceError(r) @@ -285,5 +290,5 @@ func WrapWithReconfigurable(r interface{}) (resource.Reconfigurable, error) { if reconfigurable, ok := ms.(*reconfigurableMovementSensor); ok { return reconfigurable, nil } - return &reconfigurableMovementSensor{actual: ms}, nil + return &reconfigurableMovementSensor{name: name, actual: ms}, nil } diff --git a/components/movementsensor/movementsensor_test.go b/components/movementsensor/movementsensor_test.go index 3dfd3173d81..6270bb45b00 100644 --- a/components/movementsensor/movementsensor_test.go +++ b/components/movementsensor/movementsensor_test.go @@ -154,24 +154,24 @@ func TestMovementSensorName(t *testing.T) { func TestWrapWithReconfigurable(t *testing.T) { var actualMovementSensor1 movementsensor.MovementSensor = &mock{Name: testMovementSensorName} - reconfMovementSensor1, err := movementsensor.WrapWithReconfigurable(actualMovementSensor1) + reconfMovementSensor1, err := movementsensor.WrapWithReconfigurable(actualMovementSensor1, resource.Name{}) test.That(t, err, test.ShouldBeNil) - _, err = movementsensor.WrapWithReconfigurable(nil) + _, err = movementsensor.WrapWithReconfigurable(nil, resource.Name{}) test.That(t, err, test.ShouldBeError, movementsensor.NewUnimplementedInterfaceError(nil)) - reconfMovementSensor2, err := movementsensor.WrapWithReconfigurable(reconfMovementSensor1) + reconfMovementSensor2, err := movementsensor.WrapWithReconfigurable(reconfMovementSensor1, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfMovementSensor2, test.ShouldEqual, reconfMovementSensor1) } func TestReconfigurableMovementSensor(t *testing.T) { actualMovementSensor1 := &mock{Name: testMovementSensorName} - reconfMovementSensor1, err := movementsensor.WrapWithReconfigurable(actualMovementSensor1) + reconfMovementSensor1, err := movementsensor.WrapWithReconfigurable(actualMovementSensor1, resource.Name{}) test.That(t, err, test.ShouldBeNil) actualMovementSensor2 := &mock{Name: testMovementSensorName2} - reconfMovementSensor2, err := movementsensor.WrapWithReconfigurable(actualMovementSensor2) + reconfMovementSensor2, err := movementsensor.WrapWithReconfigurable(actualMovementSensor2, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, actualMovementSensor1.reconfCount, test.ShouldEqual, 0) @@ -193,14 +193,14 @@ func TestReconfigurableMovementSensor(t *testing.T) { test.That(t, err, test.ShouldBeError, rutils.NewUnexpectedTypeError(reconfMovementSensor1, nil)) actualMovementSensor3 := &mock{Name: failMovementSensorName} - reconfMovementSensor3, err := movementsensor.WrapWithReconfigurable(actualMovementSensor3) + reconfMovementSensor3, err := movementsensor.WrapWithReconfigurable(actualMovementSensor3, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfMovementSensor3, test.ShouldNotBeNil) } func TestPosition(t *testing.T) { actualMovementSensor1 := &mock{Name: testMovementSensorName} - reconfMovementSensor1, _ := movementsensor.WrapWithReconfigurable(actualMovementSensor1) + reconfMovementSensor1, _ := movementsensor.WrapWithReconfigurable(actualMovementSensor1, resource.Name{}) test.That(t, actualMovementSensor1.positionCount, test.ShouldEqual, 0) loc1, _, err := reconfMovementSensor1.(movementsensor.MovementSensor).Position(context.Background(), make(map[string]interface{})) @@ -211,7 +211,7 @@ func TestPosition(t *testing.T) { func TestLinearVelocity(t *testing.T) { actualMovementSensor1 := &mock{Name: testMovementSensorName} - reconfMovementSensor1, _ := movementsensor.WrapWithReconfigurable(actualMovementSensor1) + reconfMovementSensor1, _ := movementsensor.WrapWithReconfigurable(actualMovementSensor1, resource.Name{}) test.That(t, actualMovementSensor1.velocityCount, test.ShouldEqual, 0) speed1, err := reconfMovementSensor1.(movementsensor.MovementSensor).LinearVelocity(context.Background(), make(map[string]interface{})) @@ -222,7 +222,7 @@ func TestLinearVelocity(t *testing.T) { func TestReadings(t *testing.T) { actualMovementSensor1 := &mock{Name: testMovementSensorName} - reconfMovementSensor1, _ := movementsensor.WrapWithReconfigurable(actualMovementSensor1) + reconfMovementSensor1, _ := movementsensor.WrapWithReconfigurable(actualMovementSensor1, resource.Name{}) readings1, err := movementsensor.Readings(context.Background(), actualMovementSensor1, make(map[string]interface{})) allReadings := map[string]interface{}{ @@ -249,7 +249,7 @@ func TestReadings(t *testing.T) { func TestClose(t *testing.T) { actualMovementSensor1 := &mock{Name: testMovementSensorName} - reconfMovementSensor1, _ := movementsensor.WrapWithReconfigurable(actualMovementSensor1) + reconfMovementSensor1, _ := movementsensor.WrapWithReconfigurable(actualMovementSensor1, resource.Name{}) test.That(t, actualMovementSensor1.reconfCount, test.ShouldEqual, 0) test.That(t, utils.TryClose(context.Background(), reconfMovementSensor1), test.ShouldBeNil) diff --git a/components/posetracker/pose_tracker.go b/components/posetracker/pose_tracker.go index 3533ba983cc..22916b5d84c 100644 --- a/components/posetracker/pose_tracker.go +++ b/components/posetracker/pose_tracker.go @@ -107,9 +107,14 @@ func Readings(ctx context.Context, poseTracker PoseTracker) (map[string]interfac type reconfigurablePoseTracker struct { mu sync.RWMutex + name resource.Name actual PoseTracker } +func (r *reconfigurablePoseTracker) Name() resource.Name { + return r.name +} + func (r *reconfigurablePoseTracker) ProxyFor() interface{} { r.mu.RLock() defer r.mu.RUnlock() @@ -162,7 +167,7 @@ func (r *reconfigurablePoseTracker) Reconfigure( // WrapWithReconfigurable converts a regular PoseTracker implementation to a reconfigurablePoseTracker. // If pose tracker is already a reconfigurablePoseTracker, then nothing is done. -func WrapWithReconfigurable(r interface{}) (resource.Reconfigurable, error) { +func WrapWithReconfigurable(r interface{}, name resource.Name) (resource.Reconfigurable, error) { poseTracker, ok := r.(PoseTracker) if !ok { return nil, NewUnimplementedInterfaceError(r) @@ -170,5 +175,5 @@ func WrapWithReconfigurable(r interface{}) (resource.Reconfigurable, error) { if reconfigurable, ok := poseTracker.(*reconfigurablePoseTracker); ok { return reconfigurable, nil } - return &reconfigurablePoseTracker{actual: poseTracker}, nil + return &reconfigurablePoseTracker{name: name, actual: poseTracker}, nil } diff --git a/components/posetracker/pose_tracker_test.go b/components/posetracker/pose_tracker_test.go index d6bf509efd9..c3690ce8965 100644 --- a/components/posetracker/pose_tracker_test.go +++ b/components/posetracker/pose_tracker_test.go @@ -124,11 +124,11 @@ func TestPoseTrackerName(t *testing.T) { func TestWrapWithReconfigurable(t *testing.T) { poseTracker := &inject.PoseTracker{} - reconfPT, err := posetracker.WrapWithReconfigurable(poseTracker) + reconfPT, err := posetracker.WrapWithReconfigurable(poseTracker, resource.Name{}) test.That(t, err, test.ShouldBeNil) - _, err = posetracker.WrapWithReconfigurable(nil) + _, err = posetracker.WrapWithReconfigurable(nil, resource.Name{}) test.That(t, err, test.ShouldBeError, posetracker.NewUnimplementedInterfaceError(nil)) - reconfPT2, err := posetracker.WrapWithReconfigurable(reconfPT) + reconfPT2, err := posetracker.WrapWithReconfigurable(reconfPT, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfPT2, test.ShouldEqual, reconfPT) } @@ -163,11 +163,11 @@ func (m *mock) DoCommand(ctx context.Context, cmd map[string]interface{}) (map[s func TestReconfigurablePoseTracker(t *testing.T) { actualPT1 := &mock{Name: workingPTName} - reconfPT1, err := posetracker.WrapWithReconfigurable(actualPT1) + reconfPT1, err := posetracker.WrapWithReconfigurable(actualPT1, resource.Name{}) test.That(t, err, test.ShouldBeNil) actualPT2 := &mock{Name: "differentPT"} - reconfPT2, err := posetracker.WrapWithReconfigurable(actualPT2) + reconfPT2, err := posetracker.WrapWithReconfigurable(actualPT2, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, actualPT1.reconfCount, test.ShouldEqual, 0) @@ -193,7 +193,7 @@ func TestReconfigurablePoseTracker(t *testing.T) { func TestReadings(t *testing.T) { actualPT1 := &mock{Name: workingPTName} - reconfPT1, err := posetracker.WrapWithReconfigurable(actualPT1) + reconfPT1, err := posetracker.WrapWithReconfigurable(actualPT1, resource.Name{}) test.That(t, err, test.ShouldBeNil) sensorPT1, isSensor := reconfPT1.(sensor.Sensor) test.That(t, isSensor, test.ShouldBeTrue) @@ -210,7 +210,7 @@ func TestReadings(t *testing.T) { func TestClose(t *testing.T) { actualPT := &mock{Name: workingPTName} - reconfPT, err := posetracker.WrapWithReconfigurable(actualPT) + reconfPT, err := posetracker.WrapWithReconfigurable(actualPT, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, actualPT.reconfCount, test.ShouldEqual, 0) @@ -220,7 +220,7 @@ func TestClose(t *testing.T) { func TestExtraOptions(t *testing.T) { actualPT := &mock{Name: workingPTName} - reconfPT, err := posetracker.WrapWithReconfigurable(actualPT) + reconfPT, err := posetracker.WrapWithReconfigurable(actualPT, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, actualPT.extra, test.ShouldEqual, nil) diff --git a/components/sensor/sensor.go b/components/sensor/sensor.go index bcde6d698f6..c4e8ae32f46 100644 --- a/components/sensor/sensor.go +++ b/components/sensor/sensor.go @@ -90,9 +90,14 @@ func NamesFromRobot(r robot.Robot) []string { type reconfigurableSensor struct { mu sync.RWMutex + name resource.Name actual Sensor } +func (r *reconfigurableSensor) Name() resource.Name { + return r.name +} + func (r *reconfigurableSensor) Close(ctx context.Context) error { r.mu.RLock() defer r.mu.RUnlock() @@ -134,7 +139,7 @@ func (r *reconfigurableSensor) Reconfigure(ctx context.Context, newSensor resour // WrapWithReconfigurable converts a regular Sensor implementation to a reconfigurableSensor. // If Sensor is already a reconfigurableSensor, then nothing is done. -func WrapWithReconfigurable(r interface{}) (resource.Reconfigurable, error) { +func WrapWithReconfigurable(r interface{}, name resource.Name) (resource.Reconfigurable, error) { Sensor, ok := r.(Sensor) if !ok { return nil, NewUnimplementedInterfaceError(r) @@ -142,5 +147,5 @@ func WrapWithReconfigurable(r interface{}) (resource.Reconfigurable, error) { if reconfigurable, ok := Sensor.(*reconfigurableSensor); ok { return reconfigurable, nil } - return &reconfigurableSensor{actual: Sensor}, nil + return &reconfigurableSensor{name: name, actual: Sensor}, nil } diff --git a/components/sensor/sensor_test.go b/components/sensor/sensor_test.go index 5a1421dfc25..03fd176f9a9 100644 --- a/components/sensor/sensor_test.go +++ b/components/sensor/sensor_test.go @@ -119,24 +119,24 @@ func TestSensorName(t *testing.T) { func TestWrapWithReconfigurable(t *testing.T) { actualSensor1 := &mock{Name: testSensorName} - reconfSensor1, err := sensor.WrapWithReconfigurable(actualSensor1) + reconfSensor1, err := sensor.WrapWithReconfigurable(actualSensor1, resource.Name{}) test.That(t, err, test.ShouldBeNil) - _, err = sensor.WrapWithReconfigurable(nil) + _, err = sensor.WrapWithReconfigurable(nil, resource.Name{}) test.That(t, err, test.ShouldBeError, sensor.NewUnimplementedInterfaceError(nil)) - reconfSensor2, err := sensor.WrapWithReconfigurable(reconfSensor1) + reconfSensor2, err := sensor.WrapWithReconfigurable(reconfSensor1, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfSensor2, test.ShouldEqual, reconfSensor1) } func TestReconfigurableSensor(t *testing.T) { actualSensor1 := &mock{Name: testSensorName} - reconfSensor1, err := sensor.WrapWithReconfigurable(actualSensor1) + reconfSensor1, err := sensor.WrapWithReconfigurable(actualSensor1, resource.Name{}) test.That(t, err, test.ShouldBeNil) actualSensor2 := &mock{Name: testSensorName2} - reconfSensor2, err := sensor.WrapWithReconfigurable(actualSensor2) + reconfSensor2, err := sensor.WrapWithReconfigurable(actualSensor2, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, actualSensor1.reconfCount, test.ShouldEqual, 0) @@ -160,7 +160,7 @@ func TestReconfigurableSensor(t *testing.T) { func TestReadings(t *testing.T) { actualSensor1 := &mock{Name: testSensorName} - reconfSensor1, _ := sensor.WrapWithReconfigurable(actualSensor1) + reconfSensor1, _ := sensor.WrapWithReconfigurable(actualSensor1, resource.Name{}) test.That(t, actualSensor1.readingsCount, test.ShouldEqual, 0) result, err := reconfSensor1.(sensor.Sensor).Readings(context.Background(), make(map[string]interface{})) @@ -171,7 +171,7 @@ func TestReadings(t *testing.T) { func TestReadingsWithExtraParams(t *testing.T) { actualSensor1 := &mock{Name: testSensorName} - reconfSensor1, _ := sensor.WrapWithReconfigurable(actualSensor1) + reconfSensor1, _ := sensor.WrapWithReconfigurable(actualSensor1, resource.Name{}) test.That(t, actualSensor1.readingsCount, test.ShouldEqual, 0) result, err := reconfSensor1.(sensor.Sensor).Readings(context.Background(), map[string]interface{}{"foo": "bar"}) @@ -183,7 +183,7 @@ func TestReadingsWithExtraParams(t *testing.T) { func TestClose(t *testing.T) { actualSensor1 := &mock{Name: testSensorName} - reconfSensor1, _ := sensor.WrapWithReconfigurable(actualSensor1) + reconfSensor1, _ := sensor.WrapWithReconfigurable(actualSensor1, resource.Name{}) test.That(t, actualSensor1.reconfCount, test.ShouldEqual, 0) test.That(t, utils.TryClose(context.Background(), reconfSensor1), test.ShouldBeNil) diff --git a/components/servo/servo.go b/components/servo/servo.go index 962868ae891..477211df271 100644 --- a/components/servo/servo.go +++ b/components/servo/servo.go @@ -134,9 +134,14 @@ func CreateStatus(ctx context.Context, resource interface{}) (*pb.Status, error) type reconfigurableServo struct { mu sync.RWMutex + name resource.Name actual Servo } +func (r *reconfigurableServo) Name() resource.Name { + return r.name +} + func (r *reconfigurableServo) ProxyFor() interface{} { r.mu.RLock() defer r.mu.RUnlock() @@ -220,7 +225,7 @@ func (r *reconfigurableLocalServo) Reconfigure(ctx context.Context, newServo res // WrapWithReconfigurable converts a regular Servo implementation to a reconfigurableServo. // If servo is already a reconfigurableServo, then nothing is done. -func WrapWithReconfigurable(r interface{}) (resource.Reconfigurable, error) { +func WrapWithReconfigurable(r interface{}, name resource.Name) (resource.Reconfigurable, error) { servo, ok := r.(Servo) if !ok { return nil, NewUnimplementedInterfaceError(r) @@ -228,7 +233,7 @@ func WrapWithReconfigurable(r interface{}) (resource.Reconfigurable, error) { if reconfigurable, ok := servo.(*reconfigurableServo); ok { return reconfigurable, nil } - rServo := &reconfigurableServo{actual: servo} + rServo := &reconfigurableServo{name: name, actual: servo} gLocal, ok := r.(LocalServo) if !ok { return rServo, nil diff --git a/components/servo/servo_test.go b/components/servo/servo_test.go index 38aafcec2d1..320fd517a4a 100644 --- a/components/servo/servo_test.go +++ b/components/servo/servo_test.go @@ -184,21 +184,21 @@ func TestServoName(t *testing.T) { func TestWrapWithReconfigurable(t *testing.T) { var actualServo1 servo.Servo = &mock{Name: testServoName} - reconfServo1, err := servo.WrapWithReconfigurable(actualServo1) + reconfServo1, err := servo.WrapWithReconfigurable(actualServo1, resource.Name{}) test.That(t, err, test.ShouldBeNil) - _, err = servo.WrapWithReconfigurable(nil) + _, err = servo.WrapWithReconfigurable(nil, resource.Name{}) test.That(t, err, test.ShouldBeError, servo.NewUnimplementedInterfaceError(nil)) - reconfServo2, err := servo.WrapWithReconfigurable(reconfServo1) + reconfServo2, err := servo.WrapWithReconfigurable(reconfServo1, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfServo2, test.ShouldEqual, reconfServo1) var actualServo2 servo.LocalServo = &mockLocal{Name: testServoName} - reconfServo3, err := servo.WrapWithReconfigurable(actualServo2) + reconfServo3, err := servo.WrapWithReconfigurable(actualServo2, resource.Name{}) test.That(t, err, test.ShouldBeNil) - reconfServo4, err := servo.WrapWithReconfigurable(reconfServo3) + reconfServo4, err := servo.WrapWithReconfigurable(reconfServo3, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfServo4, test.ShouldResemble, reconfServo3) @@ -208,11 +208,11 @@ func TestWrapWithReconfigurable(t *testing.T) { func TestReconfigurableServo(t *testing.T) { actualServo1 := &mockLocal{Name: testServoName} - reconfServo1, err := servo.WrapWithReconfigurable(actualServo1) + reconfServo1, err := servo.WrapWithReconfigurable(actualServo1, resource.Name{}) test.That(t, err, test.ShouldBeNil) actualServo2 := &mockLocal{Name: testServoName2} - reconfServo2, err := servo.WrapWithReconfigurable(actualServo2) + reconfServo2, err := servo.WrapWithReconfigurable(actualServo2, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, actualServo1.reconfCount, test.ShouldEqual, 0) @@ -234,7 +234,7 @@ func TestReconfigurableServo(t *testing.T) { test.That(t, err, test.ShouldBeError, rutils.NewUnexpectedTypeError(reconfServo1, nil)) actualServo3 := &mock{Name: failServoName} - reconfServo3, err := servo.WrapWithReconfigurable(actualServo3) + reconfServo3, err := servo.WrapWithReconfigurable(actualServo3, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfServo3, test.ShouldNotBeNil) @@ -248,7 +248,7 @@ func TestReconfigurableServo(t *testing.T) { test.That(t, err, test.ShouldBeError, rutils.NewUnexpectedTypeError(reconfServo3, reconfServo1)) actualServo4 := &mock{Name: testServoName2} - reconfServo4, err := servo.WrapWithReconfigurable(actualServo4) + reconfServo4, err := servo.WrapWithReconfigurable(actualServo4, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfServo4, test.ShouldNotBeNil) @@ -259,7 +259,7 @@ func TestReconfigurableServo(t *testing.T) { func TestStop(t *testing.T) { actualServo1 := &mockLocal{Name: testServoName} - reconfServo1, err := servo.WrapWithReconfigurable(actualServo1) + reconfServo1, err := servo.WrapWithReconfigurable(actualServo1, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, actualServo1.stopCount, test.ShouldEqual, 0) @@ -269,7 +269,7 @@ func TestStop(t *testing.T) { func TestClose(t *testing.T) { actualServo1 := &mockLocal{Name: testServoName} - reconfServo1, err := servo.WrapWithReconfigurable(actualServo1) + reconfServo1, err := servo.WrapWithReconfigurable(actualServo1, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, actualServo1.reconfCount, test.ShouldEqual, 0) @@ -279,7 +279,7 @@ func TestClose(t *testing.T) { func TestExtra(t *testing.T) { actualServo1 := &mockLocal{Name: testServoName} - reconfServo1, err := servo.WrapWithReconfigurable((actualServo1)) + reconfServo1, err := servo.WrapWithReconfigurable((actualServo1), resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, actualServo1.extra, test.ShouldBeNil) diff --git a/examples/mycomponent/component/mycomponent.go b/examples/mycomponent/component/mycomponent.go index cb4e1b8f5fd..88da1398884 100644 --- a/examples/mycomponent/component/mycomponent.go +++ b/examples/mycomponent/component/mycomponent.go @@ -119,7 +119,7 @@ func NewUnimplementedInterfaceError(actual interface{}) error { return utils.NewUnimplementedInterfaceError((MyComponent)(nil), actual) } -func wrapWithReconfigurable(r interface{}) (resource.Reconfigurable, error) { +func wrapWithReconfigurable(r interface{}, name resource.Name) (resource.Reconfigurable, error) { mc, ok := r.(MyComponent) if !ok { return nil, NewUnimplementedInterfaceError(r) @@ -127,7 +127,7 @@ func wrapWithReconfigurable(r interface{}) (resource.Reconfigurable, error) { if reconfigurable, ok := mc.(*reconfigurableMyComponent); ok { return reconfigurable, nil } - return &reconfigurableMyComponent{actual: mc}, nil + return &reconfigurableMyComponent{name: name, actual: mc}, nil } var ( @@ -137,9 +137,14 @@ var ( type reconfigurableMyComponent struct { mu sync.RWMutex + name resource.Name actual MyComponent } +func (mc *reconfigurableMyComponent) Name() resource.Name { + return mc.name +} + func (mc *reconfigurableMyComponent) ProxyFor() interface{} { mc.mu.RLock() defer mc.mu.RUnlock() diff --git a/go.sum b/go.sum index faceee2f2fa..b1e35851c2d 100644 --- a/go.sum +++ b/go.sum @@ -676,8 +676,6 @@ github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t github.com/grpc-ecosystem/grpc-gateway v1.9.5/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY= github.com/grpc-ecosystem/grpc-gateway v1.12.1/go.mod h1:8XEsbTttt/W+VvjtQhLACqCisSPWTxCZ7sBRjU6iH9c= github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= -github.com/grpc-ecosystem/grpc-gateway/v2 v2.11.2 h1:BqHID5W5qnMkug0Z8UmL8tN0gAy4jQ+B4WFt8cCgluU= -github.com/grpc-ecosystem/grpc-gateway/v2 v2.11.2/go.mod h1:ZbS3MZTZq/apAfAEHGoB5HbsQQstoqP92SjAqtQ9zeg= github.com/grpc-ecosystem/grpc-gateway/v2 v2.11.3 h1:lLT7ZLSzGLI08vc9cpd+tYmNWjdKDqyr/2L+f6U12Fk= github.com/grpc-ecosystem/grpc-gateway/v2 v2.11.3/go.mod h1:o//XUCC/F+yRGJoPO/VU0GSB0f8Nhgmxx0VIRUvaC0w= github.com/guptarohit/asciigraph v0.5.1 h1:rzRUdibSt3ff75gVGtcUXQ0dEkNgG0A20fXkA8cOMsA= @@ -1576,7 +1574,6 @@ golang.org/x/image v0.0.0-20201208152932-35266b937fa6/go.mod h1:FeLwcggjj3mMvU+o golang.org/x/image v0.0.0-20210607152325-775e3b0c77b9/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM= golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM= golang.org/x/image v0.0.0-20211028202545-6944b10bf410/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM= -golang.org/x/image v0.0.0-20220722155232-062f8c9fd539 h1:/eM0PCrQI2xd471rI+snWuu251/+/jpBpZqir2mPdnU= golang.org/x/image v0.0.0-20220722155232-062f8c9fd539/go.mod h1:doUCurBvlfPMKfmIpRIywoHmhN3VyhnoFDbvIEWF4hY= golang.org/x/image v0.0.0-20220902085622-e7cb96979f69 h1:Lj6HJGCSn5AjxRAH2+r35Mir4icalbqku+CLUtjnvXY= golang.org/x/image v0.0.0-20220902085622-e7cb96979f69/go.mod h1:doUCurBvlfPMKfmIpRIywoHmhN3VyhnoFDbvIEWF4hY= @@ -1945,8 +1942,6 @@ golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2/go.mod h1:K8+ghG5WaK9qNq gonum.org/v1/gonum v0.0.0-20180816165407-929014505bf4/go.mod h1:Y+Yx5eoAFn32cQvJDxZx5Dpnq+c3wtXuadVZAcxbbBo= gonum.org/v1/gonum v0.7.0/go.mod h1:L02bwd0sqlsvRv41G7wGWFCsVNZFv/k1xzGIxeANHGM= gonum.org/v1/gonum v0.8.1/go.mod h1:oe/vMfY3deqTw+1EZJhuvEW2iwGF1bW9wwu7XCu0+v0= -gonum.org/v1/gonum v0.11.0 h1:f1IJhK4Km5tBJmaiJXtk/PkL4cdVX6J+tGiM187uT5E= -gonum.org/v1/gonum v0.11.0/go.mod h1:fSG4YDCxxUZQJ7rKsQrj0gMOg00Il0Z96/qMA4bVQhA= gonum.org/v1/gonum v0.12.0 h1:xKuo6hzt+gMav00meVPUlXwSdoEJP46BR+wdxQEFK2o= gonum.org/v1/gonum v0.12.0/go.mod h1:73TDxJfAAHeA8Mk9mf8NlIppyhQNo5GLTcYeqgo2lvY= gonum.org/v1/netlib v0.0.0-20190313105609-8cb42192e0e0/go.mod h1:wa6Ws7BG/ESfp6dHfk7C6KdzKA7wR7u/rKwOGE66zvw= diff --git a/grpc/resource.go b/grpc/resource.go index 104a9333bed..67084dc3359 100644 --- a/grpc/resource.go +++ b/grpc/resource.go @@ -3,10 +3,15 @@ package grpc import ( "github.com/jhump/protoreflect/dynamic/grpcdynamic" "go.viam.com/utils/rpc" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" "go.viam.com/rdk/resource" ) +// UnimplementedError is returned any time a gRPC method is unimplemented. +var UnimplementedError = status.Error(codes.Unimplemented, codes.Unimplemented.String()) + // An ForeignResource is used to dynamically invoke RPC calls to resources that have their // RPC information dervied on demand. type ForeignResource struct { diff --git a/registry/registry.go b/registry/registry.go index 7b3ec190762..be966bfa8d1 100644 --- a/registry/registry.go +++ b/registry/registry.go @@ -89,7 +89,7 @@ type ( CreateComponent func(ctx context.Context, deps Dependencies, config config.Component, logger golog.Logger) (interface{}, error) // A CreateReconfigurable makes a reconfigurable resource from a given resource. - CreateReconfigurable func(resource interface{}) (resource.Reconfigurable, error) + CreateReconfigurable func(resource interface{}, name resource.Name) (resource.Reconfigurable, error) // CreateStatus creates a status from a given resource. The return type is expected to be comprised of string keys // (or it should be possible to decompose it into string keys) and values comprised of primitives, list of primitives, diff --git a/registry/registry_test.go b/registry/registry_test.go index c8296578136..93fb30bbb38 100644 --- a/registry/registry_test.go +++ b/registry/registry_test.go @@ -38,7 +38,7 @@ func TestComponentRegistry(t *testing.T) { } func TestResourceSubtypeRegistry(t *testing.T) { - rf := func(r interface{}) (resource.Reconfigurable, error) { + rf := func(r interface{}, n resource.Name) (resource.Reconfigurable, error) { return nil, nil } statf := func(context.Context, interface{}) (interface{}, error) { diff --git a/resource/resource.go b/resource/resource.go index 0ff22d0b5ed..f527dc41d40 100644 --- a/resource/resource.go +++ b/resource/resource.go @@ -291,6 +291,9 @@ func ReconfigureResource(ctx context.Context, old, newR interface{}) (interface{ // Reconfigurable is implemented when component/service of a robot is reconfigurable. type Reconfigurable interface { + // TODO(RSDK-895): hold over until all resources have names. This doesn't guarantee + // everything is named since everything may not be a reconfigurable (but should be). + Name() Name // Reconfigure reconfigures the resource Reconfigure(ctx context.Context, newResource Reconfigurable) error } @@ -319,3 +322,19 @@ type OldStoppable interface { // Stop stops all movement for the resource Stop(context.Context) error } + +// StopResource attempts to stops the given resource. +func StopResource(ctx context.Context, res interface{}, extra map[string]interface{}) error { + sr, ok := res.(Stoppable) + if ok { + return sr.Stop(ctx, extra) + } + + // TODO[njooma]: OldStoppable - Will be deprecated + osr, ok := res.(OldStoppable) + if ok { + return osr.Stop(ctx) + } + + return nil +} diff --git a/robot/client/client.go b/robot/client/client.go index 0cb5624f694..33cb4dda8b0 100644 --- a/robot/client/client.go +++ b/robot/client/client.go @@ -53,6 +53,7 @@ var ( // RobotClient satisfies the robot.Robot interface through a gRPC based // client conforming to the robot.proto contract. type RobotClient struct { + remoteName string address string conn rpc.ClientConn client pb.RobotServiceClient @@ -182,6 +183,7 @@ func New(ctx context.Context, address string, logger golog.Logger, opts ...Robot closeCtx, cancel := context.WithCancel(ctx) rc := &RobotClient{ + remoteName: rOpts.remoteName, address: address, cancelBackgroundWorkers: cancel, mu: &sync.RWMutex{}, @@ -523,7 +525,7 @@ func (rc *RobotClient) createClient(name resource.Name) (interface{}, error) { if c.Reconfigurable == nil { return resourceClient, nil } - return c.Reconfigurable(resourceClient) + return c.Reconfigurable(resourceClient, name.PrependRemote(resource.RemoteName(rc.remoteName))) } func (rc *RobotClient) resources(ctx context.Context) ([]resource.Name, []resource.RPCSubtype, error) { diff --git a/robot/client/client_options.go b/robot/client/client_options.go index 97814808d0a..ab29a316bc1 100644 --- a/robot/client/client_options.go +++ b/robot/client/client_options.go @@ -26,6 +26,9 @@ type robotClientOpts struct { // dialOptions are options using for clients dialing gRPC servers. dialOptions []rpc.DialOption + + // the name of the robot. + remoteName string } // RobotClientOption configures how we set up the connection. @@ -72,6 +75,13 @@ func WithReconnectEvery(reconnectEvery time.Duration) RobotClientOption { }) } +// WithRemoteName returns a RobotClientOption setting the name of the remote robot. +func WithRemoteName(remoteName string) RobotClientOption { + return newFuncRobotClientOption(func(o *robotClientOpts) { + o.remoteName = remoteName + }) +} + // WithDialOptions returns a RobotClientOption which sets the options for making // gRPC connections to other servers. func WithDialOptions(opts ...rpc.DialOption) RobotClientOption { diff --git a/robot/client/client_test.go b/robot/client/client_test.go index 96683f7eed8..46b3331fc92 100644 --- a/robot/client/client_test.go +++ b/robot/client/client_test.go @@ -887,6 +887,10 @@ type mockType struct { reconfCount int64 } +func (mt *mockType) Name() resource.Name { + return resource.Name{} +} + func (mt *mockType) Reconfigure(ctx context.Context, newRes resource.Reconfigurable) error { atomic.AddInt64(&mt.reconfCount, 1) return nil diff --git a/robot/impl/discovery_test.go b/robot/impl/discovery_test.go index f494e2aaf1a..11970961e57 100644 --- a/robot/impl/discovery_test.go +++ b/robot/impl/discovery_test.go @@ -46,7 +46,7 @@ var ( workingDiscovery = map[string]interface{}{"position": "up"} errFailed = errors.New("can't get discovery") - mockReconfigurable = func(resource interface{}) (resource.Reconfigurable, error) { + mockReconfigurable = func(resource interface{}, name resource.Name) (resource.Reconfigurable, error) { return nil, nil } ) diff --git a/robot/impl/local_robot.go b/robot/impl/local_robot.go index ff968736719..3bf2099ed82 100644 --- a/robot/impl/local_robot.go +++ b/robot/impl/local_robot.go @@ -167,21 +167,8 @@ func (r *localRobot) StopAll(ctx context.Context, extra map[resource.Name]map[st continue } - sr, ok := res.(resource.Stoppable) - if ok { - err = sr.Stop(ctx, extra[name]) - if err != nil { - resourceErrs = append(resourceErrs, name.Name) - } - } - - // TODO[njooma]: OldStoppable - Will be deprecated - osr, ok := res.(resource.OldStoppable) - if ok { - err = osr.Stop(ctx) - if err != nil { - resourceErrs = append(resourceErrs, name.Name) - } + if err := resource.StopResource(ctx, res, extra[name]); err != nil { + resourceErrs = append(resourceErrs, name.Name) } } @@ -527,7 +514,7 @@ func (r *localRobot) newService(ctx context.Context, config config.Service) (int if c == nil || c.Reconfigurable == nil { return svc, nil } - return c.Reconfigurable(svc) + return c.Reconfigurable(svc, rName) } // getDependencies derives a collection of dependencies from a robot for a given @@ -574,7 +561,7 @@ func (r *localRobot) newResource(ctx context.Context, config config.Component) ( if c == nil || c.Reconfigurable == nil { return newResource, nil } - wrapped, err := c.Reconfigurable(newResource) + wrapped, err := c.Reconfigurable(newResource, rName) if err != nil { return nil, multierr.Combine(err, goutils.TryClose(ctx, newResource)) } @@ -710,12 +697,13 @@ func (r *localRobot) DiscoverComponents(ctx context.Context, qs []discovery.Quer return discoveries, nil } -func dialRobotClient(ctx context.Context, +func dialRobotClient( + ctx context.Context, config config.Remote, logger golog.Logger, dialOpts ...rpc.DialOption, ) (*client.RobotClient, error) { - rOpts := []client.RobotClientOption{client.WithDialOptions(dialOpts...)} + rOpts := []client.RobotClientOption{client.WithDialOptions(dialOpts...), client.WithRemoteName(config.Name)} if config.ConnectionCheckInterval != 0 { rOpts = append(rOpts, client.WithCheckConnectedEvery(config.ConnectionCheckInterval)) diff --git a/robot/impl/resource_manager_test.go b/robot/impl/resource_manager_test.go index 85d121e529f..26195b002b7 100644 --- a/robot/impl/resource_manager_test.go +++ b/robot/impl/resource_manager_test.go @@ -1642,7 +1642,12 @@ func TestManagerEmptyResourceDesc(t *testing.T) { subtype := resource.NewSubtype(resource.ResourceNamespaceRDK, resource.ResourceTypeComponent, "mockDesc") registry.RegisterResourceSubtype( subtype, - registry.ResourceSubtype{Reconfigurable: func(resource interface{}) (resource.Reconfigurable, error) { return nil, nil }}, + registry.ResourceSubtype{Reconfigurable: func( + resource interface{}, + name resource.Name, + ) (resource.Reconfigurable, error) { + return nil, nil + }}, ) injectRobot.ResourceNamesFunc = func() []resource.Name { @@ -1718,17 +1723,23 @@ func TestUpdateConfig(t *testing.T) { var _ = resource.Reconfigurable(&mock{}) -func WrapWithReconfigurable(s interface{}) (resource.Reconfigurable, error) { +func WrapWithReconfigurable(s interface{}, name resource.Name) (resource.Reconfigurable, error) { sMock, _ := s.(*mock) sMock.wrap++ + sMock.name = name return sMock, nil } type mock struct { + name resource.Name wrap int reconfigCount int } +func (m *mock) Name() resource.Name { + return m.name +} + func (m *mock) Reconfigure(ctx context.Context, newSvc resource.Reconfigurable) error { m.reconfigCount++ return nil diff --git a/robot/impl/robot_reconfigure_test.go b/robot/impl/robot_reconfigure_test.go index 38448a74812..863270777b6 100644 --- a/robot/impl/robot_reconfigure_test.go +++ b/robot/impl/robot_reconfigure_test.go @@ -77,7 +77,7 @@ func TestRobotReconfigure(t *testing.T) { if config.ConvertedAttributes.(*mockFakeConfig).ShouldFail { return nil, errors.Errorf("cannot build %q for some obscure reason", config.Name) } - return &mockFake{x: 5}, nil + return &mockFake{name: config.ResourceName(), x: 5}, nil }, }) @@ -98,7 +98,7 @@ func TestRobotReconfigure(t *testing.T) { Constructor: func(ctx context.Context, deps registry.Dependencies, config config.Component, logger golog.Logger) (interface{}, error) { if reconfigurableTrue && testReconfiguringMismatch { reconfigurableTrue = false - return &mockFake{x: 5}, nil + return &mockFake{name: config.ResourceName(), x: 5}, nil } return &mockFake2{x: 5}, nil }, @@ -2632,6 +2632,7 @@ func TestRemoteRobotsGold(t *testing.T) { } type mockFake struct { + name resource.Name x int reconfCount int } @@ -2642,6 +2643,10 @@ type mockFakeConfig struct { Blah int `json:"blah"` } +func (m *mockFake) Name() resource.Name { + return m.name +} + func (m *mockFake) Reconfigure(ctx context.Context, newResource resource.Reconfigurable) error { res, ok := newResource.(*mockFake) if !ok { diff --git a/services/armremotecontrol/arm_remote_control.go b/services/armremotecontrol/arm_remote_control.go index cf410ed08ef..a9e5db3b730 100644 --- a/services/armremotecontrol/arm_remote_control.go +++ b/services/armremotecontrol/arm_remote_control.go @@ -43,9 +43,14 @@ var _ = resource.Reconfigurable(&reconfigurableArmRemoteControl{}) type reconfigurableArmRemoteControl struct { mu sync.RWMutex + name resource.Name actual Service } +func (svc *reconfigurableArmRemoteControl) Name() resource.Name { + return svc.name +} + func (svc *reconfigurableArmRemoteControl) Close(ctx context.Context) error { svc.mu.RLock() defer svc.mu.RUnlock() @@ -67,7 +72,7 @@ func (svc *reconfigurableArmRemoteControl) Reconfigure(ctx context.Context, newS } // WrapWithReconfigurable wraps a ArmRemoteControl as a Reconfigurable. -func WrapWithReconfigurable(s interface{}) (resource.Reconfigurable, error) { +func WrapWithReconfigurable(s interface{}, name resource.Name) (resource.Reconfigurable, error) { if reconfigurable, ok := s.(*reconfigurableArmRemoteControl); ok { return reconfigurable, nil } @@ -77,5 +82,5 @@ func WrapWithReconfigurable(s interface{}) (resource.Reconfigurable, error) { return nil, rdkutils.NewUnimplementedInterfaceError("armremotecontrol.Service", s) } - return &reconfigurableArmRemoteControl{actual: svc}, nil + return &reconfigurableArmRemoteControl{name: name, actual: svc}, nil } diff --git a/services/armremotecontrol/arm_remote_control_test.go b/services/armremotecontrol/arm_remote_control_test.go index d891278fcdb..359d47a2924 100644 --- a/services/armremotecontrol/arm_remote_control_test.go +++ b/services/armremotecontrol/arm_remote_control_test.go @@ -7,6 +7,7 @@ import ( "go.viam.com/test" "go.viam.com/rdk/registry" + "go.viam.com/rdk/resource" "go.viam.com/rdk/services/armremotecontrol" rdkutils "go.viam.com/rdk/utils" ) @@ -20,25 +21,25 @@ func TestRegisteredReconfigurable(t *testing.T) { func TestWrapWithReconfigurable(t *testing.T) { actualSvc := returnMock("svc1") - reconfSvc, err := armremotecontrol.WrapWithReconfigurable(actualSvc) + reconfSvc, err := armremotecontrol.WrapWithReconfigurable(actualSvc, resource.Name{}) test.That(t, err, test.ShouldBeNil) - _, err = armremotecontrol.WrapWithReconfigurable(nil) + _, err = armremotecontrol.WrapWithReconfigurable(nil, resource.Name{}) test.That(t, err, test.ShouldBeError, rdkutils.NewUnimplementedInterfaceError("armremotecontrol.Service", nil)) - reconfSvc2, err := armremotecontrol.WrapWithReconfigurable(reconfSvc) + reconfSvc2, err := armremotecontrol.WrapWithReconfigurable(reconfSvc, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfSvc2, test.ShouldEqual, reconfSvc) } func TestReconfigure(t *testing.T) { actualSvc := returnMock("svc1") - reconfSvc, err := armremotecontrol.WrapWithReconfigurable(actualSvc) + reconfSvc, err := armremotecontrol.WrapWithReconfigurable(actualSvc, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfSvc, test.ShouldNotBeNil) actualSvc2 := returnMock("svc1") - reconfSvc2, err := armremotecontrol.WrapWithReconfigurable(actualSvc2) + reconfSvc2, err := armremotecontrol.WrapWithReconfigurable(actualSvc2, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfSvc2, test.ShouldNotBeNil) test.That(t, actualSvc.reconfCount, test.ShouldEqual, 0) diff --git a/services/baseremotecontrol/base_remote_control.go b/services/baseremotecontrol/base_remote_control.go index db8d83bfdc5..44d63e2aa66 100644 --- a/services/baseremotecontrol/base_remote_control.go +++ b/services/baseremotecontrol/base_remote_control.go @@ -50,9 +50,14 @@ type Service interface { type reconfigurableBaseRemoteControl struct { mu sync.RWMutex + name resource.Name actual Service } +func (svc *reconfigurableBaseRemoteControl) Name() resource.Name { + return svc.name +} + func (svc *reconfigurableBaseRemoteControl) Close(ctx context.Context) error { svc.mu.RLock() defer svc.mu.RUnlock() @@ -74,7 +79,7 @@ func (svc *reconfigurableBaseRemoteControl) Reconfigure(ctx context.Context, new } // WrapWithReconfigurable wraps a BaseRemoteControl as a Reconfigurable. -func WrapWithReconfigurable(s interface{}) (resource.Reconfigurable, error) { +func WrapWithReconfigurable(s interface{}, name resource.Name) (resource.Reconfigurable, error) { if reconfigurable, ok := s.(*reconfigurableBaseRemoteControl); ok { return reconfigurable, nil } @@ -83,5 +88,5 @@ func WrapWithReconfigurable(s interface{}) (resource.Reconfigurable, error) { return nil, utils.NewUnimplementedInterfaceError("baseremotecontrol.Service", s) } - return &reconfigurableBaseRemoteControl{actual: svc}, nil + return &reconfigurableBaseRemoteControl{name: name, actual: svc}, nil } diff --git a/services/baseremotecontrol/base_remote_control_test.go b/services/baseremotecontrol/base_remote_control_test.go index 3b5264d18bf..4bd5a4ed58a 100644 --- a/services/baseremotecontrol/base_remote_control_test.go +++ b/services/baseremotecontrol/base_remote_control_test.go @@ -7,6 +7,7 @@ import ( "go.viam.com/test" "go.viam.com/rdk/registry" + "go.viam.com/rdk/resource" "go.viam.com/rdk/services/baseremotecontrol" rutils "go.viam.com/rdk/utils" ) @@ -20,25 +21,25 @@ func TestRegisteredReconfigurable(t *testing.T) { func TestWrapWithReconfigurable(t *testing.T) { actualSvc := returnMock("svc1") - reconfSvc, err := baseremotecontrol.WrapWithReconfigurable(actualSvc) + reconfSvc, err := baseremotecontrol.WrapWithReconfigurable(actualSvc, resource.Name{}) test.That(t, err, test.ShouldBeNil) - _, err = baseremotecontrol.WrapWithReconfigurable(nil) + _, err = baseremotecontrol.WrapWithReconfigurable(nil, resource.Name{}) test.That(t, err, test.ShouldBeError, rutils.NewUnimplementedInterfaceError("baseremotecontrol.Service", nil)) - reconfSvc2, err := baseremotecontrol.WrapWithReconfigurable(reconfSvc) + reconfSvc2, err := baseremotecontrol.WrapWithReconfigurable(reconfSvc, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfSvc2, test.ShouldEqual, reconfSvc) } func TestReconfigure(t *testing.T) { actualSvc := returnMock("svc1") - reconfSvc, err := baseremotecontrol.WrapWithReconfigurable(actualSvc) + reconfSvc, err := baseremotecontrol.WrapWithReconfigurable(actualSvc, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfSvc, test.ShouldNotBeNil) actualSvc2 := returnMock("svc1") - reconfSvc2, err := baseremotecontrol.WrapWithReconfigurable(actualSvc2) + reconfSvc2, err := baseremotecontrol.WrapWithReconfigurable(actualSvc2, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfSvc2, test.ShouldNotBeNil) test.That(t, actualSvc.reconfCount, test.ShouldEqual, 0) diff --git a/services/datamanager/data_manager.go b/services/datamanager/data_manager.go index 773034eaa2e..1e9b3926eed 100644 --- a/services/datamanager/data_manager.go +++ b/services/datamanager/data_manager.go @@ -98,9 +98,14 @@ func FirstFromRobot(r robot.Robot) (Service, error) { type reconfigurableDataManager struct { mu sync.RWMutex + name resource.Name actual Service } +func (svc *reconfigurableDataManager) Name() resource.Name { + return svc.name +} + func (svc *reconfigurableDataManager) Sync(ctx context.Context, extra map[string]interface{}) error { svc.mu.RLock() defer svc.mu.RUnlock() @@ -139,7 +144,7 @@ func (svc *reconfigurableDataManager) Reconfigure(ctx context.Context, newSvc re } // WrapWithReconfigurable wraps a data_manager as a Reconfigurable. -func WrapWithReconfigurable(s interface{}) (resource.Reconfigurable, error) { +func WrapWithReconfigurable(s interface{}, name resource.Name) (resource.Reconfigurable, error) { svc, ok := s.(Service) if !ok { return nil, NewUnimplementedInterfaceError(s) @@ -149,5 +154,5 @@ func WrapWithReconfigurable(s interface{}) (resource.Reconfigurable, error) { return reconfigurable, nil } - return &reconfigurableDataManager{actual: svc}, nil + return &reconfigurableDataManager{name: name, actual: svc}, nil } diff --git a/services/datamanager/data_manager_test.go b/services/datamanager/data_manager_test.go index 343910044da..0659ec95ff2 100644 --- a/services/datamanager/data_manager_test.go +++ b/services/datamanager/data_manager_test.go @@ -7,6 +7,7 @@ import ( "go.viam.com/test" "go.viam.com/rdk/registry" + "go.viam.com/rdk/resource" "go.viam.com/rdk/services/datamanager" rutils "go.viam.com/rdk/utils" ) @@ -25,25 +26,25 @@ func TestRegisteredReconfigurable(t *testing.T) { func TestWrapWithReconfigurable(t *testing.T) { svc := &mock{name: testSvcName1} - reconfSvc1, err := datamanager.WrapWithReconfigurable(svc) + reconfSvc1, err := datamanager.WrapWithReconfigurable(svc, resource.Name{}) test.That(t, err, test.ShouldBeNil) - _, err = datamanager.WrapWithReconfigurable(nil) + _, err = datamanager.WrapWithReconfigurable(nil, resource.Name{}) test.That(t, err, test.ShouldBeError, datamanager.NewUnimplementedInterfaceError(nil)) - reconfSvc2, err := datamanager.WrapWithReconfigurable(reconfSvc1) + reconfSvc2, err := datamanager.WrapWithReconfigurable(reconfSvc1, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfSvc2, test.ShouldEqual, reconfSvc1) } func TestReconfigurable(t *testing.T) { actualSvc1 := &mock{name: testSvcName1} - reconfSvc1, err := datamanager.WrapWithReconfigurable(actualSvc1) + reconfSvc1, err := datamanager.WrapWithReconfigurable(actualSvc1, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfSvc1, test.ShouldNotBeNil) actualSvc2 := &mock{name: testSvcName2} - reconfSvc2, err := datamanager.WrapWithReconfigurable(actualSvc2) + reconfSvc2, err := datamanager.WrapWithReconfigurable(actualSvc2, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfSvc2, test.ShouldNotBeNil) test.That(t, actualSvc1.reconfCount, test.ShouldEqual, 0) @@ -60,7 +61,7 @@ func TestReconfigurable(t *testing.T) { func TestExtraOptions(t *testing.T) { actualSvc1 := &mock{name: testSvcName1} - reconfSvc1, err := datamanager.WrapWithReconfigurable(actualSvc1) + reconfSvc1, err := datamanager.WrapWithReconfigurable(actualSvc1, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, actualSvc1.extra, test.ShouldEqual, nil) diff --git a/services/motion/motion.go b/services/motion/motion.go index 27b493d13b9..1544a17465d 100644 --- a/services/motion/motion.go +++ b/services/motion/motion.go @@ -103,9 +103,14 @@ func FromRobot(r robot.Robot, name string) (Service, error) { type reconfigurableMotionService struct { mu sync.RWMutex + name resource.Name actual Service } +func (svc *reconfigurableMotionService) Name() resource.Name { + return svc.name +} + func (svc *reconfigurableMotionService) Move( ctx context.Context, componentName resource.Name, @@ -164,7 +169,7 @@ func (svc *reconfigurableMotionService) Reconfigure(ctx context.Context, newSvc } // WrapWithReconfigurable wraps a Motion Service as a Reconfigurable. -func WrapWithReconfigurable(s interface{}) (resource.Reconfigurable, error) { +func WrapWithReconfigurable(s interface{}, name resource.Name) (resource.Reconfigurable, error) { svc, ok := s.(Service) if !ok { return nil, NewUnimplementedInterfaceError(s) @@ -174,5 +179,5 @@ func WrapWithReconfigurable(s interface{}) (resource.Reconfigurable, error) { return reconfigurable, nil } - return &reconfigurableMotionService{actual: svc}, nil + return &reconfigurableMotionService{name: name, actual: svc}, nil } diff --git a/services/motion/motion_test.go b/services/motion/motion_test.go index 1b6f47d8e47..b78a8954dc4 100644 --- a/services/motion/motion_test.go +++ b/services/motion/motion_test.go @@ -116,25 +116,25 @@ func TestRegisteredReconfigurable(t *testing.T) { func TestWrapWithReconfigurable(t *testing.T) { svc := &mock{name: "svc1"} - reconfSvc1, err := motion.WrapWithReconfigurable(svc) + reconfSvc1, err := motion.WrapWithReconfigurable(svc, resource.Name{}) test.That(t, err, test.ShouldBeNil) - _, err = motion.WrapWithReconfigurable(nil) + _, err = motion.WrapWithReconfigurable(nil, resource.Name{}) test.That(t, err, test.ShouldBeError, motion.NewUnimplementedInterfaceError(nil)) - reconfSvc2, err := motion.WrapWithReconfigurable(reconfSvc1) + reconfSvc2, err := motion.WrapWithReconfigurable(reconfSvc1, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfSvc2, test.ShouldEqual, reconfSvc1) } func TestReconfigurable(t *testing.T) { actualSvc1 := &mock{name: "svc1"} - reconfSvc1, err := motion.WrapWithReconfigurable(actualSvc1) + reconfSvc1, err := motion.WrapWithReconfigurable(actualSvc1, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfSvc1, test.ShouldNotBeNil) actualArm2 := &mock{name: "svc2"} - reconfSvc2, err := motion.WrapWithReconfigurable(actualArm2) + reconfSvc2, err := motion.WrapWithReconfigurable(actualArm2, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfSvc2, test.ShouldNotBeNil) test.That(t, actualSvc1.reconfCount, test.ShouldEqual, 0) diff --git a/services/navigation/navigation.go b/services/navigation/navigation.go index 0bcc4d10904..cbe8d85f70d 100644 --- a/services/navigation/navigation.go +++ b/services/navigation/navigation.go @@ -105,9 +105,14 @@ func (config *Config) Validate(path string) error { type reconfigurableNavigation struct { mu sync.RWMutex + name resource.Name actual Service } +func (svc *reconfigurableNavigation) Name() resource.Name { + return svc.name +} + func (svc *reconfigurableNavigation) Mode(ctx context.Context, extra map[string]interface{}) (Mode, error) { svc.mu.RLock() defer svc.mu.RUnlock() @@ -172,7 +177,7 @@ func NewUnimplementedInterfaceError(actual interface{}) error { } // WrapWithReconfigurable wraps a navigation service as a Reconfigurable. -func WrapWithReconfigurable(s interface{}) (resource.Reconfigurable, error) { +func WrapWithReconfigurable(s interface{}, name resource.Name) (resource.Reconfigurable, error) { svc, ok := s.(Service) if !ok { return nil, NewUnimplementedInterfaceError(s) @@ -182,5 +187,5 @@ func WrapWithReconfigurable(s interface{}) (resource.Reconfigurable, error) { return reconfigurable, nil } - return &reconfigurableNavigation{actual: svc}, nil + return &reconfigurableNavigation{name: name, actual: svc}, nil } diff --git a/services/navigation/navigation_test.go b/services/navigation/navigation_test.go index 7c465c626d0..cbcab06e5f8 100644 --- a/services/navigation/navigation_test.go +++ b/services/navigation/navigation_test.go @@ -7,6 +7,7 @@ import ( "go.viam.com/test" "go.viam.com/rdk/registry" + "go.viam.com/rdk/resource" "go.viam.com/rdk/services/navigation" rutils "go.viam.com/rdk/utils" ) @@ -25,25 +26,25 @@ func TestRegisteredReconfigurable(t *testing.T) { func TestWrapWithReconfigurable(t *testing.T) { svc := &mock{name: testSvcName1} - reconfSvc1, err := navigation.WrapWithReconfigurable(svc) + reconfSvc1, err := navigation.WrapWithReconfigurable(svc, resource.Name{}) test.That(t, err, test.ShouldBeNil) - _, err = navigation.WrapWithReconfigurable(nil) + _, err = navigation.WrapWithReconfigurable(nil, resource.Name{}) test.That(t, err, test.ShouldBeError, navigation.NewUnimplementedInterfaceError(nil)) - reconfSvc2, err := navigation.WrapWithReconfigurable(reconfSvc1) + reconfSvc2, err := navigation.WrapWithReconfigurable(reconfSvc1, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfSvc2, test.ShouldEqual, reconfSvc1) } func TestReconfigurable(t *testing.T) { actualSvc1 := &mock{name: testSvcName1} - reconfSvc1, err := navigation.WrapWithReconfigurable(actualSvc1) + reconfSvc1, err := navigation.WrapWithReconfigurable(actualSvc1, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfSvc1, test.ShouldNotBeNil) actualArm2 := &mock{name: testSvcName2} - reconfSvc2, err := navigation.WrapWithReconfigurable(actualArm2) + reconfSvc2, err := navigation.WrapWithReconfigurable(actualArm2, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfSvc2, test.ShouldNotBeNil) test.That(t, actualSvc1.reconfCount, test.ShouldEqual, 0) diff --git a/services/sensors/sensors.go b/services/sensors/sensors.go index 765c4f086af..975f44d623b 100644 --- a/services/sensors/sensors.go +++ b/services/sensors/sensors.go @@ -104,9 +104,14 @@ func FirstFromRobot(r robot.Robot) (Service, error) { type reconfigurableSensors struct { mu sync.RWMutex + name resource.Name actual Service } +func (svc *reconfigurableSensors) Name() resource.Name { + return svc.name +} + func (svc *reconfigurableSensors) Update(ctx context.Context, resources map[resource.Name]interface{}) error { svc.mu.RLock() defer svc.mu.RUnlock() @@ -151,7 +156,7 @@ func (svc *reconfigurableSensors) Reconfigure(ctx context.Context, newSvc resour } // WrapWithReconfigurable wraps a Sensors service as a Reconfigurable. -func WrapWithReconfigurable(s interface{}) (resource.Reconfigurable, error) { +func WrapWithReconfigurable(s interface{}, name resource.Name) (resource.Reconfigurable, error) { svc, ok := s.(Service) if !ok { return nil, NewUnimplementedInterfaceError(s) @@ -161,5 +166,5 @@ func WrapWithReconfigurable(s interface{}) (resource.Reconfigurable, error) { return reconfigurable, nil } - return &reconfigurableSensors{actual: svc}, nil + return &reconfigurableSensors{name: name, actual: svc}, nil } diff --git a/services/sensors/sensors_test.go b/services/sensors/sensors_test.go index 17044e03cda..b09cfa39dd0 100644 --- a/services/sensors/sensors_test.go +++ b/services/sensors/sensors_test.go @@ -75,25 +75,25 @@ func TestRegisteredReconfigurable(t *testing.T) { func TestWrapWithReconfigurable(t *testing.T) { svc := &mock{name: testSvcName1} - reconfSvc1, err := sensors.WrapWithReconfigurable(svc) + reconfSvc1, err := sensors.WrapWithReconfigurable(svc, resource.Name{}) test.That(t, err, test.ShouldBeNil) - _, err = sensors.WrapWithReconfigurable(nil) + _, err = sensors.WrapWithReconfigurable(nil, resource.Name{}) test.That(t, err, test.ShouldBeError, sensors.NewUnimplementedInterfaceError(nil)) - reconfSvc2, err := sensors.WrapWithReconfigurable(reconfSvc1) + reconfSvc2, err := sensors.WrapWithReconfigurable(reconfSvc1, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfSvc2, test.ShouldEqual, reconfSvc1) } func TestReconfigurable(t *testing.T) { actualSvc1 := &mock{name: testSvcName1} - reconfSvc1, err := sensors.WrapWithReconfigurable(actualSvc1) + reconfSvc1, err := sensors.WrapWithReconfigurable(actualSvc1, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfSvc1, test.ShouldNotBeNil) actualArm2 := &mock{name: testSvcName2} - reconfSvc2, err := sensors.WrapWithReconfigurable(actualArm2) + reconfSvc2, err := sensors.WrapWithReconfigurable(actualArm2, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfSvc2, test.ShouldNotBeNil) test.That(t, actualSvc1.reconfCount, test.ShouldEqual, 0) diff --git a/services/shell/shell.go b/services/shell/shell.go index 90ec79d3f6e..181d447acf5 100644 --- a/services/shell/shell.go +++ b/services/shell/shell.go @@ -74,9 +74,14 @@ func Named(name string) resource.Name { type reconfigurableShell struct { mu sync.RWMutex + name resource.Name actual Service } +func (svc *reconfigurableShell) Name() resource.Name { + return svc.name +} + func (svc *reconfigurableShell) Shell( ctx context.Context, extra map[string]interface{}, @@ -108,7 +113,7 @@ func (svc *reconfigurableShell) Reconfigure(ctx context.Context, newSvc resource } // WrapWithReconfigurable wraps a shell service as a Reconfigurable. -func WrapWithReconfigurable(s interface{}) (resource.Reconfigurable, error) { +func WrapWithReconfigurable(s interface{}, name resource.Name) (resource.Reconfigurable, error) { svc, ok := s.(Service) if !ok { return nil, NewUnimplementedInterfaceError(s) @@ -118,5 +123,5 @@ func WrapWithReconfigurable(s interface{}) (resource.Reconfigurable, error) { return reconfigurable, nil } - return &reconfigurableShell{actual: svc}, nil + return &reconfigurableShell{name: name, actual: svc}, nil } diff --git a/services/shell/shell_test.go b/services/shell/shell_test.go index 5dc350443d1..26648c36a27 100644 --- a/services/shell/shell_test.go +++ b/services/shell/shell_test.go @@ -7,6 +7,7 @@ import ( "go.viam.com/test" "go.viam.com/rdk/registry" + "go.viam.com/rdk/resource" "go.viam.com/rdk/services/shell" rutils "go.viam.com/rdk/utils" ) @@ -25,25 +26,25 @@ func TestRegisteredReconfigurable(t *testing.T) { func TestWrapWithReconfigurable(t *testing.T) { svc := &mock{name: testSvcName1} - reconfSvc1, err := shell.WrapWithReconfigurable(svc) + reconfSvc1, err := shell.WrapWithReconfigurable(svc, resource.Name{}) test.That(t, err, test.ShouldBeNil) - _, err = shell.WrapWithReconfigurable(nil) + _, err = shell.WrapWithReconfigurable(nil, resource.Name{}) test.That(t, err, test.ShouldBeError, shell.NewUnimplementedInterfaceError(nil)) - reconfSvc2, err := shell.WrapWithReconfigurable(reconfSvc1) + reconfSvc2, err := shell.WrapWithReconfigurable(reconfSvc1, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfSvc2, test.ShouldEqual, reconfSvc1) } func TestReconfigurable(t *testing.T) { actualSvc1 := &mock{name: testSvcName1} - reconfSvc1, err := shell.WrapWithReconfigurable(actualSvc1) + reconfSvc1, err := shell.WrapWithReconfigurable(actualSvc1, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfSvc1, test.ShouldNotBeNil) actualArm2 := &mock{name: testSvcName2} - reconfSvc2, err := shell.WrapWithReconfigurable(actualArm2) + reconfSvc2, err := shell.WrapWithReconfigurable(actualArm2, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfSvc2, test.ShouldNotBeNil) test.That(t, actualSvc1.reconfCount, test.ShouldEqual, 0) diff --git a/services/slam/slam.go b/services/slam/slam.go index 20ca1b10a41..71c2b02602b 100644 --- a/services/slam/slam.go +++ b/services/slam/slam.go @@ -80,9 +80,14 @@ type Service interface { type reconfigurableSlam struct { mu sync.RWMutex + name resource.Name actual Service } +func (svc *reconfigurableSlam) Name() resource.Name { + return svc.name +} + func (svc *reconfigurableSlam) Position( ctx context.Context, val string, @@ -127,7 +132,7 @@ func (svc *reconfigurableSlam) Reconfigure(ctx context.Context, newSvc resource. } // WrapWithReconfigurable wraps a slam service as a Reconfigurable. -func WrapWithReconfigurable(s interface{}) (resource.Reconfigurable, error) { +func WrapWithReconfigurable(s interface{}, name resource.Name) (resource.Reconfigurable, error) { svc, ok := s.(Service) if !ok { return nil, NewUnimplementedInterfaceError(s) @@ -137,5 +142,5 @@ func WrapWithReconfigurable(s interface{}) (resource.Reconfigurable, error) { return reconfigurable, nil } - return &reconfigurableSlam{actual: svc}, nil + return &reconfigurableSlam{name: name, actual: svc}, nil } diff --git a/services/slam/slam_test.go b/services/slam/slam_test.go index a4b52205ea0..abdaab116e7 100644 --- a/services/slam/slam_test.go +++ b/services/slam/slam_test.go @@ -10,6 +10,7 @@ import ( "go.viam.com/test" "go.viam.com/rdk/registry" + "go.viam.com/rdk/resource" "go.viam.com/rdk/services/slam" rdkutils "go.viam.com/rdk/utils" ) @@ -28,25 +29,25 @@ func TestRegisteredReconfigurable(t *testing.T) { func TestWrapWithReconfigurable(t *testing.T) { svc := &mock{name: testSvcName1} - reconfSvc1, err := slam.WrapWithReconfigurable(svc) + reconfSvc1, err := slam.WrapWithReconfigurable(svc, resource.Name{}) test.That(t, err, test.ShouldBeNil) - _, err = slam.WrapWithReconfigurable(nil) + _, err = slam.WrapWithReconfigurable(nil, resource.Name{}) test.That(t, err, test.ShouldBeError, slam.NewUnimplementedInterfaceError(nil)) - reconfSvc2, err := slam.WrapWithReconfigurable(reconfSvc1) + reconfSvc2, err := slam.WrapWithReconfigurable(reconfSvc1, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfSvc2, test.ShouldEqual, reconfSvc1) } func TestReconfigurable(t *testing.T) { actualSvc1 := &mock{name: testSvcName1} - reconfSvc1, err := slam.WrapWithReconfigurable(actualSvc1) + reconfSvc1, err := slam.WrapWithReconfigurable(actualSvc1, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfSvc1, test.ShouldNotBeNil) actualArm2 := &mock{name: testSvcName2} - reconfSvc2, err := slam.WrapWithReconfigurable(actualArm2) + reconfSvc2, err := slam.WrapWithReconfigurable(actualArm2, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfSvc2, test.ShouldNotBeNil) test.That(t, actualSvc1.reconfCount, test.ShouldEqual, 0) diff --git a/services/vision/reconfigure_test.go b/services/vision/reconfigure_test.go index 07a8419600b..1c34370ca71 100644 --- a/services/vision/reconfigure_test.go +++ b/services/vision/reconfigure_test.go @@ -7,6 +7,7 @@ import ( "go.viam.com/test" "go.viam.com/rdk/registry" + "go.viam.com/rdk/resource" "go.viam.com/rdk/services/vision" rutils "go.viam.com/rdk/utils" ) @@ -25,25 +26,25 @@ func TestRegisteredReconfigurable(t *testing.T) { func TestWrapWithReconfigurable(t *testing.T) { svc := &mock1{name: testSvcName1} - reconfSvc1, err := vision.WrapWithReconfigurable(svc) + reconfSvc1, err := vision.WrapWithReconfigurable(svc, resource.Name{}) test.That(t, err, test.ShouldBeNil) - _, err = vision.WrapWithReconfigurable(nil) + _, err = vision.WrapWithReconfigurable(nil, resource.Name{}) test.That(t, err, test.ShouldBeError, vision.NewUnimplementedInterfaceError(nil)) - reconfSvc2, err := vision.WrapWithReconfigurable(reconfSvc1) + reconfSvc2, err := vision.WrapWithReconfigurable(reconfSvc1, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfSvc2, test.ShouldEqual, reconfSvc1) } func TestReconfigurable(t *testing.T) { actualSvc1 := &mock1{name: testSvcName1} - reconfSvc1, err := vision.WrapWithReconfigurable(actualSvc1) + reconfSvc1, err := vision.WrapWithReconfigurable(actualSvc1, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfSvc1, test.ShouldNotBeNil) actualArm2 := &mock1{name: testSvcName2} - reconfSvc2, err := vision.WrapWithReconfigurable(actualArm2) + reconfSvc2, err := vision.WrapWithReconfigurable(actualArm2, resource.Name{}) test.That(t, err, test.ShouldBeNil) test.That(t, reconfSvc2, test.ShouldNotBeNil) test.That(t, actualSvc1.reconfCount, test.ShouldEqual, 0) diff --git a/services/vision/vision.go b/services/vision/vision.go index 5d0d36d63aa..224e22c30e9 100644 --- a/services/vision/vision.go +++ b/services/vision/vision.go @@ -149,9 +149,14 @@ type Attributes struct { type reconfigurableVision struct { mu sync.RWMutex + name resource.Name actual Service } +func (svc *reconfigurableVision) Name() resource.Name { + return svc.name +} + func (svc *reconfigurableVision) GetModelParameterSchema( ctx context.Context, modelType VisModelType, @@ -281,16 +286,11 @@ func (svc *reconfigurableVision) Reconfigure(ctx context.Context, newSvc resourc golog.Global().Errorw("error closing old", "error", err) } svc.actual = rSvc.actual - /* - theOldServ := svc.actual.(*visionService) - theNewSerc := rSvc.actual.(*visionService) - *theOldServ = *theNewSerc - */ return nil } // WrapWithReconfigurable wraps a vision service as a Reconfigurable. -func WrapWithReconfigurable(s interface{}) (resource.Reconfigurable, error) { +func WrapWithReconfigurable(s interface{}, name resource.Name) (resource.Reconfigurable, error) { svc, ok := s.(Service) if !ok { return nil, NewUnimplementedInterfaceError(s) @@ -300,5 +300,5 @@ func WrapWithReconfigurable(s interface{}) (resource.Reconfigurable, error) { return reconfigurable, nil } - return &reconfigurableVision{actual: svc}, nil + return &reconfigurableVision{name: name, actual: svc}, nil }