Added update to telemetry on completion of an activity

This commit is contained in:
Philip Abbey
2024-07-20 17:28:57 +01:00
parent c18736e40d
commit 98af5578f0
2 changed files with 103 additions and 82 deletions

View File

@ -23,6 +23,7 @@ using Toybox.Lang;
using Toybox.Application.Properties;
using Toybox.Background;
using Toybox.System;
using Toybox.Activity;
(:background)
class BackgroundServiceDelegate extends System.ServiceDelegate {
@ -37,12 +38,41 @@ class BackgroundServiceDelegate extends System.ServiceDelegate {
Background.exit(null);
}
function onActivityCompleted(activity as { :sport as Activity.Sport, :subSport as Activity.SubSport }) as Void {
if (!System.getDeviceSettings().phoneConnected) {
// System.println("BackgroundServiceDelegate onActivityCompleted(): No Phone connection, skipping API call.");
} else if (!System.getDeviceSettings().connectionAvailable) {
// System.println("BackgroundServiceDelegate onActivityCompleted(): No Internet connection, skipping API call.");
} else {
// Ensure we're logging completion, i.e. ignore 'activity' parameter
// System.println("BackgroundServiceDelegate onActivityCompleted(): Event triggered");
doUpdate(-1, -1);
}
}
function onTemporalEvent() as Void {
if (!System.getDeviceSettings().phoneConnected) {
// System.println("BackgroundServiceDelegate onTemporalEvent(): No Phone connection, skipping API call.");
} else if (!System.getDeviceSettings().connectionAvailable) {
// System.println("BackgroundServiceDelegate onTemporalEvent(): No Internet connection, skipping API call.");
} else {
var activity = Activity.getProfileInfo().sport;
var sub_activity = Activity.getProfileInfo().subSport;
// We need to check if we are actually tracking any activity as the enumerated type does not include "No Sport".
if ((Activity.getActivityInfo() != null) and
((Activity.getActivityInfo().elapsedTime == null) or
(Activity.getActivityInfo().elapsedTime == 0))) {
// Indicate no activity with -1, not part of Garmin's activity codes.
// https://developer.garmin.com/connect-iq/api-docs/Toybox/Activity.html#Sport-module
activity = -1;
sub_activity = -1;
}
// System.println("BackgroundServiceDelegate onTemporalEvent(): Event triggered, activity = " + activity + " sub_activity = " + sub_activity);
doUpdate(activity, sub_activity);
}
}
private function doUpdate(activity as Lang.Number, sub_activity as Lang.Number) {
// System.println("BackgroundServiceDelegate onTemporalEvent(): Making API call.");
var position = Position.getInfo();
// System.println("BackgroundServiceDelegate onTemporalEvent(): gps: " + position.position.toDegrees());
@ -103,17 +133,6 @@ class BackgroundServiceDelegate extends System.ServiceDelegate {
}
];
if ((Activity has :getActivityInfo) and (Activity has :getProfileInfo)) {
var activity = Activity.getProfileInfo().sport;
var sub_activity = Activity.getProfileInfo().subSport;
// We need to check if we are actually tracking any activity as the enumerated type does not include "No Sport".
if ((Activity.getActivityInfo() != null) and
((Activity.getActivityInfo().elapsedTime == null) or
(Activity.getActivityInfo().elapsedTime == 0))) {
// Indicate no activity with -1, not part of Garmin's activity codes.
// https://developer.garmin.com/connect-iq/api-docs/Toybox/Activity.html#Sport-module
activity = -1;
sub_activity = -1;
}
data.add({
"state" => activity,
"type" => "sensor",
@ -141,6 +160,5 @@ class BackgroundServiceDelegate extends System.ServiceDelegate {
method(:onReturnBatteryUpdate)
);
}
}
}

View File

@ -95,9 +95,11 @@ class Settings {
if ((Background.getTemporalEventRegisteredTime() == null) or
(Background.getTemporalEventRegisteredTime() != (mBatteryRefreshRate * 60))) {
Background.registerForTemporalEvent(new Time.Duration(mBatteryRefreshRate * 60)); // Convert to seconds
Background.registerForActivityCompletedEvent();
}
} else if (Background.getTemporalEventRegisteredTime() != null) {
Background.deleteTemporalEvent();
Background.deleteActivityCompletedEvent();
}
} else {
// Explicitly disable the background event which persists when the application closes.
@ -182,6 +184,7 @@ class Settings {
Properties.setValue("enable_battery_level", mIsSensorsLevelEnabled);
if (mHasService and (Background.getTemporalEventRegisteredTime() != null)) {
Background.deleteTemporalEvent();
Background.deleteActivityCompletedEvent();
}
}