@serialpilot/reconnect
v1.0.1
Published
Auto-reconnect wrapper for SerialPilot
Downloads
11,818
Maintainers
Readme
Auto-reconnect wrapper for SerialPilot.
Usage
const { SerialPilot } = require('serialpilot')
const { SerialPilotReconnect } = require('@serialpilot/reconnect')
const port = new SerialPilot({ path: '/dev/ttyUSB0', baudRate: 9600, autoOpen: false })
const reconnect = new SerialPilotReconnect({
port,
reconnectInterval: 1000,
maxReconnectAttempts: 10,
backoffFactor: 1.5,
})
reconnect.on('reconnecting', (attempt) => console.log(`Reconnect attempt ${attempt}`))
reconnect.on('reconnected', (attempt) => console.log(`Reconnected after ${attempt} attempts`))
reconnect.on('reconnect-failed', () => console.log('All reconnect attempts failed'))
reconnect.start()
port.open()API
new SerialPilotReconnect(options)
port— SerialPilotStream instance to manageautoReconnect— Enable auto-reconnect (default:true)reconnectInterval— ms between attempts (default:1000)maxReconnectAttempts— Maximum attempts (default:Infinity)backoffFactor— Exponential backoff multiplier (default:1)maxReconnectInterval— Cap for backoff in ms (default:30000)deviceFilter— DeviceFilter to use when searching for device on reconnectonDisconnect— Callback on disconnectonReconnect— Callback on successful reconnectonReconnectFailed— Callback when all attempts exhausted
Events
reconnecting— Emitted on each reconnect attemptreconnected— Emitted on successful reconnectreconnect-failed— Emitted when all attempts exhausteddisconnect— Emitted when device disconnects
Methods
start()— Begin monitoring for disconnectsstop()— Stop monitoring and cancel pending reconnectionforceReconnect()— Immediately attempt reconnection
