diff --git a/README.md b/README.md index d2f1e05..741e752 100644 --- a/README.md +++ b/README.md @@ -24,3 +24,25 @@ via the plugin's configuration UI in the Homebridge UI. The Node Red Editor is available at *http://homebridge_server_host:1956/red* , where 1956 must be changed in case you have configured another port number. +The Node Red system does not use a settings.js file, instead some primary settings are hardcoded into the plugin. Arbitrary settings (see a settings.js file) may be added +via the plugin's configuration. Each setting has a name, which corresponds to property names in settings.js. Each setting also has a value. These values are identical to the +values described in settings.js. These values are javascript values and the setting value to enter is a JSON representation of the value. +String values, must be enclosed in double-quote characters. Since object property names are represented as strings in JSON, these must also be enclosed in double-quotes. +The use of double-quote characters should be applied in case you enter the vales using the plugin's configuration UI. In case you edit the config.json directly, all JSON double-quote +characters have to be escaped (prepended) by a backslash ('\') character. If you enter double-quotes in the plugin's UI, you could then have a look at the config.json and you should then +see that the double-quotes has been escaped. + +``` + { + "name": "Node Red Platform", + "port": 1956, + "settings": [ + { + "name": "credentialSecret", + "value": "\"hemligt\"" + } + ], + "platform": "NodeRedHomebridgePlugin" + } + +``` diff --git a/config.schema.json b/config.schema.json index 95c74a4..882b71c 100644 --- a/config.schema.json +++ b/config.schema.json @@ -16,6 +16,24 @@ "type": "number", "required": true, "default": 1956 + }, + "settings": { + "type": "array", + "required": false, + "items": { + "title": "Node Red settings.", + "type": "object", + "properties": { + "name": { + "type": "string", + "required": true + }, + "value": { + "type": "string", + "required": true + } + } + } } } } diff --git a/package-lock.json b/package-lock.json index 87b68dc..3ba71bc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@helander/homebridge-node-red", - "version": "2.0.1", + "version": "2.1.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@helander/homebridge-node-red", - "version": "2.0.1", + "version": "2.1.0", "license": "Apache-2.0", "dependencies": { "node-red": "^3.1.3", diff --git a/package.json b/package.json index 36fc083..5aa7844 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "private": false, "displayName": "Node Red plugin for Homebridge", "name": "@helander/homebridge-node-red", - "version": "2.1.0", + "version": "2.2.0", "description": "Run Node Red inside Homebridge.", "license": "Apache-2.0", "repository": { diff --git a/src/platform.ts b/src/platform.ts index a1091f9..a3feff6 100644 --- a/src/platform.ts +++ b/src/platform.ts @@ -5,6 +5,11 @@ import http from 'http'; import express from 'express'; import RED from 'node-red'; +type Setting = { + name: string; + value: string; +}; + /** * NodeRedHomebridgePlatform */ @@ -12,6 +17,7 @@ export class NodeRedHomebridgePlatform implements DynamicPlatformPlugin { public readonly Service: typeof Service = this.api.hap.Service; public readonly Characteristic: typeof Characteristic = this.api.hap.Characteristic; private port: number; + private settings: Setting[]; private storagePath: string; private hapPin: string|null; constructor( @@ -22,6 +28,10 @@ export class NodeRedHomebridgePlatform implements DynamicPlatformPlugin { this.log = log; this.log.info('Finished initializing platform:', config.name); this.port = config.port; + this.settings = config.settings; + if (this.settings === undefined) { + this.settings = []; + } this.storagePath = api.user.storagePath(); this.hapPin = null; try { @@ -68,6 +78,25 @@ export class NodeRedHomebridgePlatform implements DynamicPlatformPlugin { functionGlobalContext: { }, // enables global context }; + + + + for(let i=0;i < this.settings.length; i+=1) { + const setting: Setting = this.settings[i]; + const setting_value = setting.value.replaceAll(String.fromCharCode(8220), '"').replaceAll(String.fromCharCode(8221), '"'); + let jvalue; + try { + jvalue = JSON.parse(setting_value); + } catch (err) { + this.log.error(`Illegal JSON @ ${setting.name} value ${setting_value}`); + } + settings[setting.name] = jvalue; + } + + this.log.info('settings', settings); + + + // setup userDir and manage migration from old userDir naming const oldUserDir = `${this.storagePath}/nodered`; if (!fs.existsSync(settings.userDir)) { diff --git a/tsconfig.json b/tsconfig.json index bf07099..1f2b606 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,7 +6,8 @@ "es2015", "es2016", "es2017", - "es2018" + "es2018", + "es2021" ], "declaration": true, "declarationMap": true,