-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
419 lines (380 loc) · 15 KB
/
main.js
File metadata and controls
419 lines (380 loc) · 15 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
const url = require('url')
const http = require('http')
const fs = require('fs')
const express = require('express')
const app = express()
const oneMinute = 60000
var switches
const token = process.env.SMARTTHINGS_API_TOKEN
const hassPW = process.env.HASS_PW
const hassApiToken = process.env.HASS_FLIC_INTERFACE_TOKEN
const hassHost = process.env.HASS_HOST
const serverPort = process.env.SERVER_PORT || 9090
function postSmartThingsRequest(deviceId,commandString) {
var http = require('https');
var post_req = null,
post_data = [
{
command: commandString,
capability: 'switch',
component: 'main',
arguments: []
}
];
var post_options = {
hostname: 'api.smartthings.com',
//hostname: 'requestb.in',
port : '443',
path : '/v1/devices/' + deviceId + '/commands',
//path : '/13sgkba1',
method : 'POST',
headers : {
'Content-Type': 'application/json',
"Authorization": "Bearer: " + token
}
};
post_req = http.request(post_options, function (res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('Response: ', chunk);
});
});
post_req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
post_req.write(JSON.stringify(post_data));
post_req.end();
}
function toggle(deviceId) {
var http = require('https');
var get_options = {
hostname: 'api.smartthings.com',
//hostname: 'requestb.in',
port : '443',
path : '/v1/devices/' + deviceId + '/components/main/capabilities/switch/status',
//path : '/13sgkba1',
method : 'GET',
headers : {
"Authorization": "Bearer: " + token
}
};
var get_req = http.request(get_options, function (res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
console.log(res.data);
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('Response: ', chunk);
var reply = JSON.parse(chunk)
var currentStatus = reply.switch.value;
postSmartThingsRequest(deviceId,(currentStatus == 'on') ? 'off' : 'on')
});
});
get_req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
get_req.write("");
get_req.end();
}
function switchOn(request,response) {
switchOnOff(request,response,"on")
}
function switchOff(request,response) {
switchOnOff(request,response,"off")
}
function switchOnOff(request,response,commandString) {
var queryData = url.parse(request.url, true).query
var devicename = queryData.deviceName
var deviceId = queryData.deviceId
var targetid = ""
if (switches != undefined) {
if (deviceId != undefined) {
targetid=deviceId;
}
if (devicename != undefined) {
for (var i = 0; i < switches.length; i++) {
if (switches[i].label == devicename) {
targetid = switches[i].deviceId
break
}
}
}
if (targetid != "") {
postSmartThingsRequest(targetid,commandString)
response.writeHead(200, {"content-type": "text/html"});
response.write('<h1>success found switch and requested switch "' + commandString + '"</h1>')
response.write('<br>request=' + request.url + '');
response.write('<br>devicename=' + devicename + '');
response.write('<br>targetid=' + targetid + '');
response.end();
} else {
response.writeHead(200, {"content-type": "text/html"});
response.write('<br>request=' + request.url + '');
response.write('<br>devicename=' + devicename + '');
response.write('<br>deviceId=' + deviceId + '');
response.write('<br>targetid=' + 'not found');
response.end();
}
} else {
response.writeHead(200, {"content-type": "text/html"});
response.write(`<br>request=` + request.url + '');
response.write(`<br>devicename=` + devicename + '');
response.write('<br>deviceId=' + deviceId + '');
response.write(`<br>targetid=` + 'no devices loaded check server');
response.end();
}
}
function showDevices(request,response) {
response.writeHead(200, {'Content-Type': 'text/html'});
response.write('<html>\n')
response.write('<head>\n')
response.write(addCss())
response.write('</head>\n')
response.write('<body>\n')
response.write('<h1>Switches (with Name based links)</h1>\n')
response.write('<h3>NOTE:Spaces don\'t work with Flic Internet Request so use <a href="/showDevicesId">ID based links</a>)</h3>\n')
response.write('<table>\n')
if (switches != undefined) {
for (var i = 0; i < switches.length; i++) {
response.write('<tr class="spacedRow">\n')
response.write('<td><h3 style="text-align: right;">' + switches[i].label + '</h3></td>\n')
response.write('<td>     </td>')
response.write('<td><a class=button href="/toggle?deviceName=' + switches[i].label + '">toggle</a></td>\n')
response.write('<td>     </td>')
response.write('<td><a class=button href="/switchOn?deviceName=' + switches[i].label + '">switchOn</a></td>\n')
response.write('<td>     </td>')
response.write('<td><a class=button href="/switchOff?deviceName=' + switches[i].label + '">switchOff</a></td>\n')
response.write('</tr>\n')
}
}
response.write('</table>\n')
response.write('</body>\n')
response.write('</html>\n')
response.end();
}
function addCss() {
var css = "";
css = css.concat('<style>\n')
css = css.concat('a.button {\n' +
' background-color: #4CAF50; /* Green */\n' +
' border: none;\n' +
' color: white;\n' +
' padding: 15px 32px;\n' +
' text-align: center;\n' +
' text-decoration: none;\n' +
' display: inline-block;\n' +
' font-size: 16px;' +
'}\n' +
'tr.spacedRow>td {\n' +
' padding-bottom: 1em;\n' +
' padding-top: 1em;\n' +
'}')
css = css.concat('</style>\n')
return css
}
function showDevicesId(request,response) {
response.writeHead(200, {'Content-Type': 'text/html'});
response.write('<html>\n')
response.write('<head>\n')
response.write(addCss())
response.write('</head>\n')
response.write('<body>\n')
response.write('<h1>Switches (with ID based links)</h1>\n')
response.write('<table>\n')
if (switches != undefined) {
for (var i = 0; i < switches.length; i++) {
response.write('<tr class="spacedRow">\n')
response.write('<td><h3 style="text-align: right;">' + switches[i].label + '</h3></td>\n')
response.write('<td>     </td>')
response.write('<td><a class=button href="/toggle?deviceId=' + switches[i].deviceId + '">toggle</a></td>\n')
response.write('<td>     </td>')
response.write('<td><a class=button href="/switchOn?deviceId=' + switches[i].deviceId + '">switchOn</a></td>\n')
response.write('<td>     </td>')
response.write('<td><a class=button href="/switchOff?deviceId=' + switches[i].deviceId + '">switchOff</a></td>\n')
response.write('</tr>\n')
}
}
response.write('</table>\n')
response.write('</body>\n')
response.write('</html>\n')
response.end();
}
function loadDevices(request,response) {
getDevicesFromSmartThings(request,response,0);
}
function getDevicesFromSmartThings(request,response,retryWaitTime) {
var https = require('https');
var get_options = {
hostname: 'api.smartthings.com',
port : '443',
path : '/v1/devices?capability=switch&max=200',
method : 'GET',
headers : {
"Authorization": "Bearer: " + token
}
};
var get_req = https.request(get_options, function (res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
console.log(res.data);
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('Response: ', chunk);
});
let data = ""
res.on('data', function(chunk) {
data += chunk;
}).on('end', function() {
var devices = JSON.parse(data)
function compareElements(a, b) {
var alabel=a.label.toLowerCase()
var blabel=b.label.toLowerCase()
if (alabel < blabel)
return -1;
if (alabel > blabel)
return 1;
return 0;
}
var items = devices.items
switches = items.sort(compareElements);
if (response != undefined) {
showDevices(request,response)
} else {
console.log(devices);
}
});
});
get_req.on('error', function(e) {
console.log('problem with request: ' + e.message);
// if retryWait is 0 then don't retry, this is used for user requested load
// if retryWait reaches 1hr then stop retrying
// otherwise keep retrying on a logorithmic basis 1->2->4->8->16->32->64
if (retryWaitTime!=0 && retryWaitTime < oneMinute*60) {
setTimeout(getDevicesFromSmartThings, retryWaitTime, request, response, retryWaitTime * 2)
}
});
get_req.write("");
get_req.end();
}
function toggleSwitch(request,response) {
var queryData = url.parse(request.url, true).query
var devicename = queryData.deviceName
var deviceId = queryData.deviceId
var targetid = ""
if (switches != undefined) {
if (deviceId != undefined) {
targetid=deviceId;
}
if (devicename != undefined) {
for (var i = 0; i < switches.length; i++) {
if (switches[i].label == devicename) {
targetid = switches[i].deviceId
break
}
}
}
if (targetid != "") {
toggle(targetid)
response.writeHead(200, {"content-type": "text/html"});
response.write('<h1>success found switch and requested toggle</h1>')
response.write('<br>request=' + request.url + '');
response.write('<br>devicename=' + devicename + '');
response.write('<br>targetid=' + targetid + '');
response.end();
} else {
response.writeHead(200, {"content-type": "text/html"});
response.write('<br>request=' + request.url + '');
response.write('<br>devicename=' + devicename + '');
response.write('<br>targetid=' + 'not found');
response.end();
}
} else {
response.writeHead(200, {"content-type": "text/html"});
response.write(`<br>request=` + request.url + '');
response.write(`<br>devicename=` + devicename + '');
response.write(`<br>targetid=` + 'no devices loaded check server');
response.end();
}
}
function help(request,response) {
response.write('<html>')
response.write('<head>\n')
response.write(addCss())
response.write('</head>\n')
response.write('<body>\n')
response.write('<h1>Help Details</h1>')
response.write('<table>\n')
response.write('<tr class="spacedRow"><tr><td><a class=button href="/reloadDevices">/reloadDevices</a> will send new request to SmartThings for all switches</td></tr></tr>');
response.write('<tr class="spacedRow"><td><a class=button href="/showDevices">/showDevices</a> will display currently loaded devices</td></tr>');
response.write('<tr class="spacedRow"><td><a class=button href="/showDevicesId">/showDevicesId</a> will display currently loaded devices</td></tr>');
response.write('<tr class="spacedRow"><td>/toggle?deviceName={device name}} will toggle the on/off state of the device</td></tr>')
response.write('<tr class="spacedRow"><td>/toggle?deviceId={device id}} will toggle the on/off state of the device</td></tr>')
response.write('<tr class="spacedRow"><td>/switchOn?deviceName={device name}} will switch device On</td></tr>')
response.write('<tr class="spacedRow"><td>/switchOn?deviceId={device id}} will switch device On</td></tr>')
response.write('<tr class="spacedRow"><td>/switchOff?deviceName={device name}} will switch device Off</td></tr>')
response.write('<tr class="spacedRow"><td>/switchOff?deviceId={device id}} will switch device Off</td></tr>')
response.write('</table>')
response.write('</body>\n')
response.end();
}
function hassApi(request,response) {
var queryData = url.parse(request.url, true).query
var entityId = queryData.entity_id
var apiCommand = queryData.api_command
/*
curl -X POST -H "x-ha-access: YOUR_PASSWORD" \
-H "Content-Type: application/json" \
-d '{"entity_id": "switch.christmas_lights"}' \
http://localhost:8123/api/services/switch/turn_on
*/
var http = require('http');
var post_req = null,
post_data = {
entity_id: entityId
} ;
var post_options = {
hostname: hassHost,
port : '8123',
path : '/api/' + apiCommand,
method : 'POST',
headers : {
'Content-Type': 'application/json',
//'x-ha-access': hassPW
'Authorization': "Bearer " + hassApiToken
}
};
console.log('PostOptions: ', post_options);
console.log('PostData: ', post_data);
post_req = http.request(post_options, function (res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('Response: ', chunk);
});
});
post_req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
post_req.write(JSON.stringify(post_data));
post_req.end();
response.write('<html>')
response.write('<body>\n')
response.write('OK\n')
response.write('</body>\n')
response.write('</html>')
response.end();
}
//app.get('/showDevicesId',showDevicesId)
//app.get('/showDevices',showDevices)
//app.get('/reloadDevices',loadDevices)
//app.get('/toggle',toggleSwitch)
//app.get('/switchOn',switchOn)
//app.get('/switchOff',switchOff)
app.get('/hassApi',hassApi)
//app.get('/help',help)
app.get('/',help)
//app.listen(serverPort,getDevicesFromSmartThings(undefined,undefined,oneMinute))
app.listen(serverPort)