Skip to content

Commit daf29aa

Browse files
committed
feat: changing to new device attributes
1 parent f2a1eef commit daf29aa

File tree

1 file changed

+61
-17
lines changed

1 file changed

+61
-17
lines changed

lib/Services/cayenne.js

Lines changed: 61 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ class Cayenne {
131131
* Begin Device Attribute Routes
132132
*/
133133

134+
/*
135+
* Begin Device Attribute Routes
136+
*/
137+
134138
/**
135139
* Gets all attribute groups and attributes for a device
136140
* @param {String} deviceId The device id to get attributes for
@@ -144,27 +148,37 @@ class Cayenne {
144148
}
145149

146150
/**
147-
* Create or Update an attribute group. Pass null to group_id to create a new group.
151+
* Update an attribute group. Pass null to group_id to create a new group.
148152
* @param {String} deviceId The id of the parent device
149153
* @param {String?} groupId Optional: Updates group, if null creates instead
150-
* @param {Object} payload The group to create or update
154+
* @param {Object} payload The group to update
151155
* @param {UUID?} payload.id Optional: The group id, will be overriden by group_id parameter
152156
* @param {String} payload.name The name of the group
153157
* @param {Integer} payload.order The display order of the group
154158
* @param {String?} payload.device_id Optional: The device id this group is attached to. Overriden by deviceId paramter
155159
* @param {UUID} payload.user_id The user id of the device owner
156160
* @param {UUID} payload.client_id The application id of the API call
157161
*/
158-
createOrUpdateGroup(deviceId, groupId, payload) {
159-
var url = null;
160-
if (groupId == null) {
161-
url = `${this.url}/things/${deviceId}/groups`;
162-
} else {
163-
url = `${this.url}/things/${deviceId}/groups/${groupId}`;
164-
}
162+
updateGroup(deviceId, groupId, payload) {
163+
const url = `${this.url}/things/${deviceId}/groups/${groupId}`;
164+
165165
return this.auth.send(this.service, 'PUT', url, { payload });
166166
}
167167

168+
/**
169+
* Create an attribute group. Pass null to group_id to create a new group.
170+
* @param {String} deviceId The id of the parent device
171+
* @param {Object} payload The group to create
172+
* @param {UUID?} payload.id Optional: The group id, will be overriden by group_id parameter
173+
* @param {String} payload.name The name of the group
174+
* @param {Integer} payload.order The display order of the group
175+
*/
176+
createGroup(deviceId, payload) {
177+
const url = `${this.url}/things/${deviceId}/groups`;
178+
179+
return this.auth.send(this.service, 'POST', url, { payload });
180+
}
181+
168182
/**
169183
* Delete a group
170184
* @param {String} deviceId
@@ -179,7 +193,6 @@ class Cayenne {
179193
* Create or Update an attribute. Pass null to attribute_id to create a new attribute.
180194
* @param {String} deviceId The id of the parent device
181195
* @param {String} groupId The id of the parent group
182-
* @param {String?} attributeId Optional: Updates attribute, if null creates instead
183196
* @param {Object} payload The attribute to create or update
184197
* @param {UUID?} payload.id Optional: The attribute id, will be overriden by attributeId parameter
185198
* @param {String} payload.name The name of the attribute
@@ -190,15 +203,32 @@ class Cayenne {
190203
* @param {String} payload.value The value, contents vary based on value_type
191204
* @param {String} payload.value_type The type of content contained in value string
192205
*/
193-
createOrUpdateAttribute(deviceId, groupId, attributeId, payload) {
194-
var url = null;
195-
if (attributeId == null) {
196-
url = `${this.url}/things/${deviceId}/groups/${groupId}/attributes`;
197-
} else {
198-
url = `${
206+
createAttribute(deviceId, groupId, payload) {
207+
const url = `${this.url}/things/${deviceId}/groups/${groupId}/attributes`;
208+
209+
return this.auth.send(this.service, 'POST', url, { payload });
210+
}
211+
212+
/**
213+
* Update an attribute. Pass null to attribute_id to create a new attribute.
214+
* @param {String} deviceId The id of the parent device
215+
* @param {String} groupId The id of the parent group
216+
* @param {String?} attributeId Updates attribute
217+
* @param {Object} payload The attribute to update
218+
* @param {UUID?} payload.id Optional: The attribute id, will be overriden by attributeId parameter
219+
* @param {String} payload.name The name of the attribute
220+
* @param {Integer} payload.order The display order of the attribute
221+
* @param {String?} payload.device_id Optional: The device id this attribute is attached to. Overriden by deviceId paramter
222+
* @param {UUID} payload.user_id The user id of the device owner
223+
* @param {UUID?} payload.group_id The group id of the parent group
224+
* @param {String} payload.value The value, contents vary based on value_type
225+
* @param {String} payload.value_type The type of content contained in value string
226+
*/
227+
updateAttribute(deviceId, groupId, attributeId, payload) {
228+
const url = `${
199229
this.url
200230
}/things/${deviceId}/groups/${groupId}/attributes/${attributeId}`;
201-
}
231+
202232
return this.auth.send(this.service, 'PUT', url, { payload });
203233
}
204234

@@ -215,10 +245,24 @@ class Cayenne {
215245
return this.auth.send(this.service, 'DELETE', url);
216246
}
217247

248+
/**
249+
* Get an attribute
250+
* @param {String} deviceId
251+
* @param {String} groupId
252+
* @param {String} attributeId
253+
*/
254+
getAttribute(deviceId, groupId, attributeId) {
255+
const url = `${
256+
this.url
257+
}/things/${deviceId}/groups/${groupId}/attributes/${attributeId}`;
258+
return this.auth.send(this.service, 'GET', url);
259+
}
260+
218261
/*
219262
* End Device Attribute Routes
220263
*/
221264

265+
222266
/*
223267
* Begin Registry Routes
224268
*/

0 commit comments

Comments
 (0)