Skip to content

Commit aeaa690

Browse files
committed
feature(client): Add helper function to convert enum into string
1 parent 3b60b36 commit aeaa690

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

lib/asn1.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,8 +484,10 @@ const bacappEncodeApplicationData = module.exports.bacappEncodeApplicationData =
484484
case baEnum.ApplicationTags.READ_ACCESS_SPECIFICATION:
485485
encodeReadAccessSpecification(buffer, value.value);
486486
break;
487+
case undefined:
488+
throw new Error('Cannot encode a value if the type has not been specified');
487489
default:
488-
throw 'Unknown type';
490+
throw 'Unknown ApplicationTags type: ' + baEnum.getEnumName(baEnum.ApplicationTags, value.type);
489491
}
490492
};
491493

lib/enum.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
'use strict';
22

3+
/**
4+
* Turn an enum into a string suitable for debugging.
5+
*
6+
* @param object group
7+
* Enum group, e.g. bacnet.enum.ConfirmedServiceChoice.
8+
*
9+
* @param Number value
10+
* Enum value, e.g. 1. Note that this *must* be an integer value, so you may
11+
* need to use parseInt(). Non-integer values will result in an exception.
12+
*
13+
* @example
14+
* const s = bacnet.enum.getEnumName(
15+
* bacnet.enum.PropertyIdentifier,
16+
* bacnet.enum.PropertyIdentifier.PRESENT_VALUE
17+
* );
18+
* console.log(s); // "PRESENT_VALUE(85)"
19+
*/
20+
module.exports.getEnumName = function(group, value) {
21+
if (!Number.isInteger(value)) {
22+
throw new Error('getEnumName() can only be passed an integer value, was given "' + value + '"');
23+
}
24+
return Object.keys(group).find(key => group[key] === value) + '(' + value + ')';
25+
}
26+
327
module.exports.PDU_TYPE_MASK = 0xF0;
428
module.exports.ASN1_MAX_OBJECT = 0x3FF;
529
module.exports.ASN1_INSTANCE_BITS = 22;

0 commit comments

Comments
 (0)