Skip to content

Commit

Permalink
Check device feature exist
Browse files Browse the repository at this point in the history
  • Loading branch information
rpochet committed Oct 9, 2023
1 parent 61bd0b7 commit c0f7383
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 30 deletions.
75 changes: 45 additions & 30 deletions server/services/sunspec/lib/sunspec.scanDevices.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ async function scanDevices() {
const values = ModelFactory.createModel(await device.modbus.readModel(device.valueModel));
Object.entries(values).forEach(async (entry) => {
const [name, value] = entry;
this.eventManager.emit(EVENTS.DEVICE.NEW_STATE, {
device_feature_external_id: getDeviceFeatureExternalId({
...device,
property: name,
}),
state: value,
const externalId = getDeviceFeatureExternalId({
...device,
property: name,
});
if (this.stateManager.get('deviceFeatureByExternalId', externalId)) {
this.eventManager.emit(EVENTS.DEVICE.NEW_STATE, {
device_feature_external_id: externalId,
state: value,
});
}
}, this);
}, this);

Expand All @@ -35,34 +38,46 @@ async function scanDevices() {
const { mppt } = ModelFactory.createModel(await device.modbus.readModel(MODEL.MPPT_INVERTER_EXTENSION));
const { DCA, DCV, DCW, DCWH } = mppt[device.mppt - 1];

this.eventManager.emit(EVENTS.DEVICE.NEW_STATE, {
device_feature_external_id: getDeviceFeatureExternalId({
...device,
property: PROPERTY.DCA,
}),
state: DCA,
let externalId = getDeviceFeatureExternalId({
...device,
property: PROPERTY.DCA,
});
this.eventManager.emit(EVENTS.DEVICE.NEW_STATE, {
device_feature_external_id: getDeviceFeatureExternalId({
...device,
property: PROPERTY.DCV,
}),
state: DCV,
if (this.stateManager.get('deviceFeatureByExternalId', externalId)) {
this.eventManager.emit(EVENTS.DEVICE.NEW_STATE, {
device_feature_external_id: externalId,
state: DCA,
});
}
externalId = getDeviceFeatureExternalId({
...device,
property: PROPERTY.DCV,
});
this.eventManager.emit(EVENTS.DEVICE.NEW_STATE, {
device_feature_external_id: getDeviceFeatureExternalId({
...device,
property: PROPERTY.DCW,
}),
state: DCW,
if (this.stateManager.get('deviceFeatureByExternalId', externalId)) {
this.eventManager.emit(EVENTS.DEVICE.NEW_STATE, {
device_feature_external_id: externalId,
state: DCV,
});
}
externalId = getDeviceFeatureExternalId({
...device,
property: PROPERTY.DCW,
});
this.eventManager.emit(EVENTS.DEVICE.NEW_STATE, {
device_feature_external_id: getDeviceFeatureExternalId({
...device,
property: PROPERTY.DCWH,
}),
state: DCWH,
if (this.stateManager.get('deviceFeatureByExternalId', externalId)) {
this.eventManager.emit(EVENTS.DEVICE.NEW_STATE, {
device_feature_external_id: externalId,
state: DCW,
});
}
externalId = getDeviceFeatureExternalId({
...device,
property: PROPERTY.DCWH,
});
if (this.stateManager.get('deviceFeatureByExternalId', externalId)) {
this.eventManager.emit(EVENTS.DEVICE.NEW_STATE, {
device_feature_external_id: externalId,
state: DCWH,
});
}
}, this);

logger.debug(`SunSpec: Scanning devices done`);
Expand Down
3 changes: 3 additions & 0 deletions server/test/services/sunspec/lib/sunspec.scanDevices.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ describe('SunSpec scanDevices', () => {
eventManager: {
emit: fake.resolves(null),
},
stateManager: {
get: fake.resolves({}),
},
modbuses: [modbus],
devices: [],
};
Expand Down

0 comments on commit c0f7383

Please sign in to comment.