Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var tiltLR, tiltFB, tiltDR, run, client;
- var Device = APPID = APPKEY = APPSECRET = slotx = sloty = slotz = '';
- var sendData = false;
- var freq = 500;
- $(document).ready(function() {
- if (!window.DeviceOrientationEvent) {
- alert('ขอโทษครับ :( อุปกรณ์ของคุณ ไม่รองรับการใช้งานแอพฯนี้');
- return false;
- }
- $("#setNew").click(function(e) {
- e.preventDefault();
- Device = $("#dname").val();
- var d = new Date();
- d.setTime(d.getTime() + (30*24*60*60*1000));
- document.cookie = "IoTChulaMyMotion="+Device+"; expires="+ d.toUTCString();
- document.cookie = "username=John Doe; expires=Thu, 18 Dec 2013 12:00:00 UTC; path=/";
- APPID = $("#aid").val();
- APPKEY = $("#key").val();
- APPSECRET = $("#secret").val();
- slotx = $("#xslot").val();
- sloty = $("#yslot").val();
- slotz = $("#zslot").val();
- freq = parseInt($("#interval").val());
- if (freq < 100) {
- freq = 500;
- $("#interval").val(freq);
- }
- // Create a client instance
- sendData = false;
- if(client != null) client.disconnect();
- client = new Paho.MQTT.Client('old.oneclickiot.com', 8083, "/", Device);
- // set callback handlers
- client.onConnectionLost = onConnectionLost;
- client.onMessageDelivered = onMessageDelivered;
- // connect the client
- client.connect({onSuccess:onConnect,userName:APPKEY,useSSL:true,password:APPSECRET});
- });
- $("#imgLogo").click(function() {
- sendData = !sendData;
- $("#sendStat, #lastSent").html('');
- });
- window.addEventListener('deviceorientation', function(eventData) {
- tiltLR = Math.round(eventData.gamma)%360;
- tiltFB = Math.round(eventData.beta)%360;
- tiltDR = Math.round(eventData.alpha)%360;
- $("#tLR").text(tiltLR);
- $("#tFB").text(tiltFB);
- $("#tDR").text(tiltDR);
- var logo = document.getElementById("imgLogo");
- var deg = "rotateX(" + tiltFB + "deg) rotateY(" + tiltLR + "deg) rotateZ(" + tiltDR + "deg)";
- logo.style.webkitTransform = deg;
- logo.style.MozTransform = deg;
- logo.style.transform = deg;
- }, false);
- });
- function onConnect() {
- // Once a connection has been made, make a subscription and send a message.
- $("#status").removeClass("off").addClass("on").html("ออนไลน์");
- if (navigator.geolocation) {
- navigator.geolocation.getCurrentPosition(function(position) {
- message = new Paho.MQTT.Message(position.coords.latitude+","+position.coords.longitude);
- message.destinationName = "latlon";
- client.send(message);
- });
- }
- clearInterval(run);
- run = setInterval(function() {
- $("#sendStat").html('ไม่มีการส่งข้อมูล');
- if (!sendData) return;
- var x = senp(slotx,tiltLR,client);
- var y = senp(sloty,tiltFB,client);
- var z = senp(slotz,tiltDR,client);
- if(x||y||z){
- var aaa = new Date().toISOString().replace("T", "<br />");
- $("#sendStat").html("ส่งข้อมูลล่าสุดเมื่อ ");
- $("#lastSent").html(aaa);
- }
- }, freq);
- }
- function senp(slot,value,client){
- if(slot == '' || slot == 'null' || value == null || value == "" || client == null) {
- return false;
- } else {
- message = new Paho.MQTT.Message(value+"");
- message.destinationName = APPID+"/"+slot;
- client.send(message);
- return true;
- }
- }
- // called when the client loses its connection
- function onConnectionLost(responseObject) {
- $("#status").removeClass("on").addClass("off").html("ออฟไลน์");
- if (responseObject.errorCode !== 0) {
- var r = confirm("ขาดดการเชื่อมต่อกับเซิฟเวอร์ ("+responseObject.errorCode+") ต้องการเชื่อมต่อใหม่หรือไม่?");
- if (r == true) {
- window.location.reload();
- } else {
- console.log("onConnectionLost:"+responseObject.errorMessage);
- }
- }
- }
- // called when a message arrives
- function onMessageDelivered(message) {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement