Skip to content

Commit 0b2abb0

Browse files
committed
feat: added check on empy object
Added a check before sending MQTT empty objects.
1 parent abed98c commit 0b2abb0

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ node_modules
22
lib
33
dist
44
es
5-
.tmp
5+
.tmp
6+
misc

src/connection/Connection.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,11 @@ export class Connection implements IConnection {
111111
else valueToSend = value;
112112
});
113113

114-
// the condition `if (valueToSend !== {}) ` has been removed bc it always evaluates to true
115-
messages.push({ topic, propertyName: current, value: valueToSend });
114+
// If the message is an object, and it's empty, it makes no sense to send it
115+
// All other messages (e.g. non-empty objects or primitives) must be sent
116+
if (!(typeof valueToSend === 'object' && Object.keys(valueToSend).length == 0)) {
117+
messages.push({ topic, propertyName: current, value: valueToSend });
118+
}
116119

117120
return messages;
118121
}

test/arduino-cloud.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ describe('Test the library basic functionalities', () => {
9292
await ArduinoIoTCloud.onPropertyValue(thingId, propertyBoolName, (value) => value === propertyBoolVal ? done() : null);
9393
sendPropertyAsDevice(deviceId, thingId, propertyBoolName, propertyBoolVal);
9494
});
95+
96+
it('Simulate client read boolean as FALSE property sent by device', async (done) => {
97+
await ArduinoIoTCloud.onPropertyValue(thingId, propertyBoolName, (value) => !value ? done() : null);
98+
sendPropertyAsDevice(deviceId, thingId, propertyBoolName, false);
99+
});
100+
95101
})
96102
});
97103

0 commit comments

Comments
 (0)