-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensor.go
92 lines (77 loc) · 2.16 KB
/
sensor.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
package clocks
import (
"context"
"sync"
"go.viam.com/rdk/components/sensor"
"go.viam.com/rdk/logging"
"go.viam.com/rdk/resource"
"github.com/viam-soleng/viam-raspi-sensors/utils"
)
var Model = resource.NewModel("viam-soleng", "raspi", "clocks")
var PrettyName = "Raspberry Pi Clock Sensor"
var Description = "A sensor that reports the clock frequencies of the Raspberry Pi."
var Version = utils.Version
type Config struct {
resource.Named
mu sync.RWMutex
logger logging.Logger
cancelCtx context.Context
cancelFunc func()
}
func init() {
resource.RegisterComponent(
sensor.API,
Model,
resource.Registration[sensor.Sensor, *ComponentConfig]{Constructor: NewSensor})
}
func NewSensor(ctx context.Context, deps resource.Dependencies, conf resource.Config, logger logging.Logger) (sensor.Sensor, error) {
logger.Infof("Starting %s %s", PrettyName, Version)
cancelCtx, cancelFunc := context.WithCancel(context.Background())
b := Config{
Named: conf.ResourceName().AsNamed(),
logger: logger,
cancelCtx: cancelCtx,
cancelFunc: cancelFunc,
mu: sync.RWMutex{},
}
if err := b.Reconfigure(ctx, deps, conf); err != nil {
return nil, err
}
return &b, nil
}
func (c *Config) Reconfigure(ctx context.Context, _ resource.Dependencies, conf resource.Config) error {
c.mu.Lock()
defer c.mu.Unlock()
c.logger.Debugf("Reconfiguring %s", PrettyName)
// In case the module has changed name
c.Named = conf.ResourceName().AsNamed()
return nil
}
func (c *Config) Readings(ctx context.Context, extra map[string]interface{}) (map[string]interface{}, error) {
c.mu.RLock()
defer c.mu.RUnlock()
arm, core, h264, isp, v3d, uart, pwm, emmc, pixel, vec, hdmi, dpi, err := getClockFrequencies()
if err != nil {
return nil, err
}
return map[string]interface{}{
"arm": arm,
"core": core,
"h264": h264,
"isp": isp,
"v3d": v3d,
"uart": uart,
"pwm": pwm,
"emmc": emmc,
"pixel": pixel,
"vec": vec,
"hdmi": hdmi,
"dpi": dpi,
}, nil
}
func (c *Config) Close(ctx context.Context) error {
return nil
}
func (c *Config) Ready(ctx context.Context, extra map[string]interface{}) (bool, error) {
return false, nil
}