Skip to content

Commit

Permalink
Promise onExecute
Browse files Browse the repository at this point in the history
  • Loading branch information
atrovato committed May 27, 2023
1 parent e192614 commit 855a64d
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 100 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const Promise = require('bluebird');

const logger = require('../../../../utils/logger');
const { ACTIONS, ACTIONS_STATUS, EVENTS } = require('../../../../utils/constants');

Expand All @@ -7,77 +9,65 @@ const { TRAIT_BY_COMMAND } = require('../traits');
* @description The function that will run for an EXECUTE request.
* It should return a valid response or a Promise that resolves to valid response.
* @param {object} body - Request body.
* @returns {object} A valid response.
* @returns {Promise<object>} A valid response.
* @example
* googleActions.onExecute({});
* await googleActions.onExecute({});
* @see https://actions-on-google.github.io/actions-on-google-nodejs/interfaces/smarthome.smarthomeapp.html#onexecute
*/
function onExecute(body) {
async function onExecute(body) {
const commands = [];

body.inputs
.filter((input) => input.payload && input.payload.commands)
.forEach((input) => {
input.payload.commands
.filter((command) => {
return command.devices && command.devices.length > 0;
})
.forEach((command) => {
const { devices, execution } = command;
const requestedDevices = devices
.map((device) => {
// Load related device
const gladysDevice = this.gladys.stateManager.get('device', device.id);

if (!gladysDevice) {
commands.push({ ids: [device.id], status: 'ERROR' });
}

return gladysDevice;
})
.filter(Boolean);
const inputCommands = body.inputs
.filter((input) => input.payload)
.map((input) => input.payload)
.map((payload) => payload.commands)
.filter(Boolean)
.flatMap((command) => command);

requestedDevices.forEach((device) => {
// Each execution triggered
execution.forEach((exec) => {
const trait = TRAIT_BY_COMMAND[exec.command];
const deviceStatus = { ids: [device.selector], status: 'ERROR' };
await Promise.each(inputCommands, async ({ devices = [], execution }) => {
await Promise.each(devices, async (device) => {
const { id: selector } = device;
// Load related device
const gladysDevice = this.gladys.stateManager.get('device', selector);

if (!trait || !trait.commands[exec.command]) {
// Command key not found
logger.error(`GoogleActions "${exec.command}" command is not managed.`);
// All devices are failure
deviceStatus.status = 'ERROR';
} else {
const commandExecutor = trait.commands[exec.command];
if (!gladysDevice) {
commands.push({ ids: [selector], status: 'ERROR' });
} else {
// Each execution triggered
await Promise.each(execution, async (exec) => {
const trait = TRAIT_BY_COMMAND[exec.command];

// Build related feature events according incomping attributes
const { events = [], states } = commandExecutor(device, exec.params);
if (!trait || !trait.commands[exec.command]) {
// Command key not found
logger.error(`GoogleActions "${exec.command}" command is not managed.`);
// All devices are failure
commands.push({ ids: [selector], status: 'ERROR' });
} else {
const commandExecutor = trait.commands[exec.command];

if (events.length > 0) {
events.forEach((eventMessage) => {
const action = {
type: ACTIONS.DEVICE.SET_VALUE,
status: ACTIONS_STATUS.PENDING,
device: device.selector,
...eventMessage,
};
this.gladys.event.emit(EVENTS.ACTION.TRIGGERED, action);
deviceStatus.status = 'PENDING';
});
}
const deviceStatus = { ids: [selector], status: 'ERROR' };
// Build related feature events according incomping attributes
const { events = [] } = await commandExecutor(gladysDevice, exec.params, this.gladys);

if (states) {
deviceStatus.status = 'SUCCESS';
deviceStatus.states = states;
}
}
if (events.length > 0) {
events.forEach((eventMessage) => {
const action = {
type: ACTIONS.DEVICE.SET_VALUE,
status: ACTIONS_STATUS.PENDING,
device: selector,
...eventMessage,
};
this.gladys.event.emit(EVENTS.ACTION.TRIGGERED, action);
});
deviceStatus.status = 'PENDING';
}

commands.push(deviceStatus);
});
});
commands.push(deviceStatus);
}
});
}
});
});

return {
requestId: body.requestId,
Expand Down
27 changes: 12 additions & 15 deletions server/services/google-actions/package-lock.json

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

4 changes: 2 additions & 2 deletions server/services/google-actions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"arm64"
],
"dependencies": {
"set-value": "^4.0.1",
"uuid": "^9.0.0"
"bluebird": "^3.7.2",
"set-value": "^4.0.1"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ describe('GoogleActions Handler - onSync - brightness (light)', () => {
assert.notCalled(gladys.event.emit);
});

it('should emit Gladys event with new value - onExecute', () => {
const result = googleActionsHandler.onExecute(body);
it('should emit Gladys event with new value - onExecute', async () => {
const result = await googleActionsHandler.onExecute(body);

const expectedResult = {
requestId: 'request-id',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ describe('GoogleActions Handler - onSync - brightness (switch)', () => {
assert.notCalled(gladys.event.emit);
});

it('should emit Gladys event with new value - onExecute', () => {
const result = googleActionsHandler.onExecute(body);
it('should emit Gladys event with new value - onExecute', async () => {
const result = await googleActionsHandler.onExecute(body);

const expectedResult = {
requestId: 'request-id',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ describe('GoogleActions Handler - onSync - color', () => {
});

it('should emit Gladys event with new value - onExecute', async () => {
const result = googleActionsHandler.onExecute(body);
const result = await googleActionsHandler.onExecute(body);

const expectedResult = {
requestId: 'request-id',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ describe('GoogleActions Handler - onSync - color', () => {
assert.notCalled(gladys.event.emit);
});

it('should emit Gladys event with new value - onExecute', () => {
const result = googleActionsHandler.onExecute(body);
it('should emit Gladys event with new value - onExecute', async () => {
const result = await googleActionsHandler.onExecute(body);

const expectedResult = {
requestId: 'request-id',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ describe('GoogleActions Handler - onSync - openClose - curtain', () => {
assert.notCalled(gladys.event.emit);
});

it('should emit Gladys event with new value - onExecute', () => {
const result = googleActionsHandler.onExecute(body);
it('should emit Gladys event with new value - onExecute', async () => {
const result = await googleActionsHandler.onExecute(body);

const expectedResult = {
requestId: 'request-id',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ describe('GoogleActions Handler - onSync - openClose - shutter', () => {
assert.notCalled(gladys.event.emit);
});

it('should emit Gladys event with new value - onExecute', () => {
const result = googleActionsHandler.onExecute(body);
it('should emit Gladys event with new value - onExecute', async () => {
const result = await googleActionsHandler.onExecute(body);

const expectedResult = {
requestId: 'request-id',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe('GoogleActions Handler - onExecute', () => {
sinon.reset();
});

it('should do nothing - empty payload', () => {
it('should do nothing - empty payload', async () => {
const body = {
requestId: 'request-id',
user: {
Expand All @@ -47,7 +47,7 @@ describe('GoogleActions Handler - onExecute', () => {
inputs: [],
};

const result = googleActionsHandler.onExecute(body);
const result = await googleActionsHandler.onExecute(body);

const exptectedResult = {
requestId: 'request-id',
Expand All @@ -62,7 +62,7 @@ describe('GoogleActions Handler - onExecute', () => {
assert.notCalled(gladys.event.emit);
});

it('should do nothing - empty commands', () => {
it('should do nothing - empty commands', async () => {
const body = {
requestId: 'request-id',
user: {
Expand All @@ -79,7 +79,7 @@ describe('GoogleActions Handler - onExecute', () => {
],
};

const result = googleActionsHandler.onExecute(body);
const result = await googleActionsHandler.onExecute(body);

const exptectedResult = {
requestId: 'request-id',
Expand All @@ -93,7 +93,7 @@ describe('GoogleActions Handler - onExecute', () => {
assert.notCalled(gladys.event.emit);
});

it('should do nothing - empty devices', () => {
it('should do nothing - empty devices', async () => {
const body = {
requestId: 'request-id',
user: {
Expand All @@ -112,7 +112,7 @@ describe('GoogleActions Handler - onExecute', () => {
],
};

const result = googleActionsHandler.onExecute(body);
const result = await googleActionsHandler.onExecute(body);

const exptectedResult = {
requestId: 'request-id',
Expand All @@ -126,7 +126,7 @@ describe('GoogleActions Handler - onExecute', () => {
assert.notCalled(gladys.event.emit);
});

it('should send errorneous device - unkonwn device', () => {
it('should send errorneous device - unkonwn device', async () => {
gladys.stateManager.get = fake.returns(null);

const body = {
Expand All @@ -148,7 +148,7 @@ describe('GoogleActions Handler - onExecute', () => {
],
};

const result = googleActionsHandler.onExecute(body);
const result = await googleActionsHandler.onExecute(body);

const exptectedResult = {
requestId: 'request-id',
Expand All @@ -167,7 +167,7 @@ describe('GoogleActions Handler - onExecute', () => {
assert.notCalled(gladys.event.emit);
});

it('should send errorneous device - command not managed', () => {
it('should send errorneous device - command not managed', async () => {
const body = {
requestId: 'request-id',
user: {
Expand All @@ -192,7 +192,7 @@ describe('GoogleActions Handler - onExecute', () => {
],
};

const result = googleActionsHandler.onExecute(body);
const result = await googleActionsHandler.onExecute(body);

const exptectedResult = {
requestId: 'request-id',
Expand All @@ -211,7 +211,7 @@ describe('GoogleActions Handler - onExecute', () => {
assert.notCalled(gladys.event.emit);
});

it('should send errorneous device - no events generated', () => {
it('should send errorneous device - no events generated', async () => {
gladys.stateManager.get = fake.returns({
selector: 'device-1',
features: [],
Expand Down Expand Up @@ -244,7 +244,7 @@ describe('GoogleActions Handler - onExecute', () => {
],
};

const result = googleActionsHandler.onExecute(body);
const result = await googleActionsHandler.onExecute(body);

const exptectedResult = {
requestId: 'request-id',
Expand All @@ -263,7 +263,7 @@ describe('GoogleActions Handler - onExecute', () => {
assert.notCalled(gladys.event.emit);
});

it('onExecute - success', () => {
it('should emit event - onExecute', async () => {
const body = {
requestId: 'request-id',
user: {
Expand Down Expand Up @@ -291,7 +291,7 @@ describe('GoogleActions Handler - onExecute', () => {
],
};

const result = googleActionsHandler.onExecute(body);
const result = await googleActionsHandler.onExecute(body);

const exptectedResult = {
requestId: 'request-id',
Expand Down

0 comments on commit 855a64d

Please sign in to comment.