×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Javascript
Posted by: AR Robocup
Added: Sep 4, 2019 12:37 PM
Modified: Sep 4, 2019 12:53 PM
Views: 4057
Tags: no tags
  1. var ipBroker = "<?php echo $config->Robots->Robot[0]->ip ?>";  //mqtt websocket enabled broker
  2. var portBroker = <?php echo $config->Robots->Robot[0]->port ?>;
  3. var mqttClient = new Paho.MQTT.Client(ipBroker, portBroker, "clientIdFromArScene" + parseInt(Math.random() * 100, 10));
  4.  
  5. mqttClient.onConnectionLost = function (responseObject)
  6. {
  7.     console.log("connection lost: " + responseObject.errorMessage);
  8. };
  9.  
  10. mqttClient.onMessageArrived = function (message)
  11. {
  12.     switch(message.destinationName)
  13.     {
  14.         <?php
  15.         for($i = 0; $i < $NUM_TOPICS; $i++)
  16.         {
  17.             echo "case '" . $config->ViewTopics->Topic[$i]->path . "' :\r\n\t\t"
  18.             . "switch('" . $config->ViewTopics->Topic[$i]->type . "') \r\n\t\t"
  19.             . "{\r\n\t\t case '" . $NAME_TEXT . "' :\r\n\t\tvar entity = document.querySelector('#viewTopic" . $i . "');\r\n\t\t"
  20.             . "entity.setAttribute('text', 'color: red; align: center; value: ' + message.payloadString + ';width:3');\r\n\t\tbreak;\r\n\t\t"
  21.             . "case '" . $NAME_BALKEN . "' :\r\n\t\tvar barEntity = document.querySelector('#viewTopic" . $i . "');\r\n\t\t"
  22.             . "var textEntity = document.querySelector('#textViewTopic" . $i . "');\r\n\t\t"
  23.             . "if((message.payloadString * 1) >= " . $config->ViewTopics->Topic[$i]->min . "){\r\n\t\t"
  24.             . "barEntity.setAttribute('width', " . ($MAX_WIDTH_BAR / ($config->ViewTopics->Topic[$i]->max - $config->ViewTopics->Topic[$i]->min)) . " * (message.payloadString - " . $config->ViewTopics->Topic[$i]->min . "));\r\n\t\t}"
  25.             . "else{\r\n\t\tbarEntity.setAttribute('width', '0.1');\r\n\t\t}\r\n\t\t}\r\n\t\t\tbreak;\r\n\t";                    
  26.         }
  27.         ?>
  28.     }
  29. };
  30.  
  31. var options =
  32. {
  33.     timeout: 3,
  34.     onSuccess: function ()
  35.     {
  36.         console.log("Connectet to MQTT-Broker");
  37.  
  38.         // Subscribing the actual relevant Topics
  39.         mqttClient.subscribe('<?php echo $config->ViewTopics->Topic[0]->path ?>', {qos: 0});
  40.         mqttClient.subscribe('<?php echo $config->ViewTopics->Topic[1]->path ?>', {qos: 0});
  41.         mqttClient.subscribe('<?php echo $config->ViewTopics->Topic[2]->path ?>', {qos: 0});
  42.         mqttClient.subscribe('<?php echo $config->ViewTopics->Topic[3]->path ?>', {qos: 0});
  43.     },
  44.     onFailure: function (message)
  45.     {
  46.         console.log("Connection to MQTT-Broker failed: " + message.errorMessage);
  47.     }
  48. };
  49.  
  50. function mqttSend(topic, msg)
  51. {
  52.     console.log("Message get send: " + msg);
  53.     message = new Paho.MQTT.Message(msg);
  54.     message.destinationName = topic;
  55.     mqttClient.send(message);
  56. }
  57.  
  58. function init()
  59. {
  60.     console.log("Trying to connect to MQTT-Broker");
  61.     mqttClient.connect(options);
  62.     console.log(parseInt(10) *2);
  63. }