From 2d4b32be5b75ef6fd99b27be637d5cac417c7d1d Mon Sep 17 00:00:00 2001 From: DEV2DEV_DE Date: Wed, 29 Nov 2023 20:57:07 +0100 Subject: [PATCH] replace invalid chars in state id's --- main.js | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/main.js b/main.js index e970b52..19e205f 100644 --- a/main.js +++ b/main.js @@ -290,9 +290,10 @@ class Jablotron extends utils.Adapter { * @param {string} name */ async createFolder(id, name) { - if (!this.existsState(id)) { - await this.extendObjectAsync(id, { type: 'folder', common: { name: `${name}` }, native: {}, }); - this.states.push(id); + const stateId = this.name2id(id); + if (!this.existsState(stateId)) { + await this.extendObjectAsync(stateId, { type: 'folder', common: { name: `${name}` }, native: {}, }); + this.states.push(stateId); } } @@ -302,9 +303,10 @@ class Jablotron extends utils.Adapter { * @param {string} name */ async createChannel(id, name) { - if (!this.existsState(id)) { - await this.extendObjectAsync(id, { type: 'channel', common: { name: `${name}` }, native: {}, }); - this.states.push(id); + const stateId = this.name2id(id); + if (!this.existsState(stateId)) { + await this.extendObjectAsync(stateId, { type: 'channel', common: { name: `${name}` }, native: {}, }); + this.states.push(stateId); } } @@ -334,11 +336,12 @@ class Jablotron extends utils.Adapter { break; default: throw new Error('Unknown type for value "' + name + '"'); } - if (!this.existsState(id)) { - await this.extendObjectAsync(id, { type: 'state', common: { name: `${name}`, type: `${type}`, role: role, read: read, write: write }, native: {}, }); - this.states.push(id); + const stateId = this.name2id(id); + if (!this.existsState(stateId)) { + await this.extendObjectAsync(stateId, { type: 'state', common: { name: `${name}`, type: `${type}`, role: role, read: read, write: write }, native: {}, }); + this.states.push(stateId); } - await this.setStateAsync(id, value, true); + await this.setStateAsync(stateId, value, true); } /** @@ -350,6 +353,16 @@ class Jablotron extends utils.Adapter { return this.states.indexOf(id) >= 0; } + /** + * Replaces forbidden characters in a name with underscores. + * + * @param {string} name - The name to be processed. + * @returns {string} - The processed name with forbidden characters replaced by underscores. + */ + name2id (name) { + return name.replace(adapter.FORBIDDEN_CHARS, '_'); + } + /** * Is called when adapter shuts down - callback has to be called under any circumstances! * @param {() => void} callback