-
Notifications
You must be signed in to change notification settings - Fork 80
/
counter.js
191 lines (182 loc) · 7.94 KB
/
counter.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/**
* 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();
*
**/
let counterProperties = [];
function get_td(addr) {
servient.start().then((thingFactory) => {
helpers
.fetch(addr)
.then((td) => {
thingFactory.consume(td).then((thing) => {
showInteractions(thing);
});
})
.catch((error) => {
window.alert("Could not fetch TD.\n" + error);
});
});
}
function showInteractions(thing) {
let td = thing.getThingDescription();
counterProperties = [];
for (let property in td.properties) {
if (td.properties.hasOwnProperty(property)) {
let dtItem = document.createElement("dt");
counterProperties.push(dtItem);
let ddItem = document.createElement("dd");
ddItem.setAttribute("dir", "auto"); // direction-independence, direction-heuristic
ddItem.appendChild(document.createTextNode("???"));
let link = document.createElement("a");
link.appendChild(document.createTextNode(property));
dtItem.appendChild(link);
document.getElementById("properties").appendChild(dtItem);
document.getElementById("properties").appendChild(ddItem);
dtItem.onclick = (click) => {
thing
.readProperty(property)
.then(async (res) => {
switch (res.form?.contentType) {
case "image/svg+xml":
ddItem.innerHTML = await res.value();
break;
case "image/png;base64":
const img = (await res.arrayBuffer()).toString("base64");
ddItem.innerHTML = `<img src="data:image/png;base64,${img}">`;
break;
default:
ddItem.textContent = await res.value();
break;
}
})
.catch((err) => window.alert("error: " + err));
};
// update value right-away
dtItem.click();
}
}
for (let action in td.actions) {
if (td.actions.hasOwnProperty(action)) {
let item = document.createElement("li");
item.setAttribute("dir", "auto"); // direction-independence, direction-heuristic
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) => {
thing
.invokeAction(action)
.then((res) => {
button.style.background = "rgb(0,255,0,0.2)";
setTimeout(function () {
button.style.background = null;
}, 500);
updateProperties();
})
.catch((err) => {
button.style.background = "rgb(255,0,0,0.2)";
setTimeout(function () {
button.style.background = null;
}, 500);
});
};
}
}
let eventSubscriptions = {};
for (let evnt in td.events) {
if (td.events.hasOwnProperty(evnt)) {
let item = document.createElement("li");
item.setAttribute("dir", "auto"); // direction-independence, direction-heuristic
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);
eventSubscriptions[evnt] = false;
let subscription;
let eventCounter = 0;
const eventDisplay = document.getElementById("event-display");
checkbox.onclick = (click) => {
if (
document.getElementById(evnt).checked &&
!eventSubscriptions[evnt] &&
eventSubscriptions[evnt] === false
) {
console.log("Try subscribing for event: " + evnt);
eventSubscriptions[evnt] = true;
thing
.subscribeEvent(evnt, async function (data) {
const value = await data.value();
console.log('Event "' + evnt + '"', value);
eventCounter++;
updateProperties();
document.getElementById("event-count").innerHTML = eventCounter;
document.getElementById("event-payload").innerHTML = value;
})
.then((sub) => {
subscription = sub;
console.log("Subscribed for event: " + evnt);
eventDisplay.style.display = "block";
})
.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] = false;
if (subscription) {
subscription
.stop()
.then(() => {
console.log("Unsubscribed for event: " + evnt);
eventDisplay.style.display = "none";
eventCounter = 0;
document.getElementById("event-count").innerHTML = eventCounter;
document.getElementById("event-payload").innerHTML = "";
})
.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 updateProperties() {
counterProperties.forEach(function (prop) {
prop.click();
});
}
var servient = new Wot.Core.Servient();
servient.addClientFactory(new Wot.Http.HttpClientFactory());
var helpers = new Wot.Core.Helpers(servient);
window.onload = () => {
// process passed URL
let $_GET = location.search
.substr(1)
.split("&")
.reduce((o, i) => ((u = decodeURIComponent), ([k, v] = i.split("=")), (o[u(k)] = v && u(v)), o), {});
if ($_GET["url"]) {
document.getElementById("td_addr").value = $_GET["url"];
}
get_td(document.getElementById("td_addr").value);
};