Conditional cloud sync

By default, viam-server checks for new data to sync at the configured interval (sync_interval_mins). You can additionally configure sync to only happen when certain conditions are met. For example:

  • Only sync when on WiFi
  • Sync when conditions are met or events are detected
  • Sync during certain time windows

To sync only when certain conditions are met, you must provide the data manager a sensor that returns "should_sync": true when sync should happen and "should_sync": false otherwise.

This page shows 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.

You can also view the trigger-sync-examples module for more examples.

Prerequisites

A running machine connected to Viam. Click to see instructions.

Add a new machine on Viam. Then follow the setup instructions to install viam-server or viam-micro-server on the device you’re using for your project and connect to Viam.

Wait until your machine has successfully connected.

Enable data capture and sync on your machine.

Add the data management service:

On your machine’s CONFIGURE tab, click the + icon next to your machine part in the left-hand menu and select Component or service.

Select the data management / RDK service and click Create. You can leave the default data sync interval of 0.1 minutes to sync every 6 seconds. Also leave both Capturing and Syncing toggles in the “on” position.

Create a sensor module. Click to see instructions.

Start by creating a sensor module.

Your sensor should have access to the information you need to determine if your machine should sync or not. Based on that data, make the sensor return true when the machine should sync and false when it should not.

For example, if you want your machine to sync data only during a specific time interval, your sensor needs to be able to access the time as well as be configured with the time interval during which you would like to sync data. It can then return true during the specified sync time interval and false otherwise.

Return should_sync as a reading from a sensor

The following example implementation of the Readings() method returns "should_sync": true if the current time is in a specified time window, and "should_sync": false otherwise.

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 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 nil, err
    }

    zone, err := time.LoadLocation(s.zone)
    if err != nil {
        s.logger.Error("Time zone cannot be loaded: ", s.zone)
        return nil, err
    }

    startTime := time.Date(currentTime.Year(), currentTime.Month(), currentTime.Day(),
        hStart, mStart, sStart, 0, zone)
    endTime := time.Date(currentTime.Year(), currentTime.Month(), currentTime.Day(),
        hEnd, mEnd, sEnd, 0, zone)

    // Handle time windows that span midnight (e.g., 23:00 to 01:00)
    // If end time is earlier than or equal to start time, the window spans midnight
    if endTime.Before(startTime) || endTime.Equal(startTime) {
        // Add 24 hours to endTime to account for midnight crossover
        endTime = endTime.Add(24 * time.Hour)
    }

    readings := map[string]interface{}{"should_sync": false}
    readings["time"] = currentTime.String()

    // Check if current time is within the sync window
    if currentTime.After(startTime) && currentTime.Before(endTime) {
        s.logger.Debug("Syncing")
        readings["should_sync"] = true
        return readings, nil
    }

    // Otherwise, do not sync.
    s.logger.Debug("Not syncing. Current time not in sync window: " + currentTime.String())
    return readings, nil
}

You must also update the Config and resource struct to include the required attributes and update the init() and Validate() functions appropriately. Check the implementation of the sensor on GitHub for more detailed information.

For additional examples, see the Readings function of the time-interval-trigger code and the color-trigger code.

Add your sensor to determine when to sync

This section shows how to add and configure sync-at-time:timesyncsensor. If you created your own sensor module following the previous section, you will need to follow similar steps with your module:

1

Add the sensor to your machine

On your machine’s CONFIGURE tab, click the + button next to your machine part in the left menu. Select Component or service, then search for and select the sync-at-time:timesyncsensor model provided by the sync-at-time module.

Click Add module, then enter a name or use the suggested name for your sensor and click Create.

2

Configure your time frame

Go to the new component panel and copy and paste the following attribute template into your sensor’s attributes field:

{
  "start": "HH:MM:SS",
  "end": "HH:MM:SS",
  "zone": "<TIMEZONE>"
}
{
  "start": "18:29:00",
  "end": "18:30:00",
  "zone": "CET"
}

The following attributes are available for the naomi:sync-at-time:timesyncsensor sensor:

NameTypeRequired?Description
startstringRequiredThe start time for the time frame during which you want to sync. Example: "14:10:00".
endstringRequiredThe end of the sync time frame, for example: "15:35:00".
zonestringRequiredThe time zone for the start and end time, for example: "CET".

In the next step, you will configure the data manager to take the sensor into account when syncing.

Configure the data manager to sync based on sensor

On your machine’s CONFIGURE tab, switch to JSON mode and add a selective_syncer_name field with the name of the sensor you configured, and add the sensor to the depends_on field:

{
  "name": "data_manager-1",
  "api": "rdk:service:data_manager",
  "model": "rdk:builtin:builtin",
  "attributes": {
    "additional_sync_paths": [],
    "capture_dir": "",
    "capture_disabled": false,
    "selective_syncer_name": "<SENSOR-NAME>",
    "sync_disabled": false,
    "sync_interval_mins": 0.1,
    "tags": []
  },
  "depends_on": ["<SENSOR-NAME>"]
}
{
  "name": "datamanager",
  "api": "rdk:service:data_manager",
  "model": "rdk:builtin:builtin",
  "attributes": {
    "additional_sync_paths": [],
    "selective_syncer_name": "timesensor",
    "sync_interval_mins": 0.1,
    "capture_dir": "",
    "tags": []
  },
  "depends_on": ["timesensor"]
}
Click to view a full configuration example
{
  "components": [
    {
      "name": "camera-1",
      "api": "rdk:component:camera",
      "model": "webcam",
      "attributes": {
        "video_path": "0x114000005a39331"
      }
    },
    {
      "name": "timesensor",
      "api": "rdk:component:sensor",
      "model": "naomi:sync-at-time:timesyncsensor",
      "attributes": {
        "start": "18:29:00",
        "end": "18:30:00",
        "zone": "CET"
      }
    }
  ],
  "services": [
    {
      "name": "data_manager-1",
      "api": "rdk:service:data_manager",
      "model": "rdk:builtin:builtin",
      "attributes": {
        "capture_dir": "",
        "tags": [],
        "additional_sync_paths": [],
        "selective_syncer_name": "timesensor",
        "sync_interval_mins": 0.1
      },
      "depends_on": ["timesensor"]
    }
  ],
  "modules": [
    {
      "type": "registry",
      "name": "naomi_sync-at-time",
      "module_id": "naomi:sync-at-time",
      "version": "3.0.3"
    }
  ]
}

You have now configured sync to happen during a specific time slot.

Test your sync configuration

To test your setup, configure a webcam or another component and enable data capture on the component. Make sure to physically connect any hardware parts to the computer controlling your machine. For a camera component, use the ReadImage method. The data manager will now capture data. Go to the CONTROL tab. You should see the sensor. Click on GetReadings.

Control tab with sensor panel

If you are in the time frame for sync, the time sync sensor will return true. If you are not in the time frame, it will return false.

To verify that sync is working:

  1. If the sensor returns true and data has been captured, go to the Data tab to see your synced data.
  2. If the sensor returns false but you want to test sync, adjust the configuration of your time sync sensor to include the current time.
  3. Check again on the CONTROL tab to verify the sensor reading, and on the Data tab to confirm data is syncing.