From f86b76ff4ab858ecbbc59d42dc2f8c09ab2a8de2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Escandell?= Date: Thu, 14 Nov 2019 23:42:34 +0100 Subject: [PATCH 1/3] Various changes: zWave device integration and BinaryDeviceFeature component fixes --- .../device-in-room/device-features/BinaryDeviceFeature.jsx | 2 +- package-lock.json | 2 +- server/lib/device/device.setValue.js | 2 +- server/lib/gateway/gateway.forwardWebsockets.js | 6 ++++++ server/services/zwave/index.js | 1 + server/services/zwave/lib/index.js | 2 ++ 6 files changed, 12 insertions(+), 3 deletions(-) diff --git a/front/src/components/boxs/device-in-room/device-features/BinaryDeviceFeature.jsx b/front/src/components/boxs/device-in-room/device-features/BinaryDeviceFeature.jsx index 1c477e9b5e..3a0cc416dc 100644 --- a/front/src/components/boxs/device-in-room/device-features/BinaryDeviceFeature.jsx +++ b/front/src/components/boxs/device-in-room/device-features/BinaryDeviceFeature.jsx @@ -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 ); } diff --git a/package-lock.json b/package-lock.json index 07fd9952d2..c2e7e1409e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "gladys", - "version": "4.0.0", + "version": "4.0.0-beta", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/server/lib/device/device.setValue.js b/server/lib/device/device.setValue.js index 7c0c83bef4..b3069b5c60 100644 --- a/server/lib/device/device.setValue.js +++ b/server/lib/device/device.setValue.js @@ -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); } } diff --git a/server/lib/gateway/gateway.forwardWebsockets.js b/server/lib/gateway/gateway.forwardWebsockets.js index a55e8c31a4..2139196e3c 100644 --- a/server/lib/gateway/gateway.forwardWebsockets.js +++ b/server/lib/gateway/gateway.forwardWebsockets.js @@ -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) { diff --git a/server/services/zwave/index.js b/server/services/zwave/index.js index 78010f35f4..137cb8a4e7 100644 --- a/server/services/zwave/index.js +++ b/server/services/zwave/index.js @@ -35,6 +35,7 @@ module.exports = function ZwaveService(gladys, serviceId) { return Object.freeze({ start, stop, + device: zwaveManager, controllers: ZwaveController(gladys, zwaveManager, serviceId), }); }; diff --git a/server/services/zwave/lib/index.js b/server/services/zwave/lib/index.js index f1f2b8572e..e6c1b8e1b6 100644 --- a/server/services/zwave/lib/index.js +++ b/server/services/zwave/lib/index.js @@ -22,6 +22,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, @@ -74,5 +75,6 @@ ZwaveManager.prototype.getInfos = getInfos; ZwaveManager.prototype.getNodes = getNodes; ZwaveManager.prototype.getNodeNeighbors = getNodeNeighbors; ZwaveManager.prototype.removeNode = removeNode; +ZwaveManager.prototype.setValue = setValue; module.exports = ZwaveManager; From 9df3f03cbf5b0baf7e3720ba028c0782de720a7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Escandell?= Date: Fri, 15 Nov 2019 00:29:46 +0100 Subject: [PATCH 2/3] Add test --- server/test/lib/gateway/gateway.test.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/server/test/lib/gateway/gateway.test.js b/server/test/lib/gateway/gateway.test.js index a472c507f5..913dfde8d7 100644 --- a/server/test/lib/gateway/gateway.test.js +++ b/server/test/lib/gateway/gateway.test.js @@ -136,9 +136,9 @@ describe('gateway', () => { }); }); describe('gateway.forwardWebsockets', () => { - it('should forward a websocket message', async () => { + it('should forward a websocket message when connected', async () => { 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: {}, @@ -146,6 +146,16 @@ describe('gateway', () => { gateway.forwardWebsockets(websocketMessage); assert.calledWith(gateway.gladysGatewayClient.newEventInstance, websocketMessage.type, websocketMessage.payload); }); + it('should prevent forwarding a websocket message when not connected', async () => { + 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', () => { From ee045cb4d3de358e1c9bc92d41d43d771824806c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Escandell?= Date: Fri, 15 Nov 2019 22:42:55 +0100 Subject: [PATCH 3/3] Fix tests... actually Mock were broken: using spy on prototype should not be authorized. --- .../gateway/GladysGatewayClientMock.test.js | 101 ++++++++---------- server/test/lib/gateway/gateway.test.js | 5 +- 2 files changed, 50 insertions(+), 56 deletions(-) diff --git a/server/test/lib/gateway/GladysGatewayClientMock.test.js b/server/test/lib/gateway/GladysGatewayClientMock.test.js index 990908353b..8932437d8a 100644 --- a/server/test/lib/gateway/GladysGatewayClientMock.test.js +++ b/server/test/lib/gateway/GladysGatewayClientMock.test.js @@ -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; diff --git a/server/test/lib/gateway/gateway.test.js b/server/test/lib/gateway/gateway.test.js index 913dfde8d7..5ca5736e19 100644 --- a/server/test/lib/gateway/gateway.test.js +++ b/server/test/lib/gateway/gateway.test.js @@ -136,9 +136,10 @@ describe('gateway', () => { }); }); describe('gateway.forwardWebsockets', () => { - it('should forward a websocket message when connected', async () => { + it('should forward a websocket message when connected', () => { const gateway = new Gateway({}, event, system, sequelize, config); gateway.connected = true; + const websocketMessage = { type: 'zwave.new-node', payload: {}, @@ -146,7 +147,7 @@ describe('gateway', () => { gateway.forwardWebsockets(websocketMessage); assert.calledWith(gateway.gladysGatewayClient.newEventInstance, websocketMessage.type, websocketMessage.payload); }); - it('should prevent forwarding a websocket message when not connected', async () => { + it('should prevent forwarding a websocket message when not connected', () => { const gateway = new Gateway({}, event, system, sequelize, config); const websocketMessage = {