From e701ea9b61ac47b3996aca42774ea90d9ca6a10f Mon Sep 17 00:00:00 2001 From: Commit-La-Grenouille Date: Sun, 24 Apr 2022 15:19:34 +0200 Subject: [PATCH 1/3] issue #3 | PI+APP: offering config & supporting code to display data from the response --- Sources/app.js | 69 +++++++++++++++++++++------ Sources/propertyinspector/index.html | 41 ++++++++++++++++ Sources/propertyinspector/index_pi.js | 3 ++ 3 files changed, 98 insertions(+), 15 deletions(-) diff --git a/Sources/app.js b/Sources/app.js index 42865d4..bd0c33c 100644 --- a/Sources/app.js +++ b/Sources/app.js @@ -172,39 +172,78 @@ function APIRequest(jsonObj) { } async function updateImage(resp, do_status_poll) { - if (!settings.advanced_settings || !settings.response_parse || !settings.image_matched || !settings.image_unmatched) + /* + * Making sure we run only in one of the 2 relevant cases: + * (1) when asked to parse and match to define the background image + * (2) when asked to parse and display the data from the response on the key + */ + + // Common / top-level options + if (!settings.advanced_settings || (!settings.response_parse && !settings.response_data)) + return; + + // Case 1 missing config detection + if (settings.response_parse && (!settings.image_matched || !settings.image_unmatched)) + return; + + // Case 2 missing config detection (could be commented if we decide that the background image is optional) + if (settings.response_data && !settings.background_image) return; let json, body; var new_key_state = key_state; + const want_data = (settings.response_data) ? true : false; + const field_name = (want_data) ? 'data' : 'parse'; const prefix = (do_status_poll && settings.poll_status && settings.poll_status_parse) ? 'poll_status' : 'response'; - const field = Utils.getProp(settings, `${prefix}_parse_field`, undefined); + const field = Utils.getProp(settings, `${prefix}_${field_name}_field`, undefined); const value = Utils.getProp(settings, `${prefix}_parse_value`, undefined); - - if (field !== undefined && value !== undefined) { - json = await resp.json(); - new_key_state = (Utils.getProperty(json, field) == value); - } else if (field !== undefined) { - json = await resp.json(); - new_key_state = !(['false', '0', '', 'undefined'].indexOf(String(Utils.getProperty(json, field)).toLowerCase().trim()) + 1); - } else if (value !== undefined) { - body = await resp.text(); - new_key_state = body.includes(value); + // The value will always be undef in Case 2... + + if (want_data) { + if (field !== undefined) { + json = await resp.json(); + new_key_state = Utils.getProperty(json, field); + } else { + new_key_state = '?????'; + } + } else { + if (field !== undefined && value !== undefined) { + json = await resp.json(); + new_key_state = (Utils.getProperty(json, field) == value); + } else if (field !== undefined) { + json = await resp.json(); + new_key_state = !(['false', '0', '', 'undefined'].indexOf(String(Utils.getProperty(json, field)).toLowerCase().trim()) + 1); + } else if (value !== undefined) { + body = await resp.text(); + new_key_state = body.includes(value); + } } if (new_key_state == key_state) return; key_state = new_key_state; - path = key_state - ? settings.image_matched - : settings.image_unmatched; + // adapting the background image to the Case we are working for + if (want_data) { + path = settings.background_image; + } else { + path = key_state + ? settings.image_matched + : settings.image_unmatched; + } log('updateImage(): FILE:', path, 'JSON:', json, 'BODY:', body); Utils.loadImage(path, img => $SD.api.setImage(context, img)); + // Defining the text that must be rendered over the image + if (want_data) { + var name = (settings.response_data_name) ? `${settings.response_data_name}\n\n` : ''; + var unit = (settings.response_data_unit) ? ` ${settings.response_data_unit}` : ''; + $SD.api.setTitle(context, `${name}${new_key_state}${unit}`, null); + } + return resp; } diff --git a/Sources/propertyinspector/index.html b/Sources/propertyinspector/index.html index cd8a43d..246ca13 100644 --- a/Sources/propertyinspector/index.html +++ b/Sources/propertyinspector/index.html @@ -81,6 +81,47 @@ +
+
+
+
+ + +
+
+
+ +
diff --git a/Sources/propertyinspector/index_pi.js b/Sources/propertyinspector/index_pi.js index 54ee254..572cc0e 100644 --- a/Sources/propertyinspector/index_pi.js +++ b/Sources/propertyinspector/index_pi.js @@ -342,6 +342,9 @@ function showHideSettings() { d = document.getElementById('request_parameters_container'); d.style.display = settings.request_parameters ? "" : "none"; + d = document.getElementById('response_data_container'); + d.style.display = settings.response_data ? "" : "none"; + d = document.getElementById('response_parse_container'); d.style.display = settings.response_parse ? "" : "none"; From 63cd1d523083b727a1c167fffdc6b8f0c84ed2d9 Mon Sep 17 00:00:00 2001 From: Commit-La-Grenouille Date: Mon, 25 Apr 2022 13:14:46 +0200 Subject: [PATCH 2/3] issue #3 | PI+APP: adding polling sub-option (like with parsing) and cabling it correctly in the code to keep all PI elements perfectly distinct in name --- Sources/app.js | 16 +++++---- Sources/propertyinspector/index.html | 49 +++++++++++++++++++++++++++ Sources/propertyinspector/index_pi.js | 6 ++++ 3 files changed, 65 insertions(+), 6 deletions(-) diff --git a/Sources/app.js b/Sources/app.js index bd0c33c..2a2347c 100644 --- a/Sources/app.js +++ b/Sources/app.js @@ -79,7 +79,7 @@ function APIRequest(jsonObj) { destroy(); - if (settings.advanced_settings && settings.poll_status) { + if (settings.advanced_settings && (settings.poll_status || settings.poll_status_data)) { sendRequest(do_status_poll = true); poll_timer = setInterval(function() { @@ -95,18 +95,22 @@ function APIRequest(jsonObj) { } if (do_status_poll) { - if (!Boolean(settings.response_parse) || !Boolean(settings.poll_status)) return; - if (!settings.poll_status_url) return; + // We check the parent-child relationship between settings to skip early when not needed + // (left-side: parsing for background && right-side: parsing for displaying data) + if (!Boolean(settings.response_parse) && !Boolean(settings.response_data)) return; + if (!Boolean(settings.poll_status) && !Boolean(settings.poll_status_data)) return; + if (!settings.poll_status_url && !settings.poll_status_data_url) return; } let url = settings.request_url; let body = undefined; let method = 'GET'; if (settings.advanced_settings) { - if (do_status_poll) url = settings.poll_status_url; + if (do_status_poll) url = settings.poll_status_url ?? settings.poll_status_data_url; if (settings.request_parameters) { - body = settings.request_body; - method = (do_status_poll ? settings.poll_status_method : settings.request_method) ?? method; + body = settings.request_body; + poll_method = settings.poll_status_method ?? settings.poll_status_data_method; + method = (do_status_poll ? poll_method : settings.request_method) ?? method; } } diff --git a/Sources/propertyinspector/index.html b/Sources/propertyinspector/index.html index 246ca13..18b723a 100644 --- a/Sources/propertyinspector/index.html +++ b/Sources/propertyinspector/index.html @@ -120,6 +120,55 @@
Unit
+
+
+
+
+ + +
+
+
+
diff --git a/Sources/propertyinspector/index_pi.js b/Sources/propertyinspector/index_pi.js index 572cc0e..c7dc73d 100644 --- a/Sources/propertyinspector/index_pi.js +++ b/Sources/propertyinspector/index_pi.js @@ -351,8 +351,14 @@ function showHideSettings() { d = document.getElementById('poll_status_container'); d.style.display = settings.poll_status ? "" : "none"; + d = document.getElementById('poll_status_data_container'); + d.style.display = settings.poll_status_data ? "" : "none"; + d = document.getElementById('poll_status_parse_container'); d.style.display = settings.poll_status_parse ? "" : "none"; + + d = document.getElementById('poll_status_data_parse_container'); + d.style.display = settings.poll_status_data_parse ? "" : "none"; } function localize(s) { From 3d369ce4ff1984de6c092f64f6c06915e03ead61 Mon Sep 17 00:00:00 2001 From: Commit-La-Grenouille Date: Sun, 17 Jul 2022 16:10:32 +0200 Subject: [PATCH 3/3] issue #3 | PI+APP: making sure that the new polling option is properly used --- Sources/app.js | 2 +- Sources/propertyinspector/index.html | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Sources/app.js b/Sources/app.js index 2a2347c..fd04f12 100644 --- a/Sources/app.js +++ b/Sources/app.js @@ -75,7 +75,7 @@ function APIRequest(jsonObj) { key_state = null; function restartPeriodicPoll() { - const frequency = settings.poll_status_frequency || 15; + const frequency = settings.poll_status_frequency || settings.poll_status_data_frequency || 15; destroy(); diff --git a/Sources/propertyinspector/index.html b/Sources/propertyinspector/index.html index 18b723a..6bb0b4b 100644 --- a/Sources/propertyinspector/index.html +++ b/Sources/propertyinspector/index.html @@ -124,8 +124,8 @@
- - + +