Skip to content

Commit

Permalink
Update trigger sync page
Browse files Browse the repository at this point in the history
  • Loading branch information
npentrel committed Nov 4, 2024
1 parent 959a5d6 commit 436e132
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions docs/how-tos/trigger-sync.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
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"]
Expand Down Expand Up @@ -28,7 +28,7 @@ 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. [Return `should_sync` in the `Readings()`](#return-should_sync-in-the-readings)
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)
Expand Down Expand Up @@ -65,27 +65,28 @@ 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` in the `Readings()`

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.

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:
Regardless of the specifics of your trigger sync logic, to trigger sync your sensor needs to return a value for `should_sync` as part of your modular sensor's `Readings` function.
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"}
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)
readings := map[string]interface{}{}

if err != nil || n != 3 {
s.logger.Error("Start time is not in the format HH:MM:SS.")
return datamanager.CreateShouldSyncReading(false), err
readings["should_sync"] = false
return readings, 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
readings["should_sync"] = false
return readings, err
}

zone, err := time.LoadLocation(s.zone)
Expand All @@ -101,14 +102,20 @@ func (s *timeSyncer) Readings(context.Context, map[string]interface{}) (map[stri
// 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"] = false
return readings, nil
}

// Otherwise, do not sync.
return datamanager.CreateShouldSyncReading(false), nil
readings["should_sync"] = false
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

0 comments on commit 436e132

Please sign in to comment.