Skip to content

Commit 912d5a2

Browse files
committed
subkey for triggers
1 parent 75d83f0 commit 912d5a2

2 files changed

Lines changed: 19 additions & 7 deletions

File tree

main.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1+
//go:build windows
2+
13
package main
24

35
import (
4-
"win-reg-sensor/models"
56
"context"
7+
"win-reg-sensor/models"
8+
9+
"go.viam.com/rdk/components/sensor"
610
"go.viam.com/rdk/logging"
711
"go.viam.com/rdk/module"
812
"go.viam.com/utils"
9-
"go.viam.com/rdk/components/sensor"
10-
1113
)
1214

1315
func main() {
@@ -19,12 +21,11 @@ func mainWithArgs(ctx context.Context, args []string, logger logging.Logger) err
1921
if err != nil {
2022
return err
2123
}
22-
24+
2325
if err = winRegSensor.AddModelFromRegistry(ctx, sensor.API, models.Registry); err != nil {
2426
return err
2527
}
2628

27-
2829
err = winRegSensor.Start(ctx)
2930
defer winRegSensor.Close(ctx)
3031
if err != nil {

models/module.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
//go:build windows
2+
13
package models
24

35
import (
46
"context"
57
"errors"
8+
"strings"
69

710
errw "github.com/pkg/errors"
811
"go.viam.com/rdk/components/sensor"
@@ -86,9 +89,14 @@ func (s *winRegSensorRegistry) NewClientFromConn(ctx context.Context, conn rpc.C
8689
func (s *winRegSensorRegistry) Readings(ctx context.Context, extra map[string]any) (map[string]any, error) {
8790
ret := make(map[string]any)
8891
s.logger.Debugf("reading %d keys", len(s.cfg.Keys))
89-
for _, key := range s.cfg.Keys {
92+
for _, fullKey := range s.cfg.Keys {
9093
subMap := make(map[string]any)
91-
ret[key] = subMap
94+
// note: in colon/subKey mode, we set a single value instead of the submap.
95+
// we do this because triggers can't access nested maps.
96+
key, subKey, hasColon := strings.Cut(fullKey, ":")
97+
if !hasColon {
98+
ret[fullKey] = subMap
99+
}
92100
err := func() error {
93101
s.logger.Debugf("opening key %s", key)
94102
k, err := registry.OpenKey(registry.LOCAL_MACHINE, key, registry.QUERY_VALUE)
@@ -110,6 +118,9 @@ func (s *winRegSensorRegistry) Readings(ctx context.Context, extra map[string]an
110118
}
111119
subMap[name] = val
112120
}
121+
if hasColon {
122+
ret[fullKey] = subMap[subKey]
123+
}
113124
return nil
114125
}()
115126
if err != nil {

0 commit comments

Comments
 (0)