fixes reconnection issues in node_helper
This commit is contained in:
parent
086d9fb51b
commit
ac290d18ae
@ -8,6 +8,7 @@ module.exports = NodeHelper.create({
|
|||||||
stop,
|
stop,
|
||||||
socketNotificationReceived,
|
socketNotificationReceived,
|
||||||
connect,
|
connect,
|
||||||
|
connectWs,
|
||||||
getState,
|
getState,
|
||||||
toggleState,
|
toggleState,
|
||||||
setCoverPosition,
|
setCoverPosition,
|
||||||
@ -83,6 +84,10 @@ async function connect(payload) {
|
|||||||
entities: [],
|
entities: [],
|
||||||
};
|
};
|
||||||
|
|
||||||
|
this.connectWs(connectionConfig, payload)
|
||||||
|
}
|
||||||
|
|
||||||
|
async function connectWs(connectionConfig, payload) {
|
||||||
const self = this;
|
const self = this;
|
||||||
HomeAssistantWS.default({
|
HomeAssistantWS.default({
|
||||||
...connectionConfig,
|
...connectionConfig,
|
||||||
@ -90,13 +95,22 @@ async function connect(payload) {
|
|||||||
})
|
})
|
||||||
.then((hassWs) => {
|
.then((hassWs) => {
|
||||||
this.connections[payload.identifier].websocket = hassWs;
|
this.connections[payload.identifier].websocket = hassWs;
|
||||||
hassWs.onEvent("state_changed", onStateChangedEvent.bind(self));
|
hassWs.on("state_changed", onStateChangedEvent.bind(self));
|
||||||
|
hassWs.on('ws_close', () => {
|
||||||
|
this.logger.debug(`Lost connection for ${payload.identifier}... Trying to reconnect in 2secs`)
|
||||||
|
setTimeout(() => {
|
||||||
|
this.connectWs(connectionConfig, payload)
|
||||||
|
}, 2000)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
this.logger.error(
|
this.logger.error(
|
||||||
`WS connection for ${payload.identifier} failed with`,
|
`WS connection for ${payload.identifier} failed...`
|
||||||
err
|
|
||||||
);
|
);
|
||||||
|
this.logger.debug(`Trying to reconnect for ${payload.identifier} in 2secs`)
|
||||||
|
setTimeout(() => {
|
||||||
|
this.connectWs(connectionConfig, payload)
|
||||||
|
}, 2000)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user