mirror of
https://github.com/house-of-abbey/GarminHomeAssistant.git
synced 2025-06-20 05:18:38 +00:00
2 fixes as required by issue 185
Solution to a race condition with starting updates and amendment to error message presentation on Webhook ID creation failure. Co-Authored-By: Joseph Abbey <me@josephabbey.dev>
This commit is contained in:
@ -39,6 +39,7 @@ class HomeAssistantApp extends Application.AppBase {
|
|||||||
private var mIsApp as Lang.Boolean = false; // Or Widget
|
private var mIsApp as Lang.Boolean = false; // Or Widget
|
||||||
private var mUpdating as Lang.Boolean = false; // Don't start a second chain of updates
|
private var mUpdating as Lang.Boolean = false; // Don't start a second chain of updates
|
||||||
private var mTemplates as Lang.Dictionary = {};
|
private var mTemplates as Lang.Dictionary = {};
|
||||||
|
private var startUpdating as Lang.Boolean = false;
|
||||||
|
|
||||||
function initialize() {
|
function initialize() {
|
||||||
AppBase.initialize();
|
AppBase.initialize();
|
||||||
@ -259,29 +260,17 @@ class HomeAssistantApp extends Application.AppBase {
|
|||||||
private function buildMenu(menu as Lang.Dictionary) {
|
private function buildMenu(menu as Lang.Dictionary) {
|
||||||
mHaMenu = new HomeAssistantView(menu, null);
|
mHaMenu = new HomeAssistantView(menu, null);
|
||||||
mQuitTimer.begin();
|
mQuitTimer.begin();
|
||||||
|
if (startUpdating) {
|
||||||
|
startUpdates();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function startUpdates() {
|
function startUpdates() {
|
||||||
if (mHaMenu != null and !mUpdating) {
|
if (mHaMenu != null and !mUpdating) {
|
||||||
mItemsToUpdate = mHaMenu.getItemsToUpdate();
|
|
||||||
// Start the continuous update process that continues for as long as the application is running.
|
// Start the continuous update process that continues for as long as the application is running.
|
||||||
mTemplates = {};
|
|
||||||
for (var i = 0; i < mItemsToUpdate.size(); i++) {
|
|
||||||
var item = mItemsToUpdate[i];
|
|
||||||
var template = item.buildTemplate();
|
|
||||||
if (template != null) {
|
|
||||||
mTemplates.put(i.toString(), {
|
|
||||||
"template" => template
|
|
||||||
});
|
|
||||||
}
|
|
||||||
if (item instanceof HomeAssistantToggleMenuItem) {
|
|
||||||
mTemplates.put(i.toString() + "t", {
|
|
||||||
"template" => (item as HomeAssistantToggleMenuItem).buildToggleTemplate()
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
updateMenuItems();
|
updateMenuItems();
|
||||||
}
|
}
|
||||||
|
startUpdating = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function onReturnUpdateMenuItems(responseCode as Lang.Number, data as Null or Lang.Dictionary) as Void {
|
function onReturnUpdateMenuItems(responseCode as Lang.Number, data as Null or Lang.Dictionary) as Void {
|
||||||
@ -332,6 +321,8 @@ class HomeAssistantApp extends Application.AppBase {
|
|||||||
|
|
||||||
case 200:
|
case 200:
|
||||||
status = WatchUi.loadResource($.Rez.Strings.Available) as Lang.String;
|
status = WatchUi.loadResource($.Rez.Strings.Available) as Lang.String;
|
||||||
|
// System.println("mItemsToUpdate: " + mItemsToUpdate);
|
||||||
|
if (mItemsToUpdate != null) {
|
||||||
for (var i = 0; i < mItemsToUpdate.size(); i++) {
|
for (var i = 0; i < mItemsToUpdate.size(); i++) {
|
||||||
var item = mItemsToUpdate[i];
|
var item = mItemsToUpdate[i];
|
||||||
var state = data.get(i.toString());
|
var state = data.get(i.toString());
|
||||||
@ -346,6 +337,7 @@ class HomeAssistantApp extends Application.AppBase {
|
|||||||
} else {
|
} else {
|
||||||
updateMenuItems();
|
updateMenuItems();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
@ -365,6 +357,24 @@ class HomeAssistantApp extends Application.AppBase {
|
|||||||
ErrorView.show(WatchUi.loadResource($.Rez.Strings.NoInternet) as Lang.String + ".");
|
ErrorView.show(WatchUi.loadResource($.Rez.Strings.NoInternet) as Lang.String + ".");
|
||||||
setApiStatus(WatchUi.loadResource($.Rez.Strings.Unavailable) as Lang.String);
|
setApiStatus(WatchUi.loadResource($.Rez.Strings.Unavailable) as Lang.String);
|
||||||
} else {
|
} else {
|
||||||
|
if (mItemsToUpdate == null or mTemplates == null) {
|
||||||
|
mItemsToUpdate = mHaMenu.getItemsToUpdate();
|
||||||
|
mTemplates = {};
|
||||||
|
for (var i = 0; i < mItemsToUpdate.size(); i++) {
|
||||||
|
var item = mItemsToUpdate[i];
|
||||||
|
var template = item.buildTemplate();
|
||||||
|
if (template != null) {
|
||||||
|
mTemplates.put(i.toString(), {
|
||||||
|
"template" => template
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (item instanceof HomeAssistantToggleMenuItem) {
|
||||||
|
mTemplates.put(i.toString() + "t", {
|
||||||
|
"template" => (item as HomeAssistantToggleMenuItem).buildToggleTemplate()
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
// https://developers.home-assistant.io/docs/api/native-app-integration/sending-data/#render-templates
|
// https://developers.home-assistant.io/docs/api/native-app-integration/sending-data/#render-templates
|
||||||
var url = Settings.getApiUrl() + "/webhook/" + Settings.getWebhookId();
|
var url = Settings.getApiUrl() + "/webhook/" + Settings.getWebhookId();
|
||||||
// System.println("HomeAssistantApp updateMenuItems() URL=" + url + ", Template='" + mTemplate + "'");
|
// System.println("HomeAssistantApp updateMenuItems() URL=" + url + ", Template='" + mTemplate + "'");
|
||||||
|
@ -181,8 +181,12 @@ class WebhookManager {
|
|||||||
// Webhook ID might have been deleted on Home Assistant server and a Lang.String is trying to tell us an error message
|
// Webhook ID might have been deleted on Home Assistant server and a Lang.String is trying to tell us an error message
|
||||||
Settings.unsetWebhookId();
|
Settings.unsetWebhookId();
|
||||||
Settings.unsetIsSensorsLevelEnabled();
|
Settings.unsetIsSensorsLevelEnabled();
|
||||||
// ErrorView.show(WatchUi.loadResource($.Rez.Strings.WebhookFailed) as Lang.String + "\n" + data.toString());
|
if (data == null) {
|
||||||
ErrorView.show(WatchUi.loadResource($.Rez.Strings.WebhookFailed) as Lang.String);
|
ErrorView.show(WatchUi.loadResource($.Rez.Strings.WebhookFailed) as Lang.String + "\nNull data");
|
||||||
|
} else {
|
||||||
|
// All objects have toString()
|
||||||
|
ErrorView.show(WatchUi.loadResource($.Rez.Strings.WebhookFailed) as Lang.String + "\n" + data.toString());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user