-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsystem-action-node.html
More file actions
377 lines (303 loc) · 13 KB
/
system-action-node.html
File metadata and controls
377 lines (303 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
<script type="text/javascript">
RED.nodes.registerType('system-action-node', {
category: 'Web of Things',
color: '#a6bbcf',
defaults: {
name: { value: "" },
thingDirectoryURI: { value: "" },
thingAction: { value: "" },
thingActionValue: { value: "" },
outputToMsg: { value: true },
redeploy: { value: "false" } //Only used to trigger UI redeploy
},
inputs: 1,
inputLabels: function () {
let text = "";
try {
text = formParameterText(JSON.parse(this.thingActionValue).params);
}
catch {
return "";
}
if(text === "") {
text = "No parameters";
}
return text;
function formParameterText(params) {
let text = "";
for (let parameter of Object.keys(params)) {
text += parameter + " : " + params[parameter] + "\n";
}
return text.substring(0, text.length - 1);
}
},
outputs: 1,
outputLabels: function () {
try {
return JSON.parse(this.thingActionValue).output["type"];
}
catch {
return "undefined";
}
},
icon: "bridge.svg",
label: function () {
if (this.name) {
return this.name;
}
else if (this.thingActionValue) {
return JSON.parse(this.thingActionValue).action;
}
else {
return "system-action-node";
}
},
oneditprepare: function () {
var node = this;
// hideFunctionOption(); //TODO: Buffer options whilst waiting for connection to Thing Directory
if(node.thingActionValue === undefined) { //Ensures value is added to this node object
node.thingActionValue = "";
}
else {
try {
let actionObj = JSON.parse(node.thingActionValue);
updateParameterLabels(actionObj);
updateOutputLabel(actionObj);
}
catch {
node.thingActionValue = "";
}
}
$("#node-input-thingDirectoryURI").on("change", async function (event) {
let input = $(this).val();
let emptyInput = ["", undefined, null].includes(input);
if (!emptyInput) {
await populateActions(input, node);
}
displayActionOption(!emptyInput);
});
function showActionOption() {
$("div#action-row").show();
}
function hideActionOption() {
$("#node-input-thingAction").innerText = "";
this.thingAction = "";
$("div#action-row").hide();
}
function displayActionOption(on) {
if (on) {
showActionOption();
} else {
hideActionOption();
}
}
/**
* @returns Boolean, the completion of this functionality
*/
async function populateActions(tdURI, node) {
emptyActionsSelect();
let thingURIs;
try {
thingURIs = await fetchThingURIs(tdURI);
}
catch (reason) {
//TODO: Handle failure (display error, with icon and notifiction)
// node.status({fill:"red",shape:"ring",text:"Disconnected from Thing directory"});
// error("Failed to connect to Thing Directory");
return reason;
}
let actionMap = await getActions(thingURIs);
let actionSelect = document.getElementById("node-input-thingAction");
if (Object.keys(actionMap).length === 0) { //No actions
let option = document.createElement("option");
option.innerHTML = "No actions available";
actionSelect.appendChild(option);
actionSelect.disabled = true;
// node.status({fill:"yellow",shape:"ring",text:"No actions found"}); //TODO: Allow status changes
document.getElementById("label-parameters").innerText = "";
document.getElementById("label-output").innerText = "";
return;
}
actionSelect.disabled = false;
// node.status({fill:"green",shape:"dot",text:"connected"}); //TODO: Allow status changes
actionSelect.addEventListener("change", async function () { //Set thingAction value to new selection
let selection = actionSelect.options[actionSelect.selectedIndex];
let actionObject = $(selection).data("value");
node.thingAction = selection.text;
let actionJSON = JSON.stringify(actionObject); //Access object in select's data-value
this.thingActionValue = actionJSON;
node.thingActionValue = actionJSON;
//TODO: Use description and title
updateParameterLabels(actionObject);
updateOutputLabel(actionObject);
enableDeploy();
});
let index = 0;
for (let action of Object.keys(actionMap)) {
let option = document.createElement('option');
actionMap[action]["action"] = action; //Move action name from a key to a value
$(option).data("value", actionMap[action]);
option.innerHTML = action;
actionSelect.appendChild(option);
if (action === node.thingAction) { //Select the thingAction option
actionSelect.selectedIndex = index;
}
index++;
}
let selection = actionSelect.options[actionSelect.selectedIndex];
let actionObject = $(selection).data("value");
let actionJSON = JSON.stringify(actionObject);
node.thingActionValue = actionJSON;
updateParameterLabels(actionObject);
updateOutputLabel(actionObject);
}
function emptyActionsSelect() {
$("select#node-input-thingAction").empty();
}
async function fetchThingURIs(tdURI) {
return fetch(tdURI + "/actions/getURIs", {
method: "POST"
})
.then((response) => {
return response.json();
})
.catch((reason) => {
throw reason;
});
}
/**
* Note: No action names may be duplicated in the system
* @returns A map of action to the Thing URIs, list of parameters (along with their types is provided), output type, description and title
*/
async function getActions(uriList) {
let uriMap = {}; //Maps actions to the URIs they are associated with
for (let uri of uriList) {
let actions = await fetchObject(uri, "actions");
if (actions === undefined) continue;
for (let action of Object.keys(actions)) {
uriMap[action] = {
"uri" : uri,
"params" : {},
"output" : undefined,
"description" : undefined,
"title" : undefined
}; //Note: Will override an action if a duplicate occurs
let actionObj = actions[action];
uriMap[action]["output"] = actionObj["output"];
uriMap[action]["description"] = actionObj["description"];
uriMap[action]["title"] = actionObj["title"];
let params = actionObj["input"];
if (params === undefined || params === null) continue;
let usingProperties = false;
if (Object.keys(params).includes("properties")) { // If "inputs.properties" exists, take paramaters from this
params = params.properties;
usingProperties = true;
}
for (let parameter of Object.keys(params)) {
if (!usingProperties && parameter === "type") continue; //Ignore "inputs.type"
let type = params[parameter]["type"];
uriMap[action]["params"][parameter] = type; //Map parameter to it's type
}
}
}
return uriMap;
}
async function fetchObject(uri, key) {
let response = await fetch(uri, {
method: "GET"
})
.then((response) => {
return response.json();
})
.catch((reason) => {
throw reason;
});
return response[key]; //Only allows keys from top-level
}
function enableDeploy() {
let holder = document.getElementById("node-input-redeploy");
if (holder.value !== "true") {
holder.value = "true";
}
else {
holder.value = "false";
}
}
function updateParameterLabels(actionObject) {
let params = actionObject["params"];
let paramHolder = document.getElementById("label-parameters");
let paramText = formParameterText(params);
if(paramText === "") {
paramHolder.innerText = "No parameters";
}
else {
paramHolder.innerText = paramText;
}
}
//TODO: Fix for multi outputs (for all node types)
//TODO: Add output descriptions
function updateOutputLabel(actionObject) {
let output = actionObject["output"];
let outputHolder = document.getElementById("label-output");
try {
outputHolder.innerText = output["type"];
}
catch {
outputHolder.innerText = "undefined";
}
}
//TODO: Add parameter descriptions
function formParameterText(params) {
let text = "";
for (let parameter of Object.keys(params)) {
text += parameter + " : " + params[parameter] + "\n";
}
return text.substring(0, text.length - 1);
}
}
});
</script>
<style>
.parameterLabel {
vertical-align: top;
width: 60%;
}
#label-parameters {
width: 60%;
}
#label-output {
width: 60%;
}
</style>
<script type="text/html" data-template-name="system-action-node">
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
<div class="form-row">
<label for="node-input-thingDirectoryURI"><i class="fa fa-tag"></i> Thing Directory</label>
<input type="text" id="node-input-thingDirectoryURI" placeholder="http://localhost:8080/td">
</div>
<div id="action-row" class="form-row">
<label for="node-input-thingAction"><i class="fa fa-tag"></i> Action</label>
<select name="actionSelect" id="node-input-thingAction">
<option>Placeholder</option>
</select>
<div class="form-row">
<label for="label-parameters"> Parameters</label>
<label id="label-parameters" class="parameterLabel"></label>
</div>
<div class="form-row">
<label for="label-output"> Output Type</label>
<label id="label-output" class="parameterLabel"></label>
</div>
</div>
<div class="form-row">
<label for="node-input-outputToMsg"><i class="fa fa-tag"></i> Output to msg.payload</label>
<input type="checkbox" checked id="node-input-outputToMsg" name="outputToMsg">
</div>
<input type="hidden" id="node-input-redeploy" name="holder">
</script>
<script type="text/html" data-help-name="system-action-node">
<p>A node describing all Thing actions held within its ThingDirectory</p>
</script>