UNPKG

@iotize/device-com-wifi.cordova

Version:
185 lines 6.91 kB
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 { promiseTimeout } from '@iotize/common/promise'; import { BehaviorSubject, Subject } from 'rxjs'; import { debug } from '../logger'; const TAG = 'ZeroConfScannerCordova'; const DEFAULT_STOP_TIMEOUT = 10 * 1000; export class ZeroConfScannerCordova { constructor(options = { domain: 'local.', type: '_tapm2m._tcp.', }) { this.options = options; // _hostname: string; this._services = []; this._services$ = new Subject(); this._isRunning = new BehaviorSubject(false); if (typeof cordova !== undefined && cordova && cordova.plugins) { this.zeroconf = cordova.plugins.zeroconf; } else { throw new Error(`Cordova plugin zeroconf is missing`); } } get results() { return this._services$.asObservable(); } get scanning() { return this._isRunning.asObservable(); } get isScanning() { return this._isRunning.value; } set type(type) { this.options.type = type; } set domain(domain) { this.options.domain = domain; } /** * Start scan */ start(option) { return __awaiter(this, void 0, void 0, function* () { debug(TAG, 'start'); this._isRunning.next(true); try { if (this._closingProcess) { yield this._closingProcess; } yield this.init(); yield this._start(); } catch (err) { this._isRunning.next(false); debug('_start error', err); throw err; } }); } stop(options) { return __awaiter(this, void 0, void 0, function* () { if (this._closingProcess) { yield this._closingProcess; return; } try { this._isRunning.next(false); this._closingProcess = (() => __awaiter(this, void 0, void 0, function* () { debug(TAG, 'close start!'); yield this.unwatch().catch((err) => { }); yield this._close(options).catch((err) => { }); debug(TAG, 'close done!'); }))(); yield this._closingProcess; } finally { this._closingProcess = undefined; } }); } /** * Initialize/reinitialize scanner */ init() { return __awaiter(this, void 0, void 0, function* () { debug(TAG, 'init zero conf...'); yield new Promise((resolve, reject) => { this.zeroconf.reInit(resolve, reject); }); }); } _close(options) { return __awaiter(this, void 0, void 0, function* () { return yield promiseTimeout((options === null || options === void 0 ? void 0 : options.timeout) || DEFAULT_STOP_TIMEOUT, new Promise((resolve, reject) => { debug(TAG, 'stoping...', this.options); this.zeroconf.close(() => { debug(TAG, 'stopped'); resolve(); }, (err) => { debug(TAG, 'stop error', err); reject(err); }); })); }); } unwatch() { return new Promise((resolve, reject) => { debug(TAG, 'unwatching...'); this.zeroconf.unwatch(this.options.type, this.options.domain, () => { debug(TAG, 'unwatch stop', this.options); resolve(); }, (err) => { debug(TAG, 'unwatch error', this.options, err); reject(err); }); }); } getHostname() { return new Promise((resolve, reject) => { this.zeroconf.getHostname((hostname) => { resolve(hostname); }, reject); }); } _start() { return __awaiter(this, void 0, void 0, function* () { // if (!this._hostname) { // this._hostname = await this.getHostname(); // } this._services = []; this._services$.next(this._services); // debug('Hostname: ', this._hostname); debug('Zero conf watch: ', this.options); this.zeroconf.watch(this.options.type, this.options.domain, (result) => { debug('Zero conf result', result); const action = result.action; const service = result.service; if (service) { switch (action) { case 'added': break; case 'resolved': this._onServiceResolved(resolveHost(service)); break; default: this._removeService(resolveHost(service)); } } }); }); } _removeService(service) { const existsingIndex = this._findServiceIndex(service); if (existsingIndex >= 0) { this._services.splice(existsingIndex, 1); } this._services$.next(this._services); } _findServiceIndex(service) { return this._services.findIndex((s2) => JSON.stringify(s2) === JSON.stringify(service)); } _onServiceResolved(service) { if (this._findServiceIndex(service) < 0) { this._services.push(service); const serviceListCopy = [...this._services]; debug('Emit new results: ', serviceListCopy); this._services$.next(serviceListCopy); } } } function resolveHost(input) { const host = input.ipv4Addresses && input.ipv4Addresses.length > 0 ? input.ipv4Addresses[0] : undefined; return Object.assign(Object.assign({}, input), { host }); } //# sourceMappingURL=zeroconf-scanner.js.map