Skip to content

Commit

Permalink
Multiple fixes on Z-Wave device integration, BinaryDeviceFeature comp…
Browse files Browse the repository at this point in the history
…onent and Gateway (#608)

* Various changes: zWave device integration and BinaryDeviceFeature component fixes

* Add test

* Fix tests... actually Mock were broken: using spy on prototype should not be authorized.
  • Loading branch information
sescandell authored and Pierre-Gilles committed Nov 20, 2019
1 parent 5685b45 commit 09c8732
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const BinaryDeviceType = ({ children, ...props }) => {
props.deviceFeature,
props.deviceIndex,
props.deviceFeatureIndex,
props.deviceFeature.last_value === 1 ? 0 : 1
!props.deviceFeature.last_value
);
}

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion server/lib/device/device.setValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ async function setValue(device, deviceFeature, value) {
throw new NotFoundError(`Function device.setValue in service ${device.service.name} does not exist.`);
}
await service.device.setValue(device, deviceFeature, value);
if (!deviceFeature.has_state_feedback) {
if (!deviceFeature.has_feedback) {
await this.saveState(deviceFeature, value);
}
}
Expand Down
6 changes: 6 additions & 0 deletions server/lib/gateway/gateway.forwardWebsockets.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ const logger = require('../../utils/logger');
* });
*/
async function forwardWebsockets(event) {
if (!this.connected) {
logger.debug('Gateway: not connected. Prevent forwarding event.');

return;
}

logger.debug(`Gateway : Forward websocket message : ${event.type}`);
try {
if (event.userId) {
Expand Down
1 change: 1 addition & 0 deletions server/services/zwave/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ module.exports = function ZwaveService(gladys, serviceId) {
return Object.freeze({
start,
stop,
device: zwaveManager,
controllers: ZwaveController(gladys, zwaveManager, serviceId),
});
};
2 changes: 2 additions & 0 deletions server/services/zwave/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const { getInfos } = require('./commands/zwave.getInfos');
const { getNodes } = require('./commands/zwave.getNodes');
const { getNodeNeighbors } = require('./commands/zwave.getNodeNeighbors');
const { removeNode } = require('./commands/zwave.removeNode');
const { setValue } = require('./commands/zwave.setValue');

const DEFAULT_ZWAVE_OPTIONS = {
Logging: false,
Expand Down Expand Up @@ -81,5 +82,6 @@ ZwaveManager.prototype.getInfos = getInfos;
ZwaveManager.prototype.getNodes = getNodes;
ZwaveManager.prototype.getNodeNeighbors = getNodeNeighbors;
ZwaveManager.prototype.removeNode = removeNode;
ZwaveManager.prototype.setValue = setValue;

module.exports = ZwaveManager;
101 changes: 47 additions & 54 deletions server/test/lib/gateway/GladysGatewayClientMock.test.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,53 @@
const fs = require('fs');
const { fake } = require('sinon');

class GladysGatewayClientMock {}

GladysGatewayClientMock.prototype.login = fake.resolves({
two_factor_token: 'token',
});

// AUTHENTICATION
GladysGatewayClientMock.prototype.loginInstance = fake.resolves({});
GladysGatewayClientMock.prototype.createInstance = fake.resolves({
instance: {
id: '25239392-debf-40c9-9363-fc8d3b9ebbbe',
refresh_token: 'token',
},
rsaPrivateKeyJwk: {},
ecdsaPrivateKeyJwk: {},
rsaPublicKeyJwk: {},
ecdsaPublicKeyJwk: {},
});
GladysGatewayClientMock.prototype.instanceConnect = fake.resolves({});

// BACKUPS
GladysGatewayClientMock.prototype.uploadBackup = fake.resolves({
success: true,
});

GladysGatewayClientMock.prototype.getBackups = fake.resolves([
{
id: '74dc8d58-3997-484a-a791-53e5b07279d7',
account_id: 'b2d23f66-487d-493f-8acb-9c8adb400def',
path: 'http://backup-url',
size: 1000,
created_at: '2018-10-16T02:21:25.901Z',
updated_at: '2018-10-16T02:21:25.901Z',
is_deleted: false,
},
]);

GladysGatewayClientMock.prototype.downloadBackup = (backupUrl, writeStream) => {
const readStream = fs.createReadStream(backupUrl);
readStream.pipe(writeStream);
return new Promise((resolve, reject) => {
writeStream.on('finish', resolve);
writeStream.on('error', reject);
});
const GladysGatewayClientMock = function() {
return {
login: fake.resolves({
two_factor_token: 'token',
}),
loginInstance: fake.resolves({}),
createInstance: fake.resolves({
instance: {
id: '25239392-debf-40c9-9363-fc8d3b9ebbbe',
refresh_token: 'token',
},
rsaPrivateKeyJwk: {},
ecdsaPrivateKeyJwk: {},
rsaPublicKeyJwk: {},
ecdsaPublicKeyJwk: {},
}),
instanceConnect: fake.resolves({}),
uploadBackup: fake.resolves({
success: true,
}),
getBackups: fake.resolves([
{
id: '74dc8d58-3997-484a-a791-53e5b07279d7',
account_id: 'b2d23f66-487d-493f-8acb-9c8adb400def',
path: 'http://backup-url',
size: 1000,
created_at: '2018-10-16T02:21:25.901Z',
updated_at: '2018-10-16T02:21:25.901Z',
is_deleted: false,
},
]),
downloadBackup: (backupUrl, writeStream) => {
const readStream = fs.createReadStream(backupUrl);
readStream.pipe(writeStream);
return new Promise((resolve, reject) => {
writeStream.on('finish', resolve);
writeStream.on('error', reject);
});
},
getLatestGladysVersion: fake.resolves({
name: 'v4.0.0-alpha',
created_at: '2018-10-16T02:21:25.901Z',
}),
disconnect: fake.returns(null),
newEventInstance: fake.returns(null),
generateFingerprint: fake.resolves('fingerprint'),
};
};

GladysGatewayClientMock.prototype.getLatestGladysVersion = fake.resolves({
name: 'v4.0.0-alpha',
created_at: '2018-10-16T02:21:25.901Z',
});

GladysGatewayClientMock.prototype.disconnect = fake.returns(null);

GladysGatewayClientMock.prototype.newEventInstance = fake.returns(null);
GladysGatewayClientMock.prototype.generateFingerprint = fake.resolves('fingerprint');

module.exports = GladysGatewayClientMock;
15 changes: 13 additions & 2 deletions server/test/lib/gateway/gateway.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,16 +136,27 @@ describe('gateway', () => {
});
});
describe('gateway.forwardWebsockets', () => {
it('should forward a websocket message', async () => {
it('should forward a websocket message when connected', () => {
const gateway = new Gateway({}, event, system, sequelize, config);
await gateway.login('tony.stark@gladysassistant.com', 'warmachine123');
gateway.connected = true;

const websocketMessage = {
type: 'zwave.new-node',
payload: {},
};
gateway.forwardWebsockets(websocketMessage);
assert.calledWith(gateway.gladysGatewayClient.newEventInstance, websocketMessage.type, websocketMessage.payload);
});
it('should prevent forwarding a websocket message when not connected', () => {
const gateway = new Gateway({}, event, system, sequelize, config);

const websocketMessage = {
type: 'zwave.new-node',
payload: {},
};
gateway.forwardWebsockets(websocketMessage);
assert.notCalled(gateway.gladysGatewayClient.newEventInstance);
});
});

describe('gateway.handleNewMessage', () => {
Expand Down

0 comments on commit 09c8732

Please sign in to comment.