Skip to content

Commit a849246

Browse files
authored
Merge pull request #14 from myDevicesIoT/feat/datatypes-v2
feat: datatypes v2 retrieval
2 parents da1af5b + f5cc58c commit a849246

File tree

1 file changed

+100
-19
lines changed

1 file changed

+100
-19
lines changed

lib/Services/cayenne.js

Lines changed: 100 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ class Cayenne {
129129
* Begin Device Attribute Routes
130130
*/
131131

132+
/*
133+
* Begin Device Attribute Routes
134+
*/
135+
132136
/**
133137
* Gets all attribute groups and attributes for a device
134138
* @param {String} deviceId The device id to get attributes for
@@ -142,27 +146,37 @@ class Cayenne {
142146
}
143147

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

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

@@ -213,6 +243,19 @@ class Cayenne {
213243
return this.auth.send(this.service, 'DELETE', url);
214244
}
215245

246+
/**
247+
* Get an attribute
248+
* @param {String} deviceId
249+
* @param {String} groupId
250+
* @param {String} attributeId
251+
*/
252+
getAttribute(deviceId, groupId, attributeId) {
253+
const url = `${
254+
this.url
255+
}/things/${deviceId}/groups/${groupId}/attributes/${attributeId}`;
256+
return this.auth.send(this.service, 'GET', url);
257+
}
258+
216259
/*
217260
* End Device Attribute Routes
218261
*/
@@ -295,6 +338,44 @@ class Cayenne {
295338
return this.auth.send(this.service, 'GET', url);
296339
}
297340

341+
/**
342+
* Gets V2 all things data types
343+
* @param {Object} query
344+
* @param {Number} [query.limit]
345+
* @param {Number} [query.page]
346+
*/
347+
getThingDataTypesV2(query) {
348+
const url = `${this.url}/v2/things/datatypes`;
349+
return this.auth.send(this.service, 'GET', url, { query });
350+
}
351+
352+
/**
353+
* Get V2 one things data type
354+
*/
355+
getThingDataTypeV2(id) {
356+
const url = `${this.url}/v2/things/datatypes/${id}`;
357+
return this.auth.send(this.service, 'GET', url);
358+
}
359+
360+
/**
361+
* Get V2 one things data type properties
362+
* @param {Object} query
363+
* @param {Number} [query.limit]
364+
* @param {Number} [query.page]
365+
*/
366+
getThingDataTypePropertiesV2(id, query) {
367+
const url = `${this.url}/v2/things/datatypes/${id}/properties`;
368+
return this.auth.send(this.service, 'GET', url, { query });
369+
}
370+
371+
/**
372+
* Get V2 one things data type property by it's id
373+
*/
374+
getThingDataTypePropertyV2(id, pid) {
375+
const url = `${this.url}/v2/things/datatypes/${id}/properties/${pid}`;
376+
return this.auth.send(this.service, 'GET', url);
377+
}
378+
298379
/**
299380
* Gets thing types by query
300381
* @param {Object} query

0 commit comments

Comments
 (0)