use of org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.dlmsobjectconfig.DlmsCaptureObject in project open-smart-grid-platform by OSGP.
the class GetPeriodicMeterReadsCommandExecutorTest method testHappy.
@Test
void testHappy() throws Exception {
// SETUP
final PeriodTypeDto periodType = PeriodTypeDto.DAILY;
final PeriodicMeterReadsRequestDto request = new PeriodicMeterReadsRequestDto(periodType, new Date(this.from), new Date(this.to));
// SETUP - dlms objects
final DlmsObject dlmsClock = new DlmsClock("0.0.1.0.0.255");
final DlmsCaptureObject captureObject1 = new DlmsCaptureObject(dlmsClock, 2);
final DlmsObject activeEnergyImportRate1 = new DlmsObject(DlmsObjectType.ACTIVE_ENERGY_IMPORT_RATE_1, 0, "1.0.1.8.1.255");
final DlmsCaptureObject captureObject2 = new DlmsCaptureObject(activeEnergyImportRate1, 2);
final DlmsObject activeEnergyImportRate2 = new DlmsObject(DlmsObjectType.ACTIVE_ENERGY_IMPORT_RATE_2, 0, "1.0.1.8.2.255");
final DlmsCaptureObject captureObject3 = new DlmsCaptureObject(activeEnergyImportRate2, 2);
final DlmsObject activeEnergyExportRate1 = new DlmsObject(DlmsObjectType.ACTIVE_ENERGY_EXPORT_RATE_1, 0, "1.0.2.8.1.255");
final DlmsCaptureObject captureObject4 = new DlmsCaptureObject(activeEnergyExportRate1, 2);
final DlmsObject activeEnergyExportRate2 = new DlmsObject(DlmsObjectType.ACTIVE_ENERGY_EXPORT_RATE_2, 0, "1.0.2.8.2.255");
final DlmsCaptureObject captureObject5 = new DlmsCaptureObject(activeEnergyExportRate2, 2);
final List<DlmsCaptureObject> captureObjects = Arrays.asList(captureObject1, captureObject2, captureObject3, captureObject4, captureObject5);
final DlmsProfile dlmsProfile = new DlmsProfile(DlmsObjectType.DAILY_LOAD_PROFILE, "1.0.99.2.0.255", captureObjects, ProfileCaptureTime.DAY, Medium.ELECTRICITY);
// SETUP - mock dlms object config to return attribute addresses
final AttributeAddressForProfile attributeAddressForProfile = this.createAttributeAddressForProfile(dlmsProfile, captureObjects);
final AttributeAddress attributeAddress = this.createAttributeAddress(dlmsProfile);
when(this.dlmsObjectConfigService.findAttributeAddressForProfile(this.device, DlmsObjectType.DAILY_LOAD_PROFILE, 0, this.fromDateTime, this.toDateTime, Medium.ELECTRICITY)).thenReturn(Optional.of(attributeAddressForProfile));
final DlmsObject intervalTime = mock(DlmsObject.class);
when(this.dlmsObjectConfigService.findDlmsObject(any(Protocol.class), any(DlmsObjectType.class), any(Medium.class))).thenReturn(Optional.of(intervalTime));
// SETUP - mock dlms helper to return data objects on request
final DataObject data0 = mock(DataObject.class);
// make sure to set logTime on first dataObject
final List<DataObject> bufferedObjectValue = new ArrayList<>();
when(data0.getValue()).thenReturn(bufferedObjectValue);
final DataObject data1 = mock(DataObject.class);
when(data1.isNumber()).thenReturn(true);
final DataObject data2 = mock(DataObject.class);
final DataObject data3 = mock(DataObject.class);
final DataObject data4 = mock(DataObject.class);
final DataObject data5 = mock(DataObject.class);
final DataObject bufferedObject1 = mock(DataObject.class);
when(bufferedObject1.getValue()).thenReturn(asList(data0, data1, data2, data3, data4, data5));
final DataObject bufferedObject2 = mock(DataObject.class);
when(bufferedObject2.getValue()).thenReturn(asList(data0, data1, data2, data3, data4, data5));
final DataObject resultData = mock(DataObject.class);
when(resultData.getValue()).thenReturn(Arrays.asList(bufferedObject1, bufferedObject2));
final String expectedDescription = "retrieve periodic meter reads for " + periodType;
final GetResult result0 = mock(GetResult.class);
final GetResult result1 = mock(GetResult.class);
final GetResult result2 = mock(GetResult.class);
final GetResult result3 = mock(GetResult.class);
final GetResult result4 = mock(GetResult.class);
final GetResult result5 = mock(GetResult.class);
final GetResult getResult = mock(GetResult.class);
when(this.dlmsHelper.getAndCheck(this.connectionManager, this.device, expectedDescription, attributeAddress)).thenReturn(asList(result0, result1, result2, result3, result4, result5));
when(this.dlmsHelper.readDataObject(result0, PERIODIC_E_METER_READS)).thenReturn(resultData);
when(this.dlmsHelper.getAndCheck(this.connectionManager, this.device, expectedDescription, attributeAddressForProfile.getAttributeAddress())).thenReturn(Collections.singletonList(getResult));
when(this.dlmsHelper.getAndCheck(this.connectionManager, this.device, expectedDescription, attributeAddress)).thenReturn(Collections.singletonList(getResult));
when(this.dlmsHelper.readDataObject(eq(getResult), any(String.class))).thenReturn(resultData);
final CosemDateTimeDto cosemDateTime = mock(CosemDateTimeDto.class);
final String expectedDateTimeDescription = String.format("Clock from %s buffer", periodType);
when(this.dlmsHelper.readDateTime(data0, expectedDateTimeDescription)).thenReturn(cosemDateTime);
final DateTime bufferedDateTime = DateTime.now();
when(cosemDateTime.asDateTime()).thenReturn(bufferedDateTime);
// CALL
final PeriodicMeterReadsResponseDto result = this.executor.execute(this.connectionManager, this.device, request, this.messageMetadata);
// VERIFY calls to mocks
verify(this.dlmsMessageListener).setDescription(String.format("GetPeriodicMeterReads DAILY from %s until %s, retrieve attribute: {%s,%s,%s}", new DateTime(this.from), new DateTime(this.to), dlmsProfile.getClassId(), dlmsProfile.getObisCode(), dlmsProfile.getDefaultAttributeId()));
verify(this.dlmsHelper, times(2)).validateBufferedDateTime(same(bufferedDateTime), argThat(new DateTimeMatcher(this.from)), argThat(new DateTimeMatcher(this.to)));
verify(this.dlmsObjectConfigService).findDlmsObject(any(Protocol.class), any(DlmsObjectType.class), any(Medium.class));
// ASSERT - the result should contain 2 values
final List<PeriodicMeterReadsResponseItemDto> periodicMeterReads = result.getPeriodicMeterReads();
assertThat(periodicMeterReads.size()).isEqualTo(2);
periodicMeterReads.forEach(p -> assertThat(p.getLogTime()).isNotNull());
}
use of org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.dlmsobjectconfig.DlmsCaptureObject in project open-smart-grid-platform by OSGP.
the class GetPeriodicMeterReadsGasCommandExecutor method readScalerUnit.
private DataObject readScalerUnit(final List<GetResult> getResultList, final List<AttributeAddress> attributeAddresses, final AttributeAddressForProfile attributeAddressForProfile, final Integer channel) {
final DlmsCaptureObject captureObject = attributeAddressForProfile.getCaptureObject(DlmsObjectType.MBUS_MASTER_VALUE);
int index = 0;
Integer scalerUnitIndex = null;
for (final AttributeAddress address : attributeAddresses) {
final String obisCode = captureObject.getRelatedObject().getObisCodeAsString().replace("<c>", channel.toString());
if (address.getInstanceId().equals(new ObisCode(obisCode))) {
scalerUnitIndex = index;
}
index++;
}
// and should be skipped. The first scaler unit is at index 1.
if (scalerUnitIndex != null) {
return getResultList.get(scalerUnitIndex + 1).getResultData();
}
return null;
}
use of org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.dlmsobjectconfig.DlmsCaptureObject in project open-smart-grid-platform by OSGP.
the class GetPeriodicMeterReadsCommandExecutor method readScalerUnit.
private DataObject readScalerUnit(final List<GetResult> getResultList, final List<AttributeAddress> attributeAddresses, final AttributeAddressForProfile attributeAddressForProfile, final DlmsObjectType objectType) {
final DlmsCaptureObject captureObject = attributeAddressForProfile.getCaptureObject(objectType);
int index = 0;
Integer scalerUnitIndex = null;
for (final AttributeAddress address : attributeAddresses) {
final ObisCode obisCode = captureObject.getRelatedObject().getObisCode();
if (address.getInstanceId().equals(obisCode)) {
scalerUnitIndex = index;
}
index++;
}
// and should be skipped. The first scaler unit is at index 1.
if (scalerUnitIndex != null) {
return getResultList.get(scalerUnitIndex + 1).getResultData();
}
return null;
}
use of org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.dlmsobjectconfig.DlmsCaptureObject in project open-smart-grid-platform by OSGP.
the class GetPeriodicMeterReadsGasCommandExecutorTest method testHappy.
@Test
public void testHappy() throws Exception {
// SETUP - request
final PeriodTypeDto periodType = PeriodTypeDto.DAILY;
final ChannelDto channel = ChannelDto.ONE;
final PeriodicMeterReadsRequestDto request = new PeriodicMeterReadsRequestDto(periodType, this.fromDateTime.toDate(), this.toDateTime.toDate(), channel);
// SETUP - dlms objects
final DlmsObject dlmsClock = new DlmsClock("0.0.1.0.0.255");
final DlmsCaptureObject captureObject1 = new DlmsCaptureObject(dlmsClock, 2);
final DlmsObject dlmsExtendedRegister = new DlmsExtendedRegister(DlmsObjectType.MBUS_MASTER_VALUE, "0.0.24.0.0.255", 0, RegisterUnit.M3, Medium.GAS);
final DlmsCaptureObject captureObject2 = new DlmsCaptureObject(dlmsExtendedRegister, 2);
final DlmsCaptureObject captureObject3 = new DlmsCaptureObject(dlmsExtendedRegister, 5);
final List<DlmsCaptureObject> captureObjects = Arrays.asList(captureObject1, captureObject2, captureObject3);
final DlmsProfile dlmsProfile = new DlmsProfile(DlmsObjectType.DAILY_LOAD_PROFILE, "1.2.3.4.5.6", captureObjects, ProfileCaptureTime.DAY, Medium.COMBINED);
// SETUP - mock dlms object config to return attribute addresses
final AttributeAddressForProfile attributeAddressForProfile = this.createAttributeAddressForProfile(dlmsProfile, captureObjects);
final AttributeAddress attributeAddressScalerUnit = this.createAttributeAddress(dlmsExtendedRegister);
when(this.dlmsObjectConfigService.findAttributeAddressForProfile(eq(this.device), eq(DlmsObjectType.DAILY_LOAD_PROFILE), eq(channel.getChannelNumber()), eq(this.fromDateTime), eq(this.toDateTime), eq(Medium.GAS))).thenReturn(Optional.of(attributeAddressForProfile));
when(this.dlmsObjectConfigService.getAttributeAddressesForScalerUnit(eq(attributeAddressForProfile), eq(channel.getChannelNumber()))).thenReturn(Collections.singletonList(attributeAddressScalerUnit));
final DlmsObject intervalTime = mock(DlmsObject.class);
when(this.dlmsObjectConfigService.findDlmsObject(any(Protocol.class), any(DlmsObjectType.class), any(Medium.class))).thenReturn(Optional.of(intervalTime));
// SETUP - mock dlms helper to return data objects on request
final DataObject data0 = mock(DataObject.class);
final DataObject data1 = mock(DataObject.class);
final DataObject data2 = mock(DataObject.class);
final DataObject bufferedObject1 = mock(DataObject.class);
when(bufferedObject1.getValue()).thenReturn(asList(data0, data1, data2));
final DataObject data3 = mock(DataObject.class);
final DataObject data4 = mock(DataObject.class);
final DataObject data5 = mock(DataObject.class);
final DataObject bufferedObject2 = mock(DataObject.class);
when(bufferedObject2.getValue()).thenReturn(asList(data3, data4, data5));
final DataObject resultData = mock(DataObject.class);
when(resultData.getValue()).thenReturn(asList(bufferedObject1, bufferedObject2));
final String expectedDescription = "retrieve periodic meter reads for " + periodType + ", channel " + channel;
final GetResult getResult = mock(GetResult.class);
when(this.dlmsHelper.getAndCheck(eq(this.connectionManager), eq(this.device), eq(expectedDescription), eq(attributeAddressForProfile.getAttributeAddress()))).thenReturn(Collections.singletonList(getResult));
when(this.dlmsHelper.getAndCheck(this.connectionManager, this.device, expectedDescription, attributeAddressScalerUnit)).thenReturn(Collections.singletonList(getResult));
when(this.dlmsHelper.readDataObject(eq(getResult), any(String.class))).thenReturn(resultData);
// SETUP - mock dlms helper to handle converting the data objects
final String expectedDateTimeDescriptionLogTime = String.format("Clock from %s buffer", periodType);
final String expectedDateTimeDescriptionCaptureTime = "Clock from mbus interval extended register";
final CosemDateTimeDto cosemDateTime = new CosemDateTimeDto(this.fromDateTime);
when(this.dlmsHelper.readDateTime(data0, expectedDateTimeDescriptionLogTime)).thenReturn(cosemDateTime);
when(this.dlmsHelper.readDateTime(data3, expectedDateTimeDescriptionLogTime)).thenReturn(cosemDateTime);
when(this.dlmsHelper.readDateTime(data2, expectedDateTimeDescriptionCaptureTime)).thenReturn(cosemDateTime);
when(this.dlmsHelper.readDateTime(data5, expectedDateTimeDescriptionCaptureTime)).thenReturn(cosemDateTime);
final DlmsMeterValueDto meterValue1 = mock(DlmsMeterValueDto.class);
final DlmsMeterValueDto meterValue2 = mock(DlmsMeterValueDto.class);
when(this.dlmsHelper.getScaledMeterValue(data1, null, "gasValue")).thenReturn(meterValue1);
when(this.dlmsHelper.getScaledMeterValue(data4, null, "gasValue")).thenReturn(meterValue2);
// CALL
final PeriodicMeterReadGasResponseDto result = this.executor.execute(this.connectionManager, this.device, request, this.messageMetadata);
// VERIFY - the right functions should be called
verify(this.dlmsMessageListener).setDescription(String.format("GetPeriodicMeterReadsGas for channel ONE, DAILY from %s until %s, retrieve attribute: {%s,%s,%s}", new DateTime(this.from), new DateTime(this.to), dlmsProfile.getClassId(), dlmsProfile.getObisCodeAsString(), dlmsProfile.getDefaultAttributeId()));
verify(this.dlmsHelper, times(2)).validateBufferedDateTime(any(DateTime.class), argThat(new DateTimeMatcher(this.from)), argThat(new DateTimeMatcher(this.to)));
verify(this.dlmsObjectConfigService).findDlmsObject(any(Protocol.class), any(DlmsObjectType.class), any(Medium.class));
// ASSERT - the result should contain 2 values
final List<PeriodicMeterReadsGasResponseItemDto> periodicMeterReads = result.getPeriodicMeterReadsGas();
assertThat(periodicMeterReads.size()).isEqualTo(2);
assertThat(periodicMeterReads.stream().anyMatch(r -> r.getConsumption() == meterValue1)).isEqualTo(true);
assertThat(periodicMeterReads.stream().anyMatch(r -> r.getConsumption() == meterValue2)).isEqualTo(true);
assertThat(periodicMeterReads.stream().allMatch(r -> this.areDatesEqual(r.getLogTime(), cosemDateTime))).isEqualTo(true);
assertThat(periodicMeterReads.stream().allMatch(r -> this.areDatesEqual(r.getCaptureTime(), cosemDateTime))).isEqualTo(true);
}
Aggregations