import { ZWaveError , ZWaveErrorCodes , ZWaveLogContainer } from "@zwave-js/core" ; import type SerialPort from "serialport" ; import { ZWaveSerialPortBase } from "./ZWaveSerialPortBase" ; interface DisconnectError extends Error { disconnected : boolean ; } /** The default version of the Z-Wave serial binding that works using node-serialport */ export class ZWaveSerialPort extends ZWaveSerialPortBase { constructor ( port , loggers ) ; internal constructor ( port , loggers , Binding ) ; constructor ( port , loggers , Binding = require ( "serialport" ) ) { super ( { create : ( ) => new Binding ( port , { autoOpen : false , baudRate : 115200 , dataBits : 8 , stopBits : 1 , parity : "none" } ) , open : ( serial ) => new Promise ( ( resolve , reject ) => { let removeListeners ; const onClose = ( err ? ) => { if ( err ?. disconnected === true ) { removeListeners ( ) ; this . emit ( "error" , new ZWaveError ( `The serial port closed unexpectedly!` , ZWaveErrorCodes . Driver_Failed ) ) ; } } ; const onError = ( err ) => { removeListeners ( ) ; reject ( err ) ; } ; const onOpen = ( ) => { removeListeners ( ) ; resolve ( ) ; } ; removeListeners = ( ) => { serial . removeListener ( "close" , onClose ) ; serial . removeListener ( "error" , onError ) ; serial . removeListener ( "open" , onOpen ) ; } ; serial . once ( "close" , onClose ) ; serial . once ( "error" , onError ) ; serial . once ( "open" , onOpen ) . open ( ) ; } ) , close : ( serial ) => new Promise ( ( resolve ) => { serial . once ( "close" , resolve ) . close ( ) ; } ) } , loggers ) ; } public get isOpen ( ) : boolean { return ( this . serial as SerialPort ) . isOpen ; } }