Skip to content

Commit a015e27

Browse files
authoredFeb 20, 2019
Merge pull request #24 from arduino/sendpropertyasdevice-cloudprotocolv2
Add CloudProtocl v2 support to sendPropertyAsDevice
2 parents 2f0aabf + 991a9a9 commit a015e27

File tree

5 files changed

+507
-436
lines changed

5 files changed

+507
-436
lines changed
 

‎README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ import ArduinoCloud from 'arduino-iot-js';
1717

1818
// connect establishes a connection with mqtt, using token as the password
1919
// options = {
20-
// host: 'BROKER_URL', // Default is wss.iot.arduino.cc
21-
// port: BROKER_PORT, // Default is 8443
22-
// ssl: true/false, // Default is true
23-
// token: 'YOUR_BEARER_TOKEN' // Required!
24-
// apiUrl: 'AUTH SERVER URL', // Default is https://auth.arduino.cc
25-
// onDisconnect: message => { /* Disconnection callback */ }
20+
// host: 'BROKER_URL', // Default is wss.iot.arduino.cc
21+
// port: BROKER_PORT, // Default is 8443
22+
// ssl: true/false, // Default is true
23+
// token: 'YOUR_BEARER_TOKEN' // Required!
24+
// apiUrl: 'AUTH SERVER URL', // Default is https://auth.arduino.cc
25+
// onDisconnect: message => { /* Disconnection callback */ },
26+
// useCloudProtocolV2: true/false, // Default is false
2627
// }
2728
ArduinoCloud.connect(options).then(() => {
2829
// Connected

‎src/index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ const connect = options => new Promise((resolve, reject) => {
8181
onDisconnect: options.onDisconnect,
8282
onTrace: options.onTrace,
8383
onConnected: options.onConnected,
84+
useCloudProtocolV2: options.useCloudProtocolV2 || false,
8485
};
8586

8687
connectionOptions = opts;
@@ -392,7 +393,7 @@ const toCloudProtocolV2 = (cborValue) => {
392393
return cloudV2CBORValue;
393394
};
394395

395-
const sendProperty = (thingId, name, value, timestamp, useCloudProtocolV2 = false) => {
396+
const sendProperty = (thingId, name, value, timestamp) => {
396397
const propertyInputTopic = `/a/t/${thingId}/e/i`;
397398

398399
if (timestamp && !Number.isInteger(timestamp)) {
@@ -422,14 +423,14 @@ const sendProperty = (thingId, name, value, timestamp, useCloudProtocolV2 = fals
422423
break;
423424
}
424425

425-
if (useCloudProtocolV2) {
426+
if (connectionOptions.useCloudProtocolV2) {
426427
cborValue = toCloudProtocolV2(cborValue);
427428
}
428429

429430
return sendMessage(propertyInputTopic, CBOR.encode([cborValue]));
430431
};
431432

432-
const getSenml = (deviceId, name, value, timestamp, useCloudProtocolV2 = false) => {
433+
const getSenml = (deviceId, name, value, timestamp) => {
433434
if (timestamp && !Number.isInteger(timestamp)) {
434435
throw new Error('Timestamp must be Integer');
435436
}
@@ -462,7 +463,7 @@ const getSenml = (deviceId, name, value, timestamp, useCloudProtocolV2 = false)
462463
}
463464

464465

465-
if (useCloudProtocolV2) {
466+
if (connectionOptions.useCloudProtocolV2) {
466467
return toCloudProtocolV2(senMl);
467468
}
468469

‎test/arduino-cloud.test.js

Lines changed: 84 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -33,109 +33,113 @@ const propertyStrVal = 'ok';
3333
const propertyBoolName = 'boolean';
3434
const propertyBoolVal = true;
3535

36-
it('ArduinoCloud connection', (done) => {
36+
describe('Test the library basic functionalities', () => {
37+
afterAll(() => ArduinoCloud.disconnect());
38+
39+
it('ArduinoCloud connection', (done) => {
3740
/* global token */
38-
ArduinoCloud.connect({
39-
token,
40-
onDisconnect: (message) => {
41-
if (message.errorCode !== 0) {
42-
throw Error(message);
43-
}
44-
},
45-
})
46-
.then(() => {
47-
done();
41+
ArduinoCloud.connect({
42+
token,
43+
onDisconnect: (message) => {
44+
if (message.errorCode !== 0) {
45+
throw Error(message);
46+
}
47+
},
4848
})
49-
.catch((error) => {
50-
throw new Error(error);
51-
});
52-
});
49+
.then(() => {
50+
done();
51+
})
52+
.catch((error) => {
53+
throw new Error(error);
54+
});
55+
});
5356

54-
it('Property name must be a string in sendProperty', (done) => {
55-
try {
56-
ArduinoCloud.sendProperty(deviceId, undefined, propertyIntValue);
57-
} catch (error) {
58-
if (error.message === 'Name must be a valid string') {
59-
done();
57+
it('Property name must be a string in sendProperty', (done) => {
58+
try {
59+
ArduinoCloud.sendProperty(deviceId, undefined, propertyIntValue);
60+
} catch (error) {
61+
if (error.message === 'Name must be a valid string') {
62+
done();
63+
}
6064
}
61-
}
62-
});
65+
});
6366

64-
it('Simulate client write to cloud monitor', (done) => {
65-
ArduinoCloud.writeCloudMonitor(deviceId, `this is a test ${Math.random()}`).then(() => {
66-
done();
67-
}, (error) => {
68-
throw new Error(error);
67+
it('Simulate client write to cloud monitor', (done) => {
68+
ArduinoCloud.writeCloudMonitor(deviceId, `this is a test ${Math.random()}`).then(() => {
69+
done();
70+
}, (error) => {
71+
throw new Error(error);
72+
});
6973
});
70-
});
7174

72-
it('Simulate device write to cloud monitor', (done) => {
73-
const cloudMonitorInputTopic = `/a/d/${deviceId}/s/o`;
74-
ArduinoCloud.sendMessage(cloudMonitorInputTopic, `this is a test ${Math.random()}`).then(() => {
75-
done();
76-
}, (error) => {
77-
throw new Error(error);
75+
it('Simulate device write to cloud monitor', (done) => {
76+
const cloudMonitorInputTopic = `/a/d/${deviceId}/s/o`;
77+
ArduinoCloud.sendMessage(cloudMonitorInputTopic, `this is a test ${Math.random()}`).then(() => {
78+
done();
79+
}, (error) => {
80+
throw new Error(error);
81+
});
7882
});
79-
});
8083

81-
it('Simulate device write and client read his message from cloud monitor', (done) => {
82-
const cloudMonitorInputTopic = `/a/d/${deviceId}/s/o`;
84+
it('Simulate device write and client read his message from cloud monitor', (done) => {
85+
const cloudMonitorInputTopic = `/a/d/${deviceId}/s/o`;
8386

84-
const cb = () => {
87+
const cb = () => {
8588
// console.log(`[${new Date()}] Message from monitor: ${message}`);
86-
done();
87-
};
89+
done();
90+
};
8891

89-
ArduinoCloud.openCloudMonitor(deviceId, cb).then(() => {
92+
ArduinoCloud.openCloudMonitor(deviceId, cb).then(() => {
9093
// console.log(`Subscribed to topic: ${topic}`);
91-
const message = `This is a test ${new Date()}`;
92-
ArduinoCloud.sendMessage(cloudMonitorInputTopic, message).then(() => {
93-
// console.log(`[${new Date()}] Message sent to monitor: [${message}]`);
94+
const message = `This is a test ${new Date()}`;
95+
ArduinoCloud.sendMessage(cloudMonitorInputTopic, message).then(() => {
96+
// console.log(`[${new Date()}] Message sent to monitor: [${message}]`);
97+
}, (error) => {
98+
throw new Error(error);
99+
});
94100
}, (error) => {
95101
throw new Error(error);
96102
});
97-
}, (error) => {
98-
throw new Error(error);
99103
});
100-
});
101104

102-
it('Simulate client read integer property sent by device', (done) => {
103-
ArduinoCloud.onPropertyValue(thingId, propertyIntName, (value) => {
104-
if (value === propertyIntValue) {
105-
done();
106-
}
107-
}).then(() => {
108-
ArduinoCloud.sendPropertyAsDevice(deviceId, thingId, propertyIntName, propertyIntValue);
105+
it('Simulate client read integer property sent by device', (done) => {
106+
ArduinoCloud.onPropertyValue(thingId, propertyIntName, (value) => {
107+
if (value === propertyIntValue) {
108+
done();
109+
}
110+
}).then(() => {
111+
ArduinoCloud.sendPropertyAsDevice(deviceId, thingId, propertyIntName, propertyIntValue);
112+
});
109113
});
110-
});
111114

112-
it('Simulate client read float property sent by device', (done) => {
113-
ArduinoCloud.onPropertyValue(thingId, propertyFloatName, (value) => {
114-
if (value === propertyFloatVal) {
115-
done();
116-
}
117-
}).then(() => {
118-
ArduinoCloud.sendPropertyAsDevice(deviceId, thingId, propertyFloatName, propertyFloatVal);
115+
it('Simulate client read float property sent by device', (done) => {
116+
ArduinoCloud.onPropertyValue(thingId, propertyFloatName, (value) => {
117+
if (value === propertyFloatVal) {
118+
done();
119+
}
120+
}).then(() => {
121+
ArduinoCloud.sendPropertyAsDevice(deviceId, thingId, propertyFloatName, propertyFloatVal);
122+
});
119123
});
120-
});
121124

122-
it('Simulate client read string property sent by device', (done) => {
123-
ArduinoCloud.onPropertyValue(thingId, propertyStrName, (value) => {
124-
if (value === propertyStrVal) {
125-
done();
126-
}
127-
}).then(() => {
128-
ArduinoCloud.sendPropertyAsDevice(deviceId, thingId, propertyStrName, propertyStrVal);
125+
it('Simulate client read string property sent by device', (done) => {
126+
ArduinoCloud.onPropertyValue(thingId, propertyStrName, (value) => {
127+
if (value === propertyStrVal) {
128+
done();
129+
}
130+
}).then(() => {
131+
ArduinoCloud.sendPropertyAsDevice(deviceId, thingId, propertyStrName, propertyStrVal);
132+
});
129133
});
130-
});
131134

132-
it('Simulate client read boolean property sent by device', (done) => {
133-
ArduinoCloud.onPropertyValue(thingId, propertyBoolName, (value) => {
134-
if (value === propertyBoolVal) {
135-
ArduinoCloud.disconnect();
136-
done();
137-
}
138-
}).then(() => {
139-
ArduinoCloud.sendPropertyAsDevice(deviceId, thingId, propertyBoolName, propertyBoolVal);
135+
it('Simulate client read boolean property sent by device', (done) => {
136+
ArduinoCloud.onPropertyValue(thingId, propertyBoolName, (value) => {
137+
if (value === propertyBoolVal) {
138+
ArduinoCloud.disconnect();
139+
done();
140+
}
141+
}).then(() => {
142+
ArduinoCloud.sendPropertyAsDevice(deviceId, thingId, propertyBoolName, propertyBoolVal);
143+
});
140144
});
141145
});

‎test/cbor-cloudprotocolv2.test.js

Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
/*
2+
* Copyright 2018 ARDUINO SA (http://www.arduino.cc/)
3+
* This file is part of arduino-iot-js.
4+
* Copyright (c) 2018
5+
* Authors: Fabrizio Mirabito
6+
*
7+
* This software is released under:
8+
* The GNU General Public License, which covers the main part of
9+
* arduino-iot-js
10+
* The terms of this license can be found at:
11+
* https://www.gnu.org/licenses/gpl-3.0.en.html
12+
*
13+
* You can be released from the requirements of the above licenses by purchasing
14+
* a commercial license. Buying such a license is mandatory if you want to modify or
15+
* otherwise use the software for commercial activities involving the Arduino
16+
* software without disclosing the source code of your own applications. To purchase
17+
* a commercial license, send an email to license@arduino.cc.
18+
*
19+
*/
20+
const ArduinoCloud = require('../dist/index.js');
21+
22+
const deviceId = '1f4ced70-53ad-4b29-b221-1b0abbdfc757';
23+
const timestamp = 1536743296;
24+
25+
const base64toHEX = (base64) => {
26+
const raw = atob(base64);
27+
let HEX = '';
28+
for (let i = 0; i < raw.length; i += 1) {
29+
const hex = raw.charCodeAt(i).toString(16);
30+
const hexPart = (hex.length === 2 ? hex : `0${hex}`).toUpperCase();
31+
HEX += `0x${hexPart},`;
32+
}
33+
return HEX;
34+
};
35+
36+
describe('Test CBOR encoding using CloudProtocol v2', () => {
37+
beforeAll(() => ArduinoCloud.connect({
38+
// eslint-disable-next-line no-undef
39+
token,
40+
useCloudProtocolV2: true,
41+
onDisconnect: (message) => {
42+
if (message.errorCode !== 0) {
43+
throw Error(message);
44+
}
45+
},
46+
}));
47+
48+
afterAll(() => ArduinoCloud.disconnect());
49+
50+
// CloudProtocol v2
51+
52+
it('[CloudProtocol v2] Generate a valid cbor for a senml string property with basename', () => {
53+
const output = '0x81,0xA4,0x61,0x30,0x6B,0x74,0x65,0x73,0x74,0x5F,0x73,0x74,0x72,0x69,0x6E,0x67,0x61,0x33,0x71,0x74,0x65,0x73,0x74,0x5F,0x73,0x74,0x72,0x69,0x6E,0x67,0x20,0x76,0x61,0x6C,0x75,0x65,0x62,0x2D,0x33,0x1A,0x5B,0x98,0xD7,0x80,0x62,0x2D,0x32,0x78,0x2D,0x75,0x72,0x6E,0x3A,0x75,0x75,0x69,0x64,0x3A,0x31,0x66,0x34,0x63,0x65,0x64,0x37,0x30,0x2D,0x35,0x33,0x61,0x64,0x2D,0x34,0x62,0x32,0x39,0x2D,0x62,0x32,0x32,0x31,0x2D,0x31,0x62,0x30,0x61,0x62,0x62,0x64,0x66,0x63,0x37,0x35,0x37,';
54+
const senMl = ArduinoCloud.getSenml(deviceId, 'test_string', 'test_string value', timestamp, true);
55+
console.log([senMl]);
56+
const cborbase64 = ArduinoCloud.getCborValue([senMl]);
57+
console.log(cborbase64);
58+
const cborHex = base64toHEX(cborbase64);
59+
console.log(cborHex);
60+
expect(cborHex).toStrictEqual(output);
61+
});
62+
63+
it('[CloudProtocol v2] Generate a valid cbor for a senml string property without basename', () => {
64+
const output = '0x81,0xA3,0x61,0x30,0x6B,0x74,0x65,0x73,0x74,0x5F,0x73,0x74,0x72,0x69,0x6E,0x67,0x61,0x33,0x71,0x74,0x65,0x73,0x74,0x5F,0x73,0x74,0x72,0x69,0x6E,0x67,0x20,0x76,0x61,0x6C,0x75,0x65,0x62,0x2D,0x33,0x1A,0x5B,0x98,0xD7,0x80,';
65+
const senMl = ArduinoCloud.getSenml(null, 'test_string', 'test_string value', timestamp, true);
66+
console.log([senMl]);
67+
const cborbase64 = ArduinoCloud.getCborValue([senMl]);
68+
console.log(cborbase64);
69+
const cborHex = base64toHEX(cborbase64);
70+
console.log(cborHex);
71+
expect(cborHex).toStrictEqual(output);
72+
});
73+
74+
75+
it('[CloudProtocol v2] Generate a valid cbor for a senml unsigned int property with basename', () => {
76+
const output = '0x81,0xA4,0x61,0x30,0x69,0x74,0x65,0x73,0x74,0x5F,0x75,0x69,0x6E,0x74,0x61,0x32,0x03,0x62,0x2D,0x33,0x1A,0x5B,0x98,0xD7,0x80,0x62,0x2D,0x32,0x78,0x2D,0x75,0x72,0x6E,0x3A,0x75,0x75,0x69,0x64,0x3A,0x31,0x66,0x34,0x63,0x65,0x64,0x37,0x30,0x2D,0x35,0x33,0x61,0x64,0x2D,0x34,0x62,0x32,0x39,0x2D,0x62,0x32,0x32,0x31,0x2D,0x31,0x62,0x30,0x61,0x62,0x62,0x64,0x66,0x63,0x37,0x35,0x37,';
77+
const senMl = ArduinoCloud.getSenml(deviceId, 'test_uint', 3, timestamp, true);
78+
console.log([senMl]);
79+
const cborbase64 = ArduinoCloud.getCborValue([senMl]);
80+
console.log(cborbase64);
81+
const cborHex = base64toHEX(cborbase64);
82+
console.log(cborHex);
83+
expect(cborHex).toStrictEqual(output);
84+
});
85+
86+
87+
it('[CloudProtocol v2] Generate a valid cbor for a senml unsigned int property without basename', () => {
88+
const output = '0x81,0xA3,0x61,0x30,0x69,0x74,0x65,0x73,0x74,0x5F,0x75,0x69,0x6E,0x74,0x61,0x32,0x03,0x62,0x2D,0x33,0x1A,0x5B,0x98,0xD7,0x80,';
89+
const senMl = ArduinoCloud.getSenml(null, 'test_uint', 3, timestamp, true);
90+
console.log([senMl]);
91+
const cborbase64 = ArduinoCloud.getCborValue([senMl]);
92+
console.log(cborbase64);
93+
const cborHex = base64toHEX(cborbase64);
94+
console.log(cborHex);
95+
expect(cborHex).toStrictEqual(output);
96+
});
97+
98+
it('[CloudProtocol v2] Generate a valid cbor for a senml signed int property with basename', () => {
99+
const output = '0x81,0xA4,0x61,0x30,0x69,0x74,0x65,0x73,0x74,0x5F,0x73,0x69,0x6E,0x74,0x61,0x32,0x22,0x62,0x2D,0x33,0x1A,0x5B,0x98,0xD7,0x80,0x62,0x2D,0x32,0x78,0x2D,0x75,0x72,0x6E,0x3A,0x75,0x75,0x69,0x64,0x3A,0x31,0x66,0x34,0x63,0x65,0x64,0x37,0x30,0x2D,0x35,0x33,0x61,0x64,0x2D,0x34,0x62,0x32,0x39,0x2D,0x62,0x32,0x32,0x31,0x2D,0x31,0x62,0x30,0x61,0x62,0x62,0x64,0x66,0x63,0x37,0x35,0x37,';
100+
const senMl = ArduinoCloud.getSenml(deviceId, 'test_sint', -3, timestamp, true);
101+
console.log([senMl]);
102+
const cborbase64 = ArduinoCloud.getCborValue([senMl]);
103+
console.log(cborbase64);
104+
const cborHex = base64toHEX(cborbase64);
105+
console.log(cborHex);
106+
expect(cborHex).toStrictEqual(output);
107+
});
108+
109+
it('[CloudProtocol v2] Generate a valid cbor for a senml signed int property without basename', () => {
110+
const output = '0x81,0xA3,0x61,0x30,0x69,0x74,0x65,0x73,0x74,0x5F,0x73,0x69,0x6E,0x74,0x61,0x32,0x22,0x62,0x2D,0x33,0x1A,0x5B,0x98,0xD7,0x80,';
111+
const senMl = ArduinoCloud.getSenml(null, 'test_sint', -3, timestamp, true);
112+
console.log([senMl]);
113+
const cborbase64 = ArduinoCloud.getCborValue([senMl]);
114+
console.log(cborbase64);
115+
const cborHex = base64toHEX(cborbase64);
116+
console.log(cborHex);
117+
expect(cborHex).toStrictEqual(output);
118+
});
119+
120+
121+
it('[CloudProtocol v2] Generate a valid cbor for a senml float property with basename', () => {
122+
const output = '0x81,0xA4,0x61,0x30,0x6A,0x74,0x65,0x73,0x74,0x5F,0x66,0x6C,0x6F,0x61,0x74,0x61,0x32,0xFB,0x40,0x0C,0x00,0x00,0x00,0x00,0x00,0x00,0x62,0x2D,0x33,0x1A,0x5B,0x98,0xD7,0x80,0x62,0x2D,0x32,0x78,0x2D,0x75,0x72,0x6E,0x3A,0x75,0x75,0x69,0x64,0x3A,0x31,0x66,0x34,0x63,0x65,0x64,0x37,0x30,0x2D,0x35,0x33,0x61,0x64,0x2D,0x34,0x62,0x32,0x39,0x2D,0x62,0x32,0x32,0x31,0x2D,0x31,0x62,0x30,0x61,0x62,0x62,0x64,0x66,0x63,0x37,0x35,0x37,';
123+
const senMl = ArduinoCloud.getSenml(deviceId, 'test_float', 3.5, timestamp, true);
124+
console.log([senMl]);
125+
const cborbase64 = ArduinoCloud.getCborValue([senMl]);
126+
console.log(cborbase64);
127+
const cborHex = base64toHEX(cborbase64);
128+
console.log(cborHex);
129+
expect(cborHex).toStrictEqual(output);
130+
});
131+
132+
it('[CloudProtocol v2] Generate a valid cbor for a senml float property without basename', () => {
133+
const output = '0x81,0xA3,0x61,0x30,0x6A,0x74,0x65,0x73,0x74,0x5F,0x66,0x6C,0x6F,0x61,0x74,0x61,0x32,0xFB,0x40,0x0C,0x00,0x00,0x00,0x00,0x00,0x00,0x62,0x2D,0x33,0x1A,0x5B,0x98,0xD7,0x80,';
134+
const senMl = ArduinoCloud.getSenml(null, 'test_float', 3.5, timestamp, true);
135+
console.log([senMl]);
136+
const cborbase64 = ArduinoCloud.getCborValue([senMl]);
137+
console.log(cborbase64);
138+
const cborHex = base64toHEX(cborbase64);
139+
console.log(cborHex);
140+
expect(cborHex).toStrictEqual(output);
141+
});
142+
143+
144+
it('[CloudProtocol v2] Generate a valid cbor for a senml double property with basename', () => {
145+
const output = '0x81,0xA4,0x61,0x30,0x6B,0x74,0x65,0x73,0x74,0x5F,0x64,0x6F,0x75,0x62,0x6C,0x65,0x61,0x32,0xFB,0x40,0x08,0xFC,0xD6,0xE9,0xBA,0x37,0xB3,0x62,0x2D,0x33,0x1A,0x5B,0x98,0xD7,0x80,0x62,0x2D,0x32,0x78,0x2D,0x75,0x72,0x6E,0x3A,0x75,0x75,0x69,0x64,0x3A,0x31,0x66,0x34,0x63,0x65,0x64,0x37,0x30,0x2D,0x35,0x33,0x61,0x64,0x2D,0x34,0x62,0x32,0x39,0x2D,0x62,0x32,0x32,0x31,0x2D,0x31,0x62,0x30,0x61,0x62,0x62,0x64,0x66,0x63,0x37,0x35,0x37,';
146+
const senMl = ArduinoCloud.getSenml(deviceId, 'test_double', 3.1234567890123456, timestamp, true);
147+
console.log([senMl]);
148+
const cborbase64 = ArduinoCloud.getCborValue([senMl]);
149+
console.log(cborbase64);
150+
const cborHex = base64toHEX(cborbase64);
151+
console.log(cborHex);
152+
expect(cborHex).toStrictEqual(output);
153+
});
154+
155+
it('[CloudProtocol v2] Generate a valid cbor for a senml double property without basename', () => {
156+
const output = '0x81,0xA3,0x61,0x30,0x6B,0x74,0x65,0x73,0x74,0x5F,0x64,0x6F,0x75,0x62,0x6C,0x65,0x61,0x32,0xFB,0x40,0x08,0xFC,0xD6,0xE9,0xBA,0x37,0xB3,0x62,0x2D,0x33,0x1A,0x5B,0x98,0xD7,0x80,';
157+
const senMl = ArduinoCloud.getSenml(null, 'test_double', 3.1234567890123456, timestamp, true);
158+
console.log([senMl]);
159+
const cborbase64 = ArduinoCloud.getCborValue([senMl]);
160+
console.log(cborbase64);
161+
const cborHex = base64toHEX(cborbase64);
162+
console.log(cborHex);
163+
expect(cborHex).toStrictEqual(output);
164+
});
165+
166+
it('[CloudProtocol v2] Generate a valid cbor for a senml boolean property with basename', () => {
167+
const output = '0x81,0xA4,0x61,0x30,0x69,0x74,0x65,0x73,0x74,0x5F,0x62,0x6F,0x6F,0x6C,0x61,0x34,0xF5,0x62,0x2D,0x33,0x1A,0x5B,0x98,0xD7,0x80,0x62,0x2D,0x32,0x78,0x2D,0x75,0x72,0x6E,0x3A,0x75,0x75,0x69,0x64,0x3A,0x31,0x66,0x34,0x63,0x65,0x64,0x37,0x30,0x2D,0x35,0x33,0x61,0x64,0x2D,0x34,0x62,0x32,0x39,0x2D,0x62,0x32,0x32,0x31,0x2D,0x31,0x62,0x30,0x61,0x62,0x62,0x64,0x66,0x63,0x37,0x35,0x37,';
168+
const senMl = ArduinoCloud.getSenml(deviceId, 'test_bool', true, timestamp, true);
169+
console.log([senMl]);
170+
const cborbase64 = ArduinoCloud.getCborValue([senMl]);
171+
console.log(cborbase64);
172+
const cborHex = base64toHEX(cborbase64);
173+
console.log(cborHex);
174+
expect(cborHex).toStrictEqual(output);
175+
});
176+
177+
it('[CloudProtocol v2] Generate a valid cbor for a senml boolean property without basename', () => {
178+
const output = '0x81,0xA3,0x61,0x30,0x69,0x74,0x65,0x73,0x74,0x5F,0x62,0x6F,0x6F,0x6C,0x61,0x34,0xF5,0x62,0x2D,0x33,0x1A,0x5B,0x98,0xD7,0x80,';
179+
const senMl = ArduinoCloud.getSenml(null, 'test_bool', true, timestamp, true);
180+
console.log([senMl]);
181+
const cborbase64 = ArduinoCloud.getCborValue([senMl]);
182+
console.log(cborbase64);
183+
const cborHex = base64toHEX(cborbase64);
184+
console.log(cborHex);
185+
expect(cborHex).toStrictEqual(output);
186+
});
187+
188+
189+
it('[CloudProtocol v2] Generate a valid cbor for multiple properties with basename', () => {
190+
const output = '0x86,0xA4,0x61,0x30,0x69,0x74,0x65,0x73,0x74,0x5F,0x75,0x69,0x6E,0x74,0x61,0x32,0x04,0x62,0x2D,0x33,0x1A,0x5B,0x98,0xD7,0x80,0x62,0x2D,0x32,0x78,0x2D,0x75,0x72,0x6E,0x3A,0x75,0x75,0x69,0x64,0x3A,0x31,0x66,0x34,0x63,0x65,0x64,0x37,0x30,0x2D,0x35,0x33,0x61,0x64,0x2D,0x34,0x62,0x32,0x39,0x2D,0x62,0x32,0x32,0x31,0x2D,0x31,0x62,0x30,0x61,0x62,0x62,0x64,0x66,0x63,0x37,0x35,0x37,0xA4,0x61,0x30,0x69,0x74,0x65,0x73,0x74,0x5F,0x73,0x69,0x6E,0x74,0x61,0x32,0x23,0x62,0x2D,0x33,0x1A,0x5B,0x98,0xD7,0x80,0x62,0x2D,0x32,0x78,0x2D,0x75,0x72,0x6E,0x3A,0x75,0x75,0x69,0x64,0x3A,0x31,0x66,0x34,0x63,0x65,0x64,0x37,0x30,0x2D,0x35,0x33,0x61,0x64,0x2D,0x34,0x62,0x32,0x39,0x2D,0x62,0x32,0x32,0x31,0x2D,0x31,0x62,0x30,0x61,0x62,0x62,0x64,0x66,0x63,0x37,0x35,0x37,0xA4,0x61,0x30,0x6A,0x74,0x65,0x73,0x74,0x5F,0x66,0x6C,0x6F,0x61,0x74,0x61,0x32,0xFB,0x40,0x12,0x00,0x00,0x00,0x00,0x00,0x00,0x62,0x2D,0x33,0x1A,0x5B,0x98,0xD7,0x80,0x62,0x2D,0x32,0x78,0x2D,0x75,0x72,0x6E,0x3A,0x75,0x75,0x69,0x64,0x3A,0x31,0x66,0x34,0x63,0x65,0x64,0x37,0x30,0x2D,0x35,0x33,0x61,0x64,0x2D,0x34,0x62,0x32,0x39,0x2D,0x62,0x32,0x32,0x31,0x2D,0x31,0x62,0x30,0x61,0x62,0x62,0x64,0x66,0x63,0x37,0x35,0x37,0xA4,0x61,0x30,0x6B,0x74,0x65,0x73,0x74,0x5F,0x73,0x74,0x72,0x69,0x6E,0x67,0x61,0x33,0x6A,0x74,0x65,0x73,0x74,0x20,0x76,0x61,0x6C,0x75,0x65,0x62,0x2D,0x33,0x1A,0x5B,0x98,0xD7,0x80,0x62,0x2D,0x32,0x78,0x2D,0x75,0x72,0x6E,0x3A,0x75,0x75,0x69,0x64,0x3A,0x31,0x66,0x34,0x63,0x65,0x64,0x37,0x30,0x2D,0x35,0x33,0x61,0x64,0x2D,0x34,0x62,0x32,0x39,0x2D,0x62,0x32,0x32,0x31,0x2D,0x31,0x62,0x30,0x61,0x62,0x62,0x64,0x66,0x63,0x37,0x35,0x37,0xA4,0x61,0x30,0x69,0x74,0x65,0x73,0x74,0x5F,0x62,0x6F,0x6F,0x6C,0x61,0x34,0xF5,0x62,0x2D,0x33,0x1A,0x5B,0x98,0xD7,0x80,0x62,0x2D,0x32,0x78,0x2D,0x75,0x72,0x6E,0x3A,0x75,0x75,0x69,0x64,0x3A,0x31,0x66,0x34,0x63,0x65,0x64,0x37,0x30,0x2D,0x35,0x33,0x61,0x64,0x2D,0x34,0x62,0x32,0x39,0x2D,0x62,0x32,0x32,0x31,0x2D,0x31,0x62,0x30,0x61,0x62,0x62,0x64,0x66,0x63,0x37,0x35,0x37,0xA4,0x61,0x30,0x6B,0x74,0x65,0x73,0x74,0x5F,0x64,0x6F,0x75,0x62,0x6C,0x65,0x61,0x32,0xFB,0x7F,0xEF,0xFF,0xFC,0x57,0xCA,0x82,0xAE,0x62,0x2D,0x33,0x1A,0x5B,0x98,0xD7,0x80,0x62,0x2D,0x32,0x78,0x2D,0x75,0x72,0x6E,0x3A,0x75,0x75,0x69,0x64,0x3A,0x31,0x66,0x34,0x63,0x65,0x64,0x37,0x30,0x2D,0x35,0x33,0x61,0x64,0x2D,0x34,0x62,0x32,0x39,0x2D,0x62,0x32,0x32,0x31,0x2D,0x31,0x62,0x30,0x61,0x62,0x62,0x64,0x66,0x63,0x37,0x35,0x37,';
191+
const senMlUInt = ArduinoCloud.getSenml(deviceId, 'test_uint', 4, timestamp, true);
192+
const senMlSInt = ArduinoCloud.getSenml(deviceId, 'test_sint', -4, timestamp, true);
193+
const senMlFloat = ArduinoCloud.getSenml(deviceId, 'test_float', 4.5, timestamp, true);
194+
const senMlString = ArduinoCloud.getSenml(deviceId, 'test_string', 'test value', timestamp, true);
195+
const senMlBool = ArduinoCloud.getSenml(deviceId, 'test_bool', true, timestamp, true);
196+
const senMlDouble = ArduinoCloud.getSenml(deviceId, 'test_double', 1.79769e+308, timestamp, true);
197+
const senMl = [senMlUInt, senMlSInt, senMlFloat, senMlString, senMlBool, senMlDouble];
198+
console.log(senMl);
199+
const cborbase64 = ArduinoCloud.getCborValue(senMl);
200+
console.log(cborbase64);
201+
const cborHex = base64toHEX(cborbase64);
202+
console.log(cborHex);
203+
expect(cborHex).toStrictEqual(output);
204+
});
205+
206+
207+
it('[CloudProtocol v2] Generate a valid cbor for multiple properties with basename', () => {
208+
const output = '0x86,0xA3,0x61,0x30,0x69,0x74,0x65,0x73,0x74,0x5F,0x75,0x69,0x6E,0x74,0x61,0x32,0x04,0x62,0x2D,0x33,0x1A,0x5B,0x98,0xD7,0x80,0xA3,0x61,0x30,0x69,0x74,0x65,0x73,0x74,0x5F,0x73,0x69,0x6E,0x74,0x61,0x32,0x23,0x62,0x2D,0x33,0x1A,0x5B,0x98,0xD7,0x80,0xA3,0x61,0x30,0x6A,0x74,0x65,0x73,0x74,0x5F,0x66,0x6C,0x6F,0x61,0x74,0x61,0x32,0xFB,0x40,0x12,0x00,0x00,0x00,0x00,0x00,0x00,0x62,0x2D,0x33,0x1A,0x5B,0x98,0xD7,0x80,0xA3,0x61,0x30,0x6B,0x74,0x65,0x73,0x74,0x5F,0x73,0x74,0x72,0x69,0x6E,0x67,0x61,0x33,0x6A,0x74,0x65,0x73,0x74,0x20,0x76,0x61,0x6C,0x75,0x65,0x62,0x2D,0x33,0x1A,0x5B,0x98,0xD7,0x80,0xA3,0x61,0x30,0x69,0x74,0x65,0x73,0x74,0x5F,0x62,0x6F,0x6F,0x6C,0x61,0x34,0xF5,0x62,0x2D,0x33,0x1A,0x5B,0x98,0xD7,0x80,0xA3,0x61,0x30,0x6B,0x74,0x65,0x73,0x74,0x5F,0x64,0x6F,0x75,0x62,0x6C,0x65,0x61,0x32,0xFB,0x7F,0xEF,0xFF,0xFC,0x57,0xCA,0x82,0xAE,0x62,0x2D,0x33,0x1A,0x5B,0x98,0xD7,0x80,';
209+
const senMlUInt = ArduinoCloud.getSenml(null, 'test_uint', 4, timestamp, true);
210+
const senMlSInt = ArduinoCloud.getSenml(null, 'test_sint', -4, timestamp, true);
211+
const senMlFloat = ArduinoCloud.getSenml(null, 'test_float', 4.5, timestamp, true);
212+
const senMlString = ArduinoCloud.getSenml(null, 'test_string', 'test value', timestamp, true);
213+
const senMlBool = ArduinoCloud.getSenml(null, 'test_bool', true, timestamp, true);
214+
const senMlDouble = ArduinoCloud.getSenml(null, 'test_double', 1.79769e+308, timestamp, true);
215+
const senMl = [senMlUInt, senMlSInt, senMlFloat, senMlString, senMlBool, senMlDouble];
216+
console.log(senMl);
217+
const cborbase64 = ArduinoCloud.getCborValue(senMl);
218+
console.log(cborbase64);
219+
const cborHex = base64toHEX(cborbase64);
220+
console.log(cborHex);
221+
expect(cborHex).toStrictEqual(output);
222+
});
223+
});

‎test/cbor.test.js

Lines changed: 188 additions & 346 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.