diff --git a/CHANGELOG_OLD.md b/CHANGELOG_OLD.md index c9557fe..3d93b08 100644 --- a/CHANGELOG_OLD.md +++ b/CHANGELOG_OLD.md @@ -1,4 +1,7 @@ # Older changes +## 0.1.9 (2022-02-15) +* (HGlab01) js-controller 4.0 readiness + ## 0.1.8 (2021-11-25) * (HGlab01) save 'warnMessages'-array as file to reimport after restart of adapter diff --git a/README.md b/README.md index e23f08c..f7de237 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,11 @@ https://github.com/HGlab01/ioBroker.fuelpricemonitor Placeholder for the next version (at the beginning of the line): ### __WORK IN PROGRESS__ --> +### 0.1.14-alpha.0 (2023-10-06) +* (HGlab01) add versionInfo +* (HGlab01) add modify-methods 'toFloat' and 'toInteger' +* (DutchmanNL) couple of code improvements + ### 0.1.13 (2023-10-03) * (HGlab01) fix #113 "Cannot read properties of undefined (reading 'Warning')" @@ -52,9 +57,6 @@ https://github.com/HGlab01/ioBroker.fuelpricemonitor * (HGlab01) setLastStartTime() optimized * (HGlab01) Logs improved -### 0.1.9 (2022-02-15) -* (HGlab01) js-controller 4.0 readiness - ## License MIT License diff --git a/_template/stateAttr.js b/_template/stateAttr.js index 7a6c842..555321b 100644 --- a/_template/stateAttr.js +++ b/_template/stateAttr.js @@ -9,8 +9,8 @@ rootName: 'Device Info channel, // {default: NotUsed} Upper channel name name: 'Name of state', // {default: same as id} Name definition for object type: >typeof (value)<, // {default: typeof (value)} type of value automatically detected - read: true, // {default: true} Name defition for object - write: true, // {default: false} Name defition for object + read: true, // {default: true} Name definition for object + write: true, // {default: false} Name definition for object role: 'indicator.info', // {default: state} Role as defined by https://github.com/ioBroker/ioBroker/blob/master/doc/STATE_ROLES.md modify: '' // {default: ''} see below }, @@ -18,8 +18,8 @@ /** * Defines supported methods for element modify which can be used in stateAttr.js - * In addition: 'cumstom: YOUR CALCULATION' allows any calculation, where 'value' is the input parameter. - * Example: + * In addition: 'custom: YOUR CALCULATION' allows any calculation, where 'value' is the input parameter. + * Example: * modify: 'custom: value + 1' --> add 1 to the json-input * * * supported methods (as string): @@ -31,6 +31,8 @@ * - upperCase * - lowerCase * - ucFirst + * - toInteger + * - toFloat * * Examples for usage of embeded methods: * modify: ['multiply(3.6)', 'round(2)'] --> defined as array --> multiplied by 3.6 and then the result is rounded by 2 digits @@ -45,8 +47,8 @@ const stateAttrb = { 'NAMEOFTHESTATE1': { name: 'READABLE NAME/DESCRIPTION', type: 'number|string|array|boolean...', - read: true|false, - write: true|false, + read: true | false, + write: true | false, role: 'value', unit: 's|°|%...', modify: ['multiply(3.6)', 'round(2)'] @@ -54,8 +56,8 @@ const stateAttrb = { 'NAMEOFTHESTATE2': { name: 'READABLE NAME/DESCRIPTION', type: 'number|string|array|boolean...', - read: true|false, - write: true|false, + read: true | false, + write: true | false, role: 'vale', unit: 's|°|%...', modify: 'customer: (value+1)*2+value/2' @@ -63,8 +65,8 @@ const stateAttrb = { 'NAMEOFTHESTATE3': { name: 'READABLE NAME/DESCRIPTION', type: 'number|string|array|boolean...', - read: true|false, - write: true|false, + read: true | false, + write: true | false, role: 'vale', unit: 's|°|%...', modify: 'upperCase' @@ -72,8 +74,8 @@ const stateAttrb = { 'NAMEOFTHESTATE4': { name: 'READABLE NAME/DESCRIPTION', type: 'number|string|array|boolean...', - read: true|false, - write: true|false, + read: true | false, + write: true | false, role: 'vale', unit: 's|°|%...' } diff --git a/jsonExplorer.js b/jsonExplorer.js index aa4c58a..ada90d1 100644 --- a/jsonExplorer.js +++ b/jsonExplorer.js @@ -7,8 +7,8 @@ let stateExpire = {}, warnMessages = {}, stateAttr = {}; let adapter; //adapter-object initialized by init(); other functions do not need adapter-object in their signatur /** - * @param {object} adapter Adapter-Class (normally "this") - * @param {object} stateAttr check README + * @param {object} adapterOrigin Adapter-Class (normally "this") + * @param {object} stateAttribute check README */ function init(adapterOrigin, stateAttribute) { readWarnMessages(); @@ -38,8 +38,10 @@ function traverseJson(jObject, parent = null, replaceName = false, replaceID = f let id = null; let value = null; let name = ''; - parent = parent.replace(adapter.FORBIDDEN_CHARS, '_'); + if (parent != null) { + parent = parent.replace(adapter.FORBIDDEN_CHARS, '_'); + } try { if (parent != null && level == 0) { if (replaceName) { @@ -175,6 +177,12 @@ function modify(method, value) { case 'UCFIRST': if (typeof value == 'string') result = value.substring(0, 1).toUpperCase() + value.substring(1).toLowerCase(); break; + case 'TOINTEGER': + result = parseInt(value); + break; + case 'TOFLOAT': + result = parseFloat(value); + break; default: result = value; } @@ -253,12 +261,20 @@ async function stateSetCreate(objName, name, value) { common.type = stateAttr[name] !== undefined ? stateAttr[name].type || typeof (value) : typeof (value); common.role = stateAttr[name] !== undefined ? stateAttr[name].role || 'state' : 'state'; common.read = true; - common.unit = stateAttr[name] !== undefined ? stateAttr[name].unit || '' : ''; + //common.unit = stateAttr[name] !== undefined ? stateAttr[name].unit || '' : ''; common.write = stateAttr[name] !== undefined ? stateAttr[name].write || false : false; - common.states = stateAttr[name] !== undefined ? stateAttr[name].states || null : null; + //common.states = stateAttr[name] !== undefined ? stateAttr[name].states || null : null; common.modify = stateAttr[name] !== undefined ? stateAttr[name].modify || '' : ''; adapter.log.silly(`MODIFY to ${name}: ${JSON.stringify(common.modify)}`); + // Only add values for unit, modify and states if needed + if (stateAttr[name] != null && stateAttr[name].unit != null) { + common.unit = stateAttr[name] !== undefined ? stateAttr[name].unit || '' : ''; + } + if (stateAttr[name] != null && stateAttr[name].states != null) { + common.states = stateAttr[name] !== undefined ? stateAttr[name].states || null : null; + } + let objectDefiniton = {}; if (!adapter.createdStatesDetails[objName]) { objectDefiniton = await adapter.getObjectAsync(objName); diff --git a/package-lock.json b/package-lock.json index 6555f09..5f6df0d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "iobroker-jsonexplorer", - "version": "0.1.13", + "version": "0.1.14-alpha.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "iobroker-jsonexplorer", - "version": "0.1.13", + "version": "0.1.14-alpha.0", "license": "MIT", "devDependencies": { "@alcalzone/release-script": "^3.6.0", diff --git a/package.json b/package.json index 9953543..ca0197e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "iobroker-jsonexplorer", - "version": "0.1.13", + "version": "0.1.14-alpha.0", "description": "Creates ioBroker states based on a json-object", "main": "jsonExplorer.js", "scripts": {