Skip to content

Commit 15dc357

Browse files
committed
Add support for CloudProtocol v2
Add support for CloudProtocol v2 by using CBOR label names for SenML objects property names. This is required to support arduino-libraries/ArduinoCloudThing#6 (the same change on the ArduinoCloudThing library for sketches).
1 parent 0d0ee6a commit 15dc357

File tree

1 file changed

+74
-3
lines changed

1 file changed

+74
-3
lines changed

src/index.js

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,68 @@ const closeCloudMonitor = (deviceId) => {
331331
return unsubscribe(cloudMonitorOutputTopic);
332332
};
333333

334-
const sendProperty = (thingId, name, value, timestamp) => {
334+
const toCloudProtocolV2 = (cborValue) => {
335+
const cloudV2CBORValue = {};
336+
let cborLabel = null;
337+
338+
Object.keys(cborValue).forEach((label) => {
339+
switch (label) {
340+
case 'bn':
341+
cborLabel = -2;
342+
break;
343+
case 'bt':
344+
cborLabel = -3;
345+
break;
346+
case 'bu':
347+
cborLabel = -4;
348+
break;
349+
case 'bv':
350+
cborLabel = -5;
351+
break;
352+
case 'bs':
353+
cborLabel = -6;
354+
break;
355+
case 'bver':
356+
cborLabel = -1;
357+
break;
358+
case 'n':
359+
cborLabel = 0;
360+
break;
361+
case 'u':
362+
cborLabel = 1;
363+
break;
364+
case 'v':
365+
cborLabel = 2;
366+
break;
367+
case 'vs':
368+
cborLabel = 3;
369+
break;
370+
case 'vb':
371+
cborLabel = 4;
372+
break;
373+
case 'vd':
374+
cborLabel = 8;
375+
break;
376+
case 's':
377+
cborLabel = 5;
378+
break;
379+
case 't':
380+
cborLabel = 6;
381+
break;
382+
case 'ut':
383+
cborLabel = 7;
384+
break;
385+
default:
386+
cborLabel = label;
387+
}
388+
389+
cloudV2CBORValue[cborLabel] = cborValue[label];
390+
});
391+
392+
return cloudV2CBORValue;
393+
};
394+
395+
const sendProperty = (thingId, name, value, timestamp, useCloudProtocolV2 = true) => {
335396
const propertyInputTopic = `/a/t/${thingId}/e/i`;
336397

337398
if (timestamp && !Number.isInteger(timestamp)) {
@@ -342,7 +403,7 @@ const sendProperty = (thingId, name, value, timestamp) => {
342403
throw new Error('Name must be a valid string');
343404
}
344405

345-
const cborValue = {
406+
let cborValue = {
346407
bt: timestamp || new Date().getTime(),
347408
n: name,
348409
};
@@ -361,10 +422,14 @@ const sendProperty = (thingId, name, value, timestamp) => {
361422
break;
362423
}
363424

425+
if (useCloudProtocolV2) {
426+
cborValue = toCloudProtocolV2(cborValue);
427+
}
428+
364429
return sendMessage(propertyInputTopic, CBOR.encode([cborValue]));
365430
};
366431

367-
const getSenml = (deviceId, name, value, timestamp) => {
432+
const getSenml = (deviceId, name, value, timestamp, useCloudProtocolV2 = true) => {
368433
if (timestamp && !Number.isInteger(timestamp)) {
369434
throw new Error('Timestamp must be Integer');
370435
}
@@ -395,6 +460,12 @@ const getSenml = (deviceId, name, value, timestamp) => {
395460
default:
396461
break;
397462
}
463+
464+
465+
if (useCloudProtocolV2) {
466+
return toCloudProtocolV2(senMl);
467+
}
468+
398469
return senMl;
399470
};
400471

0 commit comments

Comments
 (0)