Skip to content

Commit

Permalink
feature(client): Add helper function to convert enum into string
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-nielsen committed Mar 28, 2019
1 parent 3b60b36 commit aeaa690
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/asn1.js
Original file line number Diff line number Diff line change
Expand Up @@ -484,8 +484,10 @@ const bacappEncodeApplicationData = module.exports.bacappEncodeApplicationData =
case baEnum.ApplicationTags.READ_ACCESS_SPECIFICATION:
encodeReadAccessSpecification(buffer, value.value);
break;
case undefined:
throw new Error('Cannot encode a value if the type has not been specified');
default:
throw 'Unknown type';
throw 'Unknown ApplicationTags type: ' + baEnum.getEnumName(baEnum.ApplicationTags, value.type);
}
};

Expand Down
24 changes: 24 additions & 0 deletions lib/enum.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
'use strict';

/**
* Turn an enum into a string suitable for debugging.
*
* @param object group
* Enum group, e.g. bacnet.enum.ConfirmedServiceChoice.
*
* @param Number value
* Enum value, e.g. 1. Note that this *must* be an integer value, so you may
* need to use parseInt(). Non-integer values will result in an exception.
*
* @example
* const s = bacnet.enum.getEnumName(
* bacnet.enum.PropertyIdentifier,
* bacnet.enum.PropertyIdentifier.PRESENT_VALUE
* );
* console.log(s); // "PRESENT_VALUE(85)"
*/
module.exports.getEnumName = function(group, value) {
if (!Number.isInteger(value)) {
throw new Error('getEnumName() can only be passed an integer value, was given "' + value + '"');
}
return Object.keys(group).find(key => group[key] === value) + '(' + value + ')';
}

module.exports.PDU_TYPE_MASK = 0xF0;
module.exports.ASN1_MAX_OBJECT = 0x3FF;
module.exports.ASN1_INSTANCE_BITS = 22;
Expand Down

0 comments on commit aeaa690

Please sign in to comment.