forked from dpjanes/iotdb-smartthings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smartthingslib.js
235 lines (199 loc) · 6.03 KB
/
smartthingslib.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
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
/*
* smartthingslib.js
*
* David Janes
* IOTDB.org
* 2014-02-01
*
* Library for interfacing with smartthings
*/
"use strict";
var unirest = require('unirest');
var fs = require('fs');
var util = require('util');
var events = require('events');
var SmartThings = function () {
var self = this;
events.EventEmitter.call(self);
self.std = {};
self.endpointd = {};
};
util.inherits(SmartThings, events.EventEmitter);
/**
* Explicitly make the settings
*/
SmartThings.prototype.configure = function (paramd) {
var self = this;
paramd = paramd || {};
self.std = {};
self.std.client_id = paramd.client_id || "";
self.std.access_token = paramd.access_token || "";
self.std.api = paramd.api || "https://graph.api.smartthings.com/api/smartapps/endpoints/" + self.std.client_id;
self.std.api_location = paramd.api_location || "graph.api.smartthings.com";
};
/**
* Load the JSON Settings file.
*
* <p>
* See the documentation, but briefly you can get it from here:
* {@link https://iotdb.org/playground/oauthorize}
*/
SmartThings.prototype.load_settings = function (filename) {
var self = this;
if (!filename) {
filename = "smartthings.json";
}
var data = fs.readFileSync(filename, 'utf8');
self.std = JSON.parse(data);
};
/**
* Get the endpoints exposed by the SmartThings App.
*
* <p>
* When the endpoints are found, we emit an 'endpoint' event.
* The endpoint itself will be stored in the SmartThings object
*
* <p>
* The first command you need to call
*/
SmartThings.prototype.request_endpoint = function () {
var self = this;
var endpoints_url = self.std["api"];
var endpoints_paramd = {
"access_token": self.std["access_token"]
};
unirest
.get(endpoints_url)
.query(endpoints_paramd)
.end(function (result) {
if (!result.ok) {
console.log("SmartThings.request_endpoints", "something went wrong", result);
return;
}
self.endpointd = result.body[0];
self.emit("endpoint", self);
});
};
/**
* Get devices.
* <p>
* When devices are found, we emit a "devices" event
*
* @param {string} device_type
* The type of device to request, i.e.
* switch, motion, acceleration, contact, presence
*/
SmartThings.prototype.request_devices = function (device_type) {
var self = this;
if (!self.endpointd.url) {
console.log("SmartThings.request_devices: no endpoint? Perhaps .request_endpoint has not been called");
return;
}
var devices_url = "https://graph.api.smartthings.com" + self.endpointd.url + "/" + device_type;
var devices_paramd = {};
var devices_headerd = {
"Authorization": "Bearer " + self.std["access_token"]
};
unirest
.get(devices_url)
.query(devices_paramd)
.headers(devices_headerd)
.end(function (result) {
if (!result.ok) {
console.log("SmartThings.request_devices", "something went wrong",
"\n url=", devices_url,
"\n error=", result.error,
"\n body=", result.body
);
return;
}
self.emit("devices", device_type, result.body);
});
};
/**
* Send a request to a device
*
* @param {dictionary} device
* A device dictionary, as found by {@link request_devices}
*
* @param {dictionary} requestd
* A request dictinary, for example something like
* <code>{ switch: 1 }</code>
*/
SmartThings.prototype.device_request = function (deviced, requestd, callback) {
var self = this;
if (!callback) {
callback = function () {};
}
if (!self.endpointd.url) {
console.log("SmartThings.device_request: no endpoint? Perhaps .request_endpoint has not been called");
return;
}
var devices_url = "https://graph.api.smartthings.com" + self.endpointd.url + "/" + deviced.type + "/" + deviced.id;
var devices_headerd = {
"Authorization": "Bearer " + self.std["access_token"]
};
unirest
.put(devices_url)
.type('json')
.send(requestd)
.headers(devices_headerd)
.end(function (result) {
if (!result.ok) {
callback(result.error, deviced, null);
console.log("SmartThings.device_request", "something went wrong",
"\n url=", devices_url,
"\n error=", result.error,
"\n body=", result.body
);
return;
}
self.emit("request", deviced);
callback(null, deviced, result.body);
});
};
/**
* Request device's state
*
* @param {dictionary} device
* A device dictionary, as found by {@link request_devices}
*/
SmartThings.prototype.device_poll = function (deviced, callback) {
var self = this;
if (!self.endpointd.url) {
console.log("SmartThings.device_request: no endpoint? Perhaps .request_endpoint has not been called");
return;
}
var devices_url = "https://graph.api.smartthings.com" + self.endpointd.url + "/" + deviced.type + "/" + deviced.id;
var devices_headerd = {
"Authorization": "Bearer " + self.std["access_token"]
};
unirest
.get(devices_url)
.type('json')
.headers(devices_headerd)
.end(function (result) {
if (!result.ok) {
callback(result.error, deviced, null);
console.log("SmartThings.device_request", "something went wrong",
"\n url=", devices_url,
"\n error=", result.error,
"\n body=", result.body
);
return;
}
self.emit("device", result.body);
callback(null, deviced, result.body);
});
};
exports.SmartThings = SmartThings;
exports.device_types = [
"switch",
"motion",
"presence",
"acceleration",
"contact",
"temperature",
"battery",
"threeAxis",
];