Skip to content

Commit 3d0737a

Browse files
committed
Move CloudProtocolV2 configuration to connection config
1 parent 6f0a915 commit 3d0737a

File tree

4 files changed

+502
-433
lines changed

4 files changed

+502
-433
lines changed

src/index.js

Lines changed: 7 additions & 7 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

@@ -474,8 +475,7 @@ const getCborValue = (senMl) => {
474475
return arrayBufferToBase64(cborEncoded);
475476
};
476477

477-
const sendPropertyAsDevice = (deviceId, thingId, name, value, timestamp,
478-
useCloudProtocolV2 = false) => {
478+
const sendPropertyAsDevice = (deviceId, thingId, name, value, timestamp) => {
479479
const propertyInputTopic = `/a/t/${thingId}/e/o`;
480480

481481
if (timestamp && !Number.isInteger(timestamp)) {
@@ -486,7 +486,7 @@ const sendPropertyAsDevice = (deviceId, thingId, name, value, timestamp,
486486
throw new Error('Name must be a valid string');
487487
}
488488

489-
const senMlValue = getSenml(deviceId, name, value, timestamp, useCloudProtocolV2);
489+
const senMlValue = getSenml(deviceId, name, value, timestamp);
490490
return sendMessage(propertyInputTopic, CBOR.encode([senMlValue]));
491491
};
492492

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
});

0 commit comments

Comments
 (0)