# Source: poweredup-node.js

    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    const boostmovehub_1 = require("./boostmovehub");
    const controlplushub_1 = require("./controlplushub");
    const duplotrainbase_1 = require("./duplotrainbase");
    const nobledevice_1 = require("./nobledevice");
    const puphub_1 = require("./puphub");
    const pupremote_1 = require("./pupremote");
    const wedo2smarthub_1 = require("./wedo2smarthub");
    const events_1 = require("events");
    const Debug = require("debug");
    const debug = Debug("poweredup");
    const noble = require("@abandonware/noble");
    let ready = false;
    let wantScan = false;
    let discoveryEventAttached = false;
    const startScanning = () => {
        noble.startScanning();
    };
    noble.on("stateChange", (state) => {
        ready = (state === "poweredOn");
        if (ready) {
            if (wantScan) {
                debug("Scanning started");
                startScanning();
            }
        }
        else {
            noble.stopScanning();
        }
    });
    /**
     * @class PoweredUP
     * @extends EventEmitter
     */
    class PoweredUP extends events_1.EventEmitter {
        constructor() {
            super();
            this.autoSubscribe = true;
            this._connectedHubs = {};
            this._discoveryEventHandler = this._discoveryEventHandler.bind(this);
        }
        /**
         * Begin scanning for Powered UP Hub devices.
         * @method PoweredUP#scan
         */
        async scan() {
            wantScan = true;
            if (!discoveryEventAttached) {
                noble.on("discover", this._discoveryEventHandler);
                discoveryEventAttached = true;
            }
            if (ready) {
                debug("Scanning started");
                startScanning();
            }
            return true;
        }
        /**
         * Stop scanning for Powered UP Hub devices.
         * @method PoweredUP#stop
         */
        stop() {
            wantScan = false;
            if (discoveryEventAttached) {
                noble.removeListener("discover", this._discoveryEventHandler);
                discoveryEventAttached = false;
            }
            noble.stopScanning();
        }
        /**
         * Retrieve a list of Powered UP Hubs.
         * @method PoweredUP#getConnectedHubs
         * @returns {Hub[]}
         */
        getConnectedHubs() {
            return Object.keys(this._connectedHubs).map((uuid) => this._connectedHubs[uuid]);
        }
        /**
         * Retrieve a Powered UP Hub by UUID.
         * @method PoweredUP#getConnectedHubByUUID
         * @param {string} uuid
         * @returns {Hub | null}
         */
        getConnectedHubByUUID(uuid) {
            return this._connectedHubs[uuid];
        }
        /**
         * Retrieve a list of Powered UP Hub by name.
         * @method PoweredUP#getConnectedHubsByName
         * @param {string} name
         * @returns {Hub[]}
         */
        getConnectedHubsByName(name) {
            return Object.keys(this._connectedHubs).map((uuid) => this._connectedHubs[uuid]).filter((hub) => hub.name === name);
        }
        async _discoveryEventHandler(peripheral) {
            peripheral.removeAllListeners();
            const device = new nobledevice_1.NobleDevice(peripheral);
            let hub;
            if (await wedo2smarthub_1.WeDo2SmartHub.IsWeDo2SmartHub(peripheral)) {
                hub = new wedo2smarthub_1.WeDo2SmartHub(device, this.autoSubscribe);
            }
            else if (await boostmovehub_1.BoostMoveHub.IsBoostMoveHub(peripheral)) {
                hub = new boostmovehub_1.BoostMoveHub(device, this.autoSubscribe);
            }
            else if (await puphub_1.PUPHub.IsPUPHub(peripheral)) {
                hub = new puphub_1.PUPHub(device, this.autoSubscribe);
            }
            else if (await pupremote_1.PUPRemote.IsPUPRemote(peripheral)) {
                hub = new pupremote_1.PUPRemote(device, this.autoSubscribe);
            }
            else if (await duplotrainbase_1.DuploTrainBase.IsDuploTrainBase(peripheral)) {
                hub = new duplotrainbase_1.DuploTrainBase(device, this.autoSubscribe);
            }
            else if (await controlplushub_1.ControlPlusHub.IsControlPlusHub(peripheral)) {
                hub = new controlplushub_1.ControlPlusHub(device, this.autoSubscribe);
            }
            else {
                return;
            }
            device.on("discoverComplete", () => {
                hub.on("connect", () => {
                    debug(`Hub ${hub.uuid} connected`);
                    this._connectedHubs[hub.uuid] = hub;
                });
                hub.on("disconnect", () => {
                    debug(`Hub ${hub.uuid} disconnected`);
                    delete this._connectedHubs[hub.uuid];
                    if (wantScan) {
                        startScanning();
                    }
                });
                debug(`Hub ${hub.uuid} discovered`);
                /**
                 * Emits when a Powered UP Hub device is found.
                 * @event PoweredUP#discover
                 * @param {WeDo2SmartHub | BoostMoveHub | ControlPlusHub | PUPHub | PUPRemote | DuploTrainBase} hub
                 */
                this.emit("discover", hub);
            });
        }
    }
    exports.PoweredUP = PoweredUP;
    //# sourceMappingURL=poweredup-node.js.map

#### Search results