From fefeb9e80951b6f790d7860a7b6d56e96fd34a46 Mon Sep 17 00:00:00 2001 From: Kleber Norbert <35381530+nkleber78@users.noreply.github.com> Date: Thu, 8 Jun 2023 11:21:39 +0200 Subject: [PATCH] Improved errorhandling on object creation Related to issue #313 --- lib/devObjects.js | 154 ++++++++++++++++++++++++---------------------- main.js | 59 +++++++++--------- 2 files changed, 112 insertions(+), 101 deletions(-) diff --git a/lib/devObjects.js b/lib/devObjects.js index e5fda7bc..5c9dc903 100644 --- a/lib/devObjects.js +++ b/lib/devObjects.js @@ -283,11 +283,11 @@ function createSensorMinMaxObjects(adapter, id, obj) { } function createPowerFlowObjects(adapter, obj, testId = '') { - if (testId != '' && testId.endsWith('.') == false) { - // make sure the path ends with a . if set - testId = testId + '.'; - } try { + if (testId != '' && testId.endsWith('.') == false) { + // make sure the path ends with a . if set + testId = testId + '.'; + } for (const id in obj.Inverters) { adapter.log.silly('createPowerFlowObjects for Inverter ' + id + ', ' + JSON.stringify(obj.Inverters[id])); addObjectNotExists(adapter, 'inverter.' + testId + id, 'E_Day', 'Produced energy current day', 'number', 'Wh', obj.Inverters[id], '', 'value'); @@ -326,11 +326,11 @@ function createPowerFlowObjects(adapter, obj, testId = '') { } function createInverterInfoObjects(adapter, obj, testId = '') { - if (testId != '' && testId.endsWith('.') == false) { - // make sure the path ends with a . if set - testId = testId + '.'; - } try { + if (testId != '' && testId.endsWith('.') == false) { + // make sure the path ends with a . if set + testId = testId + '.'; + } adapter.log.silly('createInverterInfoObjects: ' + JSON.stringify(obj)); // loop over all inverters found for (const id in obj) { @@ -424,7 +424,7 @@ function createInfoObjects(adapter, obj) { obj, ); } catch (ex) { - adapter.log.error('Error on createPowerFlowObjects: ' + ex); + adapter.log.error('Error on createInfoObjects: ' + ex); adapter.log.error('Supplied object=' + obj); } } @@ -467,88 +467,96 @@ function createOhmPilotObjects(adapter, id, obj) { } function createStates(adapter, apiObject, prefix = '') { - if (prefix != '' && prefix.endsWith('.') == false) { - // make sure the path ends with a . if set - prefix = prefix + '.'; - } - if (Object.prototype.hasOwnProperty.call(apiObject, 'Value') && Object.prototype.hasOwnProperty.call(apiObject, 'Unit')) { - // value + unit on first level -> special handling - addObjectNotExists(adapter, prefix, 'Value', '', '', apiObject.Unit, apiObject, '', ''); - apiObject = Object.assign({}, apiObject); // create a copy for further processing to not delete it from source object - delete apiObject.Value; - delete apiObject.Unit; - } - for (const key in apiObject) { - if (apiObject[key.toString()] === null) { - adapter.log.debug('API Objekt ' + key.toString() + ' is null, no object created!'); - } else if (typeof apiObject[key.toString()] == 'object') { - // this is a nested object to parse! - if (Object.prototype.hasOwnProperty.call(apiObject[key.toString()], 'Value')) { - // handling object with value and Unit below - addObjectNotExists(adapter, prefix, key.toString(), '', '', '', apiObject, '', ''); + try { + if (prefix != '' && prefix.endsWith('.') == false) { + // make sure the path ends with a . if set + prefix = prefix + '.'; + } + if (Object.prototype.hasOwnProperty.call(apiObject, 'Value') && Object.prototype.hasOwnProperty.call(apiObject, 'Unit')) { + // value + unit on first level -> special handling + addObjectNotExists(adapter, prefix, 'Value', '', '', apiObject.Unit, apiObject, '', ''); + apiObject = Object.assign({}, apiObject); // create a copy for further processing to not delete it from source object + delete apiObject.Value; + delete apiObject.Unit; + } + for (const key in apiObject) { + if (apiObject[key.toString()] === null) { + adapter.log.debug('API Objekt ' + key.toString() + ' is null, no object created!'); + } else if (typeof apiObject[key.toString()] == 'object') { + // this is a nested object to parse! + if (Object.prototype.hasOwnProperty.call(apiObject[key.toString()], 'Value')) { + // handling object with value and Unit below + addObjectNotExists(adapter, prefix, key.toString(), '', '', '', apiObject, '', ''); + } else { + // nested object to create -> recurse + createStates(adapter, apiObject[key.toString()], prefix + key.toString()); + } } else { - // nested object to create -> recurse - createStates(adapter, apiObject[key.toString()], prefix + key.toString()); + // standard object to create + addObjectNotExists(adapter, prefix, key.toString(), '', '', '', apiObject, '', ''); } - } else { - // standard object to create - addObjectNotExists(adapter, prefix, key.toString(), '', '', '', apiObject, '', ''); } + } catch (ex) { + adapter.log.error('Error on createStates: ' + ex); } } function addObjectNotExists(adapter, path, stateId, stateName = '', stateType = '', stateUnit = '', apiObject = Object(null), stateDescription = '', stateRole = '') { - const sObj = Object({ type: 'state', common: { read: true, write: false, name: stateName, type: stateType } }); + try { + const sObj = Object({ type: 'state', common: { read: true, write: false, name: stateName, type: stateType } }); - if (stateName == '') { - sObj.common.name = stateId; - } + if (stateName == '') { + sObj.common.name = stateId; + } - if (stateRole != '') { - sObj.common.role = stateRole; - } + if (stateRole != '') { + sObj.common.role = stateRole; + } - if (stateType == '') { - // ensure if not set to use 'mixed' as type - sObj.common.type = 'mixed'; - } + if (stateType == '') { + // ensure if not set to use 'mixed' as type + sObj.common.type = 'mixed'; + } - if (path != '' && path.endsWith('.') == false) { - // make sure the path ends with a . if set - path = path + '.'; - } + if (path != '' && path.endsWith('.') == false) { + // make sure the path ends with a . if set + path = path + '.'; + } - if (stateDescription != '') { - sObj.common.desc = stateDescription; - } + if (stateDescription != '') { + sObj.common.desc = stateDescription; + } - if (stateUnit != '') { - sObj.common.unit = stateUnit; - } + if (stateUnit != '') { + sObj.common.unit = stateUnit; + } - if (apiObject === null || apiObject == 'undefined') { - adapter.setObjectNotExists(path + stateId, sObj); - } else if (Object.prototype.hasOwnProperty.call(apiObject, stateId)) { - if (apiObject[stateId] === null) { - adapter.log.debug('Property ' + stateId + ' is null. No object created!'); - return; - } else if (apiObject[stateId] != null && typeof apiObject[stateId] == 'object') { - if (Object.prototype.hasOwnProperty.call(apiObject[stateId], 'Value') && apiObject[stateId]['Value'] === null) { + if (apiObject === null || apiObject == 'undefined') { + adapter.setObjectNotExists(path + stateId, sObj); + } else if (Object.prototype.hasOwnProperty.call(apiObject, stateId)) { + if (apiObject[stateId] === null) { adapter.log.debug('Property ' + stateId + ' is null. No object created!'); return; - } else if (Object.prototype.hasOwnProperty.call(apiObject[stateId], 'Value') && typeof apiObject[stateId]['Value'] != 'object') { - // check if it is a object with Value/Unit pair and overwrite in this case the stateType and stateUnit - sObj.common.type = typeof apiObject[stateId]['Value']; + } else if (apiObject[stateId] != null && typeof apiObject[stateId] == 'object') { + if (Object.prototype.hasOwnProperty.call(apiObject[stateId], 'Value') && apiObject[stateId]['Value'] === null) { + adapter.log.debug('Property ' + stateId + ' is null. No object created!'); + return; + } else if (Object.prototype.hasOwnProperty.call(apiObject[stateId], 'Value') && typeof apiObject[stateId]['Value'] != 'object') { + // check if it is a object with Value/Unit pair and overwrite in this case the stateType and stateUnit + sObj.common.type = typeof apiObject[stateId]['Value']; + } + if (Object.prototype.hasOwnProperty.call(apiObject[stateId], 'Unit')) { + sObj.common.unit = apiObject[stateId]['Unit']; + } + } else if (apiObject[stateId] != null && typeof apiObject[stateId] != 'object') { + sObj.common.type = typeof apiObject[stateId]; } - if (Object.prototype.hasOwnProperty.call(apiObject[stateId], 'Unit')) { - sObj.common.unit = apiObject[stateId]['Unit']; - } - } else if (apiObject[stateId] != null && typeof apiObject[stateId] != 'object') { - sObj.common.type = typeof apiObject[stateId]; + adapter.setObjectNotExists(path + stateId, sObj); + } else { + adapter.log.debug('Object ' + JSON.stringify(apiObject) + ' does not have a property ' + stateId + ', therefore no State was created'); } - adapter.setObjectNotExists(path + stateId, sObj); - } else { - adapter.log.debug('Object ' + JSON.stringify(apiObject) + ' does not have a property ' + stateId + ', therefore no State was created'); + } catch (ex) { + adapter.log.error('Error on addObjectNotExists: ' + ex); } } diff --git a/main.js b/main.js index b94ad994..c19707de 100644 --- a/main.js +++ b/main.js @@ -537,7 +537,7 @@ function GetArchiveData(ids) { adapter.log.warn(data.Head.Status.Reason + ' archive: ' + ids); } } catch (e) { - adapter.log.warn('GetArchiveData: ' + e); + adapter.log.error('Error on reading and processing GetArchiveData: ' + e); } } }) @@ -1010,40 +1010,43 @@ function checkStatus() { if (response.status == 200 && 'BaseURL' in testData) { // it seems everything is working, therefore proceed with readout setConnected(true); - adapter.config.inverter.split(',').forEach(function (entry) { - getInverterRealtimeData(entry); - }); - - if (adapter.config.sensorCard) { - adapter.config.sensorCard.split(',').forEach(function (entry) { - getSensorRealtimeDataNow(entry); - getSensorRealtimeDataMinMax(entry); - }); - } - - if (adapter.config.stringControl) { - adapter.config.stringControl.split(',').forEach(function (entry) { - getStringRealtimeData(entry); + try { + adapter.config.inverter.split(',').forEach(function (entry) { + getInverterRealtimeData(entry); }); - } - if (apiver === 1) { - if (adapter.config.meter) { - adapter.config.meter.split(',').forEach(function (entry) { - getMeterRealtimeData(entry); + if (adapter.config.sensorCard) { + adapter.config.sensorCard.split(',').forEach(function (entry) { + getSensorRealtimeDataNow(entry); + getSensorRealtimeDataMinMax(entry); }); } - if (adapter.config.storage) { - adapter.config.storage.split(',').forEach(function (entry) { - getStorageRealtimeData(entry); + + if (adapter.config.stringControl) { + adapter.config.stringControl.split(',').forEach(function (entry) { + getStringRealtimeData(entry); }); } - getPowerFlowRealtimeData(); - getInverterInfo(); - getOhmPilotRealtimeData(); - } - adapter.setState('info.lastsync', { val: new Date().toISOString(), ack: true }); + if (apiver === 1) { + if (adapter.config.meter) { + adapter.config.meter.split(',').forEach(function (entry) { + getMeterRealtimeData(entry); + }); + } + if (adapter.config.storage) { + adapter.config.storage.split(',').forEach(function (entry) { + getStorageRealtimeData(entry); + }); + } + getPowerFlowRealtimeData(); + getInverterInfo(); + getOhmPilotRealtimeData(); + } + adapter.setState('info.lastsync', { val: new Date().toISOString(), ack: true }); + } catch (ex) { + adapter.log.error('Error on reading and processing the data from API: ' + ex); + } } else { adapter.log.debug('Unable to read data from inverters solarAPI'); setConnected(false);