Update BackgroundServiceDelegate.mc

Put check for methods with 'has' back in.
This commit is contained in:
Philip Abbey
2024-07-22 22:18:27 +01:00
parent 7bd5e98a1a
commit 9030a00d7d

View File

@ -56,23 +56,27 @@ class BackgroundServiceDelegate extends System.ServiceDelegate {
} else if (!System.getDeviceSettings().connectionAvailable) { } else if (!System.getDeviceSettings().connectionAvailable) {
// System.println("BackgroundServiceDelegate onTemporalEvent(): No Internet connection, skipping API call."); // System.println("BackgroundServiceDelegate onTemporalEvent(): No Internet connection, skipping API call.");
} else { } else {
var activity = Activity.getProfileInfo().sport; var activity = null;
var sub_activity = Activity.getProfileInfo().subSport; var sub_activity = null;
// We need to check if we are actually tracking any activity as the enumerated type does not include "No Sport". if ((Activity has :getActivityInfo) and (Activity has :getProfileInfo)) {
if ((Activity.getActivityInfo() != null) and activity = Activity.getProfileInfo().sport;
((Activity.getActivityInfo().elapsedTime == null) or sub_activity = Activity.getProfileInfo().subSport;
(Activity.getActivityInfo().elapsedTime == 0))) { // We need to check if we are actually tracking any activity as the enumerated type does not include "No Sport".
// Indicate no activity with -1, not part of Garmin's activity codes. if ((Activity.getActivityInfo() != null) and
// https://developer.garmin.com/connect-iq/api-docs/Toybox/Activity.html#Sport-module ((Activity.getActivityInfo().elapsedTime == null) or
activity = -1; (Activity.getActivityInfo().elapsedTime == 0))) {
sub_activity = -1; // 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); // System.println("BackgroundServiceDelegate onTemporalEvent(): Event triggered, activity = " + activity + " sub_activity = " + sub_activity);
doUpdate(activity, sub_activity); doUpdate(activity, sub_activity);
} }
} }
private function doUpdate(activity as Lang.Number, sub_activity as Lang.Number) { private function doUpdate(activity as Lang.Number or Null, sub_activity as Lang.Number or Null) {
// System.println("BackgroundServiceDelegate onTemporalEvent(): Making API call."); // System.println("BackgroundServiceDelegate onTemporalEvent(): Making API call.");
var position = Position.getInfo(); var position = Position.getInfo();
// System.println("BackgroundServiceDelegate onTemporalEvent(): gps: " + position.position.toDegrees()); // System.println("BackgroundServiceDelegate onTemporalEvent(): gps: " + position.position.toDegrees());
@ -132,12 +136,14 @@ class BackgroundServiceDelegate extends System.ServiceDelegate {
"unique_id" => "battery_is_charging" "unique_id" => "battery_is_charging"
} }
]; ];
if ((Activity has :getActivityInfo) and (Activity has :getProfileInfo)) { if (activity != null) {
data.add({ data.add({
"state" => activity, "state" => activity,
"type" => "sensor", "type" => "sensor",
"unique_id" => "activity" "unique_id" => "activity"
}); });
}
if (sub_activity != null) {
data.add({ data.add({
"state" => sub_activity, "state" => sub_activity,
"type" => "sensor", "type" => "sensor",