Skip to content

Multi value properties decode management #73

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 1 commit into from
May 7, 2019
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
34 changes: 28 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,29 +121,51 @@ const connect = options => new Promise((resolve, reject) => {
}

const propertyValue = CBOR.decode(buf);
const propertyNameId = 0;
const attributeNameId = 1;

let valueToSend = {};
let propertyNameKeyPrevious = '';
propertyValue.forEach((p) => {
// Support cbor labels
const propertyNameKey = p.n !== undefined ? p.n : p['0'];
let propertyNameKey = p.n !== undefined ? p.n : p['0'];
const propertyNameKeySplit = propertyNameKey.split(':');

const valueKey = p.v !== undefined ? 'v' : '2';
const valueStringKey = p.vs !== undefined ? 'vs' : '3';
const valueBooleanKey = p.vb !== undefined ? 'vb' : '4';
let value;
propertyNameKey = propertyNameKeySplit[propertyNameId];
if (propertyCallback[msg.topic][propertyNameKey]) {
let value = null;

if (!(p[valueKey] === undefined)) {
value = p[valueKey];
} else if (!(p[valueStringKey] === undefined)) {
value = p[valueStringKey];
} else if (!(p[valueBooleanKey] === undefined)) {
value = p[valueBooleanKey];
}

propertyCallback[msg.topic][propertyNameKey](value);
}
if (propertyNameKeyPrevious === '') {
propertyNameKeyPrevious = propertyNameKeySplit[propertyNameId];
}
if (propertyNameKeyPrevious !== propertyNameKey) {
propertyCallback[msg.topic][propertyNameKeyPrevious](valueToSend);
propertyNameKeyPrevious = propertyNameKey;
valueToSend = {};
}
if (propertyNameKeySplit.length === 1) {
valueToSend = value;
} else {
const attributeName = propertyNameKeySplit[attributeNameId];
valueToSend[attributeName] = value;
}
});
if (valueToSend !== {}) {
propertyCallback[msg.topic][propertyNameKeyPrevious](valueToSend);
}
}
};

client.onConnected = (reconnect) => {
if (reconnect === true) {
// This is a re-connection: re-subscribe to all topics subscribed before the
Expand Down