Skip to content

Commit

Permalink
prevent crash case on invalid subscribe
Browse files Browse the repository at this point in the history
  • Loading branch information
foxriver76 committed Sep 28, 2023
1 parent 40eed3c commit a84907e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test-and-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:

strategy:
matrix:
node-version: [16.x]
node-version: [18.x]

steps:
- uses: actions/checkout@v3
Expand Down Expand Up @@ -93,7 +93,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [12.x]
node-version: [18.x]

steps:
- name: Checkout code
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ default: state-ID converted to a mqtt topic.
-->

## Changelog
### __WORK IN PROGRESS__
* (foxriver76) prevent crash cases on invalid subscribe

### 1.6.4 (2023-07-26)
* (DutchmanNL) Option to allow self-signed certificates in adapter settings added.

Expand Down
14 changes: 10 additions & 4 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,11 +321,18 @@ class MqttClient extends utils.Adapter {
this.client && this.client.unsubscribe(this.topicAddPrefixIn(topic), callback);
}

iobSubscribe(id, callback) {
async iobSubscribe(id, callback) {
if (!this._subscribes.includes(id)) {
this._subscribes.push(id);
this._subscribes.sort();
this.subscribeForeignStates(id, callback);
try {
await this.subscribeForeignStatesAsync(id);
} catch (e) {
this.log.error(`Cannot subscribe to "${id}": ${e.message}`);
}
if (typeof callback === 'function') {
callback();
}
}
}

Expand Down Expand Up @@ -465,7 +472,7 @@ class MqttClient extends utils.Adapter {

this.getObjectView('system', 'custom', {}, (err, doc) => {
const ids = [];
if (doc && doc.rows) {
if (doc?.rows) {
for (let i = 0, l = doc.rows.length; i < l; i++) {
const cust = doc.rows[i].value;
if (cust && cust[this.namespace] && cust[this.namespace].enabled) {
Expand Down Expand Up @@ -663,7 +670,6 @@ class MqttClient extends utils.Adapter {
}

if (custom[id].enabled) {
//@todo should this be .subscribe?
//subscribe to state changes
this.iobSubscribe(id, err => {
//publish state once
Expand Down

0 comments on commit a84907e

Please sign in to comment.