Added error messages for first request

This commit is contained in:
Joseph Abbey
2024-01-11 17:27:52 +00:00
parent 5f794388e6
commit f155ab0925
3 changed files with 87 additions and 30 deletions

View File

@ -32,6 +32,7 @@
<string id="NoJson">No JSON returned from HTTP request.</string> <string id="NoJson">No JSON returned from HTTP request.</string>
<string id="UnhandledHttpErr">HTTP request returned error code = </string> <string id="UnhandledHttpErr">HTTP request returned error code = </string>
<string id="TrailingSlashErr">API URL must not have a trailing slash '/'</string> <string id="TrailingSlashErr">API URL must not have a trailing slash '/'</string>
<string id="WebhookFailed">Failed to register Webhook</string>
<string id="Available" scope="glance">Available</string> <string id="Available" scope="glance">Available</string>
<string id="Checking" scope="glance">Checking...</string> <string id="Checking" scope="glance">Checking...</string>
<string id="Unavailable" scope="glance">Unavailable</string> <string id="Unavailable" scope="glance">Unavailable</string>

View File

@ -47,6 +47,7 @@ class RezStrings {
private static var strNoJson as Lang.String or Null; private static var strNoJson as Lang.String or Null;
private static var strUnhandledHttpErr as Lang.String or Null; private static var strUnhandledHttpErr as Lang.String or Null;
private static var strTrailingSlashErr as Lang.String or Null; private static var strTrailingSlashErr as Lang.String or Null;
private static var strWebhookFailed as Lang.String or Null;
(:glance) (:glance)
private static var strAvailable as Lang.String or Null; private static var strAvailable as Lang.String or Null;
(:glance) (:glance)
@ -98,6 +99,7 @@ class RezStrings {
strNoJson = WatchUi.loadResource($.Rez.Strings.NoJson); strNoJson = WatchUi.loadResource($.Rez.Strings.NoJson);
strUnhandledHttpErr = WatchUi.loadResource($.Rez.Strings.UnhandledHttpErr); strUnhandledHttpErr = WatchUi.loadResource($.Rez.Strings.UnhandledHttpErr);
strTrailingSlashErr = WatchUi.loadResource($.Rez.Strings.TrailingSlashErr); strTrailingSlashErr = WatchUi.loadResource($.Rez.Strings.TrailingSlashErr);
strWebhookFailed = WatchUi.loadResource($.Rez.Strings.WebhookFailed);
strAvailable = WatchUi.loadResource($.Rez.Strings.Available); strAvailable = WatchUi.loadResource($.Rez.Strings.Available);
strChecking = WatchUi.loadResource($.Rez.Strings.Checking); strChecking = WatchUi.loadResource($.Rez.Strings.Checking);
strUnavailable = WatchUi.loadResource($.Rez.Strings.Unavailable); strUnavailable = WatchUi.loadResource($.Rez.Strings.Unavailable);
@ -178,6 +180,10 @@ class RezStrings {
return strTrailingSlashErr; return strTrailingSlashErr;
} }
static function getWebhookFailed() as Lang.String {
return strWebhookFailed;
}
static function getAvailable() as Lang.String { static function getAvailable() as Lang.String {
return strAvailable; return strAvailable;
} }

View File

@ -2,38 +2,88 @@ using Toybox.Lang;
using Toybox.Communications; using Toybox.Communications;
using Toybox.System; using Toybox.System;
// Can use push view so must never be run in a glance context
class WebhookManager { class WebhookManager {
function onReturnRequestWebhookId(responseCode as Lang.Number, data as Null or Lang.Dictionary or Lang.String) as Void { function onReturnRequestWebhookId(responseCode as Lang.Number, data as Null or Lang.Dictionary or Lang.String) as Void {
// TODO: Handle errors switch (responseCode) {
if (responseCode == 201) { case Communications.BLE_HOST_TIMEOUT:
var id = data.get("webhook_id") as Lang.String or Null; case Communications.BLE_CONNECTION_UNAVAILABLE:
if (id != null) { if (Globals.scDebug) {
Settings.setWebhookId(id); System.println("WebhookManager onReturnRequestWebhookId() Response Code: BLE_HOST_TIMEOUT or BLE_CONNECTION_UNAVAILABLE, Bluetooth connection severed.");
registerWebhookSensor({ }
"device_class" => "battery", ErrorView.show(RezStrings.getWebhookFailed()+ "\n" + RezStrings.getNoPhone() + ".");
"name" => "Battery Level", break;
"state" => System.getSystemStats().battery,
"type" => "sensor", case Communications.BLE_QUEUE_FULL:
"unique_id" => "battery_level", if (Globals.scDebug) {
"unit_of_measurement" => "%", System.println("WebhookManager onReturnRequestWebhookId() Response Code: BLE_QUEUE_FULL, API calls too rapid.");
"state_class" => "measurement", }
"entity_category" => "diagnostic", ErrorView.show(RezStrings.getWebhookFailed()+ "\n" + RezStrings.getApiFlood());
"disabled" => false break;
});
registerWebhookSensor({ case Communications.NETWORK_REQUEST_TIMED_OUT:
"device_class" => "battery_charging", if (Globals.scDebug) {
"name" => "Battery is Charging", System.println("WebhookManager onReturnRequestWebhookId() Response Code: NETWORK_REQUEST_TIMED_OUT, check Internet connection.");
"state" => System.getSystemStats().charging, }
"type" => "binary_sensor", ErrorView.show(RezStrings.getWebhookFailed()+ "\n" + RezStrings.getNoResponse());
"unique_id" => "battery_is_charging", break;
"entity_category" => "diagnostic",
"disabled" => false case Communications.NETWORK_RESPONSE_OUT_OF_MEMORY:
}); if (Globals.scDebug) {
} System.println("WebhookManager onReturnRequestWebhookId() Response Code: NETWORK_RESPONSE_OUT_OF_MEMORY, are we going too fast?");
} else { }
if (Globals.scDebug) { // Ignore and see if we can carry on
System.println("WebhookManager onReturnRequestWebhookId(): Error: " + responseCode); break;
} case Communications.INVALID_HTTP_BODY_IN_NETWORK_RESPONSE:
if (Globals.scDebug) {
System.println("WebhookManager onReturnRequestWebhookId() Response Code: INVALID_HTTP_BODY_IN_NETWORK_RESPONSE, check JSON is returned.");
}
ErrorView.show(RezStrings.getWebhookFailed()+ "\n" + RezStrings.getNoJson());
break;
case 404:
if (Globals.scDebug) {
System.println("WebhookManager onReturnRequestWebhookId() Response Code: 404, page not found. Check API URL setting.");
}
ErrorView.show(RezStrings.getWebhookFailed()+ "\n" + RezStrings.getApiUrlNotFound());
break;
case 201:
var id = data.get("webhook_id") as Lang.String or Null;
if (id != null) {
Settings.setWebhookId(id);
registerWebhookSensor({
"device_class" => "battery",
"name" => "Battery Level",
"state" => System.getSystemStats().battery,
"type" => "sensor",
"unique_id" => "battery_level",
"unit_of_measurement" => "%",
"state_class" => "measurement",
"entity_category" => "diagnostic",
"disabled" => false
});
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 {
if (Globals.scDebug) {
System.println("WebhookManager onReturnRequestWebhookId(): No webhook id in response data.");
}
}
break;
default:
if (Globals.scDebug) {
System.println("WebhookManager onReturnRequestWebhookId(): Unhandled HTTP response code = " + responseCode);
}
ErrorView.show(RezStrings.getWebhookFailed()+ "\n" + RezStrings.getUnhandledHttpErr() + responseCode);
} }
} }