-
Notifications
You must be signed in to change notification settings - Fork 110
/
collectors.go
94 lines (82 loc) · 2.61 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
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
package gantry
import (
"context"
"errors"
pb "go.viam.com/api/component/gantry/v1"
"google.golang.org/protobuf/types/known/anypb"
"go.viam.com/rdk/data"
)
type method int64
const (
position method = iota
lengths
)
func (m method) String() string {
switch m {
case position:
return "Position"
case lengths:
return "Lengths"
}
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) {
gantry, err := assertGantry(resource)
if err != nil {
return nil, err
}
cFunc := data.CaptureFunc(func(ctx context.Context, _ map[string]*anypb.Any) (interface{}, error) {
v, err := gantry.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{
PositionsMm: scaleMetersToMm(v),
}, nil
})
return data.NewCollector(cFunc, params)
}
// NewLengthsCollector returns a collector to register a lengths method. If one is already registered
// with the same MethodMetadata it will panic.
func NewLengthsCollector(resource interface{}, params data.CollectorParams) (data.Collector, error) {
gantry, err := assertGantry(resource)
if err != nil {
return nil, err
}
cFunc := data.CaptureFunc(func(ctx context.Context, _ map[string]*anypb.Any) (interface{}, error) {
v, err := gantry.Lengths(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, lengths.String(), err)
}
return pb.GetLengthsResponse{
LengthsMm: scaleMetersToMm(v),
}, nil
})
return data.NewCollector(cFunc, params)
}
func scaleMetersToMm(meters []float64) []float64 {
ret := make([]float64, len(meters))
for i := range ret {
ret[i] = meters[i] * 1000
}
return ret
}
func assertGantry(resource interface{}) (Gantry, error) {
gantry, ok := resource.(Gantry)
if !ok {
return nil, data.InvalidInterfaceErr(API)
}
return gantry, nil
}