-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
439 lines (375 loc) · 13.7 KB
/
app.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
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/* Global variables*/
let mqtt = null //object for the connection
let message = null //when you get error, message should be displayed back
let status = null //part of the logs
let reconnectTimeout = 2000 //if you enter the wrong server information
let connected_flag = 0 //if broker is connected, flag = 1
let subs = [] //subscription will be in this array
let subs_qos = [] //subscription quality of service will be in this array
let subCount = 0 //when you add a subscription, add a count
let check = false; //checking if a wrong command is entered
let host = null //input for broker
let port = null //input for port
let clientId = null //input for clientID
let el = null
let startConnection = null //disconnect is null
let pulishCheck = 0 //if publish is correct, then the check will be 1
let arraybyteData = null; //if your posting a file, the data will be stored here
//files
let commingFileNames = [];
let commingFiles = [];
/* Validation for connection. Making sure x (host), y(port), z(clientID) are not empty.
If they are empty, return false */
function validate(x, y, z) {
if (x == "" || y == "" || z == "") {
return false
}
return true
}
// Called when the client loses its connection
function onConnectionLost() {
console.log("connection lost");
let data = document.createElement('span');
data.innerHTML = "connection lost";
status.appendChild(data)
let color = "#ff0000";
el.style.borderColor = color;
startConnection.style.background = "#00ff00";
connected_flag = 0;
}
/* The onFailure function shown below logs a message and attempts
a reconnect every few seconds set by the reconnecttimeout variable.*/
function onFailure() {
console.log("Failed");
let color = "#ff0000";
el.style.borderColor = color;
setTimeout(MQTTconnect, reconnectTimeout);
}
// Called when a message arrives
function onMessageArrived(r_message) {
let messageData = r_message.payloadString;
let checkFiles = messageData.split(" ")[0]
let out_msg = "Message received on topic/";
if (checkFiles == "MQTT_file_") {
let filesName = messageData.split(" ")[1]
messageData = messageData.substr(messageData.indexOf(" ") + 1);
messageData = messageData.substr(messageData.indexOf(" ") + 1);
commingFileNames.push(filesName)
commingFiles.push(messageData)
out_msg = out_msg + r_message.destinationName + ", file " + filesName;
var a = document.createElement('a');
var link = document.createTextNode("Download file");
a.appendChild(link);
a.title = "Download file";
a.classList.add("link");
a.href = commingFileNames.length - 1;
let data = document.createElement('span');
data.innerHTML = out_msg;
data.appendChild(a)
status.appendChild(data)
console.log(out_msg);
} else {
out_msg = out_msg + r_message.destinationName + ": " + r_message.payloadString;
let data = document.createElement('span');
data.innerHTML = out_msg;
status.appendChild(data)
}
var elements = document.getElementsByClassName('link');
for (var i = 0; i < elements.length; i++) {
elements[i].addEventListener('click', function (e) {
let fileIndex = parseInt(this.getAttribute("href"));
var str = commingFiles[fileIndex],
arr = new Uint8Array(str.length);
str.split("").forEach(function (ac, b) {
arr[b] = ac.charCodeAt();
});
download(arr, commingFileNames[fileIndex], "text/plain");
e.preventDefault()
});
}
notifyme(out_msg, 'success');
arraybyteData = null;
document.getElementById("myfile").value = "";
}
function onConnected(recon, url) {
var data = document.createElement('span');
data.innerHTML = " in onConnected " + reconn;
status.appendChild(data)
}
// Called when the client connects
function onConnect() {
connected_flag = 1
el = document.getElementById("connectForm");
let color = "#00ff00";
el.style.borderColor = color;
console.log("on Connect " + connected_flag);
var data = document.createElement('span');
data.innerHTML = "Connected to " + host + ":" + port;
status.appendChild(data)
notifyme("Connected to " + host + ":" + port);
check = true;
subs = []
subs_qos = []
populateSubs();
}
//called when disconnect button is pressed
function disconnect() {
let color = "#168D5F";
startConnection.style.background = color;
startConnection.innerHTML = "Connect"
el.style.borderColor = "#000000";
mqtt.disconnect();
check = false
}
function MQTTconnect() {
host = document.getElementById("url").value
port = parseInt(document.getElementById("port").value)
clientId = document.getElementById("userID").value
el = document.getElementById("connectForm");
startConnection = document.getElementById("connect");
status = document.getElementById("messages");
if (check) {
disconnect();
} else {
if (validate(host, port, clientId)) {
mqtt = new Paho.MQTT.Client(host, port, clientId);
let options = {
timeout: 3,
onSuccess: onConnect,
onFailure: onFailure,
};
mqtt.onConnectionLost = onConnectionLost;
mqtt.onMessageArrived = onMessageArrived;
//mqtt.onConnected = onConnected;
mqtt.connect(options);
startConnection.innerHTML = "Disconnect";
let color = "#ff0000";
startConnection.style.background = color;
check = true;
} else {
var data = document.createElement('span');
data.innerHTML = "Error! A correct input can be broker.hivemq.com:8000";
status.appendChild(data)
}
}
return false;
}
/*
allows the user to subscribe,
checks if user has already subscribed to a same topic,
other checks for the client
*/
function sub_topics() {
if (connected_flag == 0) {
let out_msg = "Connect to a broker first!"
console.log(out_msg);
notifyme(out_msg, 'warning');
} else {
let stopic = document.getElementById("topic").value;
if (stopic.trim() != "") {
let qosNr = parseInt(document.getElementById("qos").value);
let out_msg = "Subscribed to " + stopic + ":" + qosNr;
let data = document.createElement('span');
data.innerHTML = out_msg;
status.appendChild(data);
mqtt.subscribe(stopic, {
qos: qosNr
});
if (!subs.includes(stopic)) {
subs.push(stopic)
subs_qos.push(qosNr)
console.log(out_msg);
notifyme(out_msg, 'success');
} else {
out_msg = "Already subscribed to " + stopic + ":" + qosNr;
console.log(out_msg);
notifyme(out_msg, 'warning');
}
} else {
let out_msg = "Enter a topic!";
console.log(out_msg);
notifyme(out_msg, 'warning');
}
populateSubs()
}
return false;
}
//Everytime you subscribe, the sub will be populated.
function populateSubs() {
let dynamicSelect = document.getElementById("sub_topic");
let mysubs = document.getElementById("mysubs");
let post_topic = document.getElementById("post_topic");
post_topic.innerHTML = ""
mysubs.innerHTML = ""
dynamicSelect.innerHTML = "";
subs.forEach((element, index) => {
let option_elem = document.createElement('option');
option_elem.value = index;
option_elem.textContent = element;
dynamicSelect.appendChild(option_elem);
let post_elem = document.createElement('option');
post_elem.value = index;
post_elem.textContent = element;
post_topic.appendChild(post_elem);
var subsSpan = document.createElement('span');
subsSpan.innerHTML = element + " : " + subs_qos[index] + " ";
mysubs.appendChild(subsSpan)
});
}
//updates the sub array when unsubscribing
function unsubscribe_topics() {
let index = document.getElementById("sub_topic").value;
if (index != null) {
try {
mqtt.unsubscribe(subs[index]);
let out_msg = "Unsubscribing: " + subs[index];
console.log(out_msg);
let data = document.createElement('span');
data.innerHTML = out_msg;
status.appendChild(data)
if (index > -1) {
subs.splice(index, 1);
subs_qos.splice(index, 1);
}
notifyme(out_msg, 'success');
populateSubs()
} catch (error) {
console.log("no topic to unsubscribe");
notifyme('no topic to unsubscribe', 'success');
}
}
}
/* The publishSMS function extracts the message and topic and logs them.
We use the payload method to send and recieve messages and files*/
function publishSMS() {
if (connected_flag == 0) {
let out_msg = "Connect to a broker first!"
console.log(out_msg);
notifyme(out_msg, 'warning');
try {
let data = document.createElement('span');
data.innerHTML = out_msg;
status.appendChild(data)
} catch (error) {
console.log("error ...");
}
} else {
let payload = document.getElementById("sms").value;
let topic = null;
let qospublish = null;
let topicpublish = null;
//checking file payload
if (arraybyteData !== null) {
payload = arraybyteData;
}
if (pulishCheck == 0) {
topic = document.getElementById("post_topic").value;
if (topic == null || payload.trim() == "") {
return inputRequired();
}
} else {
topicpublish = document.getElementById("othertopic").value;
qospublish = parseInt(document.getElementById("qos").value);
if (topicpublish == null || payload.trim() == "") {
return inputRequired();
}
}
message = new Paho.MQTT.Message(payload)
if (pulishCheck == 0) {
message.destinationName = subs[topic];
message.qos = subs_qos[topic];
} else {
message.destinationName = topicpublish.trim();
message.qos = qospublish;
}
mqtt.send(message);
arraybyteData = null;
document.getElementById("myfile").value = "";
}
return false
}
function inputRequired() {
Swal.fire({
position: 'top-end',
icon: 'warning',
title: 'Oops! ',
text: 'Sorry topic , message or file are required to publish ',
showConfirmButton: false,
timer: 2000
})
return false;
}
//popup alert
function notifyme(sms, type) {
Swal.fire({
position: 'top-end',
icon: type,
title: sms,
showConfirmButton: false,
timer: 1500
})
}
function topicOption() {
let checkBox = document.getElementById("unsub");
let othertopics = document.getElementById("othertopic");
let lbothertopics = document.getElementById("lbOthertopics");
let mytopics = document.getElementById("post_topic");
let lbmytopics = document.getElementById("lbSelectTopic");
if (checkBox.checked == true) {
othertopics.style.display = "inline";
lbothertopics.style.display = "inline";
lb_qospublish.style.display = "inline";
qospublish.style.display = "inline";
mytopics.style.display = "none";
lbmytopics.style.display = "none";
pulishCheck = 1;
} else {
othertopics.style.display = "none";
lbothertopics.style.display = "none";
lb_qospublish.style.display = "none";
qospublish.style.display = "none";
mytopics.style.display = "inline";
lbmytopics.style.display = "inline";
pulishCheck = 0;
}
}
//store file byte array data here
function valueData(sms) {
let fullPath = document.getElementById('myfile');
let filename = fullPath.files[0].name;
filename = filename.split(' ').join('_');
arraybyteData = "MQTT_file_ " + filename + " " + sms;
}
/*User imports file
==> create a buffer
==> process the file to a Unit8Array
==>process the Unit8Array to a binaryString*/
document.querySelector("#myfile").addEventListener(
"change",
function () {
var reader = new FileReader();
reader.onload = function () {
var arrayBuffer = this.result,
array = new Uint8Array(arrayBuffer),
binaryString = String.fromCharCode.apply(null, array);
valueData(binaryString);
};
reader.readAsArrayBuffer(this.files[0]);
},
false
);
//subscribe model
let model = document.getElementById('subscribeWindow');
let btn = document.getElementById("showWindow");
let close = document.getElementsByClassName("close")[0];
btn.onclick = function () {
model.style.display = "block";
}
close.onclick = function () {
model.style.display = "none";
}
window.onclick = function (event) {
if (event.target == model) {
model.style.display = "none";
}
}