From 7fde6f006747cf69a0b4dd9ed831ea61f3d07ff2 Mon Sep 17 00:00:00 2001 From: stefanermens Date: Tue, 11 Feb 2025 17:00:38 +0100 Subject: [PATCH 1/2] SMHE-2524: Add errormessage in output of GetAllAttributeValues in case of an error when reading the attribute values of an object Signed-off-by: stefanermens --- .../dlms/objectconfig/CosemObject.java | 8 ++ .../GetAllAttributeValuesCommandExecutor.java | 80 +++++++++++-------- 2 files changed, 54 insertions(+), 34 deletions(-) diff --git a/osgp/protocol-adapter-dlms/osgp-dlms/src/main/java/org/opensmartgridplatform/dlms/objectconfig/CosemObject.java b/osgp/protocol-adapter-dlms/osgp-dlms/src/main/java/org/opensmartgridplatform/dlms/objectconfig/CosemObject.java index 90838f7895c..8bfd1bc1762 100644 --- a/osgp/protocol-adapter-dlms/osgp-dlms/src/main/java/org/opensmartgridplatform/dlms/objectconfig/CosemObject.java +++ b/osgp/protocol-adapter-dlms/osgp-dlms/src/main/java/org/opensmartgridplatform/dlms/objectconfig/CosemObject.java @@ -163,4 +163,12 @@ private String[] getObisInParts() throws ObjectConfigException { } return obisParts; } + + public void addNote(final String note) { + if (this.note == null || note.isBlank()) { + this.note = note; + } else { + this.note = this.note + "\n" + note; + } + } } diff --git a/osgp/protocol-adapter-dlms/osgp-protocol-adapter-dlms/src/main/java/org/opensmartgridplatform/adapter/protocol/dlms/domain/commands/misc/GetAllAttributeValuesCommandExecutor.java b/osgp/protocol-adapter-dlms/osgp-protocol-adapter-dlms/src/main/java/org/opensmartgridplatform/adapter/protocol/dlms/domain/commands/misc/GetAllAttributeValuesCommandExecutor.java index 9d1703a087b..10b416c1d70 100644 --- a/osgp/protocol-adapter-dlms/osgp-protocol-adapter-dlms/src/main/java/org/opensmartgridplatform/adapter/protocol/dlms/domain/commands/misc/GetAllAttributeValuesCommandExecutor.java +++ b/osgp/protocol-adapter-dlms/osgp-protocol-adapter-dlms/src/main/java/org/opensmartgridplatform/adapter/protocol/dlms/domain/commands/misc/GetAllAttributeValuesCommandExecutor.java @@ -187,48 +187,60 @@ private CosemObject getAllDataFromObisCode( final DlmsDevice device, final ObjectListElement objectListElement) { - final List attributeData = - this.getAllDataFromAttributes(conn, device, objectListElement); + String errorMessage = ""; + List attributeData; - return this.dataDecoder.decodeObjectData(objectListElement, attributeData, dlmsProfile); + try { + attributeData = this.getAllDataFromAttributes(conn, device, objectListElement); + } catch (final Exception e) { + log.error("Failed reading attributes from " + objectListElement.getLogicalName(), e); + errorMessage = "Failed reading attributes"; + attributeData = List.of(); + } + + final CosemObject object = + this.dataDecoder.decodeObjectData(objectListElement, attributeData, dlmsProfile); + + if (!errorMessage.isEmpty()) { + object.addNote(errorMessage); + } + + return object; } private List getAllDataFromAttributes( final DlmsConnectionManager conn, final DlmsDevice device, - final ObjectListElement objectListElement) { - try { - final int classId = objectListElement.getClassId(); - final String obisCode = objectListElement.getLogicalName(); - final AttributeAddress[] addresses = - objectListElement.getAttributes().stream() - .map(item -> new AttributeAddress(classId, obisCode, item.getAttributeId())) - .toArray(AttributeAddress[]::new); - - conn.getDlmsMessageListener() - .setDescription( - "RetrieveAllAttributeValues, retrieve attributes: " - + JdlmsObjectToStringUtil.describeAttributes(addresses)); + final ObjectListElement objectListElement) + throws ProtocolAdapterException { - log.debug( - "Retrieving data for {} attributes of class id: {}, obis code: {}", - addresses.length, - classId, - obisCode); - final List getResults = this.dlmsHelper.getWithList(conn, device, addresses); - - if (getResults.stream() - .allMatch(result -> result.getResultCode() == AccessResultCode.SUCCESS)) { - log.debug("ResultCode: SUCCESS"); - } else { - log.debug("ResultCode not SUCCESS for one or more attributes"); - } - - return getResults.stream().map(GetResult::getResultData).toList(); - } catch (final Exception e) { - log.debug("Failed reading attributes from " + objectListElement.getLogicalName(), e); - return List.of(); + final int classId = objectListElement.getClassId(); + final String obisCode = objectListElement.getLogicalName(); + final AttributeAddress[] addresses = + objectListElement.getAttributes().stream() + .map(item -> new AttributeAddress(classId, obisCode, item.getAttributeId())) + .toArray(AttributeAddress[]::new); + + conn.getDlmsMessageListener() + .setDescription( + "RetrieveAllAttributeValues, retrieve attributes: " + + JdlmsObjectToStringUtil.describeAttributes(addresses)); + + log.debug( + "Retrieving data for {} attributes of class id: {}, obis code: {}", + addresses.length, + classId, + obisCode); + final List getResults = this.dlmsHelper.getWithList(conn, device, addresses); + + if (getResults.stream() + .allMatch(result -> result.getResultCode() == AccessResultCode.SUCCESS)) { + log.debug("ResultCode: SUCCESS"); + } else { + log.debug("ResultCode not SUCCESS for one or more attributes"); } + + return getResults.stream().map(GetResult::getResultData).toList(); } private List getAllObjectListElements( From 7324fb0cd9c240871b3432440fb45830d3d314ad Mon Sep 17 00:00:00 2001 From: stefanermens Date: Thu, 13 Feb 2025 09:38:34 +0100 Subject: [PATCH 2/2] SMHE-2524: Add unit test Signed-off-by: stefanermens --- ...AllAttributeValuesCommandExecutorTest.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/osgp/protocol-adapter-dlms/osgp-protocol-adapter-dlms/src/test/java/org/opensmartgridplatform/adapter/protocol/dlms/domain/commands/misc/GetAllAttributeValuesCommandExecutorTest.java b/osgp/protocol-adapter-dlms/osgp-protocol-adapter-dlms/src/test/java/org/opensmartgridplatform/adapter/protocol/dlms/domain/commands/misc/GetAllAttributeValuesCommandExecutorTest.java index b7782c2c6c8..e9684f1e215 100644 --- a/osgp/protocol-adapter-dlms/osgp-protocol-adapter-dlms/src/test/java/org/opensmartgridplatform/adapter/protocol/dlms/domain/commands/misc/GetAllAttributeValuesCommandExecutorTest.java +++ b/osgp/protocol-adapter-dlms/osgp-protocol-adapter-dlms/src/test/java/org/opensmartgridplatform/adapter/protocol/dlms/domain/commands/misc/GetAllAttributeValuesCommandExecutorTest.java @@ -248,6 +248,37 @@ void testDecodingFailsForOneAttribute() throws Exception { """); } + @Test + void testReadingAttributesFails() throws Exception { + final DataObject objListElementForKnownObject = + this.createObjectListElement(CLASS_ID_REGISTER, VERSION_0, KNOWN_OBIS, NO_OF_ATTR_REGISTER); + final DataObject objectList = DataObject.newArrayData(List.of(objListElementForKnownObject)); + final GetResultImpl getResultObjectList = + new GetResultImpl(objectList, AccessResultCode.SUCCESS); + + when(this.connectionManager.getConnection().get(any(AttributeAddress.class))) + .thenReturn(getResultObjectList); + + when(this.connectionManager.getConnection().get(ArgumentMatchers.anyList())) + .thenThrow(new IOException()); + + final String result = this.executor.execute(this.connectionManager, DEVICE, null, MSG_METADATA); + + assertThat(this.replaceNewLinesWithSystemNewLines(result)) + .isEqualToIgnoringNewLines( + """ +[ { + "description" : "Active energy import (+A)", + "dlmsClass" : "REGISTER", + "version" : 0, + "obis" : "1.0.1.8.0.255", + "note" : "Failed reading attributes", + "attributes" : [ ], + "class-id" : 3 +} ] +"""); + } + private static DlmsDevice createDevice(final Protocol protocol) { final DlmsDevice device = new DlmsDevice(); device.setProtocol(protocol);