132 lines
		
	
	
		
			5.2 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			132 lines
		
	
	
		
			5.2 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
<!DOCTYPE html>
 | 
						|
<html>
 | 
						|
  <head>
 | 
						|
    <style type="text/css">
 | 
						|
      .widget { width: 100%; padding: 5px; margin: 10px; background-color: white; }
 | 
						|
      .widget-unset { color: gray; }
 | 
						|
      #wrapper { width: 100%; height: 100%;}
 | 
						|
      html, body { height: 100%;}
 | 
						|
      .wrapper-ok { background-color: #e9ecef;}
 | 
						|
      .wrapper-error { background-color: #ff8888;}
 | 
						|
    </style>
 | 
						|
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
 | 
						|
    <script src="https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.0.2/mqttws31.min.js" type="text/javascript"></script>
 | 
						|
    <script>
 | 
						|
      // Called after form input is processed
 | 
						|
      function startConnect() {
 | 
						|
          // Generate a random client ID
 | 
						|
          clientID = "clientID-" + parseInt(Math.random() * 100);
 | 
						|
          document.getElementById("wrapper").classList.add("wrapper-error");
 | 
						|
          document.getElementById("wrapper").classList.remove("wrapper-ok");
 | 
						|
 | 
						|
          // Fetch the hostname/IP address and port number from the form
 | 
						|
          host = "bojack"
 | 
						|
          port = 8080;
 | 
						|
 | 
						|
          // 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);
 | 
						|
 | 
						|
          // Set callback handlers
 | 
						|
          client.onConnectionLost = onConnectionLost;
 | 
						|
          client.onMessageArrived = onMessageArrived;
 | 
						|
 | 
						|
          // Connect the client, if successful, call onConnect function
 | 
						|
          client.connect({
 | 
						|
              onSuccess: onConnect,
 | 
						|
          });
 | 
						|
      }
 | 
						|
 | 
						|
      // Called when the client connects
 | 
						|
      function onConnect() {
 | 
						|
          document.getElementById("wrapper").classList.remove("wrapper-error");
 | 
						|
          document.getElementById("wrapper").classList.add("wrapper-ok");
 | 
						|
 | 
						|
          var subWidgets = document.getElementsByClassName("subscriber");
 | 
						|
          console.log("Found " + subWidgets.length + " widgets");
 | 
						|
          for (var i = 0; i < subWidgets.length; i++) {
 | 
						|
              var c = subWidgets.item(i);
 | 
						|
              var topic = c.getAttribute('data-topic');
 | 
						|
              console.log("Subscribing to: " + topic);
 | 
						|
              client.subscribe(topic);
 | 
						|
              c.classList.add('widget-unset');
 | 
						|
          }
 | 
						|
          // Subscribe to the requested topic
 | 
						|
      }
 | 
						|
 | 
						|
      // Called when the client loses its connection
 | 
						|
      function onConnectionLost(responseObject) {
 | 
						|
          console.log("onConnectionLost: Connection Lost");
 | 
						|
          document.getElementById("wrapper").classList.remove("wrapper-ok");
 | 
						|
          document.getElementById("wrapper").classList.add("wrapper-error");
 | 
						|
          if (responseObject.errorCode !== 0) {
 | 
						|
              console.log("onConnectionLost: " + responseObject.errorMessage);
 | 
						|
          }
 | 
						|
      }
 | 
						|
 | 
						|
      // Called when a message arrives
 | 
						|
      function onMessageArrived(message) {
 | 
						|
          console.log(message.destinationName + "-value");
 | 
						|
          var views = document.getElementsByClassName(message.destinationName + "-value");
 | 
						|
          for (var i = 0 ; i < views.length; i++) {
 | 
						|
              view = views.item(i);
 | 
						|
              console.log(view);
 | 
						|
              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 + "<br/>";
 | 
						|
                  view.scrollTop = view.scrollHeight;
 | 
						|
              }
 | 
						|
          }
 | 
						|
 | 
						|
          var widgets = document.querySelectorAll("[data-topic='" + message.destinationName + "']");
 | 
						|
          for (var i = 0 ; i < widgets.length; i++) {
 | 
						|
              widgets.item(i).classList.remove("widget-unset");
 | 
						|
          }
 | 
						|
 | 
						|
      }
 | 
						|
 | 
						|
      // 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;
 | 
						|
      }
 | 
						|
 | 
						|
      function publish(topic, value) {
 | 
						|
          var message = new Paho.MQTT.Message(value);
 | 
						|
          message.destinationName = topic;
 | 
						|
          client.send(message);
 | 
						|
      }
 | 
						|
      window.onload = startConnect;
 | 
						|
      </script>
 | 
						|
    <title>0xee home</title>
 | 
						|
  </head>
 | 
						|
  <body>
 | 
						|
      <div id="wrapper">
 | 
						|
         <br/>
 | 
						|
         <div id="value">
 | 
						|
         </div>
 | 
						|
 | 
						|
         <div id="widgets">
 | 
						|
           {% for w in widgets %}
 | 
						|
             {{w()|safe}}
 | 
						|
           {% endfor %}
 | 
						|
         </div>
 | 
						|
         <br/>
 | 
						|
         <div id="messages"></div>
 | 
						|
         <input type="button" onclick="startConnect()" value="Connect">
 | 
						|
         <input type="button" onclick="startDisconnect()" value="Disconnect">
 | 
						|
      </div>
 | 
						|
  </body>
 | 
						|
</html>
 |