Skip to content

Commit

Permalink
SMHE-909: Process review comments
Browse files Browse the repository at this point in the history
Signed-off-by: stefanermens <[email protected]>
  • Loading branch information
stefanermens committed May 28, 2024
1 parent 1c9d731 commit fa52e98
Show file tree
Hide file tree
Showing 10 changed files with 170 additions and 98 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// SPDX-FileCopyrightText: Copyright Contributors to the GXF project
//
// SPDX-License-Identifier: Apache-2.0

package org.opensmartgridplatform.dlms.enums;

import lombok.Getter;

public enum DayOfWeek {
MONDAY(1, "Monday"),
TUESDAY(2, "Tuesday"),
WEDNESDAY(3, "Wednesday"),
THURSDAY(4, "Thursday"),
FRIDAY(5, "Friday"),
SATURDAY(6, "Saturday"),
SUNDAY(7, "Sunday"),
NOT_SPECIFIED(255, "Day of week not specified");

private final int value;

@Getter private final String description;

DayOfWeek(final int value, final String description) {
this.value = value;
this.description = description;
}

public static DayOfWeek getByValue(final int value) {
for (final DayOfWeek method : DayOfWeek.values()) {
if (method.value == value) {
return method;
}
}

return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// SPDX-FileCopyrightText: Copyright Contributors to the GXF project
//
// SPDX-License-Identifier: Apache-2.0

package org.opensmartgridplatform.dlms.enums;

public enum SortMethod {
FIFO(1),
LIFO(2),
LARGEST(3),
SMALLEST(4),
NEAREST_TO_ZERO(5),
FURTHEST_FROM_ZERO(6),
UNKNOWN_SORT_METHOD(255);

private final int value;

SortMethod(final int value) {
this.value = value;
}

public static SortMethod getByValue(final int value) {
for (final SortMethod method : SortMethod.values()) {
if (method.value == value) {
return method;
}
}

return UNKNOWN_SORT_METHOD;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public enum ProfileGenericAttribute implements AttributeClass {
BUFFER(2),
CAPTURE_OBJECTS(3, CAPTURE_OBJECT_LIST),
CAPTURE_PERIOD(4),
SORT_METHOD(5, org.opensmartgridplatform.dlms.interfaceclass.attribute.AttributeType.SORT_METHOD),
SORT_METHOD(5, AttributeType.SORT_METHOD),
SORT_OBJECT(6, CAPTURE_OBJECT_DEFINITION),
ENTRIES_IN_USE(7),
PROFILE_ENTRIES(8);
Expand All @@ -28,12 +28,12 @@ public enum ProfileGenericAttribute implements AttributeClass {

private final AttributeType attributeType;

private ProfileGenericAttribute(final int attributeId) {
ProfileGenericAttribute(final int attributeId) {
this.attributeId = attributeId;
this.attributeType = AttributeType.UNKNOWN;
}

private ProfileGenericAttribute(final int attributeId, final AttributeType attributeType) {
ProfileGenericAttribute(final int attributeId, final AttributeType attributeType) {
this.attributeId = attributeId;
this.attributeType = attributeType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,29 @@

package org.opensmartgridplatform.dlms.objectconfig;

import lombok.Getter;

@Getter
public enum AccessType {
R,
W,
RW,
X
NO_ACCESS(0),
R(1),
W(2),
RW(3),
X(4);

final int id;

AccessType(final int id) {
this.id = id;
}

public static AccessType accessTypeFor(final int id) {
for (final AccessType accessType : AccessType.values()) {
if (accessType.id == id) {
return accessType;
}
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@
@Component
public class GetAllAttributeValuesCommandExecutor extends AbstractCommandExecutor<Void, String> {

private static final int CLASS_ID_INDEX = 0;
private static final int VERSION_INDEX = 1;
private static final int OBIS_CODE_INDEX = 2;
private static final int ATTR_INDEX = 3;
private static final int ATTR_ID_INDEX = 0;
private static final int ACCESS_MODE_INDEX = 1;
private static final int ATTRIBUTE_ID = 2;
private static final int OBJECT_LIST_ELEMENT_CLASS_ID_INDEX = 0;
private static final int OBJECT_LIST_ELEMENT_VERSION_INDEX = 1;
private static final int OBJECT_LIST_ELEMENT_OBIS_CODE_INDEX = 2;
private static final int OBJECT_LIST_ELEMENT_ATTR_INDEX = 3;
private static final int ACCESS_ITEM_ATTR_ID_INDEX = 0;
private static final int ACCESS_ITEM_ACCESS_MODE_INDEX = 1;
private static final int ASSOCIATION_LN_OBJECT_LIST_ATTR_ID = 2;

private final ObjectConfigService objectConfigService;

Expand Down Expand Up @@ -104,7 +104,11 @@ public String execute(

final AttributeAddress attributeAddress =
this.objectConfigServiceHelper.findAttributeAddress(
device, Protocol.forDevice(device), DlmsObjectType.ASSOCIATION_LN, null, ATTRIBUTE_ID);
device,
Protocol.forDevice(device),
DlmsObjectType.ASSOCIATION_LN,
null,
ASSOCIATION_LN_OBJECT_LIST_ATTR_ID);

conn.getDlmsMessageListener()
.setDescription(
Expand Down Expand Up @@ -235,10 +239,10 @@ private List<ObjectListElement> getAllObjectListElements(
final List<DataObject> elementValues = objectListDataObject.getValue();
final ObjectListElement element =
new ObjectListElement(
this.getClassId(elementValues.get(CLASS_ID_INDEX)),
this.getVersion(elementValues.get(VERSION_INDEX)),
this.getObis(elementValues.get(OBIS_CODE_INDEX)),
this.getAttributeItems(elementValues.get(ATTR_INDEX)));
this.getClassId(elementValues.get(OBJECT_LIST_ELEMENT_CLASS_ID_INDEX)),
this.getVersion(elementValues.get(OBJECT_LIST_ELEMENT_VERSION_INDEX)),
this.getObis(elementValues.get(OBJECT_LIST_ELEMENT_OBIS_CODE_INDEX)),
this.getAttributeItems(elementValues.get(OBJECT_LIST_ELEMENT_ATTR_INDEX)));

allElements.add(element);
}
Expand Down Expand Up @@ -288,8 +292,8 @@ private List<AttributeAccessItem> getAttributeItems(final DataObject dataObject)
final List<DataObject> descriptorValues = descriptor.getValue();
attributeAccessItems.add(
new AttributeAccessItem(
this.getAttributeId(descriptorValues.get(ATTR_ID_INDEX)),
this.getAccessMode(descriptorValues.get(ACCESS_MODE_INDEX))));
this.getAttributeId(descriptorValues.get(ACCESS_ITEM_ATTR_ID_INDEX)),
this.getAccessMode(descriptorValues.get(ACCESS_ITEM_ACCESS_MODE_INDEX))));
}

return attributeAccessItems;
Expand All @@ -309,13 +313,7 @@ private AccessType getAccessMode(final DataObject dataObject) throws ProtocolAda
}
final Number number = dataObject.getValue();

return switch (number.intValue()) {
case 0 -> null;
case 1 -> AccessType.R;
case 2 -> AccessType.W;
case 3 -> AccessType.RW;
default -> null;
};
return AccessType.accessTypeFor(number.intValue());
}

private void throwUnexpectedTypeProtocolAdapterException() throws ProtocolAdapterException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// SPDX-FileCopyrightText: Copyright Contributors to the GXF project
//
// SPDX-License-Identifier: Apache-2.0

package org.opensmartgridplatform.adapter.protocol.dlms.domain.datadecoder;

import lombok.Getter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.openmuc.jdlms.datatypes.DataObject.Type;
import org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.utils.DlmsHelper;
import org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ProtocolAdapterException;
import org.opensmartgridplatform.dlms.enums.DayOfWeek;
import org.opensmartgridplatform.dto.valueobjects.smartmetering.CosemDateTimeDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -104,7 +105,7 @@ public String decodeDate(final DataObject attributeData) {

return this.getDayOfWeek(dayOfWeek)
+ ", "
+ ((year == 65535) ? "Year not specified" : year)
+ ((year == 0xFFFF) ? "Year not specified" : year)
+ "-"
+ month
+ "-"
Expand Down Expand Up @@ -144,18 +145,13 @@ private String getDayOfWeek(final CosemDateTimeDto dateTimeDto) {
return this.getDayOfWeek(dateTimeDto.getDate().getDayOfWeek());
}

private String getDayOfWeek(final int dayOfWeek) {
return switch (dayOfWeek) {
case 1 -> "Monday";
case 2 -> "Tuesday";
case 3 -> "Wednesday";
case 4 -> "Thursday";
case 5 -> "Friday";
case 6 -> "Saturday";
case 7 -> "Sunday";
case 0xFF -> "Day of week not specified";
default -> "DayOfWeek value unknown: " + dayOfWeek + ", ";
};
private String getDayOfWeek(final int dayOfWeekNumber) {
final DayOfWeek dayOfWeek = DayOfWeek.getByValue(dayOfWeekNumber);
if (dayOfWeek == null) {
return "DayOfWeek value unknown: " + dayOfWeekNumber + ", ";
} else {
return dayOfWeek.getDescription();
}
}

private String decodeVisibleString(final DataObject attributeData) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.openmuc.jdlms.datatypes.DataObject;
import org.openmuc.jdlms.datatypes.DataObject.Type;
import org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.utils.DlmsHelper;
import org.opensmartgridplatform.dlms.enums.SortMethod;
import org.opensmartgridplatform.dto.valueobjects.smartmetering.CosemObjectDefinitionDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -71,30 +72,4 @@ public String decodeSortMethod(final DataObject attributeData) {
return "decoding sort method failed: " + e.getMessage();
}
}

private enum SortMethod {
FIFO(1),
LIFO(2),
LARGEST(3),
SMALLEST(4),
NEAREST_TO_ZERO(5),
FURTHEST_FROM_ZERO(6),
UNKNOWN_SORT_METHOD(255);

private final int value;

SortMethod(final int value) {
this.value = value;
}

public static SortMethod getByValue(final int value) {
for (final SortMethod method : SortMethod.values()) {
if (method.value == value) {
return method;
}
}

return UNKNOWN_SORT_METHOD;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.openmuc.jdlms.datatypes.CosemDate;
import org.openmuc.jdlms.datatypes.CosemDateTime;
Expand All @@ -22,32 +21,44 @@ class BasicDlmsDataDecoderTest {
private final DlmsHelper dlmsHelper = new DlmsHelper();
private final BasicDlmsDataDecoder decoder = new BasicDlmsDataDecoder(this.dlmsHelper);

@ParameterizedTest
@EnumSource(
value = DataObject.Type.class,
names = {
"INTEGER",
"LONG_INTEGER",
"DOUBLE_LONG",
"LONG64",
"UNSIGNED",
"LONG_UNSIGNED",
"DOUBLE_LONG_UNSIGNED",
"LONG64_UNSIGNED"
})
void testDecodeNumber(final DataObject.Type type) throws ProtocolAdapterException {
switch (type) {
case INTEGER -> this.testNumber(DataObject.newInteger8Data((byte) 255), "-1");
case LONG_INTEGER -> this.testNumber(DataObject.newInteger16Data((short) -2500), "-2500");
case DOUBLE_LONG -> this.testNumber(DataObject.newInteger32Data(-10000), "-10000");
case LONG64 -> this.testNumber(DataObject.newInteger64Data(-75000), "-75000");

case UNSIGNED -> this.testNumber(DataObject.newUInteger8Data((short) 255), "255");
case LONG_UNSIGNED -> this.testNumber(DataObject.newUInteger16Data(3000), "3000");
case DOUBLE_LONG_UNSIGNED -> this.testNumber(DataObject.newUInteger32Data(80000), "80000");
case LONG64_UNSIGNED ->
this.testNumber(DataObject.newUInteger64Data(10_000_000_000L), "10000000000");
}
@Test
void testDecodeInteger() throws ProtocolAdapterException {
this.testNumber(DataObject.newInteger8Data((byte) 255), "-1");
}

@Test
void testDecodeLongInteger() throws ProtocolAdapterException {
this.testNumber(DataObject.newInteger16Data((short) -2500), "-2500");
}

@Test
void testDecodeDoubleLong() throws ProtocolAdapterException {
this.testNumber(DataObject.newInteger32Data(-10000), "-10000");
}

@Test
void testDecodeLong64() throws ProtocolAdapterException {
this.testNumber(DataObject.newInteger64Data(-75000), "-75000");
}

@Test
void testDecodeUnsigned() throws ProtocolAdapterException {
this.testNumber(DataObject.newUInteger8Data((short) 255), "255");
}

@Test
void testDecodeLongUnsigned() throws ProtocolAdapterException {
this.testNumber(DataObject.newUInteger16Data(3000), "3000");
}

@Test
void testDecodeDoubleLongUnsigned() throws ProtocolAdapterException {
this.testNumber(DataObject.newUInteger32Data(80000), "80000");
}

@Test
void testDecodeLong64Unsigned() throws ProtocolAdapterException {
this.testNumber(DataObject.newUInteger64Data(10_000_000_000L), "10000000000");
}

void testNumber(final DataObject dataObject, final String expectedResult)
Expand Down
Loading

0 comments on commit fa52e98

Please sign in to comment.