Repository navigation
Expand file tree
/
Copy pathsmartthings.js
More file actions
262 lines (233 loc) · 7.19 KB
/
Copy pathsmartthings.js
File metadata and controls
262 lines (233 loc) · 7.19 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
This is the "master" webhook to process _all_ intents from DialogFlow
*/
"use strict";
const https = require("https");
const host = "api.smartthings.com";
const token = process.env.smartthings_token;
const devices = {
light: "", //
lock: "", //
sonos: "", //
outlet: "", //
};
var fulfillmentText;
exports.light = (capability, value) => {
return new Promise((resolve, reject) => {
const device = devices.light; // which light
if (!device) resolve("No light specified in my code");
var command, argument;
const colors = require("./colors.js");
if (value == "status") {
// return value
SmartThingsStatus(device, capability).then(status => {
status = JSON.parse(status);
switch (capability) {
case "switchLevel":
resolve(`The light is at ${status.level.value}%`);
break;
case "switch":
resolve(`The light is ${status.switch.value}`);
break;
case "colorControl":
resolve(
`The light has a hue of ${status.hue.value} and a saturation of ${
status.saturation.value
}`
);
break;
}
});
} else {
switch (capability) {
case "switch":
command = value;
fulfillmentText = `Turning the light ${command}`;
break;
case "colorControl":
command = "setColor";
argument = colors.tohs(value);
fulfillmentText = `Turning the light ${value}`;
break;
case "switchLevel":
command = "setLevel";
argument = [value];
fulfillmentText = `Setting brightness to ${value}`;
break;
}
commandSmartThings(device, capability, command, argument)
.then(() => {
resolve(fulfillmentText);
})
.catch(error => {
resolve(error); // want to resolve to minimize code
}); // end commandSmartThings
} // end if status or command
}); // end Promise
}; // end light
exports.lock = command => {
return new Promise((resolve, reject) => {
const device = devices.lock; // which lock
if (!device) resolve("No lock specified in my code");
const capability = "lock";
var fulfillmentText =
command == "lock" ? "Locking the door" : "Unlocking the door";
if (command == "status") {
// return value
SmartThingsStatus(device, capability).then(status => {
status = JSON.parse(status);
resolve(`The door is ${status.lock.value}`);
});
} else {
commandSmartThings(device, capability, command)
.then(() => {
resolve(fulfillmentText);
})
.catch(error => {
resolve(error); // want to resolve to minimize code
}); // end smartThings
}
}); // end Promise
}; // end lock
exports.sonos = (command, value) => {
return new Promise((resolve, reject) => {
const device = devices.sonos; // which sonos
if (!device) resolve("No Sonos specified in my code");
var capability = "musicPlayer",
fulfillmentText,
argument = value ? [value] : null;
if (command == "status") {
SmartThingsStatus(device, capability).then(status => {
status = JSON.parse(status);
// todo - add what's currently playing
resolve(
`Music is ${status.status.value}. Player is at volume ${
status.level.value
}`
);
});
} else {
switch (command) {
case "setLevel":
fulfillmentText = `Setting volume to ${value}`;
break;
case "play":
fulfillmentText = `Playing`;
break;
case "pause":
fulfillmentText = `Pausing`;
break;
case "playTrack":
fulfillmentText = `Playing ${value.name}`;
argument = [value.sonosUri];
break;
}
commandSmartThings(device, capability, command, argument)
.then(() => {
resolve(fulfillmentText);
})
.catch(error => {
resolve(error); // want to resolve to minimize code
}); // end smartThings
} // end if status
}); // end Promise
}; // end sonos
exports.outlet = command => {
return new Promise((resolve, reject) => {
const device = devices.outlet; // which outlet
if (!device) resolve("No outlet specified in my code");
const capability = "switch";
var fulfillmentText =
command == "on" ? "Turning on the outlet" : "Turning off the outlet";
if (command == "status") {
// return value
SmartThingsStatus(device, capability).then(status => {
status = JSON.parse(status);
resolve(`The outlet is ${status.switch.value}`);
});
} else {
commandSmartThings(device, capability, command)
.then(() => {
resolve(fulfillmentText);
})
.catch(error => {
resolve(error); // want to resolve to minimize code
}); // end smartThings
} // end if status
}); // end Promise
}; // end outlet
function commandSmartThings(device, capability, command, argument = null) {
return new Promise((resolve, reject) => {
let path = `/v1/devices/${device}/commands`;
let payload = {
commands: [
{
component: "main",
capability: capability,
command: command,
},
],
};
if (argument) {
payload.commands[0].arguments = argument;
}
let body = JSON.stringify(payload);
let options = {
host: host,
path: path,
headers: {
Authorization: "Bearer: " + token,
"Content-Length": Buffer.byteLength(body),
},
method: "POST",
}; // end options
const request = https.request(options, function(res) {
let body = "";
if (res.statusCode != "200") {
reject(`Error calling the Smarthings API: Status ${res.statusCode}`);
}
res.on("data", d => {
body += d;
});
res.on("end", () => {
resolve(body);
});
}); // end request
request.on("error", error => {
console.log(`Error calling the Smarthings API: ${error}`);
reject(`Error calling the Smarthings API: ${error}`);
});
request.write(body);
request.end();
}); // end Promise
} // end commandSmartThings
function SmartThingsStatus(device, capability) {
return new Promise((resolve, reject) => {
let path = `/v1/devices/${device}/components/main/capabilities/${capability}/status`;
let options = {
host: host,
path: path,
headers: {
Authorization: "Bearer: " + token,
},
method: "GET",
}; // end options
const request = https.request(options, function(res) {
let body = "";
if (res.statusCode != "200") {
resolve(`Error calling the Smarthings API: Status ${res.statusCode}`);
}
res.on("data", d => {
body += d;
});
res.on("end", () => {
resolve(body); // this needs to be formatted
});
}); // end request
request.on("error", error => {
console.log(`Error calling the Smarthings API: ${error}`);
resolve(`Error calling the Smarthings API: ${error}`);
});
request.end();
}); // end Promise
} // end commandSmartThings