@iotize/device-com-wifi.cordova
Version:
IoTize Cordova plugin for Wifi devices
2 lines • 13.2 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("@iotize/common/promise"),require("rxjs"),require("@iotize/common/debug"),require("@iotize/tap/protocol/core"),require("rxjs/operators")):"function"==typeof define&&define.amd?define("@iotize/device-com-wifi.cordova",["exports","@iotize/common/promise","rxjs","@iotize/common/debug","@iotize/tap/protocol/core","rxjs/operators"],e):e(((t="undefined"!=typeof globalThis?globalThis:t||self).iotize=t.iotize||{},t.iotize["device-com-wifi"]=t.iotize["device-com-wifi"]||{},t.iotize["device-com-wifi"].cordova={}),t.promise,t.rxjs,t.debug$1,t.core,t.rxjs.operators)}(this,(function(t,e,n,o,i,r){"use strict";var c=function(t,e){return c=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])},c(t,e)};function s(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");function n(){this.constructor=t}c(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}function u(t,e){var n,o,i,r,c={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return r={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(r[Symbol.iterator]=function(){return this}),r;function s(s){return function(u){return function(s){if(n)throw new TypeError("Generator is already executing.");for(;r&&(r=0,s[0]&&(c=0)),c;)try{if(n=1,o&&(i=2&s[0]?o.return:s[0]?o.throw||((i=o.return)&&i.call(o),0):o.next)&&!(i=i.call(o,s[1])).done)return i;switch(o=0,i&&(s=[2&s[0],i.value]),s[0]){case 0:case 1:i=s;break;case 4:return c.label++,{value:s[1],done:!1};case 5:c.label++,o=s[1],s=[0];continue;case 7:s=c.ops.pop(),c.trys.pop();continue;default:if(!(i=c.trys,(i=i.length>0&&i[i.length-1])||6!==s[0]&&2!==s[0])){c=0;continue}if(3===s[0]&&(!i||s[1]>i[0]&&s[1]<i[3])){c.label=s[1];break}if(6===s[0]&&c.label<i[1]){c.label=i[1],i=s;break}if(i&&c.label<i[2]){c.label=i[2],c.ops.push(s);break}i[2]&&c.ops.pop(),c.trys.pop();continue}s=e.call(t,c)}catch(t){s=[6,t],o=0}finally{n=i=0}if(5&s[0])throw s[1];return{value:s[0]?s[1]:void 0,done:!0}}([s,u])}}}Object.create;function a(t,e){var n="function"==typeof Symbol&&t[Symbol.iterator];if(!n)return t;var o,i,r=n.call(t),c=[];try{for(;(void 0===e||e-- >0)&&!(o=r.next()).done;)c.push(o.value)}catch(t){i={error:t}}finally{try{o&&!o.done&&(n=r.return)&&n.call(r)}finally{if(i)throw i.error}}return c}Object.create;var f,l,h=o.createDebugger("@iotize/device-com-wifi.cordova"),p=this&&this.__awaiter||function(t,e,n,o){return new(n||(n=Promise))((function(i,r){function c(t){try{u(o.next(t))}catch(t){r(t)}}function s(t){try{u(o.throw(t))}catch(t){r(t)}}function u(t){var e;t.done?i(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(c,s)}u((o=o.apply(t,e||[])).next())}))},d="WifiScannerCordova",v=function(){function t(){this._isScanning$=new n.BehaviorSubject(!1),this._devices$=new n.Subject}return Object.defineProperty(t.prototype,"scanning",{get:function(){return this._isScanning$.asObservable()},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"isScanning",{get:function(){return this._isScanning$.value},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"results",{get:function(){return this._devices$.asObservable()},enumerable:!1,configurable:!0}),t.prototype.isAvailable=function(){return WifiWizard2.isWifiEnabled()},t.prototype.start=function(t){return void 0===t&&(t={}),p(this,void 0,void 0,(function(){var t=this;return u(this,(function(e){return this.isScanning?(h(d,"Already scanning"),[2]):(h(d,"Start Scanning ..."),this._isScanning$.next(!0),this._scanPromise=WifiWizard2.scan().then((function(e){t._devices$.next(e),h(d,"Scanner stopped",e),t._isScanning$.next(!1)})).catch((function(e){t._isScanning$.next(!1),h(d,"Scan result error",e)})),[2])}))}))},t.prototype.stop=function(t){return p(this,void 0,void 0,(function(){return u(this,(function(n){return h(d,"Stop Scanning ..."),[2,e.promiseTimeout((null==t?void 0:t.timeout)||1e4,this._cancelScan())]}))}))},t.prototype._cancelScan=function(){return p(this,void 0,void 0,(function(){var t;return u(this,(function(e){switch(e.label){case 0:this._isScanning$.next(!1),e.label=1;case 1:return e.trys.push([1,3,,4]),[4,WifiWizard2.getScanResults()];case 2:return t=e.sent(),this._devices$&&this._devices$.next(t),[3,4];case 3:return e.sent(),[3,4];case 4:return[2]}}))}))},t}();t.WifiComProtocolError=function(t){function e(e,n,o){var i=t.call(this,n)||this;return i.code=e,i.cause=o,i}return s(e,t),e.wifiNotEnabled=function(t){return new e(e.Code.WifiNotEnabled,'WiFi is not enabled. Cannot connect to network "'+t.SSID+'"')},e.connectFailedTimeout=function(t){return new e(e.Code.ConnectFailedTimeout,'Connection to "'+t.SSID+'" timeout')},e.cannotConnectToNetwork=function(t){return new e(e.Code.CannotConnectToNetwork,'Cannot connect Wi-Fi network "'+t.SSID+'"')},e.unknownError=function(t,n){return new e(e.Code.UnknownError,t.message||"Unknown Wi-Fi protocol error",t)},e}(Error),l=t.WifiComProtocolError||(t.WifiComProtocolError={}),(f=l.Code||(l.Code={})).CannotConnectToNetwork="WifiComProtocolErrorCannotConnectToNetwork",f.ConnectFailedTimeout="WifiComProtocolErrorConnectFailedTimeout",f.UnknownError="WifiComProtocolErrorUnknown",f.WifiNotEnabled="WifiComProtocolErrorWifiNotEnabled";var w=this&&this.__awaiter||function(t,e,n,o){return new(n||(n=Promise))((function(i,r){function c(t){try{u(o.next(t))}catch(t){r(t)}}function s(t){try{u(o.throw(t))}catch(t){r(t)}}function u(t){var e;t.done?i(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(c,s)}u((o=o.apply(t,e||[])).next())}))},b=function(e){function o(t,n){var o=e.call(this)||this;return o.wifiOptions=t,o.createSubProtocol=n,o}return s(o,e),Object.defineProperty(o.prototype,"socketProtocol",{get:function(){if(!this._socketProtocol)throw new Error("Protocol is not connected yet");return this._socketProtocol},enumerable:!1,configurable:!0}),o.prototype._connect=function(e){var o=this;return new n.Observable((function(e){w(o,void 0,void 0,(function(){var n,o,i,r=this;return u(this,(function(c){switch(c.label){case 0:return c.trys.push([0,7,,8]),this.wifiOptions.network?[4,WifiWizard2.getConnectedSSID()]:[3,4];case 1:return(n=c.sent())===this.wifiOptions.network.SSID?[3,4]:(h("currently connected to",n,"expected ",this.wifiOptions.network.SSID,"=> try to connect to ",this.wifiOptions.network),[4,WifiWizard2.connect(this.wifiOptions.network.SSID,!0,this.wifiOptions.network.password,this.wifiOptions.network.algorithm,this.wifiOptions.network.hidden)]);case 2:return c.sent(),[4,WifiWizard2.getConnectedSSID()];case 3:if((n=c.sent())!==this.wifiOptions.network.SSID)return e.error(t.WifiComProtocolError.cannotConnectToNetwork(this.wifiOptions.network)),[2];h("now connected to SSID",n),c.label=4;case 4:return h("Now creating socket protocol with options",this.wifiOptions),o=this,[4,this.createSubProtocol(this.wifiOptions)];case 5:return o._socketProtocol=c.sent(),this.subProtocolConnectionStateSub&&this.subProtocolConnectionStateSub.unsubscribe(),this.subProtocolConnectionStateSub=this._socketProtocol.onConnectionStateChange().subscribe((function(t){r.setConnectionState(t.newState)})),[4,this._socketProtocol.connect().toPromise()];case 6:return c.sent(),e.complete(),[3,8];case 7:if(i=c.sent(),this.wifiOptions.network)switch(i){case"CONNECT_FAILED_TIMEOUT":i=t.WifiComProtocolError.connectFailedTimeout(this.wifiOptions.network);break;case"WIFI_NOT_ENABLED":i=t.WifiComProtocolError.wifiNotEnabled(this.wifiOptions.network)}return"string"==typeof i&&(i=t.WifiComProtocolError.unknownError(new Error(i),this.wifiOptions.network)),this.subProtocolConnectionStateSub&&this.subProtocolConnectionStateSub.unsubscribe(),h("_connect error",i),e.error(i),[3,8];case 8:return[2]}}))}))})).pipe(r.share())},o.prototype._disconnect=function(t){return this.subProtocolConnectionStateSub&&this.subProtocolConnectionStateSub.unsubscribe(),this._socketProtocol?this._socketProtocol.disconnect():n.of(!0)},o.prototype.write=function(t){return this.socketProtocol.write(t)},o.prototype.read=function(){return this.socketProtocol.read()},o}(i.QueueComProtocol),y=this&&this.__awaiter||function(t,e,n,o){return new(n||(n=Promise))((function(i,r){function c(t){try{u(o.next(t))}catch(t){r(t)}}function s(t){try{u(o.throw(t))}catch(t){r(t)}}function u(t){var e;t.done?i(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(c,s)}u((o=o.apply(t,e||[])).next())}))},_="ZeroConfScannerCordova",m=function(){function t(t){if(void 0===t&&(t={domain:"local.",type:"_tapm2m._tcp."}),this.options=t,this._services=[],this._services$=new n.Subject,this._isRunning=new n.BehaviorSubject(!1),void 0===typeof cordova||!cordova||!cordova.plugins)throw new Error("Cordova plugin zeroconf is missing");this.zeroconf=cordova.plugins.zeroconf}return Object.defineProperty(t.prototype,"results",{get:function(){return this._services$.asObservable()},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"scanning",{get:function(){return this._isRunning.asObservable()},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"isScanning",{get:function(){return this._isRunning.value},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"type",{set:function(t){this.options.type=t},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"domain",{set:function(t){this.options.domain=t},enumerable:!1,configurable:!0}),t.prototype.start=function(t){return y(this,void 0,void 0,(function(){var t;return u(this,(function(e){switch(e.label){case 0:h(_,"start"),this._isRunning.next(!0),e.label=1;case 1:return e.trys.push([1,6,,7]),this._closingProcess?[4,this._closingProcess]:[3,3];case 2:e.sent(),e.label=3;case 3:return[4,this.init()];case 4:return e.sent(),[4,this._start()];case 5:return e.sent(),[3,7];case 6:throw t=e.sent(),this._isRunning.next(!1),h("_start error",t),t;case 7:return[2]}}))}))},t.prototype.stop=function(t){return y(this,void 0,void 0,(function(){var e=this;return u(this,(function(n){switch(n.label){case 0:return this._closingProcess?[4,this._closingProcess]:[3,2];case 1:return n.sent(),[2];case 2:return n.trys.push([2,,4,5]),this._isRunning.next(!1),this._closingProcess=y(e,void 0,void 0,(function(){return u(this,(function(e){switch(e.label){case 0:return h(_,"close start!"),[4,this.unwatch().catch((function(t){}))];case 1:return e.sent(),[4,this._close(t).catch((function(t){}))];case 2:return e.sent(),h(_,"close done!"),[2]}}))})),[4,this._closingProcess];case 3:return n.sent(),[3,5];case 4:return this._closingProcess=void 0,[7];case 5:return[2]}}))}))},t.prototype.init=function(){return y(this,void 0,void 0,(function(){var t=this;return u(this,(function(e){switch(e.label){case 0:return h(_,"init zero conf..."),[4,new Promise((function(e,n){t.zeroconf.reInit(e,n)}))];case 1:return e.sent(),[2]}}))}))},t.prototype._close=function(t){return y(this,void 0,void 0,(function(){var n=this;return u(this,(function(o){switch(o.label){case 0:return[4,e.promiseTimeout((null==t?void 0:t.timeout)||1e4,new Promise((function(t,e){h(_,"stoping...",n.options),n.zeroconf.close((function(){h(_,"stopped"),t()}),(function(t){h(_,"stop error",t),e(t)}))})))];case 1:return[2,o.sent()]}}))}))},t.prototype.unwatch=function(){var t=this;return new Promise((function(e,n){h(_,"unwatching..."),t.zeroconf.unwatch(t.options.type,t.options.domain,(function(){h(_,"unwatch stop",t.options),e()}),(function(e){h(_,"unwatch error",t.options,e),n(e)}))}))},t.prototype.getHostname=function(){var t=this;return new Promise((function(e,n){t.zeroconf.getHostname((function(t){e(t)}),n)}))},t.prototype._start=function(){return y(this,void 0,void 0,(function(){var t=this;return u(this,(function(e){return this._services=[],this._services$.next(this._services),h("Zero conf watch: ",this.options),this.zeroconf.watch(this.options.type,this.options.domain,(function(e){h("Zero conf result",e);var n=e.action,o=e.service;if(o)switch(n){case"added":break;case"resolved":t._onServiceResolved(S(o));break;default:t._removeService(S(o))}})),[2]}))}))},t.prototype._removeService=function(t){var e=this._findServiceIndex(t);e>=0&&this._services.splice(e,1),this._services$.next(this._services)},t.prototype._findServiceIndex=function(t){return this._services.findIndex((function(e){return JSON.stringify(e)===JSON.stringify(t)}))},t.prototype._onServiceResolved=function(t){if(this._findServiceIndex(t)<0){this._services.push(t);var e=function(t,e,n){if(n||2===arguments.length)for(var o,i=0,r=e.length;i<r;i++)!o&&i in e||(o||(o=Array.prototype.slice.call(e,0,i)),o[i]=e[i]);return t.concat(o||Array.prototype.slice.call(e))}([],a(this._services));h("Emit new results: ",e),this._services$.next(e)}},t}();function S(t){var e=t.ipv4Addresses&&t.ipv4Addresses.length>0?t.ipv4Addresses[0]:void 0;return Object.assign(Object.assign({},t),{host:e})}t.WifiComProtocol=b,t.WifiScanner=v,t.ZeroConfScannerCordova=m,Object.defineProperty(t,"__esModule",{value:!0})}));
//# sourceMappingURL=iotize-device-com-wifi.cordova.umd.min.js.map