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

Update trigger sync page #3652

Merged
merged 4 commits into from
Nov 5, 2024
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 docs/how-tos/collect-sensor-data.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ With cloud sync enabled, captured data is automatically uploaded to the Viam app
{{< /table >}}

{{< alert title="Tip" color="tip" >}}
If you need to sync data conditionally, for example at a certain time, see [Trigger Sync](/how-tos/trigger-sync/#configure-the-data-manager-to-sync-based-on-sensor).
If you need to sync data conditionally, for example at a certain time, see [Conditional Sync](/how-tos/conditional-sync/#configure-the-data-manager-to-sync-based-on-sensor).
{{< /alert >}}

## View sensor data
Expand Down
38 changes: 21 additions & 17 deletions docs/how-tos/trigger-sync.md → docs/how-tos/conditional-sync.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
---
title: "Trigger cloud sync conditionally"
linkTitle: "Trigger data sync conditionally"
title: "Conditional cloud sync"
linkTitle: "Conditional data sync"
description: "Trigger cloud sync to sync captured data when custom conditions are met."
type: "docs"
tags: ["data management", "cloud", "sync"]
images: ["/services/icons/data-cloud-sync.svg"]
icon: true
aliases:
- /data/trigger-sync/
- /how-tos/trigger-sync/
- /services/data/trigger-sync/
languages: []
viamresources: ["sensor", "data_manager"]
Expand All @@ -23,15 +24,13 @@ For example, if you rely on mobile data but have intermittent WiFi connection in
Or, you may want to trigger sync only when your machine detects an object of a certain color.
You can use the [trigger-sync-examples module](https://github.com/viam-labs/trigger-sync-examples-v2) if one of these examples is what you are looking for.

If you need different logic, this guide will show you the implementation of the [`sync-at-time:timesyncsensor`](https://app.viam.com/module/naomi/sync-at-time) module which triggers cloud sync based on a defined time interval.
If you need different logic, you can create a modular sensor that determines if the conditions for sync are met or not.
This guide will show you the implementation of a sensor which only allows sync during a defined time interval.
You can use it as the basis of your own custom logic.

{{% alert title="In this page" color="tip" %}}

1. [Use the `CreateShouldSyncReading` function to enable sync](#use-the-createshouldsyncreading-function-to-enable-sync)
1. [Add and configure sensor to determine when to sync](#add-your-sensor-to-determine-when-to-sync)
1. [Configure the data manager to sync based on the sensor](#configure-the-data-manager-to-sync-based-on-sensor)
1. [Test your sync configuration](#test-your-sync-configuration)
{{% toc %}}

{{% /alert %}}

Expand Down Expand Up @@ -65,27 +64,26 @@ It can then return true during the specified sync time interval and false otherw

{{% /expand%}}

## Use the `CreateShouldSyncReading` function to enable sync
## Return `should_sync` as a reading from a sensor

Regardless of the specifics of your trigger sync logic, to trigger sync your sensor needs to pass `true` to the [CreateShouldSyncReading function](https://pkg.go.dev/go.viam.com/rdk/services/datamanager#CreateShouldSyncReading) within the definition of your modular sensor's `Readings` function.
If the builtin data manager is configured with a sync sensor, the data manager will check the sensor's `Readings` method for a response with a "should_sync" key.

The `sync-at-time` sensor does not sense time despite being named a time sensor.
It _senses_ whether the data manager should sync or not and determines this based on the time of day.
During the configured time of the day, the code will pass `true` to the [CreateShouldSyncReading function](https://pkg.go.dev/go.viam.com/rdk/services/datamanager#CreateShouldSyncReading) which will enable syncing:
The following example returns `"should_sync": true` if the current time is in a specified time window, and `"should_sync": false` otherwise.

```go {class="line-numbers linkable-line-numbers" data-line="7,12,28"}
```go {class="line-numbers linkable-line-numbers" data-line="26,30,31,35"}
func (s *timeSyncer) Readings(context.Context, map[string]interface{}) (map[string]interface{}, error) {
currentTime := time.Now()
var hStart, mStart, sStart, hEnd, mEnd, sEnd int
n, err := fmt.Sscanf(s.start, "%d:%d:%d", &hStart, &mStart, &sStart)

if err != nil || n != 3 {
s.logger.Error("Start time is not in the format HH:MM:SS.")
return datamanager.CreateShouldSyncReading(false), err
return nil, err
}
m, err := fmt.Sscanf(s.end, "%d:%d:%d", &hEnd, &mEnd, &sEnd)
if err != nil || m != 3 {
s.logger.Error("End time is not in the format HH:MM:SS.")
return datamanager.CreateShouldSyncReading(false), err
return nil, err
}

zone, err := time.LoadLocation(s.zone)
Expand All @@ -98,17 +96,23 @@ func (s *timeSyncer) Readings(context.Context, map[string]interface{}) (map[stri
endTime := time.Date(currentTime.Year(), currentTime.Month(), currentTime.Day(),
hEnd, mEnd, sEnd, 0, zone)

readings := map[string]interface{}{"should_sync": false}
// If it is between the start and end time, sync.
if currentTime.After(startTime) && currentTime.Before(endTime) {
s.logger.Info("Syncing")
return datamanager.CreateShouldSyncReading(true), nil
readings["should_sync"] = true
return readings, nil
}

// Otherwise, do not sync.
return datamanager.CreateShouldSyncReading(false), nil
return readings, nil
}
```

{{< alert title="Note" color="note" >}}
You can return other readings alongside the `should_sync` value.
{{< /alert >}}

If you wish to see more context, see the entire [implementation of the sensor on GitHub](https://github.com/viam-labs/sync-at-time/blob/main/timesyncsensor/timesyncsensor.go).

For additional examples, see the `Readings` function of the [time-interval-trigger code](https://github.com/viam-labs/trigger-sync-examples-v2/blob/main/time-interval-trigger/selective_sync/selective_sync.go) and the [color-trigger code](https://github.com/viam-labs/trigger-sync-examples-v2/blob/main/color-trigger/selective_sync/selective_sync.go).
Expand Down
2 changes: 1 addition & 1 deletion docs/how-tos/image-data.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ You can test the vision service from the [**CONTROL** tab](/cloud/machines/#cont
**7. (Optional) Trigger sync with custom logic**

By default, the captured data syncs at the regular interval you specified in the data capture config.
If you need to trigger sync in a different way, see [Trigger cloud sync conditionally](/how-tos/trigger-sync/) for a documented example of syncing data only at certain times of day.
If you need to trigger sync in a different way, see [Conditional cloud sync](/how-tos/conditional-sync/) for a documented example of syncing data only at certain times of day.

{{% /tablestep %}}
{{< /table >}}
Expand Down
2 changes: 1 addition & 1 deletion docs/services/data/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ You can also access data from a resource, machine part, or machine menu.

- **Sync data conditionally**: You can use a {{< glossary_tooltip term_id="module" text="module" >}} to sync data only when a certain logic condition is met, instead of at a regular time interval.
For example, if you rely on mobile data but have intermittent WiFi connection in certain locations or at certain times of the day, you may want to trigger sync to only occur when these conditions are met.
To set up triggers for syncing see [Trigger cloud sync conditionally](/how-tos/trigger-sync/).
To set up triggers for syncing see [Conditional cloud sync](/how-tos/conditional-sync/).

## API

Expand Down
Loading