mirror of
				https://github.com/house-of-abbey/GarminHomeAssistant.git
				synced 2025-11-04 08:58:13 +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,19 +321,22 @@ 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;
 | 
				
			||||||
                for (var i = 0; i < mItemsToUpdate.size(); i++) {
 | 
					                // System.println("mItemsToUpdate: " + mItemsToUpdate);
 | 
				
			||||||
                    var item = mItemsToUpdate[i];
 | 
					                if (mItemsToUpdate != null) {
 | 
				
			||||||
                    var state = data.get(i.toString());
 | 
					                    for (var i = 0; i < mItemsToUpdate.size(); i++) {
 | 
				
			||||||
                    item.updateState(state);
 | 
					                        var item = mItemsToUpdate[i];
 | 
				
			||||||
                    if (item instanceof HomeAssistantToggleMenuItem) {
 | 
					                        var state = data.get(i.toString());
 | 
				
			||||||
                        (item as HomeAssistantToggleMenuItem).updateToggleState(data.get(i.toString() + "t"));
 | 
					                        item.updateState(state);
 | 
				
			||||||
 | 
					                        if (item instanceof HomeAssistantToggleMenuItem) {
 | 
				
			||||||
 | 
					                            (item as HomeAssistantToggleMenuItem).updateToggleState(data.get(i.toString() + "t"));
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                    var delay = Settings.getPollDelay();
 | 
				
			||||||
 | 
					                    if (delay > 0) {
 | 
				
			||||||
 | 
					                        mUpdateTimer.start(method(:updateMenuItems), delay, false);
 | 
				
			||||||
 | 
					                    } else {
 | 
				
			||||||
 | 
					                        updateMenuItems();
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
                var delay = Settings.getPollDelay();
 | 
					 | 
				
			||||||
                if (delay > 0) {
 | 
					 | 
				
			||||||
                    mUpdateTimer.start(method(:updateMenuItems), delay, false);
 | 
					 | 
				
			||||||
                } else {
 | 
					 | 
				
			||||||
                    updateMenuItems();
 | 
					 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
                break;
 | 
					                break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -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