-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from viam-modules/example-save
Example save client script
- Loading branch information
Showing
3 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
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.Fatal("Please make sure you add a .env file with the necessary credentials and secrets.") | ||
} | ||
robotAddress := os.Getenv("ROBOT_ADDRESS") | ||
apiKeyID := os.Getenv("API_KEY_ID") | ||
apiKey := os.Getenv("API_KEY") | ||
if robotAddress == "" || apiKeyID == "" || apiKey == "" { | ||
logger.Fatal("Missing required environment variables: ROBOT_ADDRESS, API_KEY_ID, or 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) | ||
|
||
// Save clip of random duration every 30 seconds | ||
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) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters