Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make mysensor example single file #1451

Merged
merged 3 commits into from
Oct 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/mysensor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ To use this custom server as part of a larger robot, you’ll want to add it as
]
```

And to ensure that the custom server starts up with the rest of the robot, you can run the custom server binary as a process.
And to ensure that the custom server starts up with the rest of the robot, you can run the custom server binary as a process as part of the robot config.

```
"processes": [
Expand Down
2 changes: 1 addition & 1 deletion examples/mysensor/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func main() {
logger := golog.NewDebugLogger("client")
robot, err := client.New(
context.Background(),
"localhost:8080",
"localhost:8081",
logger,
)
if err != nil {
Expand Down
12 changes: 0 additions & 12 deletions examples/mysensor/server/config.json

This file was deleted.

18 changes: 10 additions & 8 deletions examples/mysensor/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import (
"go.viam.com/rdk/components/sensor"
"go.viam.com/rdk/config"
"go.viam.com/rdk/registry"
"go.viam.com/rdk/resource"

robotimpl "go.viam.com/rdk/robot/impl"
"go.viam.com/rdk/robot/web"
"go.viam.com/rdk/utils"
weboptions "go.viam.com/rdk/robot/web/options"
)

var logger = golog.NewDebugLogger("mysensor")
Expand Down Expand Up @@ -59,16 +60,17 @@ func main() {
}

func mainWithArgs(ctx context.Context, args []string, logger golog.Logger) (err error) {
// we set the config here, but it can also be a commandline argument if so desired.
cfg, err := config.Read(ctx, utils.ResolveFile("./examples/mysensor/server/config.json"), logger)
if err != nil {
return err
}
myRobot, err := robotimpl.RobotFromConfig(ctx, cfg, logger)
s := &mySensor{Name: "sensor1"}

myRobot, err := robotimpl.RobotFromResources(ctx, map[resource.Name]interface{}{sensor.Named("sensor1"): s}, logger)
if err != nil {
return err
}
defer myRobot.Close(ctx)
o := weboptions.New()
// the default bind address is localhost:8080, specifying a different bind address to avoid collisions.
o.Network.BindAddress = "localhost:8081"

return web.RunWebWithConfig(ctx, myRobot, cfg, logger)
// runs the web server on the robot and blocks until the program is stopped.
return web.RunWeb(ctx, myRobot, o, logger)
}