GetPeriodicMeterReadsCommandExecutor extends AbstractPeriodicMeterReadsCommandExecutor<PeriodicMeterReadsRequestDto, PeriodicMeterReadsResponseDto> { @Override public PeriodicMeterReadsResponseDto execute(final DlmsConnectionManager conn, final DlmsDevice device, final PeriodicMeterReadsRequestDto periodicMeterReadsQuery) throws ProtocolAdapterException { if (periodicMeterReadsQuery == null) { throw new IllegalArgumentException( "PeriodicMeterReadsQuery should contain PeriodType, BeginDate and EndDate."); } final PeriodTypeDto queryPeriodType = periodicMeterReadsQuery.getPeriodType(); final DateTime from = new DateTime(periodicMeterReadsQuery.getBeginDate()); final DateTime to = new DateTime(periodicMeterReadsQuery.getEndDate()); final AttributeAddressForProfile profileBufferAddress = this.getProfileBufferAddress(queryPeriodType, from, to, device); final List<AttributeAddress> scalerUnitAddresses = this.getScalerUnitAddresses(profileBufferAddress); final Optional<ProfileCaptureTime> intervalTime = this.getProfileCaptureTime(device, this.dlmsObjectConfigService, Medium.ELECTRICITY); LOGGER.debug("Retrieving current billing period and profiles for period type: {}, from: {}, to: {}", queryPeriodType, from, to); final List<GetResult> getResultList = new ArrayList<>(); final List<AttributeAddress> allAttributeAddresses = new ArrayList<>(); allAttributeAddresses.add(profileBufferAddress.getAttributeAddress()); allAttributeAddresses.addAll(scalerUnitAddresses); for (final AttributeAddress address : allAttributeAddresses) { conn.getDlmsMessageListener().setDescription(String.format(FORMAT_DESCRIPTION, queryPeriodType, from, to, JdlmsObjectToStringUtil.describeAttributes(address))); getResultList.addAll( this.dlmsHelper.getAndCheck(conn, device, "retrieve periodic meter reads for " + queryPeriodType, address)); } LOGGER.info("Received getResult: {} ", getResultList); final DataObject resultData = this.dlmsHelper.readDataObject(getResultList.get(0), PERIODIC_E_METER_READS); final List<DataObject> bufferedObjectsList = resultData.getValue(); final List<PeriodicMeterReadsResponseItemDto> periodicMeterReads = new ArrayList<>(); for (final DataObject bufferedObject : bufferedObjectsList) { final List<DataObject> bufferedObjectValue = bufferedObject.getValue(); try { periodicMeterReads.add(this.convertToResponseItem( new ConversionContext(periodicMeterReadsQuery, bufferedObjectValue, getResultList, profileBufferAddress, scalerUnitAddresses, intervalTime), periodicMeterReads)); } catch (final BufferedDateTimeValidationException e) { LOGGER.warn(e.getMessage(), e); } } return new PeriodicMeterReadsResponseDto(queryPeriodType, periodicMeterReads); } @Autowired  GetPeriodicMeterReadsCommandExecutor(final DlmsHelper dlmsHelper,
            final AmrProfileStatusCodeHelper amrProfileStatusCodeHelper,
            final DlmsObjectConfigService dlmsObjectConfigService); @Override PeriodicMeterReadsRequestDto fromBundleRequestInput(final ActionRequestDto bundleInput); @Override PeriodicMeterReadsResponseDto execute(final DlmsConnectionManager conn, final DlmsDevice device,
            final PeriodicMeterReadsRequestDto periodicMeterReadsQuery);  }
@Test public void testExecuteNullRequest() throws ProtocolAdapterException { try { this.executor.execute(this.connectionManager, this.device, null); fail("Calling execute with null query should fail"); } catch (final IllegalArgumentException e) { } }
@Test public void testExecuteObjectNotFound() { final PeriodicMeterReadsRequestDto request = new PeriodicMeterReadsRequestDto(PeriodTypeDto.DAILY, this.fromDateTime.toDate(), this.toDateTime.toDate(), ChannelDto.ONE); when(this.dlmsObjectConfigService.findAttributeAddressForProfile(any(), any(), any(), any(), any(), any())) .thenReturn(Optional.empty()); try { this.executor.execute(this.connectionManager, this.device, request); fail("When no matching object is found, then execute should fail"); } catch (final ProtocolAdapterException e) { assertThat(e.getMessage()).isEqualTo("No address found for " + DlmsObjectType.DAILY_LOAD_PROFILE); } }
@Test public void testHappy() throws Exception { final PeriodTypeDto periodType = PeriodTypeDto.DAILY; final PeriodicMeterReadsRequestDto request = new PeriodicMeterReadsRequestDto(periodType, new Date(this.from), new Date(this.to)); 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); final AttributeAddressForProfile attributeAddressForProfile = this.createAttributeAddressForProfile(dlmsProfile, captureObjects); final AttributeAddress attributeAddress = this.createAttributeAddress(dlmsProfile); when(this.dlmsObjectConfigService.findAttributeAddressForProfile(eq(this.device), eq(DlmsObjectType.DAILY_LOAD_PROFILE), eq(0), eq(this.fromDateTime), eq(this.toDateTime), eq(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)); final DataObject data0 = mock(DataObject.class); 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(eq(this.connectionManager), eq(this.device), eq(expectedDescription), eq(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); final PeriodicMeterReadsResponseDto result = this.executor.execute(this.connectionManager, this.device, request); 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)); final List<PeriodicMeterReadsResponseItemDto> periodicMeterReads = result.getPeriodicMeterReads(); assertThat(periodicMeterReads.size()).isEqualTo(2); periodicMeterReads.forEach(p -> { assertThat(p.getLogTime()).isNotNull(); }); }