-
Notifications
You must be signed in to change notification settings - Fork 110
/
collector.go
63 lines (53 loc) · 1.59 KB
/
collector.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
package sensor
import (
"context"
"errors"
pb "go.viam.com/api/common/v1"
"google.golang.org/protobuf/types/known/anypb"
"go.viam.com/rdk/data"
"go.viam.com/rdk/protoutils"
)
type method int64
const (
readings method = iota
)
func (m method) String() string {
if m == readings {
return "Readings"
}
return "Unknown"
}
// newReadingsCollector returns a collector to register a sensor reading method. If one is already registered
// with the same MethodMetadata it will panic.
func newReadingsCollector(resource interface{}, params data.CollectorParams) (data.Collector, error) {
sensorResource, err := assertSensor(resource)
if err != nil {
return nil, err
}
cFunc := data.CaptureFunc(func(ctx context.Context, arg map[string]*anypb.Any) (interface{}, error) {
values, err := sensorResource.Readings(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, readings.String(), err)
}
readings, err := protoutils.ReadingGoToProto(values)
if err != nil {
return nil, err
}
return pb.GetReadingsResponse{
Readings: readings,
}, nil
})
return data.NewCollector(cFunc, params)
}
func assertSensor(resource interface{}) (Sensor, error) {
sensorResource, ok := resource.(Sensor)
if !ok {
return nil, data.InvalidInterfaceErr(API)
}
return sensorResource, nil
}