From 9030a00d7de9fc1a6cd2fd49c3c36d6ef97520e4 Mon Sep 17 00:00:00 2001 From: Philip Abbey Date: Mon, 22 Jul 2024 22:18:27 +0100 Subject: [PATCH] Update BackgroundServiceDelegate.mc Put check for methods with 'has' back in. --- source/BackgroundServiceDelegate.mc | 30 +++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/source/BackgroundServiceDelegate.mc b/source/BackgroundServiceDelegate.mc index 98537cf..91a1ef7 100644 --- a/source/BackgroundServiceDelegate.mc +++ b/source/BackgroundServiceDelegate.mc @@ -56,23 +56,27 @@ class BackgroundServiceDelegate extends System.ServiceDelegate { } 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; + var activity = null; + var sub_activity = null; + if ((Activity has :getActivityInfo) and (Activity has :getProfileInfo)) { + activity = Activity.getProfileInfo().sport; + 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) { + private function doUpdate(activity as Lang.Number or Null, sub_activity as Lang.Number or Null) { // System.println("BackgroundServiceDelegate onTemporalEvent(): Making API call."); var position = Position.getInfo(); // System.println("BackgroundServiceDelegate onTemporalEvent(): gps: " + position.position.toDegrees()); @@ -132,12 +136,14 @@ class BackgroundServiceDelegate extends System.ServiceDelegate { "unique_id" => "battery_is_charging" } ]; - if ((Activity has :getActivityInfo) and (Activity has :getProfileInfo)) { + if (activity != null) { data.add({ "state" => activity, "type" => "sensor", "unique_id" => "activity" }); + } + if (sub_activity != null) { data.add({ "state" => sub_activity, "type" => "sensor",