Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.41 KB | None | 0 0
  1.  
  2. var tiltLR, tiltFB, tiltDR, run, client;
  3. var Device = APPID = APPKEY = APPSECRET = slotx = sloty = slotz = '';
  4. var sendData = false;
  5. var freq = 500;
  6. $(document).ready(function() {
  7. if (!window.DeviceOrientationEvent) {
  8. alert('ขอโทษครับ :( อุปกรณ์ของคุณ ไม่รองรับการใช้งานแอพฯนี้');
  9. return false;
  10. }
  11. $("#setNew").click(function(e) {
  12. e.preventDefault();
  13. Device = $("#dname").val();
  14. var d = new Date();
  15. d.setTime(d.getTime() + (30*24*60*60*1000));
  16. document.cookie = "IoTChulaMyMotion="+Device+"; expires="+ d.toUTCString();
  17.  
  18. document.cookie = "username=John Doe; expires=Thu, 18 Dec 2013 12:00:00 UTC; path=/";
  19. APPID = $("#aid").val();
  20. APPKEY = $("#key").val();
  21. APPSECRET = $("#secret").val();
  22. slotx = $("#xslot").val();
  23. sloty = $("#yslot").val();
  24. slotz = $("#zslot").val();
  25. freq = parseInt($("#interval").val());
  26. if (freq < 100) {
  27. freq = 500;
  28. $("#interval").val(freq);
  29. }
  30.  
  31. // Create a client instance
  32. sendData = false;
  33. if(client != null) client.disconnect();
  34.  
  35. client = new Paho.MQTT.Client('old.oneclickiot.com', 8083, "/", Device);
  36.  
  37. // set callback handlers
  38. client.onConnectionLost = onConnectionLost;
  39. client.onMessageDelivered = onMessageDelivered;
  40.  
  41. // connect the client
  42. client.connect({onSuccess:onConnect,userName:APPKEY,useSSL:true,password:APPSECRET});
  43.  
  44. });
  45.  
  46.  
  47. $("#imgLogo").click(function() {
  48. sendData = !sendData;
  49. $("#sendStat, #lastSent").html('');
  50. });
  51. window.addEventListener('deviceorientation', function(eventData) {
  52. tiltLR = Math.round(eventData.gamma)%360;
  53. tiltFB = Math.round(eventData.beta)%360;
  54. tiltDR = Math.round(eventData.alpha)%360;
  55. $("#tLR").text(tiltLR);
  56. $("#tFB").text(tiltFB);
  57. $("#tDR").text(tiltDR);
  58. var logo = document.getElementById("imgLogo");
  59. var deg = "rotateX(" + tiltFB + "deg) rotateY(" + tiltLR + "deg) rotateZ(" + tiltDR + "deg)";
  60. logo.style.webkitTransform = deg;
  61. logo.style.MozTransform = deg;
  62. logo.style.transform = deg;
  63. }, false);
  64. });
  65.  
  66.  
  67. function onConnect() {
  68. // Once a connection has been made, make a subscription and send a message.
  69. $("#status").removeClass("off").addClass("on").html("ออนไลน์");
  70.  
  71. if (navigator.geolocation) {
  72. navigator.geolocation.getCurrentPosition(function(position) {
  73. message = new Paho.MQTT.Message(position.coords.latitude+","+position.coords.longitude);
  74. message.destinationName = "latlon";
  75. client.send(message);
  76. });
  77. }
  78.  
  79. clearInterval(run);
  80. run = setInterval(function() {
  81. $("#sendStat").html('ไม่มีการส่งข้อมูล');
  82. if (!sendData) return;
  83.  
  84. var x = senp(slotx,tiltLR,client);
  85. var y = senp(sloty,tiltFB,client);
  86. var z = senp(slotz,tiltDR,client);
  87.  
  88. if(x||y||z){
  89. var aaa = new Date().toISOString().replace("T", "<br />");
  90. $("#sendStat").html("ส่งข้อมูลล่าสุดเมื่อ ");
  91. $("#lastSent").html(aaa);
  92. }
  93. }, freq);
  94.  
  95. }
  96.  
  97. function senp(slot,value,client){
  98. if(slot == '' || slot == 'null' || value == null || value == "" || client == null) {
  99. return false;
  100. } else {
  101. message = new Paho.MQTT.Message(value+"");
  102. message.destinationName = APPID+"/"+slot;
  103. client.send(message);
  104. return true;
  105. }
  106. }
  107.  
  108. // called when the client loses its connection
  109. function onConnectionLost(responseObject) {
  110. $("#status").removeClass("on").addClass("off").html("ออฟไลน์");
  111.  
  112. if (responseObject.errorCode !== 0) {
  113. var r = confirm("ขาดดการเชื่อมต่อกับเซิฟเวอร์ ("+responseObject.errorCode+") ต้องการเชื่อมต่อใหม่หรือไม่?");
  114. if (r == true) {
  115. window.location.reload();
  116. } else {
  117. console.log("onConnectionLost:"+responseObject.errorMessage);
  118. }
  119. }
  120. }
  121.  
  122. // called when a message arrives
  123. function onMessageDelivered(message) {
  124.  
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement