From 81f354e7fc6d70f0ef940cb8fd406d013578db30 Mon Sep 17 00:00:00 2001 From: atrovato <1839717+atrovato@users.noreply.github.com> Date: Mon, 2 Nov 2020 11:24:14 +0100 Subject: [PATCH] Add internal "created device" events Fixes #911 --- server/lib/device/device.notify.js | 12 ++-- server/test/lib/device/device.notify.test.js | 65 ++++++++++++++------ 2 files changed, 55 insertions(+), 22 deletions(-) diff --git a/server/lib/device/device.notify.js b/server/lib/device/device.notify.js index e4f4f9c25a..08159709a4 100644 --- a/server/lib/device/device.notify.js +++ b/server/lib/device/device.notify.js @@ -4,9 +4,9 @@ const logger = require('../../utils/logger'); const { EVENTS } = require('../../utils/constants'); const FUNC_BY_EVENT = { - [EVENTS.DEVICE.CREATE]: 'onNewDevice', - [EVENTS.DEVICE.UPDATE]: 'onUpdateDevice', - [EVENTS.DEVICE.DELETE]: 'onDeleteDevice', + [EVENTS.DEVICE.CREATE]: 'postCreate', + [EVENTS.DEVICE.UPDATE]: 'postUpdate', + [EVENTS.DEVICE.DELETE]: 'postDelete', }; /** @@ -34,7 +34,11 @@ function notify(device, event) { } else if (typeof get(service, `device.${serviceFuncName}`) !== 'function') { logger.info(`Function device.${serviceFuncName} in service ${service.name} does not exist.`); } else { - service.device[serviceFuncName](device); + try { + service.device[serviceFuncName](device); + } catch (e) { + logger.error(`Failed to execute device.${serviceFuncName} function on service ${device.name}:`, e); + } } } } diff --git a/server/test/lib/device/device.notify.test.js b/server/test/lib/device/device.notify.test.js index 6ce27e35a1..af887d60bb 100644 --- a/server/test/lib/device/device.notify.test.js +++ b/server/test/lib/device/device.notify.test.js @@ -23,9 +23,9 @@ describe('Device.notify', () => { const serviceManager = new ServiceManager({}, stateManager); const service = { device: { - onNewDevice: fake.returns(null), - onUpdateDevice: fake.returns(null), - onDeleteDevice: fake.returns(null), + postCreate: fake.returns(null), + postUpdate: fake.returns(null), + postDelete: fake.returns(null), }, }; @@ -42,9 +42,38 @@ describe('Device.notify', () => { device.notify(newDevice, EVENTS.DEVICE.CREATE); assert.calledWith(event.emit, EVENTS.DEVICE.CREATE, newDevice); - assert.calledWith(service.device.onNewDevice, newDevice); - assert.notCalled(service.device.onUpdateDevice); - assert.notCalled(service.device.onDeleteDevice); + assert.calledWith(service.device.postCreate, newDevice); + assert.notCalled(service.device.postUpdate); + assert.notCalled(service.device.postDelete); + }); + + it('should notify service on device errorneous creation', async () => { + const stateManager = new StateManager(event); + const serviceManager = new ServiceManager({}, stateManager); + const service = { + device: { + postCreate: fake.rejects(null), + postUpdate: fake.returns(null), + postDelete: fake.returns(null), + }, + }; + + const serviceId = 'a810b8db-6d04-4697-bed3-c4b72c996279'; + stateManager.setState('serviceById', serviceId, service); + + const device = new Device(event, messageManager, stateManager, serviceManager); + const newDevice = { + service_id: serviceId, + name: 'Philips Hue 1', + external_id: 'philips-hue-new', + }; + + device.notify(newDevice, EVENTS.DEVICE.CREATE); + + assert.calledWith(event.emit, EVENTS.DEVICE.CREATE, newDevice); + assert.calledWith(service.device.postCreate, newDevice); + assert.notCalled(service.device.postUpdate); + assert.notCalled(service.device.postDelete); }); it('should notify service on device update', async () => { @@ -52,9 +81,9 @@ describe('Device.notify', () => { const serviceManager = new ServiceManager({}, stateManager); const service = { device: { - onNewDevice: fake.returns(null), - onUpdateDevice: fake.returns(null), - onDeleteDevice: fake.returns(null), + postCreate: fake.returns(null), + postUpdate: fake.returns(null), + postDelete: fake.returns(null), }, }; @@ -71,9 +100,9 @@ describe('Device.notify', () => { device.notify(newDevice, EVENTS.DEVICE.UPDATE); assert.calledWith(event.emit, EVENTS.DEVICE.UPDATE, newDevice); - assert.notCalled(service.device.onNewDevice); - assert.calledWith(service.device.onUpdateDevice, newDevice); - assert.notCalled(service.device.onDeleteDevice); + assert.notCalled(service.device.postCreate); + assert.calledWith(service.device.postUpdate, newDevice); + assert.notCalled(service.device.postDelete); }); it('should notify service on device deletion', async () => { @@ -81,9 +110,9 @@ describe('Device.notify', () => { const serviceManager = new ServiceManager({}, stateManager); const service = { device: { - onNewDevice: fake.returns(null), - onUpdateDevice: fake.returns(null), - onDeleteDevice: fake.returns(null), + postCreate: fake.returns(null), + postUpdate: fake.returns(null), + postDelete: fake.returns(null), }, }; @@ -100,9 +129,9 @@ describe('Device.notify', () => { device.notify(newDevice, EVENTS.DEVICE.DELETE); assert.calledWith(event.emit, EVENTS.DEVICE.DELETE, newDevice); - assert.calledWith(service.device.onDeleteDevice, newDevice); - assert.notCalled(service.device.onNewDevice); - assert.notCalled(service.device.onUpdateDevice); + assert.calledWith(service.device.postDelete, newDevice); + assert.notCalled(service.device.postCreate); + assert.notCalled(service.device.postUpdate); }); it('should notify service on device creation, but no service', async () => {