import * as serialport from "serialport" ; export interface PortInformation { comName : string ; manufacturer : string ; serialNumber : string ; pnpId : string ; locationId : string ; vendorId : string ; productId : string ; } /**
 *
 */ export class NodeMcuCommunicator { private _port : serialport . SerialPort ; private _recvCallback : ( data : string ) => void ; /**
     *
     */ constructor ( portname , baud ) { let options = { autoOpen : false , baudRate : baud } ; this . _port = new serialport . SerialPort ( portname , options ) ; this . _port . on ( "data" , ( data ) => { if ( this . _recvCallback ) { this . _recvCallback ( data . toString ( 'utf8' ) ) ; } } ) ; } /**
     *
     */ public static detectPort ( cfgPort , callback ) { serialport . list ( ( error , ports ) => { if ( error != null ) { callback ( error , null ) ; } else { let filteredPorts = ports . filter ( ( port ) => { if ( cfgPort == null || cfgPort == "auto" ) { return port . pnpId . includes ( "VID_1A86" ) || port . pnpId . includes ( "VID_10C4" ) ; } return ( port . comName == cfgPort ) ; } ) ; callback ( error , filteredPorts ) ; } } ) ; } public open ( ) { this . _port . open ( ) ; } public registerOnPortDisconnect ( callback ) { this . _port . on ( "disconnect" , callback ) ; } public registerOnPortClose ( callback ) { this . _port . on ( "close" , callback ) ; } public registerOnPortOpen ( callback ) { this . _port . on ( "open" , ( ) => { callback ( ) ; } ) ; } public registerOnError ( callback ) { this . _port . on ( "error" , callback ) ; } public registerOnDataReceived ( callback ) { let oldCB = this . _recvCallback ; this . _recvCallback = callback ; return oldCB ; } public write ( data ) { if ( this . _port . isOpen ) { this . _port . write ( data , ( ) => { this . _port . drain ( ) ; } ) ; } } public close ( ) { if ( this . _port . isOpen ) { this . _port . close ( ) ; } } }