Skip to content

Commit

Permalink
Merge pull request #1525 from OSGP/feature/SMHE-2524_GetAllAttributeV…
Browse files Browse the repository at this point in the history
…alues

SMHE-2524: Add errormessage in output of GetAllAttributeValues
  • Loading branch information
stefanermens authored Feb 13, 2025
2 parents ff516fb + 7324fb0 commit 7aba53e
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -187,48 +187,60 @@ private CosemObject getAllDataFromObisCode(
final DlmsDevice device,
final ObjectListElement objectListElement) {

final List<DataObject> attributeData =
this.getAllDataFromAttributes(conn, device, objectListElement);
String errorMessage = "";
List<DataObject> 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<DataObject> 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<GetResult> 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<GetResult> 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<ObjectListElement> getAllObjectListElements(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 7aba53e

Please sign in to comment.