-
Notifications
You must be signed in to change notification settings - Fork 4
/
Devices.js
225 lines (203 loc) · 9.23 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
// MIT License
//
// Copyright 2017 Electric Imp
//
// SPDX-License-Identifier: MIT
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
// EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
'use strict';
const DeviceGroups = require('./DeviceGroups');
const Entities = require('./util/Entities');
const ParamsChecker = require('./util/ParamsChecker');
const HttpHelper = require('./util/HttpHelper');
// This class provides access to Devices impCentral API methods.
class Devices extends Entities {
constructor() {
super();
this._validFilters = {
[Devices.FILTER_OWNER_ID] : false,
[Devices.FILTER_PRODUCT_ID] : false,
[Devices.FILTER_DEVICE_GROUP_ID] : false,
[Devices.FILTER_DEVICE_GROUP_OWNER_ID] : false,
[Devices.FILTER_DEVICE_GROUP_TYPE] : false
};
this._validUpdateAttributes = {
name : true
};
}
// Retrieves a list of devices associated with the logged-in account.
//
// Parameters:
// filters : Object Optional Key/Value filters that will be applied to the result list
// The valid keys are:
// 'filter[owner.id]' - filter by the Device owner
// 'filter[product.id]' - filter by the Product that holds the
// Device Group to which the Device is assigned
// 'filter[devicegroup.id]' - filter by the Device Group to
// which the Device is assigned
// 'filter[devicegroup.owner.id]' - filter by the owner of the Device Group
// to which the Device is assigned. The value can be the literal string 'null',
// meaning return only unassigned devices.
// 'filter[devicegroup.type]' - filter by the type of the Device Group
// to which the Device is assigned
// pageNumber : Number Optional pagination page number (starts at 1).
// If not specified, the default value is 1.
// pageSize : Number Optional pagination size - maximum number of items to return.
// If not specified, the default value is 20.
//
// Returns: Promise that resolves when the Devices list is successfully
// obtained, or rejects with an error
list(filters = null, pageNumber = null, pageSize = null) {
if (filters && Devices.FILTER_DEVICE_GROUP_TYPE in filters) {
const error = DeviceGroups.validateType(filters[Devices.FILTER_DEVICE_GROUP_TYPE], Devices.FILTER_DEVICE_GROUP_TYPE);
if (error) {
return Promise.reject(error);
}
}
return super.list(filters, pageNumber, pageSize);
}
// Possible filter keys for the list() filters:
static get FILTER_OWNER_ID() {
return 'filter[owner.id]';
}
static get FILTER_PRODUCT_ID() {
return 'filter[product.id]';
}
static get FILTER_DEVICE_GROUP_ID() {
return 'filter[devicegroup.id]';
}
static get FILTER_DEVICE_GROUP_OWNER_ID() {
return 'filter[devicegroup.owner.id]';
}
static get FILTER_DEVICE_GROUP_TYPE() {
return 'filter[devicegroup.type]';
}
// Retrieves a specific Device.
//
// Parameters:
// deviceId : String the Device's identifier. This can be a MAC address, an Agent ID,
// or the device ID.
//
// Returns: Promise that resolves when the Device is successfully obtained,
// or rejects with an error
get(deviceId) {
return super.get(deviceId);
}
// Updates a specific Device.
//
// Parameters:
// deviceId : String ID of the Device to be updated. This can be a MAC address, an Agent ID,
// or the device ID.
// attributes : Object Key/Value attributes of the Device that will be updated.
// The valid keys are:
// 'name' - the Device's name
//
// Returns: Promise that resolves when the Device is successfully updated,
// or rejects with an error
update(deviceId, attributes) {
const body = {
data : {
type : Devices._TYPE,
id : deviceId,
attributes : attributes
}
};
return super.update(deviceId, attributes, body);
}
// Removes a specific device from the logged-in account.
//
// Parameters:
// deviceId : String ID of the Device to be removed. This can be a MAC address, an Agent ID,
// or the device ID.
//
// Returns: Promise that resolves when the Device is successfully removed
// from the account, or rejects with an error
delete(deviceId) {
return super.delete(deviceId);
}
// Restarts a Device.
//
// Parameters:
// deviceId : String ID of the Device to be restarted. This can be a MAC address, an Agent ID,
// or the device ID.
//
// Returns: Promise that resolves when the Device was restarted,
// or rejects with an error
restart(deviceId) {
const error = ParamsChecker.validateNonEmpty(deviceId);
if (error) {
return Promise.reject(error);
}
return HttpHelper.post(this._getPath(deviceId) + '/restart');
}
// Conditionally restarts a Device.
// Sends a SHUTDOWN_NEWSQUIRREL message to a Device running a Deployment newer-than or equal-to the
// Device Group's min_supported_deployment, or restarts the device if it is running an older Deployment.
//
// Parameters:
// deviceId : String ID of the Device to be restarted. This can be a MAC address, an Agent ID,
// or the device ID.
//
// Returns: Promise that resolves when the Device was restarted,
// or rejects with an error
conditionalRestart(deviceId) {
const error = ParamsChecker.validateNonEmpty(deviceId);
if (error) {
return Promise.reject(error);
}
return HttpHelper.post(this._getPath(deviceId) + '/conditional_restart');
}
// Gets historical logs for a specific Device.
// A limited number of logs are kept for a limited period of time. All available logs will be
// returned in chronological order (most recent last).
//
// Parameters:
// deviceId : String the Device's identifier. This can be a MAC address, an Agent ID,
// or the device ID.
// pageNumber : Number Optional pagination page number (starts at 1).
// If not specified, the default value is 1.
// pageSize : Number Optional pagination size - maximum number of items to return.
// If not specified, the default value is 20.
//
// Returns: Promise that resolves when the Device logs are successfully obtained,
// or rejects with an error
getLogs(deviceId, pageNumber = null, pageSize = null) {
let error = ParamsChecker.validateNonEmpty(deviceId);
if (error) {
return Promise.reject(error);
}
error = ParamsChecker.validatePagination(pageNumber, pageSize);
if (error) {
return Promise.reject(error);
}
const query = HttpHelper.getPaginationQuery(pageNumber, pageSize);
return HttpHelper.get(this._getPath(deviceId) + '/logs', query);
}
_getPath(deviceId = null) {
if (deviceId) {
return '/devices/' + deviceId;
}
return '/devices';
}
// Returns Device resource type
static get _TYPE() {
return 'device';
}
}
module.exports = Devices;