-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaction.js
104 lines (98 loc) · 3.56 KB
/
action.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* Copyright (c) 2017
* Qblinks Incorporated ("Qblinks").
* All rights reserved.
*
* The information contained herein is confidential and proprietary to
* Qblinks. Use of this information by anyone other than authorized employees
* of Qblinks is granted only under a written non-disclosure agreement,
* expressly prescribing the scope and manner of such use.
*/
'use strict';
const request = require('request');
/**
* [action description]
* @param {object} option Plug action properties
* @return {bool} seccess or fail
*/
function action(options, callback) {
let onoff = '';
if (options.action.onoff === true) {
onoff = 'on';
} else if (options.action.onoff === false) {
onoff = 'off';
} else if (options.action.toggle === true && typeof options.xim_content.toggle !== 'undefined') {
Object.keys(options.xim_content.toggle).forEach((key) => {
if (options.xim_content.toggle[key].device_id === options.device_id) {
if (options.xim_content.toggle[key].onoff === true) {
onoff = 'off';
} else {
onoff = 'on';
}
}
});
} else if (options.action.momentum === true) {
onoff = 'onoff';
}
const opt = {
method: 'PUT',
url: `${options.xim_content.uri}/outletAction/${options.device_id}/${onoff}`,
headers: {
Authorization: `Bearer ${options.xim_content.access_token}`,
},
};
request(opt, (error, response) => {
const callback_option = JSON.parse(JSON.stringify(options));
callback_option.result = {};
if (typeof callback_option.xim_content === 'undefined' || typeof callback_option.xim_content === 'undefined') {
callback_option.result.err_no = 999;
callback_option.result.err_msg = 'access token undefined';
} else if (typeof callback_option.xim_content.uri === 'undefined') {
callback_option.result.err_no = 999;
callback_option.result.err_msg = 'uri undefined';
} else if (typeof callback_option.device_id === 'undefined') {
callback_option.result.err_no = 999;
callback_option.result.err_msg = 'device id undefined';
} else if (response.statusCode !== 204) {
callback_option.result.err_no = 999;
callback_option.result.err_msg = 'action fail';
} else if (typeof callback_option.xim_content.toggle === 'undefined') {
if (onoff !== '') {
callback_option.xim_content.toggle = [];
const toggle = {};
toggle.device_id = options.device_id;
toggle.onoff = options.action.onoff;
callback_option.xim_content.toggle.push(toggle);
callback_option.result.err_no = 0;
callback_option.result.err_msg = 'ok';
} else {
callback_option.result.err_no = 999;
callback_option.result.err_msg = 'toggle undefined';
}
} else {
Object.keys(callback_option.xim_content.toggle).forEach((key) => {
if (callback_option.xim_content.toggle[key].device_id === options.device_id) {
if (onoff === 'on') {
callback_option.xim_content.toggle[key].onoff = true;
} else {
callback_option.xim_content.toggle[key].onoff = false;
}
} else {
const toggle = {};
toggle.device_id = options.device_id;
toggle.onoff = options.action.onoff;
callback_option.xim_content.toggle.push(toggle);
}
});
callback_option.result.err_no = 0;
callback_option.result.err_msg = 'ok';
}
delete callback_option.device_id;
delete callback_option.action;
callback(callback_option);
});
}
/**
* functions exporting
*/
module.exports = action;