@iotize/device-com-socket.node
Version:
Socket communication protocol to communicate with an iotize
78 lines • 2.68 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.NetworkServiceDiscovery = void 0;
const bonjour_1 = __importDefault(require("bonjour"));
const rxjs_1 = require("rxjs");
const debug_1 = __importDefault(require("../debug"));
const TAG = 'NetworkServiceDiscovery';
/**
* Scanner to discover Taps on your Network
*/
class NetworkServiceDiscovery {
constructor(options = { type: 'tapm2m' }, _bonjour = bonjour_1.default({})) {
this.options = options;
this._bonjour = _bonjour;
this._results = new rxjs_1.Subject();
this._scanning = new rxjs_1.BehaviorSubject(false);
this._resultCache = [];
}
get scanning() {
return this._scanning.asObservable();
}
get isScanning() {
return this._scanning.value;
}
get results() {
return this._results.asObservable();
}
get currentResults() {
return this._resultCache;
}
get services() {
return this._results.asObservable();
}
async start() {
if (this.isScanning) {
debug_1.default(TAG, 'already started');
return;
}
this._scanning.next(true);
this._results.next([...this._resultCache]);
if (this._browser) {
this._browser.stop();
}
debug_1.default(TAG, 'starting');
this._browser = this._bonjour.find({ type: this.options.type }, (service) => {
debug_1.default(TAG, 'new service detected', service);
this._resultCache.push(service);
this._results.next([...this._resultCache]);
});
// }
// else {
// debug(TAG, 're-starting');
// }
this._browser.start();
// this._browser.addListener('up', (data) => {
// console.info('addListener', 'up', data);
// })
// this._browser.addListener('down', (data) => {
// console.info('addListener', 'up', data);
// })
// this._browser.start();
// this._browser.update();
return;
}
async stop() {
var _a;
debug_1.default(TAG, 'stoping');
this._scanning.next(false);
this._resultCache = [];
(_a = this._browser) === null || _a === void 0 ? void 0 : _a.stop();
this._browser = undefined;
}
}
exports.NetworkServiceDiscovery = NetworkServiceDiscovery;
//# sourceMappingURL=network-service-discovery.js.map