-
Notifications
You must be signed in to change notification settings - Fork 110
/
sensors.go
54 lines (46 loc) · 1.69 KB
/
sensors.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
package inject
import (
"context"
"go.viam.com/rdk/resource"
"go.viam.com/rdk/services/sensors"
)
// SensorsService represents a fake instance of a sensors service.
type SensorsService struct {
sensors.Service
name resource.Name
SensorsFunc func(ctx context.Context, extra map[string]interface{}) ([]resource.Name, error)
ReadingsFunc func(ctx context.Context, resources []resource.Name, extra map[string]interface{}) ([]sensors.Readings, error)
DoCommandFunc func(ctx context.Context,
cmd map[string]interface{}) (map[string]interface{}, error)
}
// NewSensorsService returns a new injected sensors service.
func NewSensorsService(name string) *SensorsService {
return &SensorsService{name: sensors.Named(name)}
}
// Name returns the name of the resource.
func (s *SensorsService) Name() resource.Name {
return s.name
}
// Sensors call the injected Sensors or the real one.
func (s *SensorsService) Sensors(ctx context.Context, extra map[string]interface{}) ([]resource.Name, error) {
if s.SensorsFunc == nil {
return s.Service.Sensors(ctx, extra)
}
return s.SensorsFunc(ctx, extra)
}
// Readings call the injected Readings or the real one.
func (s *SensorsService) Readings(ctx context.Context, names []resource.Name, extra map[string]interface{}) ([]sensors.Readings, error) {
if s.ReadingsFunc == nil {
return s.Service.Readings(ctx, names, extra)
}
return s.ReadingsFunc(ctx, names, extra)
}
// DoCommand calls the injected DoCommand or the real variant.
func (s *SensorsService) DoCommand(ctx context.Context,
cmd map[string]interface{},
) (map[string]interface{}, error) {
if s.DoCommandFunc == nil {
return s.Service.DoCommand(ctx, cmd)
}
return s.DoCommandFunc(ctx, cmd)
}