Trigger Cloud Sync Conditionally

You can use a 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. Or, you may want to trigger sync only when your machine detects an object of a certain color. The code for both of these examples is provided by the trigger-sync-examples module, and you can create your own module if you want to use different logic.

Example: sync-at-time

This module allows you to configure cloud sync to occur only at a specific time frame by implementing a sensor, naomi:sync-at-time:timesyncsensor, that enables sync within a specified time frame and disables sync outside that time frame.

To set up conditional syncing you need to:

  1. Create or use an existing module that implements a sensor component that uses your custom logic to determine when to enable and disable sync. While this sensor is not sensing the time it senses whether the data manager should sync or not.
  2. Change the configuration of the data manager to enable selective sync.

Requirements

Before configuring your sensor, you must create a machine and you also need to:

  1. Enable data capture.
  2. Enable cloud sync.

Add sensor to determine when to sync

In this example, you will configure sync to only trigger during a specific time frame of the day using an existing module sync-at-time:timesyncsensor. If you need to trigger sync based on a different condition, you need to create your own module and adjust the module logic accordingly. Additional examples are available in this GitHub Repo.

To use sync-at-time:timesyncsensor:

  1. Go to your machine’s CONFIGURE page and click Create component.

  2. Then select the sync-at-time:timesyncsensor model from the sync-at-time module.

  3. Click Add module, then enter a name or use the suggested name for your sensor and click Create. The sensor will return true and enable sync when in a specified time frame.

  4. To 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>"
    }
    
    {
      "name": "<SENSORNAME>",
      "model": "naomi:sync-at-time:timesyncsensor",
      "type": "sensor",
      "namespace": "rdk",
      "attributes": {
        "start": "HH:MM:SS",
        "end": "HH:MM:SS",
        "zone": "<TIMEZONE>"
      },
      "depends_on": []
    }
    
    {
      "name": "timesensor",
      "model": "naomi:sync-at-time:timesyncsensor",
      "type": "sensor",
      "namespace": "rdk",
      "attributes": {
        "start": "18:29:00",
        "end": "18:30:00",
        "zone": "CET"
      },
      "depends_on": []
    }
    

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

NameTypeInclusionDescription
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 data manager to sync based on sensor

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

{
  "name": "data_manager-1",
  "type": "data_manager",
  "namespace": "rdk",
  "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",
  "type": "data_manager",
  "namespace": "rdk",
  "attributes": {
    "additional_sync_paths": [],
    "selective_syncer_name": "timesensor",
    "sync_interval_mins": 0.2,
    "capture_dir": "",
    "tags": []
  },
  "depends_on": ["timesensor"]
}
Click to view a full configuration example

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. 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. You can confirm that no data is currently syncing by going to the Data tab. If you are not in the time frame for sync, adjust the configuration of your time sync sensor. Then check again on the CONTROL and Data tab to confirm data is syncing.

Next steps



Have questions, or want to meet other people working on robots? Join our Community Discord.

If you notice any issues with the documentation, feel free to file an issue or edit this file.