UNPKG

evrythng-mqtt

Version:

Evrythng.js MQTT plugin for MQTT real-time communication with EVRYTHNG API

2 lines 2.98 kB
!function(a,b){"use strict";module.exports=b(require("mqtt"))}(this,function(a){"use strict";function b(a){return a+"_"+Math.random().toString(16).substr(2,8)}function c(a){return a.mqttClient&&a.mqttClient.connected===!0}function d(d,e){return e=e||{},k[d.apiKey]?k[d.apiKey]:(k[d.apiKey]=new Promise(function(f,g){function i(a){delete k[d.apiKey],d.mqttClient?delete d.mqttClient:(p.end(),g(a))}function j(){l[d.apiKey]&&Object.keys(l[d.apiKey]).forEach(function(a){p.subscribe(a)}),d.mqttClient=p,f(d.mqttClient)}if(c(d))f(d.mqttClient);else{var n=m.settings,o={username:"authorization",password:e.authorization||d.apiKey,clientId:b(n.clientIdPrefix),keepalive:n.keepAlive,reconnectPeriod:n.reconnectPeriod},p=a.connect(e.apiUrl||n.apiUrl,o);p.on("connect",j),p.on("close",i),p.on("error",i),p.on("message",function(a,b){h(d.apiKey,a,b)})}}),k[d.apiKey])}function e(a,b,c,e){var f=this;return d(this.scope,b).then(function(b){return new Promise(function(d,g){function h(a){a?(e&&e(a),g(a)):(c&&c(),d())}a=JSON.stringify(f.jsonify(a)),b.publish(f.path,a,h)})}).catch(function(a){return e&&e(a),Promise.reject(a)})}function f(a,b,c){l[a.apiKey]||(l[a.apiKey]={}),l[a.apiKey][b]||(l[a.apiKey][b]=[]),l[a.apiKey][b].push(c)}function g(a,b){l[a.apiKey]&&delete l[a.apiKey][b]}function h(a,b,c){l[a]&&l[a][b]&&l[a][b].forEach(function(a){a(c)})}var i="1.1.0",j={apiUrl:"mqtts://mqtt.evrythng.com:8883/mqtt",reconnectPeriod:1e3,keepAlive:50,clientIdPrefix:"evtjs"},k={},l={},m={version:i,settings:j,setup:function(a){if("[object Object]"!==Object.prototype.toString.call(a))throw new TypeError("Setup should be called with an options object.");for(var b in a)a.hasOwnProperty(b)&&(this.settings[b]=a[b]);return this.settings},install:function(a,b,h){function i(a,b,c,e){if("[object Function]"!=Object.prototype.toString.call(a))throw new TypeError("Message callback missing.");h.isFunction(b)&&(e=c,c=b,b={});var g=this;return d(g.scope,b).then(function(b){return new Promise(function(d,h){function i(i){i?(e&&e(i),h(i)):(f(g.scope,g.path,function(b){var c=b.toString();try{c=g.parse(JSON.parse(c))}catch(a){}a(c)}),c&&c(b),d(b))}b.subscribe(g.path,i)})}).catch(function(a){return e&&e(a),Promise.reject(a)})}function j(a,b){var d=this;return new Promise(function(e,f){function h(c){c?(b&&b(c),f(c)):(g(d.scope,d.path),a&&a(d.scope.mqttClient),e(d.scope.mqttClient))}if(!c(d.scope)){var i=new Error("MQTT Client is not connected.");return b&&b(i),f(i)}d.scope.mqttClient.unsubscribe(d.path,h)})}function k(a,c,d,f){var g=this;"undefined"==typeof a&&(a={}),h.isFunction(c)&&(f=d,d=c,c={});var i=this.class===b.class?"create":"update";return new Promise(function(b,h){var j={request:function(a,i){i(),e.call(g,a.data,c,d,f).then(b,h)}};g[i](a,{interceptors:[j]}).catch(function(a){if(!a.cancelled)throw a})})}a.prototype.subscribe=i,a.prototype.unsubscribe=j,a.prototype.publish=k}};return m.$inject=["resource","entity/action","utils"],m}); //# sourceMappingURL=evrythng-mqtt.min.js.map