-
Notifications
You must be signed in to change notification settings - Fork 110
/
collector.go
69 lines (57 loc) · 1.63 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
64
65
66
67
68
69
package sensor
import (
"context"
"google.golang.org/protobuf/types/known/anypb"
"go.viam.com/rdk/data"
)
type method int64
const (
readings method = iota
)
func (m method) String() string {
if m == readings {
return "Readings"
}
return "Unknown"
}
// ReadingRecords a collection of ReadingRecord.
type ReadingRecords struct {
Readings []ReadingRecord
}
// ReadingRecord a single analog reading.
type ReadingRecord struct {
ReadingName string
Reading interface{}
}
func newSensorCollector(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) {
var records []ReadingRecord
values, err := sensorResource.Readings(ctx, nil) // TODO (RSDK-1972): pass in something here from the config rather than nil?
if err != nil {
return nil, data.FailedToReadErr(params.ComponentName, readings.String(), err)
}
for name, value := range values {
if len(arg) != 0 {
// If specific sensor reading names were passed in the robot config, report only those
// Otherwise, report all sensor values
if _, ok := arg[name]; !ok {
continue
}
}
records = append(records, ReadingRecord{ReadingName: name, Reading: value})
}
return ReadingRecords{Readings: records}, nil
})
return data.NewCollector(cFunc, params)
}
func assertSensor(resource interface{}) (Sensor, error) {
sensorResource, ok := resource.(Sensor)
if !ok {
return nil, data.InvalidInterfaceErr(SubtypeName)
}
return sensorResource, nil
}