@halsystems/red-bacnet
Version:
NodeRED BACnet IP client
79 lines (72 loc) • 2.37 kB
JavaScript
;
const { randomUUID } = require('crypto');
// ---------------------------------- export ----------------------------------
module.exports = {
/**
* Convert BACnet facets string to object.
* @param {string} str - The BACnet facets string.
* @returns {Object} - The BACnet facets object.
*/
facetsStrToObj: function (str) {
/* eg
unit:L/s;precision:1
falseText:Off;trueText:On
range:{1:Reserved;2:Warning}
*/
try {
if (str.includes("range:{")) {
const content = str.match(/\{(.+)\}/)[1];
const ranges = Object.fromEntries(content.split(";").map(pair => {
const [key, value] = pair.split(":");
return [key.trim(), value.trim()];
}));
return {
range: ranges
};
}
return Object.fromEntries(str.split(";").map(pair => {
const [key, value] = pair.split(":");
return [key.trim(), value.trim()];
}));
}
catch (e) {
/* istanbul ignore next */
{
void e
return {};
}
}
},
/**
* Create an error message object.
* @param {string} funcName - The name of the function.
* @param {string} errName - The name of the error.
* @param {any} errDesc - The description of the error.
* @returns {Object} - The error message object.
*/
errMsg: function (funcName, errName, errDesc) {
return {
[`[${funcName}] ${errName}`]: errDesc
}
},
/**
* Get error message from error object.
* @param {any} err - The error object.
* @returns {string} - The error message.
*/
getErrMsg: function (err) {
if (err instanceof Error)
return err.message
else
return err
},
/**
* Generate a unique job ID using node ID and uuid
* @param {string} nodeId - The Node-RED node instance ID.
* @param {string} prefix - The prefix for the job ID (e.g., 'readPoints', 'writePoints').
* @returns {string} - A unique job ID.
*/
generateUniqueJobId: function (nodeId, prefix) {
return `${prefix}-${nodeId}-${randomUUID()}`;
},
};