Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.28 KB | None | 0 0
  1. <html>
  2.  
  3. <head>
  4. <title>JavaScript MQTT WebSocket Example</title>
  5.  
  6. <!----------------------------------------------------------------------------INCLUDES -->
  7.  
  8. <meta charset="utf-8">
  9. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  10.  
  11. <!-- MAPS STUFF -->
  12. <link rel="stylesheet" href="https://unpkg.com/leaflet@1.3.4/dist/leaflet.css" integrity="sha512-puBpdR0798OZvTTbP4A8Ix/l+A4dHDD0DGqYW6RQ+9jxkRFclaxxQb/SJAWZfWAkuyeQUytO7+7N4QKrDh+drA==" crossorigin="">
  13. <script src="https://unpkg.com/leaflet@1.3.4/dist/leaflet.js" integrity="sha512-nMMmRyTVoLYqjP9hrbed9S+FzjZHW5gY1TWCHA5ckwXZBadntCNs8kEqAWdrb9O7rxbCaA4lKTIWjDXZxflOcA==" crossorigin="">
  14. </script>
  15.  
  16. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/leaflet-easybutton@2/src/easy-button.css">
  17. <script src="https://cdn.jsdelivr.net/npm/leaflet-easybutton@2/src/easy-button.js"></script>
  18.  
  19. <!-- MQTT STUFF -->
  20. <script src="https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.0.1/mqttws31.js" type="text/javascript">
  21. </script>
  22.  
  23. <!---------------------------------------------------------------------------->
  24.  
  25. </head>
  26. <body>
  27.  
  28. <!------------------------------------------------------------------------SET PARAMETERS---->
  29. <div id="mapid" style="width: 600px; height: 400px; position: relative;"></div>
  30. <script type="text/javascript" src="https://code.jquery.com/jquery-latest.min.js"></script>
  31.  
  32. <!---------------------------------------------------------------------------->
  33.  
  34.  
  35.  
  36. <script>
  37. //<!------------------------------------------ ASSIGN VARIABLES ---------------------------------->
  38. var mqtt;
  39. var mqttCarsSubTopic = "AEV/CARS";
  40. var carIDs = ["TESTCAR"];
  41. var reconnectTimeout = 5000;
  42. var hostIP = "203.45.199.194";
  43. var port = 9001;
  44. var topic = "";
  45. var payload = "";
  46. var showPath = 0;
  47.  
  48. var zoomLevel = 14;
  49. var latLngAEV = ["-37.81426","145.29286"];
  50. var latLng1 = [0,0];
  51. var lat, lon = 0;
  52. var map = L.map('mapid').setView(latLngAEV, zoomLevel); //SET MAP START POINTS
  53. var marker1 = L.marker([0,0]).addTo(map).on('click', startTrace); //SET START MARKER
  54.  
  55. var polylinePoints = [];
  56. var polyline = L.polyline(polylinePoints);
  57.  
  58. ////////////////////////////////////////////////////////////////////////////////////////////////////
  59.  
  60. function askForData(){
  61. for(var i = 0; i < carIDs.length; i++)
  62. {
  63. sendMessage = new Paho.MQTT.Message("1");
  64. sendMessage.destinationName = mqttCarsSubTopic + "/" + carIDs[i] + "/" + "TRACE";
  65. mqtt.send(sendMessage);
  66. console.log("Sent activation message");
  67. }
  68. setTimeout(askForData, reconnectTimeout);
  69. }
  70.  
  71. function startTrace(duration){
  72. for(var i = 0; i < carIDs.length; i++)
  73. {
  74. sendMessage = new Paho.MQTT.Message(duration);
  75. sendMessage.destinationName = mqttCarsSubTopic + "/" + carIDs[i] + "/" + "TRACE";
  76. mqtt.send(sendMessage);
  77. console.log("Sent Trace activation message");
  78. }
  79. }
  80.  
  81.  
  82. function clearMap() {
  83. for(i in map._layers) {
  84. if(map._layers[i]._path != undefined) {
  85. try {
  86. map.removeLayer(map._layers[i]);
  87. }
  88. catch(e) {
  89. console.log("problem with " + e + map._layers[i]);
  90. }
  91. }
  92. }
  93. }
  94.  
  95. function onClickB2(e){
  96. if (showPath)
  97. {
  98. showPath = 0;
  99. polylinePoints = [];
  100. clearMap();
  101. } else{
  102. showPath = 1;
  103. startTrace("300");
  104. }
  105. }
  106.  
  107. function onClickB1(e){
  108. if (showPath)
  109. {
  110. showPath = 0;
  111. polylinePoints = [];
  112. clearMap();
  113. } else{
  114. showPath = 1;
  115. startTrace("1");
  116. }
  117. }
  118.  
  119.  
  120.  
  121. //MQTT UNABLE TO CONNECT FUNCTION
  122. function onFailure(message)
  123. {
  124. console.log("Connection Attempt to Host " + hostIP + "Failed");
  125. setTimeout(MQTTconnect, reconnectTimeout);
  126. }
  127.  
  128. //MQTT SUBSCRIBED MESSAGE RECIEVED FUNCTION
  129. //SPECIFICALLY THIS HANDLES INCOMING GPS DATA FOR CARS
  130. function onMessageArrived(msg)
  131. {
  132. topic = msg.destinationName;
  133. payload = msg.payloadString;
  134. var topicSplit = topic.split("/");
  135. var payloadSplit = payload.split(",");
  136. out_msg = "Message received " + payload;
  137. out_msg = out_msg + "\nMessage Topic " + topic;
  138. console.log(out_msg);
  139.  
  140.  
  141. for (var i = 0; i < carIDs.length; i++)
  142. {
  143. if (topicSplit[2] == carIDs[i])
  144. {
  145. latLng1[0] = payloadSplit[0];
  146. latLng1[1] = payloadSplit[1];
  147. }
  148. /////////////PLACE MARKER AT UPDATE LAT OR LON
  149. marker1.setLatLng(payloadSplit);
  150. if (showPath){
  151. polylinePoints.push(payloadSplit);
  152. clearMap();
  153. polyline = L.polyline(polylinePoints).addTo(map);
  154. console.log(polylinePoints);
  155. }
  156. }
  157. }
  158.  
  159.  
  160. function onConnect()
  161. {
  162. // Once a connection has been made, make a subscription and send a message.
  163. console.log("Connected ");
  164. for (var i = 0; i < carIDs.length; i++)
  165. {
  166. mqtt.subscribe(mqttCarsSubTopic + "/" + carIDs[i] + "/LATLNG");
  167. }
  168. askForData();
  169. }
  170.  
  171.  
  172.  
  173. ///////////TRY TO CONNECT TO THE MQTT BROKER/////
  174. function MQTTconnect()
  175. {
  176. console.log("connecting to " + hostIP + ":" + port);
  177. mqtt = new Paho.MQTT.Client(hostIP, port, "clientjs");
  178. var options =
  179. {
  180. timeout: 3,
  181. onSuccess: onConnect,
  182. onFailure: onFailure,
  183. };
  184. mqtt.onMessageArrived = onMessageArrived
  185.  
  186. mqtt.connect(options); //connect
  187. }
  188.  
  189. //LEAFLET TILES
  190.  
  191. L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw', {
  192. maxZoom: 18,
  193. attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, ' +
  194. '<a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
  195. 'Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
  196. id: 'mapbox.streets'
  197. }).addTo(map);
  198.  
  199.  
  200.  
  201. L.easyButton('fa-globe', function(btn, map){
  202. onClickB1();
  203. }).addTo(map);
  204.  
  205. L.easyButton('fa-globe', function(btn, map){
  206. onClickB2();
  207. }).addTo(map);
  208.  
  209.  
  210.  
  211. //MQTT CONNECT///////////////////////////////////////////////////
  212. MQTTconnect();
  213. </script>
  214.  
  215. </body>
  216. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement