// Called after form input is processed
function startConnect() {
// Generate a random client ID
clientId = "clientId-" + parseInt(Math.random() * 1000000);
document.getElementById("wrapper").classList.add("wrapper-error");
document.getElementById("wrapper").classList.remove("wrapper-ok");
// Print output for the user in the messages div
log('Connecting to: ' + window.brokerUri + ', client ID: ' + clientId);
// Initialize new Paho client connection
client = new Paho.MQTT.Client(window.brokerUri, clientId=clientId);
// Set callback handlers
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
// Connect the client, if successful, call onConnect function
client.connect({
onSuccess: onConnect,
});
connectionCheckTimeout = setTimeout(checkConnection, 2000);
}
function log(msg) {
console.log(msg);
//document.getElementById("messages").innerHTML += '' + msg + '
';
}
function checkConnection(){
clearTimeout(connectionCheckTimeout);
if (!client.isConnected()) {
//log("Error connecting to broker");
startConnect();
}
}
// Called when the client connects
function onConnect() {
clearTimeout(connectionCheckTimeout);
document.getElementById("wrapper").classList.remove("wrapper-error");
document.getElementById("wrapper").classList.add("wrapper-ok");
var subWidgets = document.getElementsByClassName("subscriber");
var topics = new Set()
for (var i = 0; i < subWidgets.length; i++) {
var c = subWidgets.item(i);
var topic = c.getAttribute('data-sub-topic');
topics.add(topic);
c.classList.add('widget-unset');
}
for (let topic of topics)
{
client.subscribe(topic);
}
// Subscribe to the requested topic
}
// Called when the client loses its connection
function onConnectionLost(responseObject) {
document.getElementById("wrapper").classList.remove("wrapper-ok");
document.getElementById("wrapper").classList.add("wrapper-error");
if (responseObject.errorCode !== 0) {
console.log("onConnectionLost: " + responseObject.errorMessage);
}
startConnect();
}
function getByPath(obj, path) {
var current=obj;
path.split('.').forEach(function(p){ current = current[p]; });
return current;
}
// Called when a message arrives
function onMessageArrived(message) {
var views = document.getElementsByClassName(message.destinationName + "-value");
for (var i = 0 ; i < views.length; i++) {
var view = views.item(i);
if (view.classList.contains("update-policy-replace-content")) {
view.innerHTML = message.payloadString;
} else if(view.classList.contains("update-policy-append-content")) {
view.innerHTML += message.payloadString + "
";
view.scrollTop = view.scrollHeight;
} else if(view.classList.contains("update-policy-add-row")) {
var now = (new Date()).toLocaleTimeString();
view.innerHTML = "