Skip to content

Commit

Permalink
Update implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
npentrel authored Nov 5, 2024
1 parent 2115172 commit cbbe2db
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 44 deletions.
41 changes: 2 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,48 +55,11 @@ The following attributes are available for the `naomi:sync-at-time:timesyncsenso
}
```

To ensure the data manager starts up before the sensor, add this `service_config` to the component configuration on the **JSON** tab.

```json
"service_configs": [
{
"attributes": {
"capture_methods": []
},
"type": "data_manager"
}
]
```

The entire component configuration should resemble this:

```json
{
"attributes": {
"start": "14:10:00",
"end": "15:35:00",
"zone": "CET"
},
"depends_on": [],
"name": "timesensor",
"model": "naomi:sync-at-time:timesyncsensor",
"type": "sensor",
"namespace": "rdk",
"service_configs": [
{
"attributes": {
"capture_methods": []
},
"type": "data_manager"
}
]
}
```

To ensure the sensor starts before the data manager, add the sensor to the dependencies of the data manager.

### Configure data manager

On your machine's **Config** tab, switch to **JSON** mode and add a `selective_syncer_name` with the name for the sensor you configured:
On your machine's **Config** tab, switch to **JSON** mode and add a `selective_syncer_name` with the name for the sensor you configured to the data manager config. Also add the sensor to the `depends_on` field:

```json
{
Expand Down
15 changes: 10 additions & 5 deletions timesyncsensor/timesyncsensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,15 @@ func (s *timeSyncer) Readings(context.Context, map[string]interface{}) (map[stri
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 @@ -135,14 +136,18 @@ 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}
readings["time"] = currentTime
// 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
s.logger.Debug("Syncing")
readings["should_sync"] = true
return readings, nil
}

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

// Close closes the underlying generic.
Expand Down

0 comments on commit cbbe2db

Please sign in to comment.