forked from pdamodaran/m2x-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevices.js
More file actions
456 lines (408 loc) · 18 KB
/
devices.js
File metadata and controls
456 lines (408 loc) · 18 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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
define(["helpers"], function(helpers) {
// Wrapper for AT&T M2X Device API
//
// https://m2x.att.com/developer/documentation/device
var Devices = function(client, keysAPI, metadata) {
this.client = client;
this.keysAPI = keysAPI;
this.metadata = metadata;
};
// List/search the catalog of public devices
//
// This allows unauthenticated users to search Devices from other users
// that have been marked as public, allowing them to read public Device
// metadata, locations, streams list, and view each Devices' stream metadata
// and its values.
//
// https://m2x.att.com/developer/documentation/v2/device#List-Search-Public-Devices-Catalog
Devices.prototype.catalog = function(params, callback, errorCallback) {
if (typeof params === "function") {
callback = params;
errorCallback = callback;
params = {};
}
return this.client.get("/devices/catalog", { qs: params || {} }, callback, errorCallback);
};
// Retrieve the list of devices accessible by the authenticated API key that
// meet the search criteria
//
// https://m2x.att.com/developer/documentation/v2/device#Search-Devices
Devices.prototype.search = function(params, callback, errorCallback) {
return this.client.post("/devices/search", {
headers: { "Content-Type": "application/json" },
params: params || {}
}, callback, errorCallback);
};
// Retrieve the list of devices accessible by the authenticated API key
//
// https://m2x.att.com/developer/documentation/v2/device#List-Devices
Devices.prototype.list = function(params, callback, errorCallback) {
return this.client.get("/devices", { qs: params || {} }, callback, errorCallback);
};
// List the devices tags for the authenticated user
//
// https://m2x.att.com/developer/documentation/v2/device#List-Device-Tags
Devices.prototype.tags = function(callback, errorCallback) {
return this.client.get("/devices/tags", callback, errorCallback);
};
// Create a new device
//
// https://m2x.att.com/developer/documentation/v2/device#Create-Device
Devices.prototype.create = function(params, callback, errorCallback) {
return this.client.post("/devices", { params: params }, callback, errorCallback);
};
// Update a device
//
// https://m2x.att.com/developer/documentation/v2/device#Update-Device-Details
Devices.prototype.update = function(id, params, callback, errorCallback) {
return this.client.put( helpers.url("/devices/{0}", id), {
headers: { "Content-Type": "application/json" },
params: params
}, callback, errorCallback);
};
// Return the details of the supplied device
//
// https://m2x.att.com/developer/documentation/v2/device#View-Device-Details
Devices.prototype.view = function(id, callback, errorCallback) {
return this.client.get(helpers.url("/devices/{0}", id), callback, errorCallback);
};
// Return the current location of the supplied device
//
// Note that this method can return an empty value (response status
// of 204) if the device has no location defined.
//
// https://m2x.att.com/developer/documentation/v2/device#Read-Device-Location
Devices.prototype.location = function(id, callback, errorCallback) {
return this.client.get(helpers.url("/devices/{0}/location", id), callback, errorCallback);
};
// Return the location history of the supplied device.
//
// https://m2x.att.com/developer/documentation/v2/device#Read-Device-Location-History
Devices.prototype.locationHistory = function(id, params, callback, errorCallback) {
if (typeof params === "function") {
errorCallback = callback;
callback = params;
params = {};
}
return this.client.get(helpers.url("/devices/{0}/location/waypoints", id), { qs: params }, callback, errorCallback);
};
// Update the current location of the device
//
// https://m2x.att.com/developer/documentation/v2/device#Update-Device-Location
Devices.prototype.updateLocation = function(id, params, callback, errorCallback) {
return this.client.put(
helpers.url("/devices/{0}/location", id),
{ params: params },
callback, errorCallback
);
};
// Return a list of the associated streams for the supplied device
//
// https://m2x.att.com/developer/documentation/v2/device#List-Data-Streams
Devices.prototype.streams = function(id, callback, errorCallback) {
return this.client.get(helpers.url("/devices/{0}/streams", id), callback, errorCallback);
};
// Update stream's properties
//
// If the stream doesn't exist it will create it. See
// https://m2x.att.com/developer/documentation/device#Create-Update-Data-Stream
// for details.
//
// https://m2x.att.com/developer/documentation/v2/device#Create-Update-Data-Stream
Devices.prototype.updateStream = function(id, name, params, callback, errorCallback) {
return this.client.put(
helpers.url("/devices/{0}/streams/{1}", id, name),
{ params: params },
callback, errorCallback
);
};
// Set the stream value
//
// https://m2x.att.com/developer/documentation/v2/device#Update-Data-Stream-Value
Devices.prototype.setStreamValue = function(id, name, params, callback, errorCallback) {
return this.client.put(
helpers.url("/devices/{0}/streams/{1}/value", id, name),
{ params: params },
callback, errorCallback
);
};
// Return the details of the supplied stream
//
// https://m2x.att.com/developer/documentation/v2/device#View-Data-Stream
Devices.prototype.stream = function(id, name, callback, errorCallback) {
return this.client.get(
helpers.url("/devices/{0}/streams/{1}", id, name),
callback, errorCallback
);
};
// List values from an existing data stream associated with a
// specific device, sorted in reverse chronological order (most
// recent values first).
//
// https://m2x.att.com/developer/documentation/v2/device#List-Data-Stream-Values
Devices.prototype.streamValues = function(id, name, params, callback, errorCallback) {
var url = helpers.url("/devices/{0}/streams/{1}/values", id, name);
if (typeof params === "function") {
errorCallback = callback;
callback = params;
params = {};
}
return this.client.get(url, { qs: params }, callback, errorCallback);
};
// Sample values from an existing stream
//
// https://m2x.att.com/developer/documentation/v2/device#Data-Stream-Sampling
Devices.prototype.sampleStreamValues = function(id, name, params, callback, errorCallback) {
return this.client.get(
helpers.url("/devices/{0}/streams/{1}/sampling", id, name),
{ qs: params },
callback, errorCallback
);
};
// Return the stream stats
//
// https://m2x.att.com/developer/documentation/v2/device#Data-Stream-Stats
Devices.prototype.streamStats = function(id, name, params, callback, errorCallback) {
return this.client.get(
helpers.url("/devices/{0}/streams/{1}/stats", id, name),
{ qs: params },
callback, errorCallback
);
};
// List values from all data stream associated with a specific device
//
// https://m2x.att.com/developer/documentation/v2/device#List-Values-from-all-Data-Streams-of-a-Device
Devices.prototype.values = function(id, params, callback, errorCallback) {
var url = helpers.url("/devices/{0}/values", id);
if (typeof params === "function") {
errorCallback = callback;
callback = params;
params = {};
}
return this.client.get(url, { qs: params }, callback, errorCallback);
};
// Search Values from all Data Streams of a Device
//
// https://m2x.att.com/developer/documentation/v2/device#Search-Values-from-all-Data-Streams-of-a-Device
Devices.prototype.valuesSearch = function(id, params, callback, errorCallback) {
return this.client.post(
helpers.url("/devices/{0}/values/search", id),
{
headers: { "Content-Type": "application/json" },
params: params
},
callback,
errorCallback
);
};
// Export Values from all Data Streams of a Device
//
// https://m2x.att.com/developer/documentation/v2/device#Export-Values-from-all-Data-Streams-of-a-Device
Devices.prototype.valuesExport = function(id, params, callback, errorCallback) {
if (typeof params === "function") {
errorCallback = callback;
callback = params;
params = {};
}
return this.client.get(
helpers.url("/devices/{0}/values/export.csv", id),
{ qs: params },
callback,
errorCallback
);
};
// Post timestamped values to an existing stream
//
// https://m2x.att.com/developer/documentation/v2/device#Post-Data-Stream-Values
Devices.prototype.postValues = function(id, name, values, callback, errorCallback) {
return this.client.post(
helpers.url("/devices/{0}/streams/{1}/values", id, name),
{ params: { values: values } },
callback, errorCallback
);
};
// Delete values from a stream by a date range
//
// https://m2x.att.com/developer/documentation/v2/device#Delete-Data-Stream-Values
Devices.prototype.deleteStreamValues = function(id, name, params, callback, errorCallback) {
return this.client.del(
helpers.url("/devices/{0}/streams/{1}/values", id, name),
{ params: params },
callback, errorCallback
);
};
// Delete the stream (and all its values) from the device
//
// https://m2x.att.com/developer/documentation/v2/device#Delete-Data-Stream
Devices.prototype.deleteStream = function(id, name, callback, errorCallback) {
return this.client.del(helpers.url("/devices/{0}/streams/{1}", id, name), callback, errorCallback);
};
// Post Device Updates (Multiple Values to Multiple Streams)
//
// This method allows posting multiple values to multiple streams
// belonging to a device and optionally, the device location.
//
// All the streams should be created before posting values using this method.
//
// The `values` parameter contains an object with one attribute per each stream to be updated.
// The value of each one of these attributes is an array of timestamped values.
//
// {
// temperature: [
// { "timestamp": <Time in ISO8601>, "value": x },
// { "timestamp": <Time in ISO8601>, "value": y }
// ],
// humidity: [
// { "timestamp": <Time in ISO8601>, "value": x },
// { "timestamp": <Time in ISO8601>, "value": y }
// ]
// }
//
// The optional `location` parameter can contain location information that will
// be used to update the current location of the specified device
//
// https://m2x.att.com/developer/documentation/v2/device#Post-Device-Updates--Multiple-Values-to-Multiple-Streams-
Devices.prototype.postUpdates = function(id, params, callback, errorCallback) {
return this.client.post(helpers.url("/devices/{0}/updates", id), {
headers: { "Content-Type": "application/json" },
params: params
}, callback, errorCallback);
};
// Post Device Update (Single Value to Multiple Streams)
//
// This method allows posting a single value to multiple streams
// belonging to a device and optionally, the device's location.
//
// All the streams should be created before posting values using this method.
//
// The `params` parameter accepts an object which can contain the following keys:
// - values: An Object in which the keys are the stream names and the values
// hold the stream values.
// - location: (optional) A hash with the current location of the specified
// device.
// - timestamp: (optional) The timestamp for all the passed values and
// location. If ommited, the M2X server's time will be used.
//
// {
// values: {
// temperature: 30,
// humidity: 80
// },
// location: {
// name: "Storage Room",
// latitude: -37.9788423562422,
// longitude: -57.5478776916862,
// elevation: 5
// }
// }
//
// https://m2x.att.com/developer/documentation/v2/device#Post-Device-Update--Single-Values-to-Multiple-Streams-
Devices.prototype.postUpdate = function(id, params, callback, errorCallback) {
return this.client.post(helpers.url("/devices/{0}/update", id), {
headers: { "Content-Type": "application/json" },
params: params
}, callback, errorCallback);
};
// Return a list of access log to the supplied device
//
// https://m2x.att.com/developer/documentation/v2/device#View-Request-Log
Devices.prototype.log = function(id, callback, errorCallback) {
return this.client.get(helpers.url("/devices/{0}/log", id), callback, errorCallback);
};
// Delete an existing device
//
// https://m2x.att.com/developer/documentation/v2/device#Delete-Device
Devices.prototype.deleteDevice = function(id, callback, errorCallback) {
return this.client.del(helpers.url("/devices/{0}", id), callback, errorCallback);
};
// Returns a list of API keys associated with the device
Devices.prototype.keys = function(id, callback, errorCallback) {
return this.client.get("/keys", { qs: { device: id } }, callback, errorCallback);
};
// Creates a new API key associated to the device
//
// If a parameter named `stream` is supplied with a stream name, it
// will create an API key associated with that stream only.
Devices.prototype.createKey = function(id, params, callback, errorCallback) {
this.keysAPI.create(helpers.extend(params, { device: id }), callback, errorCallback);
};
// Updates an API key properties
Devices.prototype.updateKey = function(id, key, params, callback, errorCallback) {
this.keysAPI.update(key, helpers.extend(params, { device: id }), callback, errorCallback);
};
// Read device metadata
//
// https://m2x.att.com/developer/documentation/v2/device#Read-Device-Metadata
Devices.prototype.readMetadata = function(id, callback, errorCallback) {
this.metadata.read("devices", id, callback, errorCallback);
};
// Update device metadata
//
// https://m2x.att.com/developer/documentation/v2/device#Update-Device-Metadata
Devices.prototype.updateMetadata = function(id, params, callback, errorCallback) {
this.metadata.update("devices", id, params, callback, errorCallback);
};
// Read device metadata field
//
// https://m2x.att.com/developer/documentation/v2/device#Read-Device-Metadata-Field
Devices.prototype.readMetadataField = function(id, field, callback, errorCallback) {
this.metadata.readField("devices", id, field, callback, errorCallback);
};
// Update device metadata field
//
// https://m2x.att.com/developer/documentation/v2/device#Update-Device-Metadata-Field
Devices.prototype.updateMetadataField = function(id, field, value, callback, errorCallback) {
this.metadata.updateField("devices", id, field, value, callback, errorCallback);
};
// Get device's list of received commands
//
// https://m2x.att.com/developer/documentation/v2/commands#Device-s-List-of-Received-Commands
Devices.prototype.commands = function(id, params, callback, errorCallback) {
if (typeof params === "function") {
errorCallback = callback;
callback = params;
params = {};
}
return this.client.get(
helpers.url("/devices/{0}/commands", id),
{ qs: params },
callback,
errorCallback
);
};
// Get device's view of command details
//
// https://m2x.att.com/developer/documentation/v2/commands#Device-s-View-of-Command-Details
Devices.prototype.command = function(deviceId, commandId, callback, errorCallback) {
return this.client.get(helpers.url("/devices/{0}/commands/{1}", deviceId, commandId), callback, errorCallback);
};
// Mark command as processed
//
// https://m2x.att.com/developer/documentation/v2/commands#Device-Marks-a-Command-as-Processed
Devices.prototype.processCommand = function(deviceId, commandId, params, callback, errorCallback) {
if (typeof params === "function") {
errorCallback = callback;
callback = params;
params = {};
}
return this.client.post(helpers.url("/devices/{0}/commands/{1}/process", deviceId, commandId), {
headers: { "Content-Type": "application/json" },
params: params || {}
}, callback, errorCallback);
};
// Mark command as rejected
//
// https://m2x.att.com/developer/documentation/v2/commands#Device-Marks-a-Command-as-Rejected
Devices.prototype.rejectCommand = function(deviceId, commandId, params, callback, errorCallback) {
if (typeof params === "function") {
errorCallback = callback;
callback = params;
params = {};
}
return this.client.post(helpers.url("/devices/{0}/commands/{1}/reject", deviceId, commandId), {
headers: { "Content-Type": "application/json" },
params: params || {}
}, callback, errorCallback);
};
return Devices;
});