Skip to content

Commit

Permalink
fix for duplicated states via websockets
Browse files Browse the repository at this point in the history
  • Loading branch information
algarik committed Feb 9, 2020
1 parent c67aeb9 commit 7634c93
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,10 @@ class WebServer {
_readAllEntities(cb) {
this._updateDevices()
.then(smartDevices => {
this._entities = smartDevices;
smartDevices.forEach((dev, devidx) => {
const foundIndex = this._entities.findIndex(x => x.entity_id === dev.entity_id);
if (foundIndex != -1) {this._entities[foundIndex] = dev;} else {this._entities.push(dev);}
});
this._getAllEntities(() => {
this._getAllStates(() => {
this._manageSubscribesFromConfig();
Expand Down Expand Up @@ -362,9 +365,10 @@ class WebServer {
}

this._ID2entity[obj._id] = this._ID2entity[obj._id] || [];
this._ID2entity[obj._id].push(entity);
this._addID2entity(obj._id, entity);
this._entity2ID[entity.entity_id] = entity;
this._entities.push(entity);
const foundIndex = this._entities.findIndex(x => x.entity_id === entity.entity_id);
if (foundIndex != -1) {this._entities[foundIndex] = entity;} else {this._entities.push(entity);}
return entity;
}

Expand Down Expand Up @@ -2508,9 +2512,8 @@ class WebServer {
}
};

this._entities.map(obj => entity.entity_id === obj.entity_id ? entity : obj);
//moved update of _entities here because entity variable gets undefined after the _wss call below...

const foundIndex = this._entities.findIndex(x => x.entity_id === entity.entity_id);
if (foundIndex != -1) {this._entities[foundIndex] = entity;}
this._wss.clients.forEach(ws => {
if (ws._subscribes && ws._subscribes.state_changed) {
ws._subscribes.state_changed.forEach(id => {
Expand Down

0 comments on commit 7634c93

Please sign in to comment.