-
Notifications
You must be signed in to change notification settings - Fork 110
/
main.go
84 lines (74 loc) · 1.99 KB
/
main.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
// Package main is a module for testing, with an inline generic component to return internal data and perform other test functions.
package main
import (
"context"
"fmt"
"time"
"github.com/edaniels/golog"
"github.com/pkg/errors"
"go.viam.com/utils"
"go.viam.com/rdk/components/generic"
"go.viam.com/rdk/config"
"go.viam.com/rdk/module"
"go.viam.com/rdk/registry"
"go.viam.com/rdk/resource"
)
var (
myModel = resource.NewModel("rdk", "test", "helper")
myMod *module.Module
)
func main() {
utils.ContextualMain(mainWithArgs, golog.NewDevelopmentLogger("TestModule"))
}
func mainWithArgs(ctx context.Context, args []string, logger golog.Logger) error {
var err error
myMod, err = module.NewModuleFromArgs(ctx, logger)
if err != nil {
return err
}
registry.RegisterComponent(generic.Subtype, myModel, registry.Component{Constructor: newHelper})
err = myMod.AddModelFromRegistry(ctx, generic.Subtype, myModel)
if err != nil {
return err
}
err = myMod.Start(ctx)
defer myMod.Close(ctx)
if err != nil {
return err
}
<-ctx.Done()
return nil
}
func newHelper(ctx context.Context, deps registry.Dependencies, cfg config.Component, logger golog.Logger) (interface{}, error) {
return &helper{
logger: logger,
}, nil
}
type helper struct {
generic.Generic
logger golog.Logger
}
// DoCommand is the only method of this component. It looks up the "real" command from the map it's passed.
//
//nolint:unparam
func (h *helper) DoCommand(ctx context.Context, req map[string]interface{}) (map[string]interface{}, error) {
cmd, ok := req["command"]
if !ok {
return nil, errors.New("missing 'command' string")
}
switch req["command"] {
case "sleep":
time.Sleep(time.Second * 1)
//nolint:nilnil
return nil, nil
case "get_ops":
ops := myMod.OperationManager().All()
var opsOut []string
for _, op := range ops {
opsOut = append(opsOut, op.ID.String())
}
return map[string]interface{}{"ops": opsOut}, nil
default:
return nil, fmt.Errorf("unknown command string %s", cmd)
}
}