Bug fix for devices with API 3.1.0

1) Removed WatchUi.getCurrentView() API call in favour of a new method of ensuring only one ErrorView is pushed at a time.
2) Any error view will be cancelled when responses start working again, e.g. watch gets in Bluetooth range of the phone again.
3) Added error message decoding for misspelled entities.

Co-Authored-By: Joseph Abbey <me@josephabbey.dev>
This commit is contained in:
Philip Abbey
2023-12-16 17:03:38 +00:00
parent a13f04fa6c
commit f086d0d03b
5 changed files with 171 additions and 108 deletions

View File

@ -18,6 +18,15 @@
// should not happen of course... but they do, so best make sure errors can be // should not happen of course... but they do, so best make sure errors can be
// reported. // reported.
// //
// Designed so that a single ErrorView is used for all errors and hence can ensure
// that only the first call to display is honoured until the view is dismissed.
// This compensates for older devices not being able to call WatchUi.getCurrentView()
// due to not supporting API level 3.4.0.
//
// Usage:
// 1) ErrorView.show("Error message");
// 2) return ErrorView.create("Error message"); // as Lang.Array<WatchUi.Views or WatchUi.InputDelegates>
//
//----------------------------------------------------------------------------------- //-----------------------------------------------------------------------------------
using Toybox.Graphics; using Toybox.Graphics;
@ -26,18 +35,22 @@ using Toybox.WatchUi;
using Toybox.Communications; using Toybox.Communications;
class ErrorView extends ScalableView { class ErrorView extends ScalableView {
private var mText as Lang.String = "";
private var mDelegate as ErrorDelegate;
private const cSettings as Lang.Dictionary = { private const cSettings as Lang.Dictionary = {
:errorIconMargin => 7f :errorIconMargin => 7f
}; };
// Vertical spacing between the top of the face and the error icon // Vertical spacing between the top of the face and the error icon
private var mErrorIconMargin; private var mErrorIconMargin as Lang.Number;
private var mText as Lang.String;
private var mErrorIcon; private var mErrorIcon;
private var mTextArea; private var mTextArea;
function initialize(text as Lang.String) { private static var instance;
private static var mShown as Lang.Boolean = false;
function initialize() {
ScalableView.initialize(); ScalableView.initialize();
mText = text; mDelegate = new ErrorDelegate(self);
// Convert the settings from % of screen size to pixels // Convert the settings from % of screen size to pixels
mErrorIconMargin = pixelsForScreen(cSettings.get(:errorIconMargin) as Lang.Float); mErrorIconMargin = pixelsForScreen(cSettings.get(:errorIconMargin) as Lang.Float);
} }
@ -75,15 +88,59 @@ class ErrorView extends ScalableView {
mTextArea.draw(dc); mTextArea.draw(dc);
} }
function getDelegate() as ErrorDelegate {
return mDelegate;
}
static function create(text as Lang.String) as Lang.Array<ErrorView or ErrorDelegate> {
if (instance == null) {
instance = new ErrorView();
}
if (!mShown) {
instance.setText(text);
}
return [instance, instance.getDelegate()];
}
// Create or reuse an existing ErrorView, and pass on the text.
static function show(text as Lang.String) as Void {
create(text); // Ignore returned values
if (!mShown) {
WatchUi.pushView(instance, instance.getDelegate(), WatchUi.SLIDE_UP);
mShown = true;
}
}
// Internal show now we're not a static method like 'show()'.
function setText(text as Lang.String) as Void {
mText = text;
if (mTextArea != null) {
mTextArea.setText(text);
requestUpdate();
}
}
static function unShow() as Void {
if (mShown) {
mShown = false;
WatchUi.popView(WatchUi.SLIDE_DOWN);
}
}
} }
class ErrorDelegate extends WatchUi.BehaviorDelegate { class ErrorDelegate extends WatchUi.BehaviorDelegate {
function initialize() { //private var mView as ErrorView;
function initialize(view as ErrorView) {
WatchUi.BehaviorDelegate.initialize(); WatchUi.BehaviorDelegate.initialize();
//mView = view;
} }
function onBack() { function onBack() {
getApp().getQuitTimer().reset(); getApp().getQuitTimer().reset();
WatchUi.popView(WatchUi.SLIDE_DOWN); ErrorView.unShow();
return true; return true;
} }
} }

View File

@ -74,32 +74,32 @@ class HomeAssistantApp extends Application.AppBase {
if (Globals.scDebug) { if (Globals.scDebug) {
System.println("HomeAssistantApp getInitialView(): No API key in the application settings."); System.println("HomeAssistantApp getInitialView(): No API key in the application settings.");
} }
return [new ErrorView(strNoApiKey + "."), new ErrorDelegate()] as Lang.Array<WatchUi.Views or WatchUi.InputDelegates>; return ErrorView.create(strNoApiKey + ".");
} else if (api_url.length() == 0) { } else if (api_url.length() == 0) {
if (Globals.scDebug) { if (Globals.scDebug) {
System.println("HomeAssistantApp getInitialView(): No API URL in the application settings."); System.println("HomeAssistantApp getInitialView(): No API URL in the application settings.");
} }
return [new ErrorView(strNoApiUrl + "."), new ErrorDelegate()] as Lang.Array<WatchUi.Views or WatchUi.InputDelegates>; return ErrorView.create(strNoApiUrl + ".");
} else if (api_url.substring(-1, api_url.length()).equals("/")) { } else if (api_url.substring(-1, api_url.length()).equals("/")) {
if (Globals.scDebug) { if (Globals.scDebug) {
System.println("HomeAssistantApp getInitialView(): API URL must not have a trailing slash '/'."); System.println("HomeAssistantApp getInitialView(): API URL must not have a trailing slash '/'.");
} }
return [new ErrorView(strTrailingSlashErr + "."), new ErrorDelegate()] as Lang.Array<WatchUi.Views or WatchUi.InputDelegates>; return ErrorView.create(strTrailingSlashErr + ".");
} else if ((Properties.getValue("config_url") as Lang.String).length() == 0) { } else if ((Properties.getValue("config_url") as Lang.String).length() == 0) {
if (Globals.scDebug) { if (Globals.scDebug) {
System.println("HomeAssistantApp getInitialView(): No configuration URL in the application settings."); System.println("HomeAssistantApp getInitialView(): No configuration URL in the application settings.");
} }
return [new ErrorView(strNoConfigUrl + "."), new ErrorDelegate()] as Lang.Array<WatchUi.Views or WatchUi.InputDelegates>; return ErrorView.create(strNoConfigUrl + ".");
} else if (! System.getDeviceSettings().phoneConnected) { } else if (! System.getDeviceSettings().phoneConnected) {
if (Globals.scDebug) { if (Globals.scDebug) {
System.println("HomeAssistantApp fetchMenuConfig(): No Phone connection, skipping API call."); System.println("HomeAssistantApp fetchMenuConfig(): No Phone connection, skipping API call.");
} }
return [new ErrorView(strNoPhone + "."), new ErrorDelegate()] as Lang.Array<WatchUi.Views or WatchUi.InputDelegates>; return ErrorView.create(strNoPhone + ".");
} else if (! System.getDeviceSettings().connectionAvailable) { } else if (! System.getDeviceSettings().connectionAvailable) {
if (Globals.scDebug) { if (Globals.scDebug) {
System.println("HomeAssistantApp fetchMenuConfig(): No Internet connection, skipping API call."); System.println("HomeAssistantApp fetchMenuConfig(): No Internet connection, skipping API call.");
} }
return [new ErrorView(strNoInternet + "."), new ErrorDelegate()] as Lang.Array<WatchUi.Views or WatchUi.InputDelegates>; return ErrorView.create(strNoInternet + ".");
} else { } else {
fetchMenuConfig(); fetchMenuConfig();
return [new WatchUi.View(), new WatchUi.BehaviorDelegate()] as Lang.Array<WatchUi.Views or WatchUi.InputDelegates>; return [new WatchUi.View(), new WatchUi.BehaviorDelegate()] as Lang.Array<WatchUi.Views or WatchUi.InputDelegates>;
@ -117,25 +117,23 @@ class HomeAssistantApp extends Application.AppBase {
if (Globals.scDebug) { if (Globals.scDebug) {
System.println("HomeAssistantApp onReturnFetchMenuConfig() Response Code: BLE_HOST_TIMEOUT or BLE_CONNECTION_UNAVAILABLE, Bluetooth connection severed."); System.println("HomeAssistantApp onReturnFetchMenuConfig() Response Code: BLE_HOST_TIMEOUT or BLE_CONNECTION_UNAVAILABLE, Bluetooth connection severed.");
} }
WatchUi.pushView(new ErrorView(strNoPhone + "."), new ErrorDelegate(), WatchUi.SLIDE_UP); ErrorView.show(strNoPhone + ".");
} else if (responseCode == Communications.BLE_QUEUE_FULL) { } else if (responseCode == Communications.BLE_QUEUE_FULL) {
if (Globals.scDebug) { if (Globals.scDebug) {
System.println("HomeAssistantApp onReturnFetchMenuConfig() Response Code: BLE_QUEUE_FULL, API calls too rapid."); System.println("HomeAssistantApp onReturnFetchMenuConfig() Response Code: BLE_QUEUE_FULL, API calls too rapid.");
} }
if (!(WatchUi.getCurrentView()[0] instanceof ErrorView)) { // Don't need to worry about multiple ErrorViews here as the fetch does not happen a second time.
// Avoid pushing multiple ErrorViews ErrorView.show(strApiFlood);
WatchUi.pushView(new ErrorView(strApiFlood), new ErrorDelegate(), WatchUi.SLIDE_UP);
}
} else if (responseCode == Communications.NETWORK_REQUEST_TIMED_OUT) { } else if (responseCode == Communications.NETWORK_REQUEST_TIMED_OUT) {
if (Globals.scDebug) { if (Globals.scDebug) {
System.println("HomeAssistantApp onReturnFetchMenuConfig() Response Code: NETWORK_REQUEST_TIMED_OUT, check Internet connection."); System.println("HomeAssistantApp onReturnFetchMenuConfig() Response Code: NETWORK_REQUEST_TIMED_OUT, check Internet connection.");
} }
WatchUi.pushView(new ErrorView(strNoResponse), new ErrorDelegate(), WatchUi.SLIDE_UP); ErrorView.show(strNoResponse);
} else if (responseCode == 404) { } else if (responseCode == 404) {
if (Globals.scDebug) { if (Globals.scDebug) {
System.println("HomeAssistantApp onReturnFetchMenuConfig() Response Code: 404, page not found. Check Configuration URL setting."); System.println("HomeAssistantApp onReturnFetchMenuConfig() Response Code: 404, page not found. Check Configuration URL setting.");
} }
WatchUi.pushView(new ErrorView(strConfigUrlNotFound), new ErrorDelegate(), WatchUi.SLIDE_UP); ErrorView.show(strConfigUrlNotFound);
} else if (responseCode == 200) { } else if (responseCode == 200) {
mHaMenu = new HomeAssistantView(data, null); mHaMenu = new HomeAssistantView(data, null);
WatchUi.switchToView(mHaMenu, new HomeAssistantViewDelegate(), WatchUi.SLIDE_IMMEDIATE); WatchUi.switchToView(mHaMenu, new HomeAssistantViewDelegate(), WatchUi.SLIDE_IMMEDIATE);
@ -149,12 +147,12 @@ class HomeAssistantApp extends Application.AppBase {
if (Globals.scDebug) { if (Globals.scDebug) {
System.println("HomeAssistantApp onReturnFetchMenuConfig(): Network request timeout."); System.println("HomeAssistantApp onReturnFetchMenuConfig(): Network request timeout.");
} }
WatchUi.pushView(new ErrorView(strNoMenu + ". " + strNoInternet + "?"), new ErrorDelegate(), WatchUi.SLIDE_UP); ErrorView.show(strNoMenu + ". " + strNoInternet + "?");
} else { } else {
if (Globals.scDebug) { if (Globals.scDebug) {
System.println("HomeAssistantApp onReturnFetchMenuConfig(): Unhandled HTTP response code = " + responseCode); System.println("HomeAssistantApp onReturnFetchMenuConfig(): Unhandled HTTP response code = " + responseCode);
} }
WatchUi.pushView(new ErrorView(strUnhandledHttpErr + responseCode ), new ErrorDelegate(), WatchUi.SLIDE_UP); ErrorView.show(strUnhandledHttpErr + responseCode );
} }
} }

View File

@ -26,6 +26,7 @@ using Toybox.Timer;
using Toybox.Application.Properties; using Toybox.Application.Properties;
class HomeAssistantConfirmation extends WatchUi.Confirmation { class HomeAssistantConfirmation extends WatchUi.Confirmation {
function initialize() { function initialize() {
WatchUi.Confirmation.initialize(WatchUi.loadResource($.Rez.Strings.Confirm)); WatchUi.Confirmation.initialize(WatchUi.loadResource($.Rez.Strings.Confirm));
} }
@ -33,32 +34,32 @@ class HomeAssistantConfirmation extends WatchUi.Confirmation {
} }
class HomeAssistantConfirmationDelegate extends WatchUi.ConfirmationDelegate { class HomeAssistantConfirmationDelegate extends WatchUi.ConfirmationDelegate {
private var confirmMethod; private var mConfirmMethod;
private var timeout; private var mTimer;
function initialize(callback as Method() as Void) { function initialize(callback as Method() as Void) {
WatchUi.ConfirmationDelegate.initialize(); WatchUi.ConfirmationDelegate.initialize();
confirmMethod = callback; mConfirmMethod = callback;
var timeoutSeconds = Properties.getValue("confirm_timeout") as Lang.Number; var timeoutSeconds = Properties.getValue("confirm_timeout") as Lang.Number;
if (timeoutSeconds > 0) { if (timeoutSeconds > 0) {
timeout = new Timer.Timer(); mTimer = new Timer.Timer();
timeout.start(method(:onTimeout), timeoutSeconds * 1000, true); mTimer.start(method(:onTimeout), timeoutSeconds * 1000, true);
} }
} }
function onResponse(response) as Lang.Boolean { function onResponse(response) as Lang.Boolean {
getApp().getQuitTimer().reset(); getApp().getQuitTimer().reset();
if (timeout) { if (mTimer) {
timeout.stop(); mTimer.stop();
} }
if (response == WatchUi.CONFIRM_YES) { if (response == WatchUi.CONFIRM_YES) {
confirmMethod.invoke(); mConfirmMethod.invoke();
} }
return true; return true;
} }
function onTimeout() as Void { function onTimeout() as Void {
timeout.stop(); mTimer.stop();
WatchUi.popView(WatchUi.SLIDE_RIGHT); WatchUi.popView(WatchUi.SLIDE_RIGHT);
} }
} }

View File

@ -54,25 +54,23 @@ class HomeAssistantService {
if (Globals.scDebug) { if (Globals.scDebug) {
System.println("HomeAssistantService onReturnCall() Response Code: BLE_HOST_TIMEOUT or BLE_CONNECTION_UNAVAILABLE, Bluetooth connection severed."); System.println("HomeAssistantService onReturnCall() Response Code: BLE_HOST_TIMEOUT or BLE_CONNECTION_UNAVAILABLE, Bluetooth connection severed.");
} }
WatchUi.pushView(new ErrorView(strNoPhone + "."), new ErrorDelegate(), WatchUi.SLIDE_UP); ErrorView.show(strNoPhone + ".");
} else if (responseCode == Communications.BLE_QUEUE_FULL) { } else if (responseCode == Communications.BLE_QUEUE_FULL) {
if (Globals.scDebug) { if (Globals.scDebug) {
System.println("HomeAssistantService onReturnCall() Response Code: BLE_QUEUE_FULL, API calls too rapid."); System.println("HomeAssistantService onReturnCall() Response Code: BLE_QUEUE_FULL, API calls too rapid.");
} }
if (!(WatchUi.getCurrentView()[0] instanceof ErrorView)) { // Don't need to worry about multiple ErrorViews here as the call is not on a repeat timer.
// Avoid pushing multiple ErrorViews ErrorView.show(strApiFlood);
WatchUi.pushView(new ErrorView(strApiFlood), new ErrorDelegate(), WatchUi.SLIDE_UP);
}
} else if (responseCode == Communications.NETWORK_REQUEST_TIMED_OUT) { } else if (responseCode == Communications.NETWORK_REQUEST_TIMED_OUT) {
if (Globals.scDebug) { if (Globals.scDebug) {
System.println("HomeAssistantService onReturnCall() Response Code: NETWORK_REQUEST_TIMED_OUT, check Internet connection."); System.println("HomeAssistantService onReturnCall() Response Code: NETWORK_REQUEST_TIMED_OUT, check Internet connection.");
} }
WatchUi.pushView(new ErrorView(strNoResponse), new ErrorDelegate(), WatchUi.SLIDE_UP); ErrorView.show(strNoResponse);
} else if (responseCode == 404) { } else if (responseCode == 404) {
if (Globals.scDebug) { if (Globals.scDebug) {
System.println("HomeAssistantService onReturnCall() Response Code: 404, page not found. Check API URL setting."); System.println("HomeAssistantService onReturnCall() Response Code: 404, page not found. Check API URL setting.");
} }
WatchUi.pushView(new ErrorView(strApiUrlNotFound), new ErrorDelegate(), WatchUi.SLIDE_UP); ErrorView.show(strApiUrlNotFound);
} else if (responseCode == 200) { } else if (responseCode == 200) {
if (Globals.scDebug) { if (Globals.scDebug) {
System.println("HomeAssistantService onReturnCall(): Service executed."); System.println("HomeAssistantService onReturnCall(): Service executed.");
@ -99,7 +97,7 @@ class HomeAssistantService {
if (Globals.scDebug) { if (Globals.scDebug) {
System.println("HomeAssistantService onReturnCall(): Unhandled HTTP response code = " + responseCode); System.println("HomeAssistantService onReturnCall(): Unhandled HTTP response code = " + responseCode);
} }
WatchUi.pushView(new ErrorView(strUnhandledHttpErr + responseCode ), new ErrorDelegate(), WatchUi.SLIDE_UP); ErrorView.show(strUnhandledHttpErr + responseCode );
} }
} }
@ -117,14 +115,15 @@ class HomeAssistantService {
if (Globals.scDebug) { if (Globals.scDebug) {
System.println("HomeAssistantService call(): No Phone connection, skipping API call."); System.println("HomeAssistantService call(): No Phone connection, skipping API call.");
} }
WatchUi.pushView(new ErrorView(strNoPhone + "."), new ErrorDelegate(), WatchUi.SLIDE_UP); ErrorView.show(strNoPhone + ".");
} else if (! System.getDeviceSettings().connectionAvailable) { } else if (! System.getDeviceSettings().connectionAvailable) {
if (Globals.scDebug) { if (Globals.scDebug) {
System.println("HomeAssistantService call(): No Internet connection, skipping API call."); System.println("HomeAssistantService call(): No Internet connection, skipping API call.");
} }
WatchUi.pushView(new ErrorView(strNoInternet + "."), new ErrorDelegate(), WatchUi.SLIDE_UP); ErrorView.show(strNoInternet + ".");
} else { } else {
var url = (Properties.getValue("api_url") as Lang.String) + "/services/" + service.substring(0, service.find(".")) + "/" + service.substring(service.find(".")+1, null); // Can't user null for parameters due to API version level.
var url = (Properties.getValue("api_url") as Lang.String) + "/services/" + service.substring(0, service.find(".")) + "/" + service.substring(service.find(".")+1, service.length());
if (Globals.scDebug) { if (Globals.scDebug) {
System.println("HomeAssistantService call() URL=" + url); System.println("HomeAssistantService call() URL=" + url);
System.println("HomeAssistantService call() service=" + service); System.println("HomeAssistantService call() service=" + service);

View File

@ -77,40 +77,53 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem {
} }
// Provide the ability to terminate updating chain of calls for a permanent network error. // Provide the ability to terminate updating chain of calls for a permanent network error.
var keepUpdating = true; var keepUpdating = true;
if (responseCode == Communications.BLE_HOST_TIMEOUT || responseCode == Communications.BLE_CONNECTION_UNAVAILABLE) { switch (responseCode) {
case Communications.BLE_HOST_TIMEOUT:
case Communications.BLE_CONNECTION_UNAVAILABLE:
if (Globals.scDebug) { if (Globals.scDebug) {
System.println("HomeAssistantToggleMenuItem onReturnGetState() Response Code: BLE_HOST_TIMEOUT or BLE_CONNECTION_UNAVAILABLE, Bluetooth connection severed."); System.println("HomeAssistantToggleMenuItem onReturnGetState() Response Code: BLE_HOST_TIMEOUT or BLE_CONNECTION_UNAVAILABLE, Bluetooth connection severed.");
} }
if (!(WatchUi.getCurrentView()[0] instanceof ErrorView)) { ErrorView.show(strNoPhone + ".");
// Avoid pushing multiple ErrorViews break;
WatchUi.pushView(new ErrorView(strNoPhone + "."), new ErrorDelegate(), WatchUi.SLIDE_UP); case Communications.BLE_QUEUE_FULL:
}
} else if (responseCode == Communications.BLE_QUEUE_FULL) {
if (Globals.scDebug) { if (Globals.scDebug) {
System.println("HomeAssistantToggleMenuItem onReturnGetState() Response Code: BLE_QUEUE_FULL, API calls too rapid."); System.println("HomeAssistantToggleMenuItem onReturnGetState() Response Code: BLE_QUEUE_FULL, API calls too rapid.");
} }
if (!(WatchUi.getCurrentView()[0] instanceof ErrorView)) { ErrorView.show(strApiFlood);
// Avoid pushing multiple ErrorViews break;
WatchUi.pushView(new ErrorView(strApiFlood), new ErrorDelegate(), WatchUi.SLIDE_UP); case Communications.NETWORK_REQUEST_TIMED_OUT:
}
} else if (responseCode == Communications.NETWORK_REQUEST_TIMED_OUT) {
if (Globals.scDebug) { if (Globals.scDebug) {
System.println("HomeAssistantToggleMenuItem onReturnGetState() Response Code: NETWORK_REQUEST_TIMED_OUT, check Internet connection."); System.println("HomeAssistantToggleMenuItem onReturnGetState() Response Code: NETWORK_REQUEST_TIMED_OUT, check Internet connection.");
} }
if (!(WatchUi.getCurrentView()[0] instanceof ErrorView)) { ErrorView.show(strNoResponse);
// Avoid pushing multiple ErrorViews break;
WatchUi.pushView(new ErrorView(strNoResponse), new ErrorDelegate(), WatchUi.SLIDE_UP); case 404:
var msg = null;
if (data != null) {
msg = data.get("message");
} }
} else if (responseCode == 404) { if (msg != null) {
// Should be an HTTP 405 according to curl queries
if (Globals.scDebug) {
System.println("HomeAssistantToggleMenuItem onReturnGetState() Response Code: 404. " + mIdentifier + " " + msg);
}
ErrorView.show("HTTP 405, " + mIdentifier + ". " + data.get("message"));
} else {
if (Globals.scDebug) { if (Globals.scDebug) {
System.println("HomeAssistantToggleMenuItem onReturnGetState() Response Code: 404, page not found. Check API URL setting."); System.println("HomeAssistantToggleMenuItem onReturnGetState() Response Code: 404, page not found. Check API URL setting.");
} }
if (!(WatchUi.getCurrentView()[0] instanceof ErrorView)) { ErrorView.show(strApiUrlNotFound);
// Avoid pushing multiple ErrorViews
WatchUi.pushView(new ErrorView(strApiUrlNotFound), new ErrorDelegate(), WatchUi.SLIDE_UP);
} }
keepUpdating = false; keepUpdating = false;
} else if (responseCode == 200) { break;
case 405:
if (Globals.scDebug) {
System.println("HomeAssistantToggleMenuItem onReturnGetState() Response Code: 405. " + mIdentifier + " " + data.get("message"));
}
ErrorView.show("HTTP 405, " + mIdentifier + ". " + data.get("message"));
keepUpdating = false;
break;
case 200:
var state = data.get("state") as Lang.String; var state = data.get("state") as Lang.String;
if (Globals.scDebug) { if (Globals.scDebug) {
System.println((data.get("attributes") as Lang.Dictionary).get("friendly_name") + " State=" + state); System.println((data.get("attributes") as Lang.Dictionary).get("friendly_name") + " State=" + state);
@ -119,14 +132,13 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem {
setLabel((data.get("attributes") as Lang.Dictionary).get("friendly_name") as Lang.String); setLabel((data.get("attributes") as Lang.Dictionary).get("friendly_name") as Lang.String);
} }
setUiToggle(state); setUiToggle(state);
} else { ErrorView.unShow();
break;
default:
if (Globals.scDebug) { if (Globals.scDebug) {
System.println("HomeAssistantToggleMenuItem onReturnGetState(): Unhandled HTTP response code = " + responseCode); System.println("HomeAssistantToggleMenuItem onReturnGetState(): Unhandled HTTP response code = " + responseCode);
} }
if (!(WatchUi.getCurrentView()[0] instanceof ErrorView)) { ErrorView.show(strUnhandledHttpErr + responseCode);
// Avoid pushing multiple ErrorViews
WatchUi.pushView(new ErrorView(strUnhandledHttpErr + responseCode ), new ErrorDelegate(), WatchUi.SLIDE_UP);
}
} }
if (keepUpdating) { if (keepUpdating) {
// Now this feels very "closely coupled" to the application, but it is the most reliable method instead of using a timer. // Now this feels very "closely coupled" to the application, but it is the most reliable method instead of using a timer.
@ -147,18 +159,12 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem {
if (Globals.scDebug) { if (Globals.scDebug) {
System.println("HomeAssistantToggleMenuItem getState(): No Phone connection, skipping API call."); System.println("HomeAssistantToggleMenuItem getState(): No Phone connection, skipping API call.");
} }
if (!(WatchUi.getCurrentView()[0] instanceof ErrorView)) { ErrorView.show(strNoPhone + ".");
// Avoid pushing multiple ErrorViews
WatchUi.pushView(new ErrorView(strNoPhone + "."), new ErrorDelegate(), WatchUi.SLIDE_UP);
}
} else if (! System.getDeviceSettings().connectionAvailable) { } else if (! System.getDeviceSettings().connectionAvailable) {
if (Globals.scDebug) { if (Globals.scDebug) {
System.println("HomeAssistantToggleMenuItem getState(): No Internet connection, skipping API call."); System.println("HomeAssistantToggleMenuItem getState(): No Internet connection, skipping API call.");
} }
if (!(WatchUi.getCurrentView()[0] instanceof ErrorView)) { ErrorView.show(strNoInternet + ".");
// Avoid pushing multiple ErrorViews
WatchUi.pushView(new ErrorView(strNoInternet + "."), new ErrorDelegate(), WatchUi.SLIDE_UP);
}
} else { } else {
var url = Properties.getValue("api_url") + "/states/" + mIdentifier; var url = Properties.getValue("api_url") + "/states/" + mIdentifier;
if (Globals.scDebug) { if (Globals.scDebug) {
@ -180,9 +186,11 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem {
var myTimer = new Timer.Timer(); var myTimer = new Timer.Timer();
// Now this feels very "closely coupled" to the application, but it is the most reliable method instead of using a timer. // Now this feels very "closely coupled" to the application, but it is the most reliable method instead of using a timer.
myTimer.start(getApp().method(:updateNextMenuItem), 500, false); myTimer.start(getApp().method(:updateNextMenuItem), 500, false);
if (Globals.scDebug) {
System.println("HomeAssistantToggleMenuItem getState(): Updated failed " + mIdentifier); System.println("HomeAssistantToggleMenuItem getState(): Updated failed " + mIdentifier);
} }
} }
}
// Callback function after completing the POST request to set the status. // Callback function after completing the POST request to set the status.
// //
@ -195,22 +203,22 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem {
if (Globals.scDebug) { if (Globals.scDebug) {
System.println("HomeAssistantToggleMenuItem onReturnSetState() Response Code: BLE_HOST_TIMEOUT or BLE_CONNECTION_UNAVAILABLE, Bluetooth connection severed."); System.println("HomeAssistantToggleMenuItem onReturnSetState() Response Code: BLE_HOST_TIMEOUT or BLE_CONNECTION_UNAVAILABLE, Bluetooth connection severed.");
} }
WatchUi.pushView(new ErrorView(strNoPhone + "."), new ErrorDelegate(), WatchUi.SLIDE_UP); ErrorView.show(strNoPhone + ".");
} else if (responseCode == Communications.BLE_QUEUE_FULL) { } else if (responseCode == Communications.BLE_QUEUE_FULL) {
if (Globals.scDebug) { if (Globals.scDebug) {
System.println("HomeAssistantToggleMenuItem onReturnSetState() Response Code: BLE_QUEUE_FULL, API calls too rapid."); System.println("HomeAssistantToggleMenuItem onReturnSetState() Response Code: BLE_QUEUE_FULL, API calls too rapid.");
} }
WatchUi.pushView(new ErrorView(strApiFlood), new ErrorDelegate(), WatchUi.SLIDE_UP); ErrorView.show(strApiFlood);
} else if (responseCode == Communications.NETWORK_REQUEST_TIMED_OUT) { } else if (responseCode == Communications.NETWORK_REQUEST_TIMED_OUT) {
if (Globals.scDebug) { if (Globals.scDebug) {
System.println("HomeAssistantToggleMenuItem onReturnSetState() Response Code: NETWORK_REQUEST_TIMED_OUT, check Internet connection."); System.println("HomeAssistantToggleMenuItem onReturnSetState() Response Code: NETWORK_REQUEST_TIMED_OUT, check Internet connection.");
} }
WatchUi.pushView(new ErrorView(strNoResponse), new ErrorDelegate(), WatchUi.SLIDE_UP); ErrorView.show(strNoResponse);
} else if (responseCode == 404) { } else if (responseCode == 404) {
if (Globals.scDebug) { if (Globals.scDebug) {
System.println("HomeAssistantToggleMenuItem onReturnSetState() Response Code: 404, page not found. Check API URL setting."); System.println("HomeAssistantToggleMenuItem onReturnSetState() Response Code: 404, page not found. Check API URL setting.");
} }
WatchUi.pushView(new ErrorView(strApiUrlNotFound), new ErrorDelegate(), WatchUi.SLIDE_UP); ErrorView.show(strApiUrlNotFound);
} else if (responseCode == 200) { } else if (responseCode == 200) {
var state; var state;
var d = data as Lang.Array; var d = data as Lang.Array;
@ -227,7 +235,7 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem {
if (Globals.scDebug) { if (Globals.scDebug) {
System.println("HomeAssistantToggleMenuItem onReturnSetState(): Unhandled HTTP response code = " + responseCode); System.println("HomeAssistantToggleMenuItem onReturnSetState(): Unhandled HTTP response code = " + responseCode);
} }
WatchUi.pushView(new ErrorView(strUnhandledHttpErr + responseCode ), new ErrorDelegate(), WatchUi.SLIDE_UP); ErrorView.show(strUnhandledHttpErr + responseCode );
} }
} }
@ -246,14 +254,14 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem {
} }
// Toggle the UI back // Toggle the UI back
setEnabled(!isEnabled()); setEnabled(!isEnabled());
WatchUi.pushView(new ErrorView(strNoPhone + "."), new ErrorDelegate(), WatchUi.SLIDE_UP); ErrorView.show(strNoPhone + ".");
} else if (! System.getDeviceSettings().connectionAvailable) { } else if (! System.getDeviceSettings().connectionAvailable) {
if (Globals.scDebug) { if (Globals.scDebug) {
System.println("HomeAssistantToggleMenuItem getState(): No Internet connection, skipping API call."); System.println("HomeAssistantToggleMenuItem getState(): No Internet connection, skipping API call.");
} }
// Toggle the UI back // Toggle the UI back
setEnabled(!isEnabled()); setEnabled(!isEnabled());
WatchUi.pushView(new ErrorView(strNoInternet + "."), new ErrorDelegate(), WatchUi.SLIDE_UP); ErrorView.show(strNoInternet + ".");
} else { } else {
// Updated SDK and got a new error // Updated SDK and got a new error
// ERROR: venu: Cannot find symbol ':substring' on type 'PolyType<Null or $.Toybox.Lang.Object>'. // ERROR: venu: Cannot find symbol ':substring' on type 'PolyType<Null or $.Toybox.Lang.Object>'.