-
Notifications
You must be signed in to change notification settings - Fork 80
/
index.js
171 lines (159 loc) · 6.3 KB
/
index.js
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
/**
* 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) {
servient.start().then((thingFactory) => {
helpers
.fetch(addr)
.then((td) => {
thingFactory.consume(td).then((thing) => {
removeInteractions();
showInteractions(thing);
});
})
.catch((error) => {
window.alert("Could not fetch TD.\n" + error);
});
});
}
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 removeInteractions() {
for (id of ["properties", "actions", "events"]) {
let placeholder = document.getElementById(id);
while (placeholder.firstChild) {
placeholder.removeChild(placeholder.firstChild);
}
}
}
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);
}
}
var servient = new Wot.Core.Servient();
servient.addClientFactory(new Wot.Http.HttpClientFactory());
var helpers = new Wot.Core.Helpers(servient);
document.getElementById("fetch").onclick = () => {
get_td(document.getElementById("td_addr").value);
};