-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.js
156 lines (146 loc) · 5.25 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* 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');
function arrayContain(arr, obj) {
return (arr.indexOf(obj) > -1);
}
function isEmpty(obj) {
if (!obj) {
return true;
}
if (!(typeof (obj) === 'number') && !Object.keys(obj).length) {
return true;
}
return false;
}
function execAction(access_token, isStructure, id, actionBody, actionTemperature, callback) {
const options = {
method: 'PUT',
followAllRedirects: true, // Redirects are turned on by default for GET requests only
url: `https://developer-api.nest.com/devices/thermostats/${id}`,
headers: {
'content-type': 'application/json',
Authorization: `Bearer ${access_token}`,
},
json: true,
body: actionBody, // body is jsonObject
};
if (isStructure) {
options.url = `https://developer-api.nest.com/structures/${id}`;
}
console.log(options.url);
console.log(options.body);
request(options, (error, response, body) => {
console.log(body);
if (!error && response.statusCode === 200) {
if (!isEmpty(actionTemperature)) {
execAction(access_token, false, id, actionTemperature, {}, callback);
return;
}
callback(true);
} else {
callback(body);
}
});
}
/**
* [action description]
* @param {object} opt thermostat action properties
* @return {bool} seccess or fail
*/
function action(opt, callback) {
const callback_opt = JSON.parse(JSON.stringify(opt));
delete callback_opt.device_id;
delete callback_opt.action;
callback_opt.result = {};
if (typeof opt.xim_content === 'undefined') {
callback_opt.result.err_no = 108;
callback_opt.result.err_msg = 'No XIM Content';
} else if (typeof opt.xim_content.access_token === 'undefined') {
callback_opt.result.err_no = 113;
callback_opt.result.err_msg = 'No Access Token';
// } else if (typeof opt.xim_content.structures === 'undefined') {
// callback_opt.result.err_no = 999;
// callback_opt.result.err_msg = 'Please redo discovery.';
} else {
// restructure action array, properties key may be different.
// mode -> hvac_mode
// homeaway -> away, set in isStructure
// swith mode before set temperature, do double request
const actionBody = {};
let actionTemperature = {};
let isStructure = false;
if (typeof opt.action.homeaway !== 'undefined') {
// if there is homeaway in action, set to structure api
actionBody.away = opt.action.homeaway;
isStructure = true;
} else if (typeof opt.action.mode !== 'undefined') {
actionBody.hvac_mode = opt.action.mode;
} else if (typeof opt.action.heat !== 'undefined') {
actionBody.hvac_mode = 'heat';
actionTemperature = opt.action.heat;
// Object.assign(actionBody, opt.action.heat);
} else if (typeof opt.action.cool !== 'undefined') {
actionBody.hvac_mode = 'cool';
actionTemperature = opt.action.cool;
// Object.assign(actionBody, opt.action.cool);
} else if (typeof opt.action['heat-cool'] !== 'undefined') {
actionBody.hvac_mode = 'heat-cool';
actionTemperature = opt.action['heat-cool'];
// Object.assign(actionBody, opt.action['heat-cool']);
} else if (typeof opt.action.eco !== 'undefined') {
actionBody.hvac_mode = 'eco';
// target_temperature_ -> eco_temperature_
let str_action = JSON.stringify(opt.action.eco);
str_action = str_action.replace('target', 'eco', 'gi');
actionTemperature = JSON.parse(str_action);
} else if (typeof opt.action.fan_timer_duration !== 'undefined') {
if (opt.action.fan_timer_duration === 0) {
actionBody.fan_timer_active = false;
} else if (arrayContain([15, 30, 45, 60, 120, 240, 480, 960],
opt.action.fan_timer_duration)) {
actionBody.fan_timer_duration = opt.action.fan_timer_duration;
actionBody.fan_timer_active = true;
} else {
callback_opt.result.err_no = 116;
callback_opt.result.err_msg = 'Fan duration can only be set to 15, 30, 45, 60, 120, 240, 480, or 960.';
}
}
if (isEmpty(actionBody)) {
if (typeof callback_opt.result.err_no === 'undefined') {
callback_opt.result.err_no = 200;
callback_opt.result.err_msg = 'Invalid input';
}
} else {
execAction(opt.xim_content.access_token, isStructure, opt.device_id,
actionBody, actionTemperature, (result) => {
if (result === true) {
callback_opt.result.err_no = 0;
callback_opt.result.err_msg = 'ok';
} else {
callback_opt.result.err_no = 115;
callback_opt.result.err_msg = 'Request Failed';
if (typeof result !== 'undefined' && typeof result.error !== 'undefined') {
callback_opt.result.err_detail = result.error;
}
}
callback(callback_opt);
});
return;
}
}
callback(callback_opt);
}
/**
* functions exporting
*/
module.exports = action;