Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rpochet committed Sep 22, 2023
1 parent 0625078 commit 04456c1
Show file tree
Hide file tree
Showing 13 changed files with 117 additions and 97 deletions.
5 changes: 4 additions & 1 deletion server/services/sunspec/lib/sunspec.connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { CONFIGURATION, DEFAULT, SCAN_OPTIONS } = require('./sunspec.constants');
const { WEBSOCKET_MESSAGE_TYPES, EVENTS } = require('../../../utils/constants');
const { ModbusClient } = require('./utils/sunspec.ModbusClient');
const logger = require('../../../utils/logger');
const { ServiceNotConfiguredError } = require('../../../utils/coreErrors');

/**
* @description Initialize service with dependencies and connect to devices.
Expand All @@ -20,6 +21,8 @@ async function connect() {
const mask = { ...option, networkInterface: false };
this.ipMasks.push(mask);
});
} else {
throw new ServiceNotConfiguredError();
}

// Complete masks with network interfaces
Expand Down Expand Up @@ -71,7 +74,7 @@ async function connect() {

this.connected = true;

this.gladys.event.emit(EVENTS.WEBSOCKET.SEND_ALL, {
this.eventManager.emit(EVENTS.WEBSOCKET.SEND_ALL, {
type: WEBSOCKET_MESSAGE_TYPES.SUNSPEC.CONNECTED,
});

Expand Down
2 changes: 1 addition & 1 deletion server/services/sunspec/lib/sunspec.disconnect.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ async function disconnect() {
if (this.modbus) {
this.modbus.close(() => {
if (this.connected) {
this.gladys.event.emit(EVENTS.WEBSOCKET.SEND_ALL, {
this.eventManager.emit(EVENTS.WEBSOCKET.SEND_ALL, {
type: WEBSOCKET_MESSAGE_TYPES.SUNSPEC.STATUS_CHANGE,
});
} else {
Expand Down
4 changes: 2 additions & 2 deletions server/services/sunspec/lib/sunspec.scan.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ async function scan() {
logger.info(`Sunspec starts scanning devices...`);

this.scanning = true;
this.gladys.event.emit(EVENTS.WEBSOCKET.SEND_ALL, {
this.eventManager.emit(EVENTS.WEBSOCKET.SEND_ALL, {
type: WEBSOCKET_MESSAGE_TYPES.SUNSPEC.SCANNING,
payload: {
scanning: true,
Expand All @@ -29,7 +29,7 @@ async function scan() {

const scanDone = (discoveredDevices, success) => {
this.scanning = false;
this.gladys.event.emit(EVENTS.WEBSOCKET.SEND_ALL, {
this.eventManager.emit(EVENTS.WEBSOCKET.SEND_ALL, {
type: WEBSOCKET_MESSAGE_TYPES.SUNSPEC.SCANNING,
payload: { scanning: false, success },
});
Expand Down
20 changes: 10 additions & 10 deletions server/services/sunspec/lib/sunspec.scanDevices.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ async function scanDevices() {
.filter((device) => device.mppt === undefined)
.forEach(async (device) => {
const values = ModelFactory.createModel(await device.modbus.readModel(device.valueModel));
Object.entries(values).forEach((entry) => {
Object.entries(values).forEach(async (entry) => {
const [name, value] = entry;
this.gladys.event.emit(EVENTS.DEVICE.NEW_STATE, {
this.eventManager.emit(EVENTS.DEVICE.NEW_STATE, {
device_feature_external_id: getDeviceFeatureExternalId({
...device,
property: name,
}),
state: value,
});
});
});
}, this);
}, this);

Object.values(this.devices)
.filter((device) => device.mppt !== undefined)
Expand All @@ -35,37 +35,37 @@ 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.gladys.event.emit(EVENTS.DEVICE.NEW_STATE, {
this.eventManager.emit(EVENTS.DEVICE.NEW_STATE, {
device_feature_external_id: getDeviceFeatureExternalId({
...device,
property: PROPERTY.DCA,
}),
state: DCA,
});
this.gladys.event.emit(EVENTS.DEVICE.NEW_STATE, {
this.eventManager.emit(EVENTS.DEVICE.NEW_STATE, {
device_feature_external_id: getDeviceFeatureExternalId({
...device,
property: PROPERTY.DCV,
}),
state: DCV,
});
this.gladys.event.emit(EVENTS.DEVICE.NEW_STATE, {
this.eventManager.emit(EVENTS.DEVICE.NEW_STATE, {
device_feature_external_id: getDeviceFeatureExternalId({
...device,
property: PROPERTY.DCW,
}),
state: DCW,
});
this.gladys.event.emit(EVENTS.DEVICE.NEW_STATE, {
this.eventManager.emit(EVENTS.DEVICE.NEW_STATE, {
device_feature_external_id: getDeviceFeatureExternalId({
...device,
property: PROPERTY.DCWH,
}),
state: DCWH,
});
});
}, this);

logger.debug(`SunSpec: Scanning device done`);
logger.debug(`SunSpec: Scanning devices done`);
}

module.exports = {
Expand Down
9 changes: 5 additions & 4 deletions server/services/sunspec/lib/sunspec.scanNetwork.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ const { ModelFactory } = require('./utils/sunspec.ModelFactory');
async function scanNetwork() {
logger.debug(`SunSpec: Scanning network...`);

this.modbuses.forEach(async (modbus) => {
this.devices = [];

const promises = this.modbuses.map(async (modbus) => {
const { manufacturer, product, swVersion, serialNumber } = ModelFactory.createModel(
await modbus.readModel(MODEL.COMMON),
);
Expand All @@ -22,8 +24,6 @@ async function scanNetwork() {
// SMA = N <> Fronius = N - 2
const nbOfMPPT = (await modbus.readRegisterAsInt16(REGISTER.NB_OF_MPPT)) - (manufacturer === 'Fronius' ? 2 : 0);

this.devices = [];

// AC device
this.devices.push({
manufacturer,
Expand All @@ -46,8 +46,9 @@ async function scanNetwork() {
});
}
});
await Promise.all(promises);

this.gladys.event.emit(EVENTS.WEBSOCKET.SEND_ALL, {
this.eventManager.emit(EVENTS.WEBSOCKET.SEND_ALL, {
type: WEBSOCKET_MESSAGE_TYPES.SUNSPEC.STATUS_CHANGE,
});
}
Expand Down
7 changes: 5 additions & 2 deletions server/test/services/sunspec/lib/sunspec.connect.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ const { fake, assert } = sinon;

const proxyquire = require('proxyquire');

const scanMock = fake.returns(['192.168.1.x']);
const scanNetworkMock = fake.returns(null);
const scanDevicesMock = fake.returns(null);
const ModbusTCPMock = require('./utils/ModbusTCPMock.test');

const SunSpecManager = proxyquire('../../../../services/sunspec/lib', {
ModbusTCP: { ModbusTCP: ModbusTCPMock },
'./sunspec.scan': { scan: scanMock },
'./sunspec.scanNetwork': { scanNetwork: scanNetworkMock },
'./sunspec.scanDevices': { scanDevices: scanDevicesMock },
});
Expand All @@ -35,7 +37,7 @@ describe('SunSpec connect', () => {
},
};

sunSpecManager = new SunSpecManager(gladys, ModbusTCPMock, SERVICE_ID);
sunSpecManager = new SunSpecManager(gladys, ModbusTCPMock, null, SERVICE_ID);
});

afterEach(() => {
Expand All @@ -56,11 +58,12 @@ describe('SunSpec connect', () => {
});

it('should connect', async () => {
gladys.variable.getValue = fake.resolves('sunspecUrl');
gladys.variable.getValue = fake.resolves('[{"ip":"192.168.1.0/24"}]');

await sunSpecManager.connect();

expect(sunSpecManager.connected).eql(true);
assert.calledOnce(sunSpecManager.scan);
assert.calledOnceWithExactly(gladys.event.emit, EVENTS.WEBSOCKET.SEND_ALL, {
type: WEBSOCKET_MESSAGE_TYPES.SUNSPEC.CONNECTED,
});
Expand Down
11 changes: 7 additions & 4 deletions server/test/services/sunspec/lib/sunspec.disconnect.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,32 @@ describe('SunSpec disconnect', () => {

beforeEach(() => {
gladys = {
variable: {
getValue: fake.resolves('sunspecUrl'),
},
event: {
emit: fake.returns(null),
},
};

sunSpecManager = new SunSpecManager(gladys, ModbusTCPMock, SERVICE_ID);
sunSpecManager = new SunSpecManager(gladys, ModbusTCPMock, null, SERVICE_ID);
sunSpecManager.modbus = {
close: fake.returns(null),
};
});

afterEach(() => {
sinon.reset();
});

it('should disconnect - not connected', async () => {
await sunSpecManager.disconnect();

assert.calledOnce(sunSpecManager.modbus.close);
expect(sunSpecManager.connected).eql(false);
});

it('should disconnect', async () => {
await sunSpecManager.disconnect();

assert.calledOnce(sunSpecManager.modbus.close);
expect(sunSpecManager.connected).eql(false);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,27 @@ describe('SunSpec getConfiguration', () => {
variable: {},
};

sunSpecManager = new SunSpecManager(gladys, ModbusTCPMock, SERVICE_ID);
sunSpecManager = new SunSpecManager(gladys, ModbusTCPMock, null, SERVICE_ID);
sunSpecManager.ipMasks = [
{
ip: '192.168.1.0/24',
},
];
});

it('get config from service', async () => {
gladys.variable.getValue = stub()
.onFirstCall()
.returns('sunspecUrl')
.onSecondCall()
.returns('0')
.onThirdCall()
.onSecondCall()
.returns('bdpvUsername')
.onCall(3)
.onThirdCall()
.returns('bdpvApiKey');

const configuration = await sunSpecManager.getConfiguration();

expect(configuration).deep.eq({
sunspecUrl: 'sunspecUrl',
ipMasks: sunSpecManager.ipMasks,
bdpvActive: false,
bdpvUsername: 'bdpvUsername',
bdpvApiKey: 'bdpvApiKey',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('SunSpec getDevices', () => {
},
};

sunSpecManager = new SunSpecManager(gladys, ModbusTCPMock, SERVICE_ID);
sunSpecManager = new SunSpecManager(gladys, ModbusTCPMock, null, SERVICE_ID);
});

it('should no device', async () => {
Expand Down
2 changes: 1 addition & 1 deletion server/test/services/sunspec/lib/sunspec.getStatus.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('SunSpec getStatus', () => {
beforeEach(() => {
gladys = {};

sunSpecManager = new SunSpecManager(gladys, ModbusTCPMock, SERVICE_ID);
sunSpecManager = new SunSpecManager(gladys, ModbusTCPMock, null, SERVICE_ID);
});

it('should connected', async () => {
Expand Down
Loading

0 comments on commit 04456c1

Please sign in to comment.