@iotize/device-com-wifi.cordova
Version:
IoTize Cordova plugin for Wifi devices
93 lines • 4.82 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { QueueComProtocol } from '@iotize/tap/protocol/core';
import { Observable, of } from 'rxjs';
import { share } from 'rxjs/operators';
import { WifiComProtocolError } from './errors';
import { debug } from './logger';
export class WifiComProtocol extends QueueComProtocol {
constructor(wifiOptions, createSubProtocol) {
super();
this.wifiOptions = wifiOptions;
this.createSubProtocol = createSubProtocol;
}
get socketProtocol() {
if (!this._socketProtocol) {
throw new Error(`Protocol is not connected yet`); // TODO proper error code
}
return this._socketProtocol;
}
_connect(options) {
return new Observable((emitter) => {
(() => __awaiter(this, void 0, void 0, function* () {
try {
if (this.wifiOptions.network) {
let currentSSID = yield WifiWizard2.getConnectedSSID();
if (currentSSID !== this.wifiOptions.network.SSID) {
debug('currently connected to', currentSSID, 'expected ', this.wifiOptions.network.SSID, '=> try to connect to ', this.wifiOptions.network);
yield WifiWizard2.connect(this.wifiOptions.network.SSID, true, this.wifiOptions.network.password, this.wifiOptions.network.algorithm, this.wifiOptions.network.hidden);
currentSSID = yield WifiWizard2.getConnectedSSID();
if (currentSSID !== this.wifiOptions.network.SSID) {
emitter.error(WifiComProtocolError.cannotConnectToNetwork(this.wifiOptions.network));
return;
}
debug('now connected to SSID', currentSSID);
}
}
debug('Now creating socket protocol with options', this.wifiOptions);
this._socketProtocol = yield this.createSubProtocol(this.wifiOptions);
if (this.subProtocolConnectionStateSub)
this.subProtocolConnectionStateSub.unsubscribe();
this.subProtocolConnectionStateSub = this._socketProtocol
.onConnectionStateChange()
.subscribe((event) => {
this.setConnectionState(event.newState);
});
yield this._socketProtocol.connect().toPromise();
emitter.complete();
}
catch (err) {
if (this.wifiOptions.network) {
switch (err) {
case 'CONNECT_FAILED_TIMEOUT':
err = WifiComProtocolError.connectFailedTimeout(this.wifiOptions.network);
break;
case 'WIFI_NOT_ENABLED':
err = WifiComProtocolError.wifiNotEnabled(this.wifiOptions.network);
break;
}
}
if (typeof err === 'string') {
err = WifiComProtocolError.unknownError(new Error(err), this.wifiOptions.network);
}
if (this.subProtocolConnectionStateSub)
this.subProtocolConnectionStateSub.unsubscribe();
debug('_connect error', err);
emitter.error(err);
}
}))();
}).pipe(share());
}
_disconnect(options) {
if (this.subProtocolConnectionStateSub)
this.subProtocolConnectionStateSub.unsubscribe();
if (!this._socketProtocol) {
return of(true);
}
return this._socketProtocol.disconnect();
}
write(data) {
return this.socketProtocol.write(data);
}
read() {
return this.socketProtocol.read();
}
}
//# sourceMappingURL=wifi-com-protocol.js.map