Serial/src/models/serial.js
/**
     * Auto detects ports - loads, opens, reads and removes automatically
     * @private
     */
Auto detects ports - loads, opens, reads and removes automatically
@private
Auto detects ports - loads opens reads and removes automatically @private
_startAutoDetect() {
        if(this._autoDetectingPorts) return;
        this._autoDetectingPorts = true;
        setInterval(() => {
            serialPort.list(async (err, ports) => {
                if(err) return console.log(new Error(err)); 
                let espPorts = []; 
                for(let i=0; i<ports.length; i++) {
                    if(this.chipNames.includes(ports[i].manufacturer)) {
                        espPorts.push(ports[i]);
                    }
                }
                let loadedPortIds = []; 
                for(let i=0; i<this.ports.length; i++) {
                    loadedPortIds.push(this.ports[i].pnpId);
                }
                let newPorts = []; 
                let cPortIds = []; 
                for(let i=0; i<espPorts.length; i++) {
                    cPortIds.push(espPorts[i].pnpId);
                    if(!loadedPortIds.includes(espPorts[i].pnpId)) newPorts.push(espPorts[i]);
                }
                let oldPorts = []; 
                for(let i=0; i<this.ports.length; i++) {
                    if(!cPortIds.includes(this.ports[i].pnpId)) oldPorts.push(this.ports[i]);
                }
                this.ports = espPorts;
                for(let i=0; i<newPorts.length; i++) { 
                    this.loadedPorts.push(new serialPort(newPorts[i].comName, {baudRate: 921600, autoOpen:false}));
                    try {
                        await this.openPort(newPorts[i].comName); 
                    } catch(e) {
                        return console.log(e); 
                    }
                    this.readPort(newPorts[i].comName); 
                    this.emit('open', {
                        comName: newPorts[i].comName,
                    })
                }
                for(let i=0; i<oldPorts.length; i++) {
                    let chipId = null;
                    for(let j=0; j<this.loadedPorts.length; j++) { 
                        if(oldPorts[i].comName === this.loadedPorts[j].path) {
                            chipId = this.loadedPorts[j].chipId;
                            this.loadedPorts.splice(j, 1);
                        }
                    }
                    for(let j=0; j<this.parsers.length; j++) { 
                        if(oldPorts[i].comName === this.parsers[j].comName) {
                            this.parsers.splice(j, 1);
                        }
                    }
                    this.emit('close', {
                        comName: oldPorts[i].comName,
                        chipId
                    });
                }
            });
        },100)
    }
_startAutoDetect ( ) { if ( this . _autoDetectingPorts ) return ; this . _autoDetectingPorts = true ; setInterval ( ( ) => { serialPort . list ( async ( err , ports ) => { if ( err ) return console . log ( new Error ( err ) ) ; let espPorts = [ ] ; for ( let i = 0 ; i < ports . length ; i ++ ) { if ( this . chipNames . includes ( ports [ i ] . manufacturer ) ) { espPorts . push ( ports [ i ] ) ; } } let loadedPortIds = [ ] ; for ( let i = 0 ; i < this . ports . length ; i ++ ) { loadedPortIds . push ( this . ports [ i ] . pnpId ) ; } let newPorts = [ ] ; let cPortIds = [ ] ; for ( let i = 0 ; i < espPorts . length ; i ++ ) { cPortIds . push ( espPorts [ i ] . pnpId ) ; if ( ! loadedPortIds . includes ( espPorts [ i ] . pnpId ) ) newPorts . push ( espPorts [ i ] ) ; } let oldPorts = [ ] ; for ( let i = 0 ; i < this . ports . length ; i ++ ) { if ( ! cPortIds . includes ( this . ports [ i ] . pnpId ) ) oldPorts . push ( this . ports [ i ] ) ; } this . ports = espPorts ; for ( let i = 0 ; i < newPorts . length ; i ++ ) { this . loadedPorts . push ( new serialPort ( newPorts [ i ] . comName , { baudRate : 921600 , autoOpen : false } ) ) ; try { await this . openPort ( newPorts [ i ] . comName ) ; } catch ( e ) { return console . log ( e ) ; } this . readPort ( newPorts [ i ] . comName ) ; this . emit ( 'open' , { comName : newPorts [ i ] . comName , } ) } for ( let i = 0 ; i < oldPorts . length ; i ++ ) { let chipId = null ; for ( let j = 0 ; j < this . loadedPorts . length ; j ++ ) { if ( oldPorts [ i ] . comName === this . loadedPorts [ j ] . path ) { chipId = this . loadedPorts [ j ] . chipId ; this . loadedPorts . splice ( j , 1 ) ; } } for ( let j = 0 ; j < this . parsers . length ; j ++ ) { if ( oldPorts [ i ] . comName === this . parsers [ j ] . comName ) { this . parsers . splice ( j , 1 ) ; } } this . emit ( 'close' , { comName : oldPorts [ i ] . comName , chipId } ) ; } } ) ; } , 100 ) }
Auto detects ports - loads, opens, reads and removes automatically
 @private
Auto detects ports - loads opens reads and removes automatically @private
//TODO: deal with error //all esp board ports //get loaded port ids //grab any new port ids //current port ids //grab any old ports //add the new ports //open new port //TODO: deal with error //read the new port //remove all old ports //remove old loaded ports //remove old parsers