forked from dpjanes/iotdb-smartthings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmartthings.js
99 lines (88 loc) · 2.47 KB
/
smartthings.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
/*
* smartthings.js
*
* David Janes
* IOTDB.org
* 2014-02-01
*
* Demonstrate how to use the SmartThings API from Node
*
* See also:
* Example App explanation:
* http://build.smartthings.com/blog/tutorial-creating-a-custom-rest-smartapp-endpoint/
*
* Example PHP code:
* https://www.dropbox.com/s/7m7gmlr9q3u7rmk/exampleOauth.php
*
* Example "Groovy"/SMART code (this is the app we tap into)
* https://www.dropbox.com/s/lohzziy2wjlrppb/endpointExample.groovy
*/
"use strict"
var unirest = require('unirest')
var fs = require('fs')
var util = require('util')
var events = require('events')
var minimist = require('minimist')
var SmartThings = require("./smartthingslib").SmartThings
var sm = new SmartThings()
sm.on("endpoint", function() {
sm.request_devices(ad.type)
})
sm.on("devices", function(device_type, devices) {
if (ad.device_id) {
var ds = []
for (var di in devices) {
var device = devices[di]
if ((device.id == ad.device_id) || (device.label == ad.device_id)) {
ds.push(device)
}
}
devices = ds
}
if (ad.request) {
for (var di in devices) {
var device = devices[di]
sm.device_request(device, ad.request)
}
} else {
console.log(JSON.stringify(devices, null, 2))
}
})
var help = function() {
console.log("usage:", process.argv[1], "[arguments...]")
console.log("")
console.log("--type <device_type>")
console.log(" the device type (required), one of switch, motion, presence, acceleration, contact")
console.log("")
console.log("--device_id <device_id>")
console.log(" the ID or Name of the device to manipulate")
console.log("")
console.log("--request <something=value>")
console.log(" something to do, e.g. 'switch=1', 'switch=0'")
}
var ad = require('minimist')(process.argv.slice(2));
if (Object.keys(ad).length == 1) {
help()
process.exit(1)
}
if (!ad.type) {
console.log("error:")
console.log(" --type <device_type> is required")
console.log("")
help()
process.exit(1)
}
if (ad.request) {
var parts = ad.request.split("=", 2)
if (parts.length != 2) {
console.log("error:")
console.log(" --request requires an argument like 'switch=1'")
console.log("")
help()
process.exit(1)
}
ad.request = {}
ad.request[parts[0]] = parseInt(parts[1])
}
sm.load_settings()
sm.request_endpoint()