forked from dpjanes/iotdb-smartthings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smartthings.js
117 lines (104 loc) · 3.03 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
* 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 ad = require('minimist')(process.argv.slice(2), {
boolean: ["poll"]
});
var sm = new SmartThings();
sm.on("endpoint", function () {
sm.request_devices(ad.type);
});
sm.on("devices", function (device_type, devices) {
var di;
var device;
if (ad.device_id) {
var ds = [];
for (di in devices) {
device = devices[di];
if ((device.id === ad.device_id) || (device.label === ad.device_id)) {
ds.push(device);
}
}
devices = ds;
}
if (ad.request) {
for (di in devices) {
device = devices[di];
sm.device_request(device, ad.request);
}
} else if (ad.poll && ad.device_id) {
var _on_state = function (error, _deviced, _stated) {
console.log(JSON.stringify(_stated, null, 2));
};
for (di in devices) {
device = devices[di];
sm.device_poll(device, _on_state);
}
} 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, …");
console.log(" …contact, temperature, battery, threeAxis");
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'");
console.log("");
console.log("--poll");
console.log(" request the device's state (must be paired with device_id");
};
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();