Skip to content

Commit

Permalink
Improved errorhandling on object creation
Browse files Browse the repository at this point in the history
  • Loading branch information
nkleber78 committed Jun 8, 2023
1 parent f053c28 commit fefeb9e
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 101 deletions.
154 changes: 81 additions & 73 deletions lib/devObjects.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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);
}
}

Expand Down
59 changes: 31 additions & 28 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
})
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit fefeb9e

Please sign in to comment.