Skip to content

Commit

Permalink
Add internal "created device" events
Browse files Browse the repository at this point in the history
  • Loading branch information
atrovato committed Nov 5, 2020
1 parent 34dddaf commit 81f354e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 22 deletions.
12 changes: 8 additions & 4 deletions server/lib/device/device.notify.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
};

/**
Expand Down Expand Up @@ -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);
}
}
}
}
Expand Down
65 changes: 47 additions & 18 deletions server/test/lib/device/device.notify.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
};

Expand All @@ -42,19 +42,48 @@ 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 () => {
const stateManager = new StateManager(event);
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),
},
};

Expand All @@ -71,19 +100,19 @@ 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 () => {
const stateManager = new StateManager(event);
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),
},
};

Expand All @@ -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 () => {
Expand Down

0 comments on commit 81f354e

Please sign in to comment.