mirror of
https://github.com/house-of-abbey/GarminHomeAssistant.git
synced 2025-09-20 23:31:31 +00:00
GPS and current activity in background service
This commit is contained in:
@@ -25,7 +25,8 @@
|
|||||||
Use "Monkey C: Edit Application" from the Visual Studio Code command palette
|
Use "Monkey C: Edit Application" from the Visual Studio Code command palette
|
||||||
to update the application attributes.
|
to update the application attributes.
|
||||||
-->
|
-->
|
||||||
<iq:application id="4901cdfb-b4a2-4f33-96c7-f5be5992809e" type="widget" name="@Strings.AppName" entry="HomeAssistantApp" launcherIcon="@Drawables.LauncherIcon" minApiLevel="3.1.0">
|
<iq:application id="4901cdfb-b4a2-4f33-96c7-f5be5992809e" type="widget" name="@Strings.AppName"
|
||||||
|
entry="HomeAssistantApp" launcherIcon="@Drawables.LauncherIcon" minApiLevel="3.1.0">
|
||||||
<!--
|
<!--
|
||||||
Use the following from the Visual Studio Code comand palette to edit
|
Use the following from the Visual Studio Code comand palette to edit
|
||||||
the build targets:
|
the build targets:
|
||||||
@@ -143,6 +144,7 @@
|
|||||||
<iq:uses-permission id="Background" />
|
<iq:uses-permission id="Background" />
|
||||||
<iq:uses-permission id="BluetoothLowEnergy" />
|
<iq:uses-permission id="BluetoothLowEnergy" />
|
||||||
<iq:uses-permission id="Communications" />
|
<iq:uses-permission id="Communications" />
|
||||||
|
<iq:uses-permission id="Positioning" />
|
||||||
</iq:permissions>
|
</iq:permissions>
|
||||||
<!--
|
<!--
|
||||||
Use "Monkey C: Edit Languages" from the Visual Studio Code command
|
Use "Monkey C: Edit Languages" from the Visual Studio Code command
|
||||||
|
@@ -28,9 +28,11 @@
|
|||||||
Testing in VSCode requires monkey.jungle, so for convenience, swap between
|
Testing in VSCode requires monkey.jungle, so for convenience, swap between
|
||||||
watch-app and widget by changing which of the next two lines are commented out
|
watch-app and widget by changing which of the next two lines are commented out
|
||||||
-->
|
-->
|
||||||
<iq:application id="98c36259-498a-4458-9cef-74a273ad2bc3" type="watch-app" name="@Strings.AppName" entry="HomeAssistantApp" launcherIcon="@Drawables.LauncherIcon" minApiLevel="3.1.0">
|
<iq:application id="98c36259-498a-4458-9cef-74a273ad2bc3" type="watch-app" name="@Strings.AppName"
|
||||||
|
entry="HomeAssistantApp" launcherIcon="@Drawables.LauncherIcon" minApiLevel="3.1.0">
|
||||||
<!--
|
<!--
|
||||||
<iq:application id="4901cdfb-b4a2-4f33-96c7-f5be5992809e" type="widget" name="@Strings.AppName" entry="HomeAssistantApp" launcherIcon="@Drawables.LauncherIcon" minApiLevel="3.1.0">
|
<iq:application id="4901cdfb-b4a2-4f33-96c7-f5be5992809e" type="widget" name="@Strings.AppName"
|
||||||
|
entry="HomeAssistantApp" launcherIcon="@Drawables.LauncherIcon" minApiLevel="3.1.0">
|
||||||
-->
|
-->
|
||||||
<!--
|
<!--
|
||||||
Use the following from the Visual Studio Code comand palette to edit
|
Use the following from the Visual Studio Code comand palette to edit
|
||||||
@@ -149,6 +151,7 @@
|
|||||||
<iq:uses-permission id="Background" />
|
<iq:uses-permission id="Background" />
|
||||||
<iq:uses-permission id="BluetoothLowEnergy" />
|
<iq:uses-permission id="BluetoothLowEnergy" />
|
||||||
<iq:uses-permission id="Communications" />
|
<iq:uses-permission id="Communications" />
|
||||||
|
<iq:uses-permission id="Positioning" />
|
||||||
</iq:permissions>
|
</iq:permissions>
|
||||||
<!--
|
<!--
|
||||||
Use "Monkey C: Edit Languages" from the Visual Studio Code command
|
Use "Monkey C: Edit Languages" from the Visual Studio Code command
|
||||||
|
@@ -43,7 +43,38 @@ 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 {
|
||||||
|
// System.println("BackgroundServiceDelegate onTemporalEvent(): Making API call.");
|
||||||
|
var position = Position.getInfo();
|
||||||
|
// System.println("BackgroundServiceDelegate onTemporalEvent(): gps: " + position.position.toDegrees());
|
||||||
|
// System.println("BackgroundServiceDelegate onTemporalEvent(): speed: " + position.speed);
|
||||||
|
// System.println("BackgroundServiceDelegate onTemporalEvent(): course: " + position.heading + "rad (" + (position.heading * 180 / Math.PI) + "°)");
|
||||||
|
// System.println("BackgroundServiceDelegate onTemporalEvent(): altitude: " + position.altitude);
|
||||||
|
// System.println("BackgroundServiceDelegate onTemporalEvent(): battery: " + System.getSystemStats().battery);
|
||||||
|
// System.println("BackgroundServiceDelegate onTemporalEvent(): charging: " + System.getSystemStats().charging);
|
||||||
|
// System.println("BackgroundServiceDelegate onTemporalEvent(): activity: " + Activity.getProfileInfo().name);
|
||||||
|
|
||||||
// Don't use Settings.* here as the object lasts < 30 secs and is recreated each time the background service is run
|
// Don't use Settings.* here as the object lasts < 30 secs and is recreated each time the background service is run
|
||||||
|
Communications.makeWebRequest(
|
||||||
|
(Properties.getValue("api_url") as Lang.String) + "/webhook/" + (Properties.getValue("webhook_id") as Lang.String),
|
||||||
|
{
|
||||||
|
"type" => "update_location",
|
||||||
|
"data" => {
|
||||||
|
"gps" => position.position.toDegrees(),
|
||||||
|
"gps_accuracy" => 10,
|
||||||
|
"speed" => Math.round(position.speed),
|
||||||
|
"course" => Math.round(position.heading * 180 / Math.PI),
|
||||||
|
"altitude" => Math.round(position.altitude),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
:method => Communications.HTTP_REQUEST_METHOD_POST,
|
||||||
|
:headers => {
|
||||||
|
"Content-Type" => Communications.REQUEST_CONTENT_TYPE_JSON
|
||||||
|
},
|
||||||
|
:responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON
|
||||||
|
},
|
||||||
|
method(:onReturnBatteryUpdate)
|
||||||
|
);
|
||||||
Communications.makeWebRequest(
|
Communications.makeWebRequest(
|
||||||
(Properties.getValue("api_url") as Lang.String) + "/webhook/" + (Properties.getValue("webhook_id") as Lang.String),
|
(Properties.getValue("api_url") as Lang.String) + "/webhook/" + (Properties.getValue("webhook_id") as Lang.String),
|
||||||
{
|
{
|
||||||
@@ -58,6 +89,11 @@ class BackgroundServiceDelegate extends System.ServiceDelegate {
|
|||||||
"state" => System.getSystemStats().charging,
|
"state" => System.getSystemStats().charging,
|
||||||
"type" => "binary_sensor",
|
"type" => "binary_sensor",
|
||||||
"unique_id" => "battery_is_charging"
|
"unique_id" => "battery_is_charging"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"state" => Activity.getProfileInfo().name,
|
||||||
|
"type" => "sensor",
|
||||||
|
"unique_id" => "activity"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
@@ -62,10 +62,12 @@ class WebhookManager {
|
|||||||
ErrorView.show(WatchUi.loadResource($.Rez.Strings.WebhookFailed) as Lang.String + "\n" + WatchUi.loadResource($.Rez.Strings.ApiUrlNotFound) as Lang.String);
|
ErrorView.show(WatchUi.loadResource($.Rez.Strings.WebhookFailed) as Lang.String + "\n" + WatchUi.loadResource($.Rez.Strings.ApiUrlNotFound) as Lang.String);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case 200:
|
||||||
case 201:
|
case 201:
|
||||||
var id = data.get("webhook_id") as Lang.String or Null;
|
var id = data.get("webhook_id") as Lang.String or Null;
|
||||||
if (id != null) {
|
if (id != null) {
|
||||||
Settings.setWebhookId(id);
|
Settings.setWebhookId(id);
|
||||||
|
// System.println("WebhookManager onReturnRegisterWebhookSensor(): Registering first sensor: Battery Level");
|
||||||
registerWebhookSensor({
|
registerWebhookSensor({
|
||||||
"device_class" => "battery",
|
"device_class" => "battery",
|
||||||
"name" => "Battery Level",
|
"name" => "Battery Level",
|
||||||
@@ -76,16 +78,7 @@ class WebhookManager {
|
|||||||
"state_class" => "measurement",
|
"state_class" => "measurement",
|
||||||
"entity_category" => "diagnostic",
|
"entity_category" => "diagnostic",
|
||||||
"disabled" => false
|
"disabled" => false
|
||||||
});
|
}, 0);
|
||||||
registerWebhookSensor({
|
|
||||||
"device_class" => "battery_charging",
|
|
||||||
"name" => "Battery is Charging",
|
|
||||||
"state" => System.getSystemStats().charging,
|
|
||||||
"type" => "binary_sensor",
|
|
||||||
"unique_id" => "battery_is_charging",
|
|
||||||
"entity_category" => "diagnostic",
|
|
||||||
"disabled" => false
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
// System.println("WebhookManager onReturnRequestWebhookId(): No webhook id in response data.");
|
// System.println("WebhookManager onReturnRequestWebhookId(): No webhook id in response data.");
|
||||||
Settings.unsetIsBatteryLevelEnabled();
|
Settings.unsetIsBatteryLevelEnabled();
|
||||||
@@ -129,7 +122,7 @@ class WebhookManager {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function onReturnRegisterWebhookSensor(responseCode as Lang.Number, data as Null or Lang.Dictionary or Lang.String) as Void {
|
function onReturnRegisterWebhookSensor(responseCode as Lang.Number, data as Null or Lang.Dictionary or Lang.String, step as Lang.Number) as Void {
|
||||||
switch (responseCode) {
|
switch (responseCode) {
|
||||||
case Communications.BLE_HOST_TIMEOUT:
|
case Communications.BLE_HOST_TIMEOUT:
|
||||||
case Communications.BLE_CONNECTION_UNAVAILABLE:
|
case Communications.BLE_CONNECTION_UNAVAILABLE:
|
||||||
@@ -155,6 +148,7 @@ class WebhookManager {
|
|||||||
Settings.unsetWebhookId();
|
Settings.unsetWebhookId();
|
||||||
// Ignore and see if we can carry on
|
// Ignore and see if we can carry on
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Communications.INVALID_HTTP_BODY_IN_NETWORK_RESPONSE:
|
case Communications.INVALID_HTTP_BODY_IN_NETWORK_RESPONSE:
|
||||||
// System.println("WebhookManager onReturnRegisterWebhookSensor() Response Code: INVALID_HTTP_BODY_IN_NETWORK_RESPONSE, check JSON is returned.");
|
// System.println("WebhookManager onReturnRegisterWebhookSensor() Response Code: INVALID_HTTP_BODY_IN_NETWORK_RESPONSE, check JSON is returned.");
|
||||||
Settings.unsetWebhookId();
|
Settings.unsetWebhookId();
|
||||||
@@ -169,11 +163,36 @@ class WebhookManager {
|
|||||||
ErrorView.show(WatchUi.loadResource($.Rez.Strings.WebhookFailed) as Lang.String + "\n" + WatchUi.loadResource($.Rez.Strings.ApiUrlNotFound) as Lang.String);
|
ErrorView.show(WatchUi.loadResource($.Rez.Strings.WebhookFailed) as Lang.String + "\n" + WatchUi.loadResource($.Rez.Strings.ApiUrlNotFound) as Lang.String);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case 200:
|
||||||
case 201:
|
case 201:
|
||||||
if ((data.get("success") as Lang.Boolean or Null) != true) {
|
if ((data.get("success") as Lang.Boolean or Null) != false) {
|
||||||
// When uncommenting, invert the condition above.
|
|
||||||
// System.println("WebhookManager onReturnRegisterWebhookSensor(): Success");
|
// System.println("WebhookManager onReturnRegisterWebhookSensor(): Success");
|
||||||
// } else {
|
switch (step) {
|
||||||
|
case 0:
|
||||||
|
// System.println("WebhookManager onReturnRegisterWebhookSensor(): Registering next sensor: Battery is Charging");
|
||||||
|
registerWebhookSensor({
|
||||||
|
"device_class" => "battery_charging",
|
||||||
|
"name" => "Battery is Charging",
|
||||||
|
"state" => System.getSystemStats().charging,
|
||||||
|
"type" => "binary_sensor",
|
||||||
|
"unique_id" => "battery_is_charging",
|
||||||
|
"entity_category" => "diagnostic",
|
||||||
|
"disabled" => false
|
||||||
|
}, 1);
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
// System.println("WebhookManager onReturnRegisterWebhookSensor(): Registering next sensor: Activity");
|
||||||
|
registerWebhookSensor({
|
||||||
|
"name" => "Activity",
|
||||||
|
"state" => Activity.getProfileInfo().name,
|
||||||
|
"type" => "sensor",
|
||||||
|
"unique_id" => "activity",
|
||||||
|
"disabled" => false
|
||||||
|
}, 2);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
} else {
|
||||||
// System.println("WebhookManager onReturnRegisterWebhookSensor(): Failure");
|
// System.println("WebhookManager onReturnRegisterWebhookSensor(): Failure");
|
||||||
Settings.unsetWebhookId();
|
Settings.unsetWebhookId();
|
||||||
Settings.unsetIsBatteryLevelEnabled();
|
Settings.unsetIsBatteryLevelEnabled();
|
||||||
@@ -189,7 +208,7 @@ class WebhookManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function registerWebhookSensor(sensor as Lang.Object) {
|
function registerWebhookSensor(sensor as Lang.Object, step as Lang.Number) {
|
||||||
// System.println("WebhookManager registerWebhookSensor(): Registering webhook sensor: " + sensor.toString());
|
// System.println("WebhookManager registerWebhookSensor(): Registering webhook sensor: " + sensor.toString());
|
||||||
Communications.makeWebRequest(
|
Communications.makeWebRequest(
|
||||||
Settings.getApiUrl() + "/webhook/" + Settings.getWebhookId(),
|
Settings.getApiUrl() + "/webhook/" + Settings.getWebhookId(),
|
||||||
@@ -202,7 +221,8 @@ class WebhookManager {
|
|||||||
:headers => {
|
:headers => {
|
||||||
"Content-Type" => Communications.REQUEST_CONTENT_TYPE_JSON
|
"Content-Type" => Communications.REQUEST_CONTENT_TYPE_JSON
|
||||||
},
|
},
|
||||||
:responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON
|
:responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON,
|
||||||
|
:context => step
|
||||||
},
|
},
|
||||||
method(:onReturnRegisterWebhookSensor)
|
method(:onReturnRegisterWebhookSensor)
|
||||||
);
|
);
|
||||||
|
Reference in New Issue
Block a user