Add GPS accuracy to update_location webhook and changed activity reporting

This commit is contained in:
Joseph Abbey
2024-01-27 15:39:22 +00:00
parent 8f372c03e3
commit 6b3a17bea3
2 changed files with 55 additions and 23 deletions

View File

@ -54,27 +54,42 @@ class BackgroundServiceDelegate extends System.ServiceDelegate {
// System.println("BackgroundServiceDelegate onTemporalEvent(): activity: " + Activity.getProfileInfo().name); // 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), if (position.accuracy != Position.QUALITY_NOT_AVAILABLE && position.accuracy != Position.QUALITY_LAST_KNOWN) {
{ var accuracy = 0;
"type" => "update_location", switch (position.accuracy) {
"data" => { case Position.QUALITY_POOR:
"gps" => position.position.toDegrees(), accuracy = 500;
"gps_accuracy" => 10, break;
"speed" => Math.round(position.speed), case Position.QUALITY_USABLE:
"course" => Math.round(position.heading * 180 / Math.PI), accuracy = 100;
"altitude" => Math.round(position.altitude), break;
} case Position.QUALITY_GOOD:
}, accuracy = 10;
{ break;
:method => Communications.HTTP_REQUEST_METHOD_POST, }
:headers => { Communications.makeWebRequest(
"Content-Type" => Communications.REQUEST_CONTENT_TYPE_JSON (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" => accuracy,
"speed" => Math.round(position.speed),
"course" => Math.round(position.heading * 180 / Math.PI),
"altitude" => Math.round(position.altitude),
}
}, },
:responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON {
}, :method => Communications.HTTP_REQUEST_METHOD_POST,
method(:onReturnBatteryUpdate) :headers => {
); "Content-Type" => Communications.REQUEST_CONTENT_TYPE_JSON
},
:responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON
},
method(:onReturnBatteryUpdate)
);
}
var data = [ var data = [
{ {
"state" => System.getSystemStats().battery, "state" => System.getSystemStats().battery,
@ -89,10 +104,15 @@ class BackgroundServiceDelegate extends System.ServiceDelegate {
]; ];
if (Activity has :getProfileInfo) { if (Activity has :getProfileInfo) {
data.add({ data.add({
"state" => Activity.getProfileInfo().name, "state" => Activity.getProfileInfo().sport,
"type" => "sensor", "type" => "sensor",
"unique_id" => "activity" "unique_id" => "activity"
}); });
data.add({
"state" => Activity.getProfileInfo().subSport,
"type" => "sensor",
"unique_id" => "sub_activity"
});
} }
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),

View File

@ -185,13 +185,25 @@ class WebhookManager {
if (Activity has :getProfileInfo) { if (Activity has :getProfileInfo) {
registerWebhookSensor({ registerWebhookSensor({
"name" => "Activity", "name" => "Activity",
"state" => Activity.getProfileInfo().name, "state" => Activity.getProfileInfo().sport,
"type" => "sensor", "type" => "sensor",
"unique_id" => "activity", "unique_id" => "activity",
"disabled" => false "disabled" => false
}, 2); }, 2);
break; break;
} }
case 2:
// System.println("WebhookManager onReturnRegisterWebhookSensor(): Registering next sensor: Activity");
if (Activity has :getProfileInfo) {
registerWebhookSensor({
"name" => "Sub-activity",
"state" => Activity.getProfileInfo().subSport,
"type" => "sensor",
"unique_id" => "sub_activity",
"disabled" => false
}, 3);
break;
}
default: default:
} }
} else { } else {