Skip to content

Add CloudProtocl v2 support to sendPropertyAsDevice #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ import ArduinoCloud from 'arduino-iot-js';

// connect establishes a connection with mqtt, using token as the password
// options = {
// host: 'BROKER_URL', // Default is wss.iot.arduino.cc
// port: BROKER_PORT, // Default is 8443
// ssl: true/false, // Default is true
// token: 'YOUR_BEARER_TOKEN' // Required!
// apiUrl: 'AUTH SERVER URL', // Default is https://auth.arduino.cc
// onDisconnect: message => { /* Disconnection callback */ }
// host: 'BROKER_URL', // Default is wss.iot.arduino.cc
// port: BROKER_PORT, // Default is 8443
// ssl: true/false, // Default is true
// token: 'YOUR_BEARER_TOKEN' // Required!
// apiUrl: 'AUTH SERVER URL', // Default is https://auth.arduino.cc
// onDisconnect: message => { /* Disconnection callback */ },
// useCloudProtocolV2: true/false, // Default is false
// }
ArduinoCloud.connect(options).then(() => {
// Connected
Expand Down
9 changes: 5 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ const connect = options => new Promise((resolve, reject) => {
onDisconnect: options.onDisconnect,
onTrace: options.onTrace,
onConnected: options.onConnected,
useCloudProtocolV2: options.useCloudProtocolV2 || false,
};

connectionOptions = opts;
Expand Down Expand Up @@ -392,7 +393,7 @@ const toCloudProtocolV2 = (cborValue) => {
return cloudV2CBORValue;
};

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

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

if (useCloudProtocolV2) {
if (connectionOptions.useCloudProtocolV2) {
cborValue = toCloudProtocolV2(cborValue);
}

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

const getSenml = (deviceId, name, value, timestamp, useCloudProtocolV2 = false) => {
const getSenml = (deviceId, name, value, timestamp) => {
if (timestamp && !Number.isInteger(timestamp)) {
throw new Error('Timestamp must be Integer');
}
Expand Down Expand Up @@ -462,7 +463,7 @@ const getSenml = (deviceId, name, value, timestamp, useCloudProtocolV2 = false)
}


if (useCloudProtocolV2) {
if (connectionOptions.useCloudProtocolV2) {
return toCloudProtocolV2(senMl);
}

Expand Down
164 changes: 84 additions & 80 deletions test/arduino-cloud.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,109 +33,113 @@ const propertyStrVal = 'ok';
const propertyBoolName = 'boolean';
const propertyBoolVal = true;

it('ArduinoCloud connection', (done) => {
describe('Test the library basic functionalities', () => {
afterAll(() => ArduinoCloud.disconnect());

it('ArduinoCloud connection', (done) => {
/* global token */
ArduinoCloud.connect({
token,
onDisconnect: (message) => {
if (message.errorCode !== 0) {
throw Error(message);
}
},
})
.then(() => {
done();
ArduinoCloud.connect({
token,
onDisconnect: (message) => {
if (message.errorCode !== 0) {
throw Error(message);
}
},
})
.catch((error) => {
throw new Error(error);
});
});
.then(() => {
done();
})
.catch((error) => {
throw new Error(error);
});
});

it('Property name must be a string in sendProperty', (done) => {
try {
ArduinoCloud.sendProperty(deviceId, undefined, propertyIntValue);
} catch (error) {
if (error.message === 'Name must be a valid string') {
done();
it('Property name must be a string in sendProperty', (done) => {
try {
ArduinoCloud.sendProperty(deviceId, undefined, propertyIntValue);
} catch (error) {
if (error.message === 'Name must be a valid string') {
done();
}
}
}
});
});

it('Simulate client write to cloud monitor', (done) => {
ArduinoCloud.writeCloudMonitor(deviceId, `this is a test ${Math.random()}`).then(() => {
done();
}, (error) => {
throw new Error(error);
it('Simulate client write to cloud monitor', (done) => {
ArduinoCloud.writeCloudMonitor(deviceId, `this is a test ${Math.random()}`).then(() => {
done();
}, (error) => {
throw new Error(error);
});
});
});

it('Simulate device write to cloud monitor', (done) => {
const cloudMonitorInputTopic = `/a/d/${deviceId}/s/o`;
ArduinoCloud.sendMessage(cloudMonitorInputTopic, `this is a test ${Math.random()}`).then(() => {
done();
}, (error) => {
throw new Error(error);
it('Simulate device write to cloud monitor', (done) => {
const cloudMonitorInputTopic = `/a/d/${deviceId}/s/o`;
ArduinoCloud.sendMessage(cloudMonitorInputTopic, `this is a test ${Math.random()}`).then(() => {
done();
}, (error) => {
throw new Error(error);
});
});
});

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

const cb = () => {
const cb = () => {
// console.log(`[${new Date()}] Message from monitor: ${message}`);
done();
};
done();
};

ArduinoCloud.openCloudMonitor(deviceId, cb).then(() => {
ArduinoCloud.openCloudMonitor(deviceId, cb).then(() => {
// console.log(`Subscribed to topic: ${topic}`);
const message = `This is a test ${new Date()}`;
ArduinoCloud.sendMessage(cloudMonitorInputTopic, message).then(() => {
// console.log(`[${new Date()}] Message sent to monitor: [${message}]`);
const message = `This is a test ${new Date()}`;
ArduinoCloud.sendMessage(cloudMonitorInputTopic, message).then(() => {
// console.log(`[${new Date()}] Message sent to monitor: [${message}]`);
}, (error) => {
throw new Error(error);
});
}, (error) => {
throw new Error(error);
});
}, (error) => {
throw new Error(error);
});
});

it('Simulate client read integer property sent by device', (done) => {
ArduinoCloud.onPropertyValue(thingId, propertyIntName, (value) => {
if (value === propertyIntValue) {
done();
}
}).then(() => {
ArduinoCloud.sendPropertyAsDevice(deviceId, thingId, propertyIntName, propertyIntValue);
it('Simulate client read integer property sent by device', (done) => {
ArduinoCloud.onPropertyValue(thingId, propertyIntName, (value) => {
if (value === propertyIntValue) {
done();
}
}).then(() => {
ArduinoCloud.sendPropertyAsDevice(deviceId, thingId, propertyIntName, propertyIntValue);
});
});
});

it('Simulate client read float property sent by device', (done) => {
ArduinoCloud.onPropertyValue(thingId, propertyFloatName, (value) => {
if (value === propertyFloatVal) {
done();
}
}).then(() => {
ArduinoCloud.sendPropertyAsDevice(deviceId, thingId, propertyFloatName, propertyFloatVal);
it('Simulate client read float property sent by device', (done) => {
ArduinoCloud.onPropertyValue(thingId, propertyFloatName, (value) => {
if (value === propertyFloatVal) {
done();
}
}).then(() => {
ArduinoCloud.sendPropertyAsDevice(deviceId, thingId, propertyFloatName, propertyFloatVal);
});
});
});

it('Simulate client read string property sent by device', (done) => {
ArduinoCloud.onPropertyValue(thingId, propertyStrName, (value) => {
if (value === propertyStrVal) {
done();
}
}).then(() => {
ArduinoCloud.sendPropertyAsDevice(deviceId, thingId, propertyStrName, propertyStrVal);
it('Simulate client read string property sent by device', (done) => {
ArduinoCloud.onPropertyValue(thingId, propertyStrName, (value) => {
if (value === propertyStrVal) {
done();
}
}).then(() => {
ArduinoCloud.sendPropertyAsDevice(deviceId, thingId, propertyStrName, propertyStrVal);
});
});
});

it('Simulate client read boolean property sent by device', (done) => {
ArduinoCloud.onPropertyValue(thingId, propertyBoolName, (value) => {
if (value === propertyBoolVal) {
ArduinoCloud.disconnect();
done();
}
}).then(() => {
ArduinoCloud.sendPropertyAsDevice(deviceId, thingId, propertyBoolName, propertyBoolVal);
it('Simulate client read boolean property sent by device', (done) => {
ArduinoCloud.onPropertyValue(thingId, propertyBoolName, (value) => {
if (value === propertyBoolVal) {
ArduinoCloud.disconnect();
done();
}
}).then(() => {
ArduinoCloud.sendPropertyAsDevice(deviceId, thingId, propertyBoolName, propertyBoolVal);
});
});
});
Loading