Skip to content

Commit 64ca409

Browse files
issue #3 | PI+APP: offering config & supporting code to display data from the response
1 parent 86930c2 commit 64ca409

File tree

3 files changed

+91
-15
lines changed

3 files changed

+91
-15
lines changed

Sources/app.js

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -172,39 +172,77 @@ function APIRequest(jsonObj) {
172172
}
173173

174174
async function updateImage(resp, do_status_poll) {
175-
if (!settings.advanced_settings || !settings.response_parse || !settings.image_matched || !settings.image_unmatched)
175+
/*
176+
* Making sure we run only in one of the 2 relevant cases:
177+
* (1) when asked to parse and match to define the background image
178+
* (2) when asked to parse and display the data from the response on the key
179+
*/
180+
181+
// Common / top-level options
182+
if (!settings.advanced_settings || (!settings.response_parse && !settings.response_data))
183+
return;
184+
185+
// Case 1 missing config detection
186+
if (settings.response_parse && (!settings.image_matched || !settings.image_unmatched))
187+
return;
188+
189+
// Case 2 missing config detection (could be commented if we decide that the background image is optional)
190+
if (settings.response_data && !settings.background_image)
176191
return;
177192

178193
let json, body;
179194
var new_key_state = key_state;
195+
const want_data = (settings.response_data) ? true : false;
196+
const field_name = (want_data) ? 'data' : 'parse';
180197

181198
const prefix = (do_status_poll && settings.poll_status && settings.poll_status_parse) ? 'poll_status' : 'response';
182-
const field = Utils.getProp(settings, `${prefix}_parse_field`, undefined);
199+
const field = Utils.getProp(settings, `${prefix}_${field_name}_field`, undefined);
183200
const value = Utils.getProp(settings, `${prefix}_parse_value`, undefined);
184-
185-
if (field !== undefined && value !== undefined) {
186-
json = await resp.json();
187-
new_key_state = (Utils.getProperty(json, field) == value);
188-
} else if (field !== undefined) {
189-
json = await resp.json();
190-
new_key_state = !(['false', '0', '', 'undefined'].indexOf(String(Utils.getProperty(json, field)).toLowerCase().trim()) + 1);
191-
} else if (value !== undefined) {
192-
body = await resp.text();
193-
new_key_state = body.includes(value);
201+
// The value will always be undef in Case 2...
202+
203+
if (want_data) {
204+
if (field !== undefined) {
205+
json = await resp.json();
206+
new_key_state = Utils.getProperty(json, field);
207+
} else {
208+
new_key_state = '?????';
209+
}
210+
} else {
211+
if (field !== undefined && value !== undefined) {
212+
json = await resp.json();
213+
new_key_state = (Utils.getProperty(json, field) == value);
214+
} else if (field !== undefined) {
215+
json = await resp.json();
216+
new_key_state = !(['false', '0', '', 'undefined'].indexOf(String(Utils.getProperty(json, field)).toLowerCase().trim()) + 1);
217+
} else if (value !== undefined) {
218+
body = await resp.text();
219+
new_key_state = body.includes(value);
220+
}
194221
}
195222

196223
if (new_key_state == key_state) return;
197224

198225
key_state = new_key_state;
199226

200-
path = key_state
201-
? settings.image_matched
202-
: settings.image_unmatched;
227+
// adapting the background image to the Case we are working for
228+
if (want_data) {
229+
path = settings.background_image;
230+
} else {
231+
path = key_state
232+
? settings.image_matched
233+
: settings.image_unmatched;
234+
}
203235

204236
log('updateImage(): FILE:', path, 'JSON:', json, 'BODY:', body);
205237

206238
Utils.loadImage(path, img => $SD.api.setImage(context, img));
207239

240+
// Defining the text that must be rendered over the image
241+
if (want_data) {
242+
var name = (settings.response_data_name) ? `${settings.response_data_name}\n\n` : '';
243+
$SD.api.setTitle(context, `${name}${new_key_state}`, null);
244+
}
245+
208246
return resp;
209247
}
210248

Sources/propertyinspector/index.html

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,41 @@
8181
</div>
8282
</div>
8383

84+
<div class="sdpi-item">
85+
<div style="width: 15px;"></div>
86+
<div class="sdpi-item-value">
87+
<div class="sdpi-item-child">
88+
<input id="response_data" type="checkbox">
89+
<label for="response_data" class="sdpi-item-label"><span></span>Button shows data from response</label>
90+
</div>
91+
</div>
92+
</div>
93+
<div id="response_data_container" style="display:none">
94+
<details>
95+
<summary>Expand for Response data display instructions</summary>
96+
<p>Specify the JSON path in the API response whose value will show up on the key.</p>
97+
<p>A missing path will only display question marks.</p>
98+
<p>YOU MUST NOT USE THE TITLE OTHERWISE IT WILL HIDE THE DATA RETURNED</p>
99+
<p>(but you can use the title color and size config to adjust the data displayed).</p>
100+
</details>
101+
<div class="sdpi-item">
102+
<div class="sdpi-item-label">Name</div>
103+
<input class="sdpi-item-value" type="text" id="response_data_name">
104+
</div>
105+
<div class="sdpi-item">
106+
<div class="sdpi-item-label">JSON Path</div>
107+
<input class="sdpi-item-value" type="text" id="response_data_field">
108+
</div>
109+
<div class="sdpi-item">Background</div>
110+
<div class="sdpi-item">
111+
<div class="sdpi-item-group file" id="matchedfilepickergroup">
112+
<input class="sdpi-item-value" type="file" id="background_image" accept=".jpg, .jpeg, .png, .ico, .gif, .bmp, .tiff">
113+
<label class="sdpi-file-info " for="background_image">No file...</label>
114+
<label class="sdpi-file-label" for="background_image">Choose file...</label>
115+
</div>
116+
</div>
117+
</div>
118+
84119
<div class="sdpi-item">
85120
<div style="width: 15px;"></div>
86121
<div class="sdpi-item-value">

Sources/propertyinspector/index_pi.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,9 @@ function showHideSettings() {
342342
d = document.getElementById('request_parameters_container');
343343
d.style.display = settings.request_parameters ? "" : "none";
344344

345+
d = document.getElementById('response_data_container');
346+
d.style.display = settings.response_data ? "" : "none";
347+
345348
d = document.getElementById('response_parse_container');
346349
d.style.display = settings.response_parse ? "" : "none";
347350

0 commit comments

Comments
 (0)