Skip to content

Change mqtt topic #3

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 2 commits into from
Nov 23, 2018
Merged
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -59,13 +59,13 @@ ArduinoCloud.closeCloudMonitor(connectionId, deviceId).then(topic => {
// Send a property value to a device
// - value can be a string, a boolean or a number
// - timestamp is a unix timestamp, not required
ArduinoCloud.sendProperty(connectionId, deviceId, name, value, timestamp).then(() => {
ArduinoCloud.sendProperty(connectionId, thingId, name, value, timestamp).then(() => {
// Property value sent
});

// Register a callback on a property value change
//
ArduinoCloud.onPropertyValue(connectionId, deviceId, propertyName, updateCb).then(() => {
ArduinoCloud.onPropertyValue(connectionId, thingId, propertyName, updateCb).then(() => {
// updateCb(message) will be called every time a new value is available. Value can be string, number, or a boolean depending on the property type
});

28 changes: 21 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -222,8 +222,8 @@ const closeCloudMonitor = (id, deviceId) => {
return unsubscribe(id, cloudMonitorOutputTopic);
};

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

if (timestamp && !Number.isInteger(timestamp)) {
throw new Error('Timestamp must be Integer');
@@ -294,8 +294,8 @@ const getCborValue = (senMl) => {
return arrayBufferToBase64(cborEncoded);
};

const sendPropertyAsDevice = (connectionId, deviceId, name, value, timestamp) => {
const propertyInputTopic = `/a/d/${deviceId}/e/o`;
const sendPropertyAsDevice = (connectionId, deviceId, thingId, name, value, timestamp) => {
const propertyInputTopic = `/a/t/${thingId}/e/o`;

if (timestamp && !Number.isInteger(timestamp)) {
throw new Error('Timestamp must be Integer');
@@ -309,14 +309,14 @@ const sendPropertyAsDevice = (connectionId, deviceId, name, value, timestamp) =>
return sendMessage(connectionId, propertyInputTopic, CBOR.encode([senMlValue]));
};

const onPropertyValue = (connectionId, deviceId, name, cb) => {
const onPropertyValue = (connectionId, thingId, name, cb) => {
if (!name) {
throw new Error('Invalid property name');
}
if (typeof cb !== 'function') {
throw new Error('Invalid callback');
}
const propOutputTopic = `/a/d/${deviceId}/e/o`;
const propOutputTopic = `/a/t/${thingId}/e/o`;

if (!propertyCallback[propOutputTopic]) {
propertyCallback[propOutputTopic] = {};
17 changes: 9 additions & 8 deletions test/arduino-cloud.test.js
Original file line number Diff line number Diff line change
@@ -21,6 +21,7 @@ const ArduinoCloud = require('../dist/index.js');

let connectionId;
const deviceId = '1f4ced70-53ad-4b29-b221-1b0abbdfc757';
const thingId = '2cea8542-d472-4464-859c-4ef4dfc7d1d3'
const propertyIntName = 'integer';
const propertyIntValue = 22;

@@ -100,42 +101,42 @@ it('Simulate device write and client read his message from cloud monitor', (done
});

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

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

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

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