forked from calderonroberto/thingbroker-jquery-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.thingbroker-0.4.0.min.js
1 lines (1 loc) · 7.51 KB
/
jquery.thingbroker-0.4.0.min.js
1
function setUniqueId(e){var t=getCookie("device_id");if(t==undefined){var t=(new Date).getTime();setCookie("device_id",t,365)}return e+t}function getCookie(e){var t,n,r,i=document.cookie.split(";");for(t=0;t<i.length;t++){n=i[t].substr(0,i[t].indexOf("="));r=i[t].substr(i[t].indexOf("=")+1);n=n.replace(/^\s+|\s+$/g,"");if(n==e){return unescape(r)}}}function setCookie(e,t,n){var r=new Date;r.setDate(r.getDate()+n);var i=escape(t)+(n==null?"":"; expires="+r.toUTCString());document.cookie=e+"="+i}var thingBrokerUrl="http://kimberly.magic.ubc.ca:8080/thingbroker";(function(e){e.fn.thing=function(t){function r(t,n){if(t.debug)console.log("Getting event for "+t.thingIdUnique);e.ajax({type:"GET",crossDomain:true,url:t.url+"/things/"+t.thingIdUnique+"/events?waitTime=30&after="+t.timestamp,dataType:"JSON",success:function(e){a(e,t,n)},error:function(e){o(e)}})}function i(t,n){if(t.debug)console.log("Sending event "+t.event_key+" for "+t.thingId);e.ajax({type:"POST",url:t.url+"/things/"+t.thingId+"/events?keep-stored=true",data:'{"'+t.event_key+'": "'+encodeURIComponent(t.event_value)+'"}',contentType:"application/json",dataType:"JSON",error:function(e){o(e)}})}function s(t){if(t.debug)console.log("Registering parent "+t.thingId);e.ajax({type:"POST",crossDomain:true,url:t.url+"/things",data:'{"thingId": "'+t.thingId+'","name": "'+t.thingName+'","type": "'+t.thingType+'"}',contentType:"application/json",dataType:"JSON",error:function(e){o(e)}});if(t.debug)console.log("Registering "+t.thingId+" as "+t.thingIdUnique);e.ajax({type:"POST",crossDomain:true,url:t.url+"/things",data:'{"thingId": "'+t.thingIdUnique+'","name": "'+t.thingName+'","type": "'+t.thingType+'"}',contentType:"application/json",dataType:"JSON",error:function(e){o(e)}})}function o(e){console.log("Thingbroker Connection Error.");console.log(e)}function u(n){if(t.debug)console.log("Setting "+t.thingIdUnique+" to follow "+n);e.ajax({type:"POST",crossDomain:true,url:t.url+"/things/"+t.thingIdUnique+"/follow",data:'["'+n+'"]',contentType:"application/json",dataType:"JSON",error:function(e){o(e)}})}function a(t,n,i){e.each(t,function(r,s){if(s.info==null){return false}if(n.eventCallback!=null){var o=n.eventCallback;o(t)}e.each(s.info,function(t,r){var o=e(decodeURIComponent(r));if(t=="append"){i.append(o)}if(t=="prepend"){i.prepend(o)}if(t=="remove"){if(jQuery(i).is("div")){var u=o.find("p").remove().html();e("p:contains('"+u+"')").remove()}if(jQuery(i).is("ul")){var u=o.find("li").remove().html();e("li:contains('"+u+"')").remove()}}if(t=="src"){i.attr("src",r)}n.timestamp=s.serverTimestamp})});r(n,i)}function f(e){if(e.container){var t="";var n="";if(location.href.indexOf("?")!==-1){var r=location.href.split("?")[1].split("&");data={};for(x in r){data[r[x].split("=")[0]]=r[x].split("=")[1]}t=data.display_id;n=decodeURIComponent(data.thingbroker_url)}else{t=getCookie("display_id")}if(t!==null&&t!==""&&t!==undefined){e.thingId=e.thingId+t;if(e.debug)console.log("Setting container safe thingId name "+e.thingId)}if(n!==null&&n!==""&&n!==undefined){e.url=n;if(e.debug)console.log("Setting container safe thingbroker_url "+e.url)}}return e}var n=(new Date).getTime();t=e.extend({url:thingBrokerUrl,thingId:this.attr("id"),thingName:this.attr("id"),thingType:"Web Object",thingIdUnique:setUniqueId(this.attr("id")),listen:false,eventCallback:null,event_key:"",event_value:"",container:true,timestamp:(new Date).getTime(),debug:false},t);this.each(function(){var n=e(this);t=f(t);t.thingType=this.tagName;if(!t.listen&&(t.remove||t.append||t.src||t.prepend)){if(t.to!=null){t.thingId=t.to.replace("#","");t=f(t)}if(t.remove){t.event_key="remove";t.event_value=t.remove}if(t.append){t.event_key="append";t.event_value=t.append}if(t.prepend){t.event_key="prepend";t.event_value=t.prepend}if(t.src){t.event_key="src";t.event_value=t.src}i(t,n)}else if(t.follow){u(t.follow.replace("#",""))}else if(t.listen){setTimeout(function(){s(t);u(t.thingId);setTimeout(function(){r(t,n)},1e3)},100)}else{s(t)}return this});}})(jQuery);jQuery.ThingBroker=function(e){function c(t){var n="";var r="";if(e.container){if(location.href.indexOf("?")!==-1){var i=location.href.split("?")[1].split("&");data={};for(x in i){data[i[x].split("=")[0]]=i[x].split("=")[1]}n=data.display_id;r=decodeURIComponent(data.thingbroker_url)}else{n=getCookie("display_id")}if(n!==null&&n!==""&&n!==undefined){t=t+n;if(e.debug)console.log("Setting container safe thingId name "+e.thingId)}if(r!==null&&r!==""&&r!==undefined){e.url=r;if(e.debug)console.log("Setting container safe thingbroker_url "+e.url)}}return t}e=$.extend({url:thingBrokerUrl,thingId:"",event:{},debug:false,container:true},e);var t=function(t,n){if(e.debug)console.log("Getting last "+n+" events from "+t);t=t.replace("#","");t=c(t);var r="";if(n==null)r=e.url+"/things/"+t+"/events";else saveUrl=e.url+"/things/"+t+"/events?limit="+n;var s=[];$.ajax({async:false,type:"GET",crossDomain:true,url:r,dataType:"JSON",success:function(e){for(i in e){s[i]=e[i]}},error:function(){console.log("Thingbroker Connection Error.")}});return s};var n=function(t){if(e.debug)console.log("Registering "+t);if(t.thingId){t.thingId=t.thingId.replace("#","");t.thingId=c(t.thingId)}var n={};$.ajax({type:"POST",crossDomain:true,url:e.url+"/things",data:JSON.stringify(t),contentType:"application/json",dataType:"JSON",error:function(e){connectionError(e)}});return n};var r=function(t){if(e.debug)console.log("Deleting "+t);t=t.replace("#","");var n={};$.ajax({type:"DELETE",crossDomain:true,url:e.url+"/things/"+t,contentType:"application/json",dataType:"JSON",error:function(e){connectionError(e)}});return n};var s=function(t){if(e.debug)console.log("Registering thing "+t);t=t.replace("#","");t=c(t);var n={};$.ajax({type:"POST",crossDomain:true,url:e.url+"/things",data:'{"thingId": "'+t+'","name": "'+t+'"}',contentType:"application/json",dataType:"JSON",error:function(e){connectionError(e)}});return n};var o=function(t){if(e.debug)console.log("Getting thing "+t);t=t.replace("#","");t=c(t);var n={};$.ajax({async:false,type:"GET",crossDomain:true,url:e.url+"/things/"+t,dataType:"JSON",success:function(e){n=e},error:function(){console.log("Thingbroker Connection Error.")}});return n};var u=function(t,n){var r={};t=t.replace("#","");t=c(t);if(e.debug){console.log("Sending an event for "+t);console.log(n)}$.ajax({async:false,type:"POST",url:e.url+"/things/"+t+"/events?keep-stored=true",data:JSON.stringify(n),contentType:"application/json",dataType:"JSON",success:function(e){r=e},error:function(){console.log("Thingbroker Connection Error.")}});return r};var a=function(t){if(e.debug)console.log("Getting metadata for thing "+thingID);t=t.replace("#","");t=c(t);var n={};$.ajax({async:false,type:"GET",crossDomain:true,url:e.url+"/things/"+t+"/metadata",dataType:"JSON",success:function(e){n=e},error:function(){console.log("Thingbroker Connection Error.")}});return n};var f=function(t,n){var r={};t=t.replace("#","");t=c(t);if(e.debug)console.log("Updating Metadata "+n);$.ajax({async:false,type:"POST",url:e.url+"/things/"+t+"/metadata",data:JSON.stringify(n),contentType:"application/json",dataType:"JSON",success:function(e){r=e},error:function(){console.log("Thingbroker Connection Error.")}});return r};var l=function(t,n,r){if(e.debug)console.log("Updating event "+t);$.ajax({async:false,type:"PUT",url:e.url+"/events/"+t+"?serverTimestamp="+n,data:JSON.stringify(r),contentType:"application/json",dataType:"JSON",success:function(e){response=e},error:function(){console.log("Thingbroker Connection Error.")}});return response};return{postThing:n,postEvent:u,putEvent:l,getEvents:t,getThing:o,postMetadata:f,getMetadata:a,postThingById:s,deleteThing:r}}