Skip to content

Commit

Permalink
Add save client example
Browse files Browse the repository at this point in the history
  • Loading branch information
seanavery committed Oct 1, 2024
1 parent 8d5c497 commit cdc66e5
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
89 changes: 89 additions & 0 deletions examples/save_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
This example domenstrates calling async save command on video-store resource. To setup:
- You need to have a robot running with video-store component.
- Ensure you have a .env file with the necessary credentials and secrets.
- run example script `go run save_client.go`
*/

package main

import (
"context"
"os"
"time"

"github.com/joho/godotenv"
"go.viam.com/rdk/components/camera"
"go.viam.com/rdk/logging"
"go.viam.com/rdk/robot/client"
"go.viam.com/utils/rpc"
"golang.org/x/exp/rand"
)

func main() {
logger := logging.NewDebugLogger("client")

err := godotenv.Load()
if err != nil {
logger.Fatalf("Error loading .env file")
}

robotAddress := os.Getenv("ROBOT_ADDRESS")
apiKeyID := os.Getenv("API_KEY_ID")
apiKey := os.Getenv("API_KEY")

machine, err := client.New(
context.Background(),
robotAddress,
logger,
client.WithDialOptions(rpc.WithEntityCredentials(
apiKeyID,
rpc.Credentials{
Type: rpc.CredentialsTypeAPIKey,
Payload: apiKey,
})),
)
if err != nil {
logger.Fatal(err)
}

defer machine.Close(context.Background())
logger.Info("Resources:")
logger.Info(machine.ResourceNames())

videoStore, err := camera.FromRobot(machine, "video-store")
if err != nil {
logger.Error(err)
return
}
videoStoreReturnValue, err := videoStore.Properties(context.Background())
if err != nil {
logger.Error(err)
return
}
logger.Infof("video-store Properties return value: %+v", videoStoreReturnValue)

for {
now := time.Now()
rand.Seed(uint64(time.Now().UnixNano()))
randomSeconds := rand.Intn(56) + 5 // 5 to 60 seconds
from := now.Add(-time.Duration(randomSeconds) * time.Second)
nowStr := now.Format("2006-01-02_15-04-05")
fromStr := from.Format("2006-01-02_15-04-05")
_, err = videoStore.DoCommand(context.Background(),
map[string]interface{}{
"command": "save",
"from": fromStr,
"to": nowStr,
"metadata": "metadata",
"async": true,
},
)
if err != nil {
logger.Error(err)
return
}
time.Sleep(30 * time.Second)
}

}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/fullstorydev/grpcurl v1.8.6
github.com/golangci/golangci-lint v1.54.0
github.com/google/uuid v1.6.0
github.com/joho/godotenv v1.5.1
github.com/rhysd/actionlint v1.6.24
go.viam.com/rdk v0.40.0
go.viam.com/test v1.1.1-0.20220913152726-5da9916c08a2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,8 @@ github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9Y
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/jmoiron/sqlx v1.2.0/go.mod h1:1FEQNm3xlJgrMD+FBdI9+xvCksHtbpVBBw5dYhBSsks=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
github.com/jonboulle/clockwork v0.2.0/go.mod h1:Pkfl5aHPm1nk2H9h0bjmnJD/BcgbGXUBGnn1kMkgxc8=
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
Expand Down

0 comments on commit cdc66e5

Please sign in to comment.