/
collectors.go
57 lines (48 loc) · 1.42 KB
/
collectors.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
package servo
import (
"context"
"errors"
pb "go.viam.com/api/component/servo/v1"
"google.golang.org/protobuf/types/known/anypb"
"go.viam.com/rdk/data"
)
type method int64
const (
position method = iota
)
func (m method) String() string {
if m == position {
return "Position"
}
return "Unknown"
}
// newPositionCollector returns a collector to register a position method. If one is already registered
// with the same MethodMetadata it will panic.
func newPositionCollector(resource interface{}, params data.CollectorParams) (data.Collector, error) {
servo, err := assertServo(resource)
if err != nil {
return nil, err
}
cFunc := data.CaptureFunc(func(ctx context.Context, _ map[string]*anypb.Any) (interface{}, error) {
pos, err := servo.Position(ctx, data.FromDMExtraMap)
if err != nil {
// A modular filter component can be created to filter the readings from a component. The error ErrNoCaptureToStore
// is used in the datamanager to exclude readings from being captured and stored.
if errors.Is(err, data.ErrNoCaptureToStore) {
return nil, err
}
return nil, data.FailedToReadErr(params.ComponentName, position.String(), err)
}
return pb.GetPositionResponse{
PositionDeg: pos,
}, nil
})
return data.NewCollector(cFunc, params)
}
func assertServo(resource interface{}) (Servo, error) {
servo, ok := resource.(Servo)
if !ok {
return nil, data.InvalidInterfaceErr(API)
}
return servo, nil
}