-
Notifications
You must be signed in to change notification settings - Fork 110
/
base.go
120 lines (104 loc) · 4.02 KB
/
base.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package inject
import (
"context"
"github.com/golang/geo/r3"
"go.viam.com/rdk/components/base"
"go.viam.com/rdk/resource"
"go.viam.com/rdk/spatialmath"
)
// Base is an injected base.
type Base struct {
base.Base
name resource.Name
DoFunc func(ctx context.Context, cmd map[string]interface{}) (map[string]interface{}, error)
MoveStraightFunc func(ctx context.Context, distanceMm int, mmPerSec float64, extra map[string]interface{}) error
SpinFunc func(ctx context.Context, angleDeg, degsPerSec float64, extra map[string]interface{}) error
StopFunc func(ctx context.Context, extra map[string]interface{}) error
IsMovingFunc func(context.Context) (bool, error)
CloseFunc func(ctx context.Context) error
SetPowerFunc func(ctx context.Context, linear, angular r3.Vector, extra map[string]interface{}) error
SetVelocityFunc func(ctx context.Context, linear, angular r3.Vector, extra map[string]interface{}) error
PropertiesFunc func(ctx context.Context, extra map[string]interface{}) (base.Properties, error)
GeometriesFunc func(ctx context.Context) ([]spatialmath.Geometry, error)
}
// NewBase returns a new injected base.
func NewBase(name string) *Base {
return &Base{name: base.Named(name)}
}
// Name returns the name of the resource.
func (b *Base) Name() resource.Name {
return b.name
}
// MoveStraight calls the injected MoveStraight or the real version.
func (b *Base) MoveStraight(ctx context.Context, distanceMm int, mmPerSec float64, extra map[string]interface{}) error {
if b.MoveStraightFunc == nil {
return b.Base.MoveStraight(ctx, distanceMm, mmPerSec, extra)
}
return b.MoveStraightFunc(ctx, distanceMm, mmPerSec, extra)
}
// Spin calls the injected Spin or the real version.
func (b *Base) Spin(ctx context.Context, angleDeg, degsPerSec float64, extra map[string]interface{}) error {
if b.SpinFunc == nil {
return b.Base.Spin(ctx, angleDeg, degsPerSec, extra)
}
return b.SpinFunc(ctx, angleDeg, degsPerSec, extra)
}
// Stop calls the injected Stop or the real version.
func (b *Base) Stop(ctx context.Context, extra map[string]interface{}) error {
if b.StopFunc == nil {
return b.Base.Stop(ctx, extra)
}
return b.StopFunc(ctx, extra)
}
// IsMoving calls the injected IsMoving or the real version.
func (b *Base) IsMoving(ctx context.Context) (bool, error) {
if b.IsMovingFunc == nil {
return b.Base.IsMoving(ctx)
}
return b.IsMovingFunc(ctx)
}
// Close calls the injected Close or the real version.
func (b *Base) Close(ctx context.Context) error {
if b.CloseFunc == nil {
if b.Base == nil {
return nil
}
return b.Base.Close(ctx)
}
return b.CloseFunc(ctx)
}
// DoCommand calls the injected DoCommand or the real version.
func (b *Base) DoCommand(ctx context.Context, cmd map[string]interface{}) (map[string]interface{}, error) {
if b.DoFunc == nil {
return b.Base.DoCommand(ctx, cmd)
}
return b.DoFunc(ctx, cmd)
}
// SetPower calls the injected SetPower or the real version.
func (b *Base) SetPower(ctx context.Context, linear, angular r3.Vector, extra map[string]interface{}) error {
if b.SetPowerFunc == nil {
return b.Base.SetPower(ctx, linear, angular, extra)
}
return b.SetPowerFunc(ctx, linear, angular, extra)
}
// SetVelocity calls the injected SetVelocity or the real version.
func (b *Base) SetVelocity(ctx context.Context, linear, angular r3.Vector, extra map[string]interface{}) error {
if b.SetVelocityFunc == nil {
return b.Base.SetVelocity(ctx, linear, angular, extra)
}
return b.SetVelocityFunc(ctx, linear, angular, extra)
}
// Properties returns the base's properties.
func (b *Base) Properties(ctx context.Context, extra map[string]interface{}) (base.Properties, error) {
if b.PropertiesFunc == nil {
return b.Base.Properties(ctx, extra)
}
return b.PropertiesFunc(ctx, extra)
}
// Geometries returns the base's geometries.
func (b *Base) Geometries(ctx context.Context, extra map[string]interface{}) ([]spatialmath.Geometry, error) {
if b.GeometriesFunc == nil {
return b.Base.Geometries(ctx, extra)
}
return b.GeometriesFunc(ctx)
}