-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.html
352 lines (336 loc) · 11.7 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!-- web app settings -->
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=0">
<!-- Title, icons and colors -->
<title>CLEXI Test</title>
<meta name="application-name" content="CLEXI Test"/>
<meta name="apple-mobile-web-app-title" content="CLEXI Test">
<meta name="description" content="CLEXI test-page">
<meta name="theme-color" content="#eee">
<!-- Icons
<link rel="icon" sizes="192x192" href="img/icon.png">
<link rel="apple-touch-icon" href="img/icon.png">
<meta property="og:image" content="img/icon.png"/>
-->
<!-- imports -->
<!--
<script type="text/javascript" src="js/jquery-3.1.1.min.js" charset="UTF-8"></script>
<link rel="stylesheet" href="css/main.css">
-->
<script type="text/javascript" src="lib/clexi-0.9.1.js" charset="UTF-8"></script>
<style>
html {
height: 100%;
}
body {
font-family: Sans-Serif;
display: flex;
flex-direction: column;
height: calc(100% - 16px);
}
button, input, label {
margin: 4px;
}
#log-out {
font-size: 15px;
color: #00bcd4;
background: #000;
padding: 4px 6px;
border-radius: 5px;
flex: 1 1 auto;
min-height: 200px;
overflow: auto;
}
#log-out > p {
margin: 8px 0;
}
</style>
</head>
<body>
<div>
<h3>Node.js Client Extension Interface - CLEXI.js - Test-Page</h3>
</div>
<div>
<h4>Connection</h4>
<label>Host:</label><input id="customHost"><br>
<label>Server ID:</label><input id="customServerId">
<div>
<button onclick="serverPing()">Ping CLEXI server</button>
<button onclick="connectToWebsocket()">Connect</button>
<button onclick="closeWebsocketConnection()">Close</button>
</div>
</div>
<h4>Xtensions</h4>
<div>
<label>Broadcast/HTTP-Events:</label>
<button onclick="websocketSendPing()">Broadcast Ping</button>
<button onclick="httpTestEvent()">Send Test HTTP-Event</button>
</div>
<div>
<label>Bluetooth BLE Beacon:</label>
<button onclick="websocketSendStartToBleBeaconScanner()">Start BLE Beacon Scanner</button>
<button onclick="websocketSendStopToBleBeaconScanner()">Stop BLE Beacon Scanner</button>
<button onclick="websocketSendStateRequestToBleBeaconScanner()">Get BLE Beacon Scanner State</button>
</div>
<div>
<label>Runtime Commands:</label>
<button onclick="websocketSendRuntimeCommand()">Call 'echo Hello World' in 3s</button>
</div>
<div>
<p><b>Xtension: GPIO Interface (Raspberry Pi):</b></p>
<div>
<button onclick="websocketGpioGetAll();">Get All Registered</button>
<button onclick="websocketGpioReleaseAll();">Release All</button>
</div>
<div>
<label>Button - Pin:</label><input id="gpio-button-pin" type="number">
<button onclick="websocketRegisterGpioButton(document.getElementById('gpio-button-pin').value);">Register</button>
<button onclick="websocketReleaseGpioButton(document.getElementById('gpio-button-pin').value);">Release</button>
</div>
<div>
<label>LED - Pin:</label><input id="gpio-led-pin" type="number">
<button onclick="websocketRegisterGpioLed(document.getElementById('gpio-led-pin').value);">Register</button>
<button onclick="websocketReleaseGpioLed(document.getElementById('gpio-led-pin').value);">Release</button>
<button onclick="websocketSetGpioLed(document.getElementById('gpio-led-pin').value, 1);">1</button>
<button onclick="websocketSetGpioLed(document.getElementById('gpio-led-pin').value, 0);">0</button>
</div>
<div>
<label>ReSpeaker Mic HAT LEDs:</label>
<button onclick="websocketRegisterGpioRespeakerMicHat();">Register</button>
<button onclick="websocketReleaseGpioRespeakerMicHat();">Release</button>
<button onclick="websocketGetGpioRespeakerMicHat();">Get</button>
<button onclick="websocketSetGpioRespeakerMicHat(1, 0, 150, 0);">(1) Green</button>
<button onclick="websocketSetGpioRespeakerMicHat(1, 100, 100, 0);">(1) Yellow</button>
<button onclick="websocketSetGpioRespeakerMicHat(1, 150, 0, 0);">(1) Red</button>
<button onclick="websocketSetAllRandomGpioRespeakerMicHat();">All Random</button>
<button onclick="websocketSetAllOffGpioRespeakerMicHat();">All Off</button>
</div>
</div>
<h4>Client log: <button onclick="document.getElementById('log-out').innerHTML = '';">clear</button></h4>
<div id="log-out"><p>Welcome - Please connect.</p></div>
<div style="height: 4px; width: 100%; flex: 0 0 4px;"></div>
<script>
var logOut = document.getElementById('log-out');
var customHost = document.getElementById('customHost');
var customServerId = document.getElementById('customServerId');
var cachedHost = ('sessionStorage' in window)? sessionStorage.getItem('hostURL') : undefined;
if (cachedHost){
customHost.value = cachedHost;
}else{
customHost.value = location.href.replace(/\/index\.html.*/, "").replace(/\/$/, "").replace(/^http(s|):/, "ws$1:");
}
function log(msg){
console.log(msg);
var msgEle = document.createElement("p");
msgEle.textContent = msg;
logOut.appendChild(msgEle);
logOut.scrollTop = logOut.scrollHeight;
}
function err(e){
console.error(e);
var msgEle = document.createElement("p");
msgEle.style.color = "#e91e4d";
msgEle.textContent = (e && e.msg)? e.msg : e;
logOut.appendChild(msgEle);
logOut.scrollTop = logOut.scrollHeight;
}
//CLEXI Demo
ClexiJS.clientBaseId = "TEST-CL-";
ClexiJS.onLog = log;
ClexiJS.onDebug = log;
ClexiJS.onError = err;
ClexiJS.serverId = "clexi-123";
customServerId.value = ClexiJS.serverId;
function connectToWebsocket(){
ClexiJS.serverId = customServerId.value;
var hostURL = customHost.value || cachedHost || ""; //auto-choose origin
if ('sessionStorage' in window) sessionStorage.setItem('hostURL', hostURL);
//NOTE: something to try as well: ClexiJS.pingAndConnect(host, onPingOrIdError, onOpen, onClose, onError, onConnecting);
ClexiJS.connect(hostURL, function(e){
//log("CLEXI - ready.");
}, function(e){
//log("CLEXI - lost connection.");
removeSubscriptions();
}, function(err){
//log("CLEXI - something went wrong.");
removeSubscriptions();
}, function(){
//log("CLEXI - connecting.");
}, function(welcomeInfo){
//log("CLEXI - welcome event.");
//subscribe to extension events:
subscribeAllAvailable(welcomeInfo);
});
//log("Connecting to CLEXI...");
}
function closeWebsocketConnection(){
ClexiJS.close();
}
function serverPing(){
ClexiJS.serverId = customServerId.value;
var hostURL = customHost.value || cachedHost || ""; //auto-choose origin
if (!hostURL) hostURL = location.origin;
else hostURL = hostURL.replace(/^wss/, 'https').replace(/^ws/, 'http');
ClexiJS.httpRequest("GET", hostURL + "/ping", function(data){
//Success
log("HTTP GET result to 'ping' endpoint: " + data);
}, function(){
//Error
err({msg:"CLEXI connection failed! Server not reached."});
});
}
function httpTestEvent(){
var hostURL = customHost.value || cachedHost || ""; //auto-choose origin
if (!hostURL) hostURL = location.origin;
else hostURL = hostURL.replace(/^wss/, 'https').replace(/^ws/, 'http');
//Test event via HTTP GET. HTTP POST is possible too, but not yet with this client method (v0.8.1, build your own POST).
var eventName = "testEvent";
var eventData = {
"num": 42
};
ClexiJS.sendHttpEvent(hostURL, ClexiJS.serverId, eventName, eventData, function(data){}, function(){
//Error
err({msg:"CLEXI connection failed! Server not reached or not authorized."});
});
}
//Broadcaster
function websocketSendPing(){
ClexiJS.send('clexi-broadcaster', {
text: "ping"
});
}
//BLE Beacon
function websocketSendStartToBleBeaconScanner(){
ClexiJS.send('ble-beacon-scanner', {
ctrl: "start"
});
}
function websocketSendStopToBleBeaconScanner(){
ClexiJS.send('ble-beacon-scanner', {
ctrl: "stop"
});
}
function websocketSendStateRequestToBleBeaconScanner(){
ClexiJS.send('ble-beacon-scanner', {
ctrl: "state"
});
}
//Runtime commands
var rtcCmdId = 1;
function websocketSendRuntimeCommand(){
ClexiJS.send('runtime-commands', {
id: "CMD-" + ++rtcCmdId,
cmd: "callCustom",
args: {
delay: 3000,
file: "echo",
TEXT: "Hello World"
}
});
}
//GPIO-Interface
function websocketSendGpioInterfaceRequest(action, type, config){
ClexiJS.send('gpio-interface', {
action: action,
type: type,
config: config
});
}
function websocketGpioGetAll(){
websocketSendGpioInterfaceRequest("get", "all", {});
}
function websocketGpioReleaseAll(){
websocketSendGpioInterfaceRequest("release", "all", {});
}
function websocketRegisterGpioButton(pin){
websocketSendGpioInterfaceRequest("register", "button", {pin: pin});
}
function websocketReleaseGpioButton(pin){
websocketSendGpioInterfaceRequest("release", "button", {pin: pin});
}
function websocketRegisterGpioLed(pin){
websocketSendGpioInterfaceRequest("register", "led", {pin: pin});
}
function websocketReleaseGpioLed(pin){
websocketSendGpioInterfaceRequest("release", "led", {pin: pin});
}
function websocketSetGpioLed(pin, value){
websocketSendGpioInterfaceRequest("set", "led", {pin: pin, value: value});
}
//GPIO-Interface - Custom Item - ReSpeaker Mic HAT (SPI interface)
function websocketRegisterGpioRespeakerMicHat(){
websocketSendGpioInterfaceRequest("register", "item", {
file: "rpi-respeaker-mic-hat-leds",
options: {numOfLeds: 3}
});
}
function websocketReleaseGpioRespeakerMicHat(){
websocketSendGpioInterfaceRequest("release", "item", {
file: "rpi-respeaker-mic-hat-leds"
});
}
function websocketGetGpioRespeakerMicHat(){
websocketSendGpioInterfaceRequest("get", "item", {
file: "rpi-respeaker-mic-hat-leds"
});
}
function websocketSetGpioRespeakerMicHat(ledIndex, red, green, blue){
websocketSendGpioInterfaceRequest("set", "item", {
file: "rpi-respeaker-mic-hat-leds",
data: {ledIndex: ledIndex, red: red, green: green, blue: blue}
});
}
function websocketSetAllRandomGpioRespeakerMicHat(){
websocketSetGpioRespeakerMicHat(1, getRandomLedValue(), getRandomLedValue(), getRandomLedValue());
websocketSetGpioRespeakerMicHat(2, getRandomLedValue(), getRandomLedValue(), getRandomLedValue());
websocketSetGpioRespeakerMicHat(3, getRandomLedValue(), getRandomLedValue(), getRandomLedValue());
}
function websocketSetAllOffGpioRespeakerMicHat(){
websocketSetGpioRespeakerMicHat(1, 0, 0, 0);
websocketSetGpioRespeakerMicHat(2, 0, 0, 0);
websocketSetGpioRespeakerMicHat(3, 0, 0, 0);
}
function getRandomLedValue(){
return Math.round(Math.random() * 255 * 0.2);
}
//Common subscription method
function subscribeToXtension(xName){
ClexiJS.subscribeTo(xName, function(e){
log(xName + ' event: ' + JSON.stringify(e));
}, function(e){
log(xName + ' response: ' + JSON.stringify(e));
}, function(e){
log(xName + ' error: ' + JSON.stringify(e));
});
}
function subscribeAllAvailable(welcomeInfo){
//subscribe all active xtensions
if (welcomeInfo && welcomeInfo.xtensions){
var xtensionNames = Object.keys(welcomeInfo.xtensions);
xtensionNames.forEach(function(xn){
//check if xtension is active
if (welcomeInfo.xtensions[xn].active){
subscribeToXtension(xn);
subscriptions.push(xn);
}
});
}
log("Subscriptions: " + JSON.stringify(subscriptions));
}
function removeSubscriptions(){
//unsubscribe all active xtensions
subscriptions.forEach(function(xn){
ClexiJS.removeSubscription(xn);
});
if (subscriptions.length){
log("Unsubscribed all");
}
subscriptions = [];
}
var subscriptions = [];
</script>
</body>
</html>