-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdevices.js
238 lines (224 loc) · 8.5 KB
/
devices.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
236
237
238
/*
* robin-js-sdk
* http://getrobin.com/
*
* Copyright (c) 2014 Robin Powered Inc.
* Licensed under the Apache v2 license.
* https://github.com/robinpowered/robin-js-sdk/blob/master/LICENSE
*
*/
var constants = require('../../util').constants;
module.exports = {
/**
* Get all the devices or a particular device identified by the `identifier` parameter
* @param {String|Integer|undefined} identifier A Robin device identifier
* @param {Object|undefined} params A querystring object
* @return {Function} A Promise
*/
get: function (identifier, params) {
var path = this.constructPath(constants.DEVICES, identifier);
return this.Core.GET(path, params);
},
/**
* Create a device
* @param {Object} data A data object
* @return {Function} A Promise
*/
create: function (data) {
var path;
if (data) {
path = this.constructPath(constants.DEVICES);
return this.Core.POST(path, data);
} else {
return this.rejectRequest('Bad Request: A data object is required');
}
},
/**
* Update a device
* @param {String|Integer|undefined} identifier A Robin device identifier
* @param {Object} data A data object
* @return {Function} A Promise
*/
update: function (identifier, data) {
var path;
if (identifier && data) {
path = this.constructPath(constants.DEVICES, identifier);
return this.Core.PATCH(path, data);
} else {
return this.rejectRequest('Bad Request: A device identifier and a data object are required.');
}
},
/**
* Delete a device
* @param {String|Integer} identifier A Robin device identifier
* @return {Function} A Promise
*/
delete: function (identifier) {
var path;
if (identifier) {
path = this.constructPath(constants.DEVICES, identifier);
return this.Core.DELETE(path);
} else {
return this.rejectRequest('Bad Request: A device identifier is required.');
}
},
/**
* Device Identifiers
* @type {Object}
*/
identifiers: {
/**
* Get all the identifiers for a device or a particular identifier identified by `identifierURN`
* @param {String|Integer} deviceIdentifier A Robin device identifier
* @param {String|Integer|undefined} identifierURN A Robin feed identifier
* @param {Object|undefined} params A querystring object
* @return {Function} A Promise
*/
get: function (deviceIdentifier, identifierURN, params) {
var path;
if (deviceIdentifier) {
path = this.constructPath(constants.DEVICES, deviceIdentifier, constants.IDENTIFIERS, identifierURN);
return this.Core.GET(path, params);
} else {
return this.rejectRequest('Bad Request: A device identifier is required.');
}
},
/**
* Create an identifier for a device
* @param {String|Integer} deviceIdentifier A Robin device identifier
* @param {Object} data A querystring object
* @return {Function} A Promise
*/
create: function (deviceIdentifier, data) {
var path;
if (deviceIdentifier) {
path = this.constructPath(constants.DEVICES, deviceIdentifier, constants.IDENTIFIERS);
return this.Core.POST(path, data);
} else {
return this.rejectRequest('Bad Request: A device identifier and a data object are required.');
}
},
/**
* Add an identifier for a device
* @param {String|Integer} deviceIdentifier A Robin device identifier
* @param {String|Integer} identifierUrn A Robin identifier URN
* @param {Object} data A querystring object
* @return {Function} A Promise
*/
add: function (deviceIdentifier, identifierUrn, data) {
var path;
if (deviceIdentifier) {
path = this.constructPath(constants.DEVICES, deviceIdentifier, constants.IDENTIFIERS, identifierUrn);
return this.Core.PATCH(path, data);
} else {
return this.rejectRequest('Bad Request: A device identifier, an identifier id and a data object are required.');
}
},
/**
* Delete an identifier from a device
* @param {String|Integer} deviceIdentifier A Robin device identifier
* @param {String|Integer} identifierUrn A Robin identifier URN
* @return {Function} A Promise
*/
delete: function (deviceIdentifier, identifierUrn) {
var path;
if (deviceIdentifier && identifierUrn) {
path = this.constructPath(constants.DEVICES, deviceIdentifier, constants.IDENTIFIERS, identifierUrn);
return this.Core.DELETE(path);
} else {
return this.rejectRequest('Bad Request: A device identifier and an identifier id are required.');
}
}
},
/**
* Device Channels
* @type {Object}
*/
channels: {
/**
* Get all the channels for a device or a particular channel identified by `channelIdentifier`
* @param {String|Integer} deviceIdentifier A Robin device identifier
* @param {String|Integer|undefined} channelIdentifier A Robin channel identifier
* @param {Object|undefined} params A querystring object
* @return {Function} A Promise
*/
get: function (deviceIdentifier, channelIdentifier, params) {
var path;
if (deviceIdentifier) {
path = this.constructPath(constants.DEVICES, deviceIdentifier, constants.CHANNELS, channelIdentifier);
return this.Core.GET(path, params);
} else {
return this.rejectRequest('Bad Request: A device identifier is required.');
}
},
/**
* Add a channel to a device
* @param {String|Integer} deviceIdentifier A Robin device identifier
* @param {Object} data A querystring object
* @return {Function} A Promise
*/
create: function (deviceIdentifier, data) {
var path;
if (deviceIdentifier) {
path = this.constructPath(constants.DEVICES, deviceIdentifier, constants.CHANNELS);
return this.Core.POST(path, data);
} else {
return this.rejectRequest('Bad Request: A device identifier and a data object are required.');
}
},
/**
* Update a feed on a channel
* @param {String|Integer} deviceIdentifier A Robin device identifier
* @param {String|Integer} channelIdentifier A Robin channel identifier
* @param {Object} data A querystring object
* @return {Function} A promise
*/
update: function (deviceIdentifier, channelIdentifier, data) {
var path,
rejectMsg;
if (deviceIdentifier && channelIdentifier && data) {
path = this.constructPath(constants.DEVICES, deviceIdentifier, constants.CHANNELS, channelIdentifier);
return this.Core.PATCH(path, data);
} else {
rejectMsg = 'Bad Request: A device identifier, a feed identifier and a data object are required.';
return this.rejectRequest(rejectMsg);
}
},
/**
* Delete a channel from a device
* @param {String|Integer} deviceIdentifier A Robin device identifier
* @param {String|Integer} channelIdentifier A Robin channel identifier
* @return {Function} A Promise
*/
delete: function (deviceIdentifier, channelIdentifier) {
var path;
if (deviceIdentifier && channelIdentifier) {
path = this.constructPath(constants.DEVICES, deviceIdentifier, constants.CHANNELS, channelIdentifier);
return this.Core.DELETE(path);
} else {
return this.rejectRequest('Bad Request: A device identifier and a channel identifier are required.');
}
}
},
/**
* Device Spaces
* @type {Object}
*/
spaces: {
/**
* Get a device's spaces
* @param {String|Integer} deviceIdentifier A Robin device identifier
* @param {Object|undefined} params A querystring object
* @return {Function} A promise
*/
get: function (deviceIdentifier, params) {
var path;
if (deviceIdentifier) {
path = this.constructPath(constants.DEVICES, deviceIdentifier, constants.SPACES);
return this.Core.GET(path, params);
} else {
return this.rejectRequest('Bad Request: A device identifier is required');
}
}
}
};