forked from CDOrtona/MQTT_WebSocket_Javascript_Client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqtt_webSocket.js
100 lines (83 loc) · 3.62 KB
/
mqtt_webSocket.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
var selectedClientID;
var sensors;
// Called after form input is processed
function startConnect() {
// Generate a random client ID
clientID = "clientID-" + "Control Panel";
// Fetch the hostname/IP address and port number from the form
host = document.getElementById("host").value;
port = document.getElementById("port").value;
user = document.getElementById("username").value;
pass = document.getElementById("password").value;
// Print output for the user in the messages div
document.getElementById("messages").innerHTML += '<span>Connecting to: ' + host + ' on port: ' + port + '</span><br/>';
document.getElementById("messages").innerHTML += '<span>Using the following client value: ' + clientID + '</span><br/>';
// Initialize new Paho client connection
client = new Paho.MQTT.Client(host, Number(port), clientID);
//initialize new sensor_listener object
//sensors = new listener();
// Set callback handlers
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
// Connect the client, if successful, call onConnect function
client.connect({
onSuccess: onConnect,
userName : user,
password : pass
});
}
// Called when the client connects
function onConnect() {
// Fetch the MQTT topic from the form
topic = document.getElementById("topic").value;
// Print output for the user in the messages div
document.getElementById("messages").innerHTML += '<span>Subscribing to: ' + topic + '</span><br/>';
// Subscribe to the requested topic
client.subscribe(topic);
// always subscribe to the emergency/sos token, since it's issued by any device
client.subscribe("emergency/sos")
}
// Called when the client loses its connection
function onConnectionLost(responseObject) {
console.log("onConnectionLost: Connection Lost");
if (responseObject.errorCode !== 0) {
console.log("onConnectionLost: " + responseObject.errorMessage);
}
}
// Called when a message arrives
function onMessageArrived(message) {
console.log("onMessageArrived: " + message.payloadString);
document.getElementById("messages").innerHTML += '<span>Topic: ' + message.destinationName + ' | ' + message.payloadString + '</span><br/>';
switch(message.destinationName){
case "30:AE:A4:F5:88:6E/temp":
document.getElementById("temp").innerHTML = message.payloadString;
break;
case "30:AE:A4:F5:88:6E/humidity":
document.getElementById("humidity").innerHTML = message.payloadString;
break;
case "30:AE:A4:F5:88:6E/pressure":
document.getElementById("pressure").innerHTML = message.payloadString;
break;
case "30:AE:A4:F5:88:6E/altitude":
document.getElementById("altitude").innerHTML = message.payloadString;
break;
case "emergency/sos":
obj = JSON.parse(message.payloadString);
alert("SOS DETECTED FROM " + obj.sender + '\n\n' + "Click OK to view user's location ")
var position = 'http://www.google.com/maps/place/' + obj.position
window.open(position);
break;
}
updateScroll(); // Scroll to bottom of window
}
// Called when the disconnection button is pressed
function startDisconnect() {
client.disconnect();
document.getElementById("messages").innerHTML += '<span>Disconnected</span><br/>';
updateScroll(); // Scroll to bottom of window
}
// Updates #messages div to auto-scroll
function updateScroll() {
var element = document.getElementById("messages");
element.scrollTop = element.scrollHeight;
}