From 2bb434f9009426876b56f6582e9c660746ba3afb Mon Sep 17 00:00:00 2001 From: Dirk-Jan Faber Date: Sun, 6 Oct 2024 10:42:53 +0200 Subject: [PATCH] Store all objects homogeneously as global objects (issue #198) Make sure to also remove the 'com.' part for service paths where there is no device instance for that service. --- src/nodes/victron-nodes.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/nodes/victron-nodes.js b/src/nodes/victron-nodes.js index 4dc6ac1..aebe86f 100644 --- a/src/nodes/victron-nodes.js +++ b/src/nodes/victron-nodes.js @@ -77,9 +77,16 @@ module.exports = function (RED) { return } if (this.configNode && (this.configNode.contextStore || typeof this.configNode.contextStore === 'undefined')) { + const transform = (input) => { + input = input.replace(/^com\./, '') + return input.replace(/\/(\d+\b)?|\/|(\b\d+\b)/g, (match, p1, p2) => { + if (p1) return `._${p1}` + if (p2) return `_${p2}` + return '.' + }) + } const globalContext = this.node.context().global - const v = `${this.service}${this.path}`.replace(/\//g, '.').replace(/com\.victronenergy\.(.+?)\.(\d+)\.(\w+)/, 'victronenergy.$1._$2.$3') - globalContext.set(v, msg.value) + globalContext.set(transform(`${this.service}${this.path}`), msg.value) } this.node.previousvalue = msg.value const outmsg = {