Skip to content

Commit 7aba53e

Browse files
authored
Merge pull request #1525 from OSGP/feature/SMHE-2524_GetAllAttributeValues
SMHE-2524: Add errormessage in output of GetAllAttributeValues
2 parents ff516fb + 7324fb0 commit 7aba53e

File tree

3 files changed

+85
-34
lines changed

3 files changed

+85
-34
lines changed

osgp/protocol-adapter-dlms/osgp-dlms/src/main/java/org/opensmartgridplatform/dlms/objectconfig/CosemObject.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,12 @@ private String[] getObisInParts() throws ObjectConfigException {
163163
}
164164
return obisParts;
165165
}
166+
167+
public void addNote(final String note) {
168+
if (this.note == null || note.isBlank()) {
169+
this.note = note;
170+
} else {
171+
this.note = this.note + "\n" + note;
172+
}
173+
}
166174
}

osgp/protocol-adapter-dlms/osgp-protocol-adapter-dlms/src/main/java/org/opensmartgridplatform/adapter/protocol/dlms/domain/commands/misc/GetAllAttributeValuesCommandExecutor.java

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -187,48 +187,60 @@ private CosemObject getAllDataFromObisCode(
187187
final DlmsDevice device,
188188
final ObjectListElement objectListElement) {
189189

190-
final List<DataObject> attributeData =
191-
this.getAllDataFromAttributes(conn, device, objectListElement);
190+
String errorMessage = "";
191+
List<DataObject> attributeData;
192192

193-
return this.dataDecoder.decodeObjectData(objectListElement, attributeData, dlmsProfile);
193+
try {
194+
attributeData = this.getAllDataFromAttributes(conn, device, objectListElement);
195+
} catch (final Exception e) {
196+
log.error("Failed reading attributes from " + objectListElement.getLogicalName(), e);
197+
errorMessage = "Failed reading attributes";
198+
attributeData = List.of();
199+
}
200+
201+
final CosemObject object =
202+
this.dataDecoder.decodeObjectData(objectListElement, attributeData, dlmsProfile);
203+
204+
if (!errorMessage.isEmpty()) {
205+
object.addNote(errorMessage);
206+
}
207+
208+
return object;
194209
}
195210

196211
private List<DataObject> getAllDataFromAttributes(
197212
final DlmsConnectionManager conn,
198213
final DlmsDevice device,
199-
final ObjectListElement objectListElement) {
200-
try {
201-
final int classId = objectListElement.getClassId();
202-
final String obisCode = objectListElement.getLogicalName();
203-
final AttributeAddress[] addresses =
204-
objectListElement.getAttributes().stream()
205-
.map(item -> new AttributeAddress(classId, obisCode, item.getAttributeId()))
206-
.toArray(AttributeAddress[]::new);
207-
208-
conn.getDlmsMessageListener()
209-
.setDescription(
210-
"RetrieveAllAttributeValues, retrieve attributes: "
211-
+ JdlmsObjectToStringUtil.describeAttributes(addresses));
214+
final ObjectListElement objectListElement)
215+
throws ProtocolAdapterException {
212216

213-
log.debug(
214-
"Retrieving data for {} attributes of class id: {}, obis code: {}",
215-
addresses.length,
216-
classId,
217-
obisCode);
218-
final List<GetResult> getResults = this.dlmsHelper.getWithList(conn, device, addresses);
219-
220-
if (getResults.stream()
221-
.allMatch(result -> result.getResultCode() == AccessResultCode.SUCCESS)) {
222-
log.debug("ResultCode: SUCCESS");
223-
} else {
224-
log.debug("ResultCode not SUCCESS for one or more attributes");
225-
}
226-
227-
return getResults.stream().map(GetResult::getResultData).toList();
228-
} catch (final Exception e) {
229-
log.debug("Failed reading attributes from " + objectListElement.getLogicalName(), e);
230-
return List.of();
217+
final int classId = objectListElement.getClassId();
218+
final String obisCode = objectListElement.getLogicalName();
219+
final AttributeAddress[] addresses =
220+
objectListElement.getAttributes().stream()
221+
.map(item -> new AttributeAddress(classId, obisCode, item.getAttributeId()))
222+
.toArray(AttributeAddress[]::new);
223+
224+
conn.getDlmsMessageListener()
225+
.setDescription(
226+
"RetrieveAllAttributeValues, retrieve attributes: "
227+
+ JdlmsObjectToStringUtil.describeAttributes(addresses));
228+
229+
log.debug(
230+
"Retrieving data for {} attributes of class id: {}, obis code: {}",
231+
addresses.length,
232+
classId,
233+
obisCode);
234+
final List<GetResult> getResults = this.dlmsHelper.getWithList(conn, device, addresses);
235+
236+
if (getResults.stream()
237+
.allMatch(result -> result.getResultCode() == AccessResultCode.SUCCESS)) {
238+
log.debug("ResultCode: SUCCESS");
239+
} else {
240+
log.debug("ResultCode not SUCCESS for one or more attributes");
231241
}
242+
243+
return getResults.stream().map(GetResult::getResultData).toList();
232244
}
233245

234246
private List<ObjectListElement> getAllObjectListElements(

osgp/protocol-adapter-dlms/osgp-protocol-adapter-dlms/src/test/java/org/opensmartgridplatform/adapter/protocol/dlms/domain/commands/misc/GetAllAttributeValuesCommandExecutorTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,37 @@ void testDecodingFailsForOneAttribute() throws Exception {
248248
""");
249249
}
250250

251+
@Test
252+
void testReadingAttributesFails() throws Exception {
253+
final DataObject objListElementForKnownObject =
254+
this.createObjectListElement(CLASS_ID_REGISTER, VERSION_0, KNOWN_OBIS, NO_OF_ATTR_REGISTER);
255+
final DataObject objectList = DataObject.newArrayData(List.of(objListElementForKnownObject));
256+
final GetResultImpl getResultObjectList =
257+
new GetResultImpl(objectList, AccessResultCode.SUCCESS);
258+
259+
when(this.connectionManager.getConnection().get(any(AttributeAddress.class)))
260+
.thenReturn(getResultObjectList);
261+
262+
when(this.connectionManager.getConnection().get(ArgumentMatchers.anyList()))
263+
.thenThrow(new IOException());
264+
265+
final String result = this.executor.execute(this.connectionManager, DEVICE, null, MSG_METADATA);
266+
267+
assertThat(this.replaceNewLinesWithSystemNewLines(result))
268+
.isEqualToIgnoringNewLines(
269+
"""
270+
[ {
271+
"description" : "Active energy import (+A)",
272+
"dlmsClass" : "REGISTER",
273+
"version" : 0,
274+
"obis" : "1.0.1.8.0.255",
275+
"note" : "Failed reading attributes",
276+
"attributes" : [ ],
277+
"class-id" : 3
278+
} ]
279+
""");
280+
}
281+
251282
private static DlmsDevice createDevice(final Protocol protocol) {
252283
final DlmsDevice device = new DlmsDevice();
253284
device.setProtocol(protocol);

0 commit comments

Comments
 (0)