Skip to content

Commit b468c75

Browse files
committed
Add bufferFormat option to cap.js
1 parent 1819996 commit b468c75

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

cap.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export default { open };
5353
* @param {object} [options.out.mqtt.client] the MQTT client instead of creating a new one. attention: calling close() will *not* close this client
5454
* @param {string} [options.out.mqtt.topic='zbtk'] the MQTT topic to publish the packets to
5555
* @param {number} [options.bufferSize=10485760] the buffer size to use for packet capture
56+
* @param {function} [options.bufferFormat] a function to format buffers before emitting them to console / MQTT
5657
* @returns {Promise<EventEmitter>} a promise to an event emitter (with an additional close method), emitting events of 'options.emit' and 'error' events
5758
*/
5859
export async function open(device, options) {
@@ -329,21 +330,22 @@ export async function open(device, options) {
329330
const attr = { // same as for the context variables, expose big-endian instead of little-endian
330331
id: reverseEndian(intAttr.id),
331332
value: Buffer.isBuffer(intAttr.value) ? reverseEndian(intAttr.value) : intAttr.value
332-
}, hexAttr = {
333-
id: toHex(intAttr.id),
334-
value: Buffer.isBuffer(intAttr.value) ? toHex(intAttr.value) : intAttr.value
333+
}, outAttr = {
334+
id: rawToHex(attr.id),
335+
value: Buffer.isBuffer(intAttr.value) ? (typeof options?.bufferFormat === 'function' ?
336+
options.bufferFormat(attr.value) : rawToHex(attr.value)) : attr.value
335337
};
336338

337339
if (log.has('attribute')) {
338340
const cluster = getCluster(context.cluster);
339-
console.log(`${cluster?.name || 'Unknown Cluster'} (${toHex(zbee_aps.cluster)})/${cluster?.get?.(attr.id) || 'Unknown Attribute'} (${hexAttr.id}): ${hexAttr.value} (${write ? 'written to' : 'read from'} ${!process.env.ZBTK_CAP_PASS_NO_EUI ? formatEui(eui) : toHex(addr)})`);
341+
console.log(`${cluster?.name || 'Unknown Cluster'} (${toHex(zbee_aps.cluster)})/${cluster?.get?.(attr.id) || 'Unknown Attribute'} (${outAttr.id}): ${Buffer.isBuffer(outAttr.value) ? rawToHex(outAttr.value) : outAttr.value} (${write ? 'written to' : 'read from'} ${!process.env.ZBTK_CAP_PASS_NO_EUI ? formatEui(eui) : toHex(addr)})`);
340342
}
341343

342344
eventEmitter.emit('attribute', attr, context);
343345
if (mqtt) {
344346
try {
345-
await mqttClient.publishAsync(`${mqttTopic}/${!process.env.ZBTK_CAP_PASS_NO_EUI ? formatEui(eui) : toHex(addr)}/${toHex(zbee_aps.cluster)}/${hexAttr.id}`,
346-
Buffer.isBuffer(attr.value) ? attr.value : `${attr.value}`);
347+
await mqttClient.publishAsync(`${mqttTopic}/${!process.env.ZBTK_CAP_PASS_NO_EUI ? formatEui(eui) : toHex(addr)}/${toHex(zbee_aps.cluster)}/${outAttr.id}`,
348+
Buffer.isBuffer(outAttr.value) ? outAttr.value : `${outAttr.value}`);
347349
} catch (err) {
348350
logger.error(err, 'MQTT publish attribute failed');
349351
}

0 commit comments

Comments
 (0)