connect() {
        return new Promise((resolve, reject) => {
            if (this.socket) {
                reject(new Error('Already connected to device'));
                return;
            }
            if (!this.options.ip) {
                throw new Error('No IP provided for Device');
            }

            this.socket = new net.Socket();

            const port = this.options.port || PH803W_DEFAULT_TCP_PORT;
            this.socket.connect(port, this.options.ip, () => {
                debug(`Connected to TCP ${this.options.ip}:${port}`);
                /**
                 * Connected event emitted when connection was established successfully
                 */
                this.emit('connected');
                resolve(true);
            });

            this.socket.on('data', data => {
                if (!data || !Buffer.isBuffer(data)) {
                    return;
                }
                this._handleData(data);
            });

            this.socket.on('error', err => {
                debug('Socket error: ' + err);
                /**
                 * Error event when connection has errors
                 */
                this.emit('error', err);
                try {
                    this.socket && this.socket.destroy();
                } catch {
                    // ignore
                }
                this.socket = null;
                this._handleReconnect();
            });

            this.socket.on('end', () => {
                debug(`Socket end, Reconnect: ${this.autoReconnect}`);
                this.socket = null;
                this._handleReconnect();
            });

            this.socket.on('close', () => {
                debug(`Socket close`);
                this.socket = null;
                this._handleReconnect();
            });
        });
    }
/**
     * Connect to the device, do authentication calls and register for data updates
     *
     * @returns {Promise<boolean>} resolves with true when connected successfully
     *
     * @emits connected
     * @emits error
     */
Connect to the device, do authentication calls and register for data updates
@returns {Promise} resolves with true when connected successfully

@emits connected
@emits error
Connect to the device do authentication calls and register for data updates @returns { Promise } resolves with true when connected successfully @emits connected @emits error
connect() {
        return new Promise((resolve, reject) => {
            if (this.socket) {
                reject(new Error('Already connected to device'));
                return;
            }
            if (!this.options.ip) {
                throw new Error('No IP provided for Device');
            }
            this.socket = new net.Socket();
            const port = this.options.port || PH803W_DEFAULT_TCP_PORT;
            this.socket.connect(port, this.options.ip, () => {
                debug(`Connected to TCP ${this.options.ip}:${port}`);
                this.emit('connected');
                resolve(true);
            });
            this.socket.on('data', data => {
                if (!data || !Buffer.isBuffer(data)) {
                    return;
                }
                this._handleData(data);
            });
            this.socket.on('error', err => {
                debug('Socket error: ' + err);
                this.emit('error', err);
                try {
                    this.socket && this.socket.destroy();
                } catch {
                }
                this.socket = null;
                this._handleReconnect();
            });
            this.socket.on('end', () => {
                debug(`Socket end, Reconnect: ${this.autoReconnect}`);
                this.socket = null;
                this._handleReconnect();
            });
            this.socket.on('close', () => {
                debug(`Socket close`);
                this.socket = null;
                this._handleReconnect();
            });
        });
    }
connect ( ) { return new Promise ( ( resolve , reject ) => { if ( this . socket ) { reject ( new Error ( 'Already connected to device' ) ) ; return ; } if ( ! this . options . ip ) { throw new Error ( 'No IP provided for Device' ) ; } this . socket = new net . Socket ( ) ; const port = this . options . port || PH803W_DEFAULT_TCP_PORT ; this . socket . connect ( port , this . options . ip , ( ) => { debug ( ` ${ this . options . ip } ${ port } ` ) ; this . emit ( 'connected' ) ; resolve ( true ) ; } ) ; this . socket . on ( 'data' , data => { if ( ! data || ! Buffer . isBuffer ( data ) ) { return ; } this . _handleData ( data ) ; } ) ; this . socket . on ( 'error' , err => { debug ( 'Socket error: ' + err ) ; this . emit ( 'error' , err ) ; try { this . socket && this . socket . destroy ( ) ; } catch { } this . socket = null ; this . _handleReconnect ( ) ; } ) ; this . socket . on ( 'end' , ( ) => { debug ( ` ${ this . autoReconnect } ` ) ; this . socket = null ; this . _handleReconnect ( ) ; } ) ; this . socket . on ( 'close' , ( ) => { debug ( ` ` ) ; this . socket = null ; this . _handleReconnect ( ) ; } ) ; } ) ; }
Connect to the device, do authentication calls and register for data updates
 @returns {Promise<boolean>} resolves with true when connected successfully
Connect to the device do authentication calls and register for data updates @returns { Promise<boolean > } resolves with true when connected successfully
/**
                 * Connected event emitted when connection was established successfully
                 */ /**
                 * Error event when connection has errors
                 */ // ignore