Search in sources :

Example 1 with BufferedDateTimeValidationException

use of org.opensmartgridplatform.adapter.protocol.dlms.exceptions.BufferedDateTimeValidationException in project open-smart-grid-platform by OSGP.

the class GetPeriodicMeterReadsGasCommandExecutor method execute.

@Override
public PeriodicMeterReadGasResponseDto execute(final DlmsConnectionManager conn, final DlmsDevice device, final PeriodicMeterReadsRequestDto periodicMeterReadsQuery, final MessageMetadata messageMetadata) 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, periodicMeterReadsQuery.getChannel(), from, to, device);
    final List<AttributeAddress> scalerUnitAddresses = this.getScalerUnitAddresses(periodicMeterReadsQuery.getChannel(), profileBufferAddress);
    final Optional<ProfileCaptureTime> intervalTime = this.getProfileCaptureTime(device, this.dlmsObjectConfigService, Medium.GAS);
    LOGGER.info("Retrieving current billing period and profiles for gas for period type: {}, from: " + "{}, to: {}", queryPeriodType, from, to);
    /*
     * workaround for a problem when using with_list and retrieving a profile
     * buffer, this will be returned erroneously.
     */
    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, periodicMeterReadsQuery.getChannel(), queryPeriodType, from, to, JdlmsObjectToStringUtil.describeAttributes(address)));
        getResultList.addAll(this.dlmsHelper.getAndCheck(conn, device, "retrieve periodic meter reads for " + queryPeriodType + ", channel " + periodicMeterReadsQuery.getChannel(), address));
    }
    LOGGER.info("Received getResult: {} ", getResultList);
    final DataObject resultData = this.dlmsHelper.readDataObject(getResultList.get(0), PERIODIC_G_METER_READS);
    final List<DataObject> bufferedObjectsList = resultData.getValue();
    final List<PeriodicMeterReadsGasResponseItemDto> 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);
        }
    }
    LOGGER.info("Resulting periodicMeterReads: {} ", periodicMeterReads);
    return new PeriodicMeterReadGasResponseDto(queryPeriodType, periodicMeterReads);
}
Also used : AttributeAddressForProfile(org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.dlmsobjectconfig.AttributeAddressForProfile) PeriodTypeDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.PeriodTypeDto) GetResult(org.openmuc.jdlms.GetResult) PeriodicMeterReadsGasResponseItemDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.PeriodicMeterReadsGasResponseItemDto) AttributeAddress(org.openmuc.jdlms.AttributeAddress) ArrayList(java.util.ArrayList) DateTime(org.joda.time.DateTime) BufferedDateTimeValidationException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.BufferedDateTimeValidationException) ProfileCaptureTime(org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.dlmsobjectconfig.model.ProfileCaptureTime) DataObject(org.openmuc.jdlms.datatypes.DataObject) PeriodicMeterReadGasResponseDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.PeriodicMeterReadGasResponseDto)

Example 2 with BufferedDateTimeValidationException

use of org.opensmartgridplatform.adapter.protocol.dlms.exceptions.BufferedDateTimeValidationException in project open-smart-grid-platform by OSGP.

the class GetPeriodicMeterReadsCommandExecutor method execute.

@Override
public PeriodicMeterReadsResponseDto execute(final DlmsConnectionManager conn, final DlmsDevice device, final PeriodicMeterReadsRequestDto periodicMeterReadsQuery, final MessageMetadata messageMetadata) 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);
    // Get results one by one because getWithList does not work for all devices
    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);
}
Also used : AttributeAddressForProfile(org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.dlmsobjectconfig.AttributeAddressForProfile) PeriodicMeterReadsResponseItemDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.PeriodicMeterReadsResponseItemDto) PeriodTypeDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.PeriodTypeDto) GetResult(org.openmuc.jdlms.GetResult) AttributeAddress(org.openmuc.jdlms.AttributeAddress) ArrayList(java.util.ArrayList) PeriodicMeterReadsResponseDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.PeriodicMeterReadsResponseDto) DateTime(org.joda.time.DateTime) BufferedDateTimeValidationException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.BufferedDateTimeValidationException) ProfileCaptureTime(org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.dlmsobjectconfig.model.ProfileCaptureTime) DataObject(org.openmuc.jdlms.datatypes.DataObject)

Example 3 with BufferedDateTimeValidationException

use of org.opensmartgridplatform.adapter.protocol.dlms.exceptions.BufferedDateTimeValidationException in project open-smart-grid-platform by OSGP.

the class AbstractPeriodicMeterReadsCommandExecutor method readClock.

/**
 * Calculates/derives the date of the read buffered DataObject.
 *
 * @param ctx context elements for the buffered object conversion
 * @param previousLogTime the log time of the previous meter read
 * @param dlmsHelper dlms helper object
 * @return the date of the buffered {@link DataObject} or null if it cannot be determined
 * @throws ProtocolAdapterException
 * @throws BufferedDateTimeValidationException in case the date is invalid or null
 */
Date readClock(final ConversionContext ctx, final Optional<Date> previousLogTime, final DlmsHelper dlmsHelper) throws ProtocolAdapterException, BufferedDateTimeValidationException {
    final Date logTime;
    final PeriodTypeDto queryPeriodType = ctx.periodicMeterReadsQuery.getPeriodType();
    final DateTime from = new DateTime(ctx.periodicMeterReadsQuery.getBeginDate());
    final DateTime to = new DateTime(ctx.periodicMeterReadsQuery.getEndDate());
    final Integer clockIndex = ctx.attributeAddressForProfile.getIndex(DlmsObjectType.CLOCK, null);
    CosemDateTimeDto cosemDateTime = null;
    if (clockIndex != null) {
        cosemDateTime = dlmsHelper.readDateTime(ctx.bufferedObjects.get(clockIndex), "Clock from " + queryPeriodType + " buffer");
    }
    final DateTime bufferedDateTime = cosemDateTime == null ? null : cosemDateTime.asDateTime();
    if (bufferedDateTime != null) {
        dlmsHelper.validateBufferedDateTime(bufferedDateTime, from, to);
        logTime = bufferedDateTime.toDate();
    } else {
        logTime = this.calculateIntervalTimeBasedOnPreviousValue(ctx.periodicMeterReadsQuery.getPeriodType(), previousLogTime, ctx.intervalTime);
    }
    if (logTime == null) {
        throw new BufferedDateTimeValidationException("Unable to calculate logTime");
    }
    return logTime;
}
Also used : BufferedDateTimeValidationException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.BufferedDateTimeValidationException) PeriodTypeDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.PeriodTypeDto) Date(java.util.Date) LocalDateTime(java.time.LocalDateTime) DateTime(org.joda.time.DateTime) CosemDateTimeDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.CosemDateTimeDto)

Aggregations

DateTime (org.joda.time.DateTime)3 BufferedDateTimeValidationException (org.opensmartgridplatform.adapter.protocol.dlms.exceptions.BufferedDateTimeValidationException)3 PeriodTypeDto (org.opensmartgridplatform.dto.valueobjects.smartmetering.PeriodTypeDto)3 ArrayList (java.util.ArrayList)2 AttributeAddress (org.openmuc.jdlms.AttributeAddress)2 GetResult (org.openmuc.jdlms.GetResult)2 DataObject (org.openmuc.jdlms.datatypes.DataObject)2 AttributeAddressForProfile (org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.dlmsobjectconfig.AttributeAddressForProfile)2 ProfileCaptureTime (org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.dlmsobjectconfig.model.ProfileCaptureTime)2 LocalDateTime (java.time.LocalDateTime)1 Date (java.util.Date)1 CosemDateTimeDto (org.opensmartgridplatform.dto.valueobjects.smartmetering.CosemDateTimeDto)1 PeriodicMeterReadGasResponseDto (org.opensmartgridplatform.dto.valueobjects.smartmetering.PeriodicMeterReadGasResponseDto)1 PeriodicMeterReadsGasResponseItemDto (org.opensmartgridplatform.dto.valueobjects.smartmetering.PeriodicMeterReadsGasResponseItemDto)1 PeriodicMeterReadsResponseDto (org.opensmartgridplatform.dto.valueobjects.smartmetering.PeriodicMeterReadsResponseDto)1 PeriodicMeterReadsResponseItemDto (org.opensmartgridplatform.dto.valueobjects.smartmetering.PeriodicMeterReadsResponseItemDto)1