-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsystem-event-node.html
More file actions
291 lines (234 loc) · 9.76 KB
/
system-event-node.html
File metadata and controls
291 lines (234 loc) · 9.76 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
<script type="text/javascript">
RED.nodes.registerType('system-event-node', {
category: 'Web of Things',
color: '#a6bbcf',
defaults: {
name: { value: "" },
thingDirectoryURI: { value: "" },
thingEvent: { value: "" },
thingEventValue: { value: "" },
outputToMsg: { value: true },
redeploy: { value: "false" } //Only used to trigger UI redeploy
},
inputs: 0,
outputs: 1,
outputLabels: function () {
try {
return JSON.parse(this.thingEventValue).output["type"];
}
catch {
return "undefined";
}
},
icon: "bridge.svg",
label: function () {
if (this.name) {
return this.name;
}
else if (this.thingEventValue) {
return JSON.parse(this.thingEventValue).event;
}
else {
return "system-event-node";
}
},
oneditprepare: function () {
var node = this;
// hideFunctionOption(); //TODO: Buffer options whilst waiting for connection to Thing Directory
if(node.thingEventValue === undefined) { //Ensures value is added to this node object
node.thingEventValue = "";
}
else {
try {
let eventObj = JSON.parse(node.thingEventValue);
updateOutputLabel(eventObj);
}
catch {
node.thingFunctionValue = "";
}
}
$("#node-input-thingDirectoryURI").on("change", async function (event) {
let input = $(this).val();
let emptyInput = ["", undefined, null].includes(input);
if (!emptyInput) {
await populateEvents(input, node);
}
displayEventOption(!emptyInput);
});
function showEventOption() {
$("div#event-row").show();
}
function hideEventOption() {
$("#node-input-thingEvent").innerText = "";
this.thingEvent = "";
$("div#event-row").hide();
}
function displayEventOption(on) {
if (on) {
showEventOption();
} else {
hideEventOption();
}
}
/**
* @returns Boolean, the completion of this functionality
*/
async function populateEvents(tdURI, node) {
emptyEventsSelect();
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 eventMap = await getEvents(thingURIs);
let eventSelect = document.getElementById("node-input-thingEvent");
if (Object.keys(eventMap).length === 0) { //No events
let option = document.createElement("option");
option.innerHTML = "No events available";
eventSelect.appendChild(option);
eventSelect.disabled = true;
// node.status({fill:"yellow",shape:"ring",text:"No events found"}); //TODO: Allow status changes
document.getElementById("label-output").innerText = "";
return;
}
eventSelect.disabled = false;
// node.status({fill:"green",shape:"dot",text:"connected"}); //TODO: Allow status changes
eventSelect.addEventListener("change", async function () { //Set thingEvent value to new selection
let selection = eventSelect.options[eventSelect.selectedIndex];
let eventObject = $(selection).data("value");
node.thingEvent = selection.text;
let eventJSON = JSON.stringify(eventObject); //Access object in select's data-value
this.thingEventValue = eventJSON;
node.thingEventValue = eventJSON;
//TODO: Use description and title
updateOutputLabel(eventObject);
enableDeploy();
});
let index = 0;
for (let event of Object.keys(eventMap)) {
let option = document.createElement('option');
eventMap[event]["event"] = event; //Move event name from a key to a value
$(option).data("value", eventMap[event]);
option.innerHTML = event;
eventSelect.appendChild(option);
if (event === node.thingEvent) { //Select the thingEvent option
eventSelect.selectedIndex = index;
}
index++;
}
let selection = eventSelect.options[eventSelect.selectedIndex];
let eventObject = $(selection).data("value");
let eventJSON = JSON.stringify(eventObject);
node.thingEventValue = eventJSON;
updateOutputLabel(eventObject);
}
function emptyEventsSelect() {
$("select#node-input-thingEvent").empty();
}
async function fetchThingURIs(tdURI) {
return fetch(tdURI + "/actions/getURIs", {
method: "POST"
})
.then((response) => {
return response.json();
})
.catch((reason) => {
throw Error("Failed to access Thing directory");
});
}
/**
* Note: No event names may be duplicated in the system
* @returns A map of events to the Thing URIs, output type, description and title
*/
async function getEvents(uriList) {
let uriMap = {}; //Maps events to the URIs they are associated with
for (let uri of uriList) {
let events = await fetchObject(uri, "events");
if (events === undefined) continue;
for (let event of Object.keys(events)) {
uriMap[event] = {
"uri" : uri,
"output" : undefined,
"description" : undefined,
"title" : undefined
}; //Note: Will override an event if a duplicate occurs
let eventObj = events[event];
uriMap[event]["output"] = eventObj["data"];
uriMap[event]["description"] = eventObj["description"];
uriMap[event]["title"] = eventObj["title"];
}
}
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 updateOutputLabel(eventObject) {
let output = eventObject["output"];
let outputHolder = document.getElementById("label-output");
try {
outputHolder.innerText = output["type"];
}
catch {
outputHolder.innerText = "undefined";
}
}
}
});
</script>
<style>
.parameterLabel {
vertical-align: top;
width: 60%;
}
#label-output {
width: 60%;
}
</style>
<script type="text/html" data-template-name="system-event-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="event-row" class="form-row">
<label for="node-input-thingEvent"><i class="fa fa-tag"></i> Event</label>
<select name="eventSelect" id="node-input-thingEvent">
<option>Placeholder</option>
</select>
<div class="form-row">
<label for="label-output"> Output Type</label>
<label id="label-output" class="parameterLabel"></label>
</div>
</div>
<input type="hidden" id="node-input-redeploy" name="holder">
</script>
<script type="text/html" data-help-name="system-event-node">
<p>A node describing all Thing events held within its ThingDirectory</p>
</script>