Skip to content

Commit

Permalink
Merge pull request #70 from joyja/portErrorRetry
Browse files Browse the repository at this point in the history
Port error retry
  • Loading branch information
joyja committed Sep 26, 2020
2 parents 1f221ac + 5b362be commit 4ecb4e4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/device/__tests__/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ describe(`Modbus: `, () => {
modbus.client.connectTCP.mockReset()
})
test(`Disconnect calls client close throws an error on reject.`, async () => {
modbus.client.close.mockImplementation(() => {
modbus.client.close.mockImplementation((callback) => {
throw new Error(`Close connection failed.`)
})
expect(await modbus.disconnect().catch((e) => e)).toMatchInlineSnapshot(
Expand Down
19 changes: 14 additions & 5 deletions src/device/modbus.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,9 @@ class Modbus extends Model {
logger.info(`Disconnecting from modbus device ${this.device.name}`)
const logText = `Closed connection to modbus device ${this.device.name}.`
if (this.connected) {
this.client.close(() => {
logger.info(logText)
resolve()
})
this.client.close(() => {})
logger.info(logText)
resolve()
} else {
logger.info(logText)
resolve()
Expand Down Expand Up @@ -300,9 +299,15 @@ class ModbusSource extends Model {
}
)
}).catch(async (error) => {
if (error.name === 'TransactionTimedOutError') {
if (
error.name === 'TransactionTimedOutError' ||
error.name === 'PortNotOpenError'
) {
logger.info(`Connection Timed Out on device ${this.device.name}`)
await this.modbus.disconnect()
await this.modbus.connect()
} else {
logger.error(error)
}
})
} else if (this.registerType === 'HOLDING_REGISTER') {
Expand All @@ -326,6 +331,8 @@ class ModbusSource extends Model {
if (error.name === 'TransactionTimedOutError') {
await this.modbus.disconnect()
await this.modbus.connect()
} else {
logger.error(error)
}
})
} else if (this.registerType === 'DISCRETE_INPUT') {
Expand All @@ -349,6 +356,8 @@ class ModbusSource extends Model {
if (error.name === 'TransactionTimedOutError') {
await this.modbus.disconnect()
await this.modbus.connect()
} else {
logger.error(error)
}
})
}
Expand Down

0 comments on commit 4ecb4e4

Please sign in to comment.