forked from cakebake/node-red-contrib-alexa-remote-cakebaked
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathalexa-remote-list.js
53 lines (43 loc) · 2.32 KB
/
alexa-remote-list.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const tools = require('../lib/common.js');
module.exports = function (RED) {
function AlexaRemoteList(input) {
RED.nodes.createNode(this, input);
tools.assign(this, ['config', 'outputs'], input);
tools.assignNode(RED, this, ['account'], input);
if(!tools.nodeSetup(this, input, true)) return;
this.on('input', function (msg) {
const send = tools.nodeGetSendCb(this, msg);
const error = tools.nodeGetErrorCb(this);
if(this.account.state.code !== 'READY') return error('account not initialised');
this.status({ shape: 'dot', fill: 'grey', text: 'sending' });
const alexa = this.account.alexa;
const config = tools.nodeEvaluateProperties(RED, this, msg, this.config);
if(!tools.matches(config, { option: '', value: undefined })) return error(`invalid input: "${JSON.stringify(config)}"`);
const {option, value} = config;
switch(option) {
case 'getLists':
return alexa.getListsPromise().then(send).catch(error);
case 'getList':
if(!tools.matches(value, { list: '' })) return error(`expected a string 'list'`);
return alexa.getListPromise(value.list).then(send).catch(error);
case 'getListItems':
if(!tools.matches(value, { list: '' })) return error(`expected a string 'list'`);
return alexa.getListItemsPromise(value.list).then(send).catch(error);
case 'addItem':
if(!tools.matches(value, { list: '', text: '' })) return error(`expected a string 'list' and 'text'`);
return alexa.addListItemPromise(value.list, value.text).then(send).catch(error);
case 'editItem':
if(!tools.matches(value, { list: '', item: '', text: '', completed: undefined, version: 0 })) return error(`expected a string 'list', 'item', 'text' and number 'version'`);
let options = { value: value.text, version: value.version };
if(typeof value.completed === 'boolean') options.completed = value.completed;
return alexa.updateListItemPromise(value.list, value.item, options).then(send).catch(error);
case 'removeItem':
if(!tools.matches(value, { list: '', item: '' })) return error(`expected a string 'list' and 'item'`);
return alexa.deleteListItemPromise(value.list, value.item).then(send).catch(error);
default:
return error('invalid option');
}
});
}
RED.nodes.registerType("alexa-remote-list", AlexaRemoteList);
};