-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathindex.js
More file actions
335 lines (304 loc) · 12.2 KB
/
index.js
File metadata and controls
335 lines (304 loc) · 12.2 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
/**
* In the browser, node-wot only works in client mode with limited binding support.
* Supported bindings: HTTP / HTTPS / WebSockets
*
* After adding the following <script> tag to your HTML page:
* <script src="https://cdn.jsdelivr.net/npm/@node-wot/browser-bundle@latest/dist/wot-bundle.min.js" defer></script>
*
* you can access all node-wot functionality / supported packages through the "WoT" global object.
* Examples:
* var servient = new WoT.Core.Servient();
* var client = new WoT.Http.HttpClient();
*
**/
function get_td(addr) {
clearAllInteractions();
servient
.start()
.then((thingFactory) => {
helpers
.fetch(addr)
.then((td) => {
thingFactory
.consume(td)
.then((thing) => {
showInteractions(thing);
updateTabDescription(addr, td);
})
.catch((err) => {
updateTabDescription(addr, null, "Failed to consume TD: " + err);
});
})
.catch((err) => {
updateTabDescription(addr, null, "Failed to fetch TD: " + err);
});
})
.catch((err) => {
updateTabDescription(addr, null, "Failed to start servient: " + err);
});
}
function showInteractions(thing) {
let td = thing.getThingDescription();
for (let property in td.properties) {
if (td.properties.hasOwnProperty(property)) {
let item = document.createElement("li");
let link = document.createElement("a");
link.appendChild(document.createTextNode(property));
item.appendChild(link);
document.getElementById("properties").appendChild(item);
item.onclick = (click) => {
thing
.readProperty(property)
.then(async (res) => window.alert(property + ": " + (await res.value())))
.catch((err) => window.alert("error: " + err));
};
}
}
for (let action in td.actions) {
if (td.actions.hasOwnProperty(action)) {
let item = document.createElement("li");
let button = document.createElement("button");
button.appendChild(document.createTextNode(action));
button.className = "button tiny secondary";
item.appendChild(button);
document.getElementById("actions").appendChild(item);
item.onclick = (click) => {
showSchemaEditor(action, thing);
};
}
}
let eventSubscriptions = {};
for (let evnt in td.events) {
if (td.events.hasOwnProperty(evnt)) {
let item = document.createElement("li");
let link = document.createElement("a");
link.appendChild(document.createTextNode(evnt));
let checkbox = document.createElement("div");
checkbox.className = "switch small";
checkbox.innerHTML = '<input id="' + evnt + '" type="checkbox">\n<label for="' + evnt + '"></label>';
item.appendChild(link);
item.appendChild(checkbox);
document.getElementById("events").appendChild(item);
checkbox.onclick = (click) => {
if (document.getElementById(evnt).checked && !eventSubscriptions[evnt]) {
console.log("Try subscribing for event: " + evnt);
thing
.subscribeEvent(evnt, async function (data) {
window.alert("Event " + evnt + " detected");
})
.then((sub) => {
eventSubscriptions[evnt] = sub;
console.log("Subscribed for event: " + evnt);
})
.catch((error) => {
window.alert("Event " + evnt + " error\nMessage: " + error);
});
} else if (!document.getElementById(evnt).checked && eventSubscriptions[evnt]) {
console.log("Try to unsubscribing for event: " + evnt);
eventSubscriptions[evnt]
.stop()
.then(() => {
console.log("Unsubscribed for event: " + evnt);
eventSubscriptions[evnt] = undefined;
})
.catch((error) => {
window.alert("Event " + evnt + " error\nMessage: " + error);
});
}
};
}
}
// Check if visible
let placeholder = document.getElementById("interactions");
if (placeholder.style.display === "none") {
placeholder.style.display = "block";
}
}
function showSchemaEditor(action, thing) {
let td = thing.getThingDescription();
// Remove old editor
removeSchemaEditor();
let placeholder = document.getElementById("editor_holder");
let editor;
if (td.actions[action] && td.actions[action].input) {
td.actions[action].input.title = action;
editor = new JSONEditor(placeholder, {
schema: td.actions[action].input,
form_name_root: action,
});
}
// Add invoke button
let button = document.createElement("button");
button.appendChild(document.createTextNode("Invoke"));
placeholder.appendChild(button);
button.onclick = () => {
let input = editor ? editor.getValue() : undefined;
thing
.invokeAction(action, input)
.then(async (res) => {
if (typeof res === "object" && res.schema) {
window.alert("Success! Received response: " + (await res.value()));
} else {
window.alert("Executed successfully.");
}
})
.catch((err) => {
window.alert(err);
});
removeSchemaEditor();
};
}
function removeSchemaEditor() {
let placeholder = document.getElementById("editor_holder");
while (placeholder.firstChild) {
placeholder.removeChild(placeholder.firstChild);
}
}
// Error handling functions to show/hide error messages on web page instead of using alert window
function showError(message) {
const errorContainer = document.getElementById("error-container");
const errorText = document.getElementById("error-text");
if (errorContainer && errorText) {
errorText.textContent = message;
errorContainer.style.display = "block";
}
}
function hideError() {
const errorContainer = document.getElementById("error-container");
if (errorContainer) {
errorContainer.style.display = "none";
}
}
function updateTabDescription(url, td, error) {
// Find active tab and update its description
const activeTab = document.querySelector(".tabs-content .content.active");
if (!activeTab) return;
// Update URL link
const urlElement = activeTab.querySelector(".td-url");
if (urlElement && url) {
urlElement.href = url;
urlElement.textContent = url;
}
// Update description
const descriptionElement = activeTab.querySelector(".td-description");
const descriptionParent = descriptionElement ? descriptionElement.closest("p") : null;
if (error) {
// Hide description, show error banner
if (descriptionParent) descriptionParent.style.display = "none";
showError(error);
} else {
// Show description, hide error banner
if (descriptionParent) descriptionParent.style.display = "";
if (descriptionElement) {
descriptionElement.textContent = td?.description || "No description available";
descriptionElement.style.color = "";
}
hideError();
}
}
// Clear all interactions and editor
function clearAllInteractions() {
hideError();
const interactions = document.getElementById("interactions");
if (interactions) {
interactions.style.display = "none";
}
["properties", "actions", "events"].forEach((id) => {
const element = document.getElementById(id);
if (element) element.innerHTML = "";
});
removeSchemaEditor();
}
var servient = new WoT.Core.Servient();
servient.addClientFactory(new WoT.Http.HttpClientFactory());
var helpers = new WoT.Core.Helpers(servient);
// Tab configuration
const TD_URLS = {
testthing: "http://plugfest.thingweb.io/http-data-schema-thing",
smartcoffee: "http://plugfest.thingweb.io/http-advanced-coffee-machine",
counter: "http://plugfest.thingweb.io/counter",
};
document.addEventListener("DOMContentLoaded", function () {
// Parse URL parameters to pre-fill the input field
let $_GET = location.search
.substr(1)
.split("&")
.reduce((o, i) => ((u = decodeURIComponent), ([k, v] = i.split("=")), (o[u(k)] = v && u(v)), o), {});
// Tab configuration in correct sequence
const tabLinks = [
{ id: "tab-link-testthing", tab: "tab-testthing" },
{ id: "tab-link-smartcoffee", tab: "tab-smartcoffee" },
{ id: "tab-link-counter", tab: "tab-counter" },
{ id: "tab-link-custom", tab: "tab-custom" },
];
const tdInput = document.getElementById("td_addr");
const fetchBtn = document.getElementById("fetch");
const closeErrorBtn = document.getElementById("close-error");
// Error close button handler
if (closeErrorBtn) {
closeErrorBtn.onclick = (e) => {
e.preventDefault();
hideError();
};
}
// Pre-fill input from URL parameter if provided
if ($_GET["url"]) {
tdInput.value = $_GET["url"];
}
// Tab click handlers
tabLinks.forEach(({ id, tab }) => {
const link = document.getElementById(id);
if (link) {
link.addEventListener("click", function (e) {
e.preventDefault();
clearAllInteractions();
// Switch active tab
document.querySelectorAll("#td-tabs .tab-title").forEach((li) => li.classList.remove("active"));
link.parentElement.classList.add("active");
document.querySelectorAll(".tabs-content .content").forEach((c) => c.classList.remove("active"));
document.getElementById(tab).classList.add("active");
// Auto-consume TD based on tab
if (tab === "tab-testthing") {
get_td(TD_URLS.testthing);
} else if (tab === "tab-smartcoffee") {
get_td(TD_URLS.smartcoffee);
} else if (tab === "tab-counter") {
get_td(TD_URLS.counter);
} else if (tab === "tab-custom") {
// Reset custom TD tab
const customDesc = document.querySelector("#tab-custom .td-description");
if (customDesc) {
customDesc.textContent = "Enter a TD URL above to consume it.";
customDesc.style.color = "";
}
const customUrl = document.querySelector("#tab-custom .td-url");
if (customUrl) {
customUrl.href = "";
customUrl.textContent = "";
}
}
});
}
});
// Custom TD fetch button
if (fetchBtn) {
fetchBtn.onclick = () => {
if (tdInput.value) {
get_td(tdInput.value);
} else {
showError("Please enter a valid URL.");
}
};
}
// Auto-load TD if URL parameter was provided
if ($_GET["url"]) {
document.querySelectorAll("#td-tabs .tab-title").forEach((li) => li.classList.remove("active"));
document.getElementById("tab-link-custom").parentElement.classList.add("active");
document.querySelectorAll(".tabs-content .content").forEach((c) => c.classList.remove("active"));
document.getElementById("tab-custom").classList.add("active");
get_td($_GET["url"]);
} else {
// Default to Test Thing tab
document.getElementById("tab-link-testthing").click();
}
});