Skip to content

Commit 4af6790

Browse files
committed
feat(client): Add helper function for constructing a bit string
1 parent 22ec7c7 commit 4af6790

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

lib/client.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,5 +1204,34 @@ class Client extends EventEmitter {
12041204
close() {
12051205
this._transport.close();
12061206
}
1207+
1208+
/**
1209+
* Helper function to take an array of enums and produce a bitstring suitable
1210+
* for inclusion as a property.
1211+
*
1212+
* @example
1213+
* [bacnet.enum.PropertyIdentifier.PROTOCOL_OBJECT_TYPES_SUPPORTED]: [
1214+
* {value: bacnet.createBitstring([
1215+
* bacnet.enum.ObjectTypesSupported.ANALOG_INPUT,
1216+
* bacnet.enum.ObjectTypesSupported.ANALOG_OUTPUT,
1217+
* ]),
1218+
* type: bacnet.enum.ApplicationTags.BIT_STRING},
1219+
* ],
1220+
*/
1221+
static createBitstring(items) {
1222+
let value = 0;
1223+
items.forEach(i => value |= 1 << i);
1224+
let bytes = [], bitsUsed = 0;
1225+
while (value & 0xFF) {
1226+
bytes.push(value & 0xFF);
1227+
bitsUsed += 8;
1228+
value >>= 8;
1229+
}
1230+
return {
1231+
value: bytes,
1232+
bitsUsed: bitsUsed,
1233+
};
1234+
}
1235+
12071236
}
12081237
module.exports = Client;

0 commit comments

Comments
 (0)