Search in sources :

Example 1 with CosemDateTimeDto

use of org.opensmartgridplatform.dto.valueobjects.smartmetering.CosemDateTimeDto 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());
}
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) Date(java.util.Date) DateTime(org.joda.time.DateTime) CosemDateTimeDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.CosemDateTimeDto) DlmsClock(org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.dlmsobjectconfig.model.DlmsClock) DataObject(org.openmuc.jdlms.datatypes.DataObject) Medium(org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.dlmsobjectconfig.model.Medium) DlmsProfile(org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.dlmsobjectconfig.model.DlmsProfile) DlmsCaptureObject(org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.dlmsobjectconfig.DlmsCaptureObject) Protocol(org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.Protocol) PeriodicMeterReadsRequestDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.PeriodicMeterReadsRequestDto) DlmsObject(org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.dlmsobjectconfig.model.DlmsObject) DlmsObjectType(org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.dlmsobjectconfig.DlmsObjectType) Test(org.junit.jupiter.api.Test)

Example 2 with CosemDateTimeDto

use of org.opensmartgridplatform.dto.valueobjects.smartmetering.CosemDateTimeDto in project open-smart-grid-platform by OSGP.

the class FindEventsCommandExecutorTest method before.

@BeforeEach
public void before() throws ProtocolAdapterException, IOException {
    final DataObject fromDate = mock(DataObject.class);
    final DataObject toDate = mock(DataObject.class);
    this.messageMetadata = MessageMetadata.newBuilder().withCorrelationUid("123456").build();
    this.findEventsRequestDto = new FindEventsRequestDto(EventLogCategoryDto.POWER_QUALITY_EVENT_LOG, DateTime.now().minusDays(70), DateTime.now());
    final DataObjectToEventListConverter dataObjectToEventListConverter = new DataObjectToEventListConverter(this.dlmsHelper);
    final DlmsObjectConfigService dlmsObjectConfigService = new DlmsObjectConfigService(this.dlmsHelper, new DlmsObjectConfigConfiguration().getDlmsObjectConfigs());
    this.executor = new FindEventsCommandExecutor(this.dlmsHelper, dataObjectToEventListConverter, dlmsObjectConfigService);
    when(this.dlmsHelper.asDataObject(this.findEventsRequestDto.getFrom())).thenReturn(fromDate);
    when(this.dlmsHelper.asDataObject(this.findEventsRequestDto.getUntil())).thenReturn(toDate);
    when(this.dlmsHelper.convertDataObjectToDateTime(any(DataObject.class))).thenReturn(new CosemDateTimeDto());
    when(this.conn.getDlmsMessageListener()).thenReturn(this.dlmsMessageListener);
    when(this.conn.getConnection()).thenReturn(this.dlmsConnection);
    when(this.dlmsConnection.get(any(AttributeAddress.class))).thenReturn(this.getResult);
}
Also used : DlmsObjectConfigConfiguration(org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.dlmsobjectconfig.DlmsObjectConfigConfiguration) FindEventsRequestDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.FindEventsRequestDto) DlmsObjectConfigService(org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.dlmsobjectconfig.DlmsObjectConfigService) DataObject(org.openmuc.jdlms.datatypes.DataObject) DataObjectToEventListConverter(org.opensmartgridplatform.adapter.protocol.dlms.application.mapping.DataObjectToEventListConverter) AttributeAddress(org.openmuc.jdlms.AttributeAddress) CosemDateTimeDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.CosemDateTimeDto) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 3 with CosemDateTimeDto

use of org.opensmartgridplatform.dto.valueobjects.smartmetering.CosemDateTimeDto in project open-smart-grid-platform by OSGP.

the class CosemDateTimeConverter method convertFrom.

@Override
public CosemDateTimeDto convertFrom(final org.openmuc.jdlms.datatypes.CosemDateTime source, final Type<CosemDateTimeDto> destinationType, final MappingContext context) {
    if (source == null) {
        return null;
    }
    final int year = source.get(CosemDateFormat.Field.YEAR);
    final int month = source.get(CosemDateFormat.Field.MONTH);
    final int dayOfMonth = source.get(CosemDateFormat.Field.DAY_OF_MONTH);
    final int dayOfWeek = source.get(CosemDateFormat.Field.DAY_OF_WEEK);
    final org.opensmartgridplatform.dto.valueobjects.smartmetering.CosemDateDto date = new org.opensmartgridplatform.dto.valueobjects.smartmetering.CosemDateDto(year, month, dayOfMonth, dayOfWeek);
    final int hour = source.get(CosemDateFormat.Field.HOUR);
    final int minute = source.get(CosemDateFormat.Field.MINUTE);
    final int second = source.get(CosemDateFormat.Field.SECOND);
    final int hundredths = source.get(CosemDateFormat.Field.HUNDREDTHS);
    final org.opensmartgridplatform.dto.valueobjects.smartmetering.CosemTimeDto time = new org.opensmartgridplatform.dto.valueobjects.smartmetering.CosemTimeDto(hour, minute, second, hundredths);
    final int deviation = source.get(CosemDateFormat.Field.DEVIATION);
    final int clockStatusValue = source.get(CosemDateFormat.Field.CLOCK_STATUS);
    final org.opensmartgridplatform.dto.valueobjects.smartmetering.ClockStatusDto clockStatus = new org.opensmartgridplatform.dto.valueobjects.smartmetering.ClockStatusDto(clockStatusValue);
    return new org.opensmartgridplatform.dto.valueobjects.smartmetering.CosemDateTimeDto(date, time, deviation, clockStatus);
}
Also used : CosemDateDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.CosemDateDto) CosemDateDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.CosemDateDto) CosemTimeDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.CosemTimeDto) CosemDateTimeDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.CosemDateTimeDto) CosemTimeDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.CosemTimeDto)

Example 4 with CosemDateTimeDto

use of org.opensmartgridplatform.dto.valueobjects.smartmetering.CosemDateTimeDto in project open-smart-grid-platform by OSGP.

the class GetGsmDiagnosticCommandExecutor method getCaptureTime.

private Date getCaptureTime(final List<GetResult> getResultList) throws ProtocolAdapterException {
    // createAttributeAddresses.
    if (RESULT_CAPTURE_TIME_INDEX >= getResultList.size()) {
        return null;
    }
    final GetResult result = getResultList.get(RESULT_CAPTURE_TIME_INDEX);
    if (this.isResultSuccess(result)) {
        final CosemDateTimeDto cosemDateTime = this.dlmsHelper.readDateTime(result.getResultData(), "Clock from gsm diagnostic");
        final Date captureTime;
        if (cosemDateTime.isDateTimeSpecified()) {
            captureTime = cosemDateTime.asDateTime().toDate();
        } else {
            throw new ProtocolAdapterException("Unexpected values in gsm diagnostic capture time");
        }
        return captureTime;
    } else {
        return null;
    }
}
Also used : GetResult(org.openmuc.jdlms.GetResult) ProtocolAdapterException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ProtocolAdapterException) Date(java.util.Date) CosemDateTimeDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.CosemDateTimeDto)

Example 5 with CosemDateTimeDto

use of org.opensmartgridplatform.dto.valueobjects.smartmetering.CosemDateTimeDto in project open-smart-grid-platform by OSGP.

the class GetActualMeterReadsGasCommandExecutor method execute.

@Override
public MeterReadsGasResponseDto execute(final DlmsConnectionManager conn, final DlmsDevice device, final ActualMeterReadsQueryDto actualMeterReadsRequest, final MessageMetadata messageMetadata) throws ProtocolAdapterException {
    final ObisCode obisCodeMbusMasterValue = this.masterValueForChannel(actualMeterReadsRequest.getChannel());
    LOGGER.debug("Retrieving current MBUS master value for ObisCode: {}", obisCodeMbusMasterValue);
    final AttributeAddress mbusValue = new AttributeAddress(CLASS_ID_MBUS, this.masterValueForChannel(actualMeterReadsRequest.getChannel()), ATTRIBUTE_ID_VALUE);
    LOGGER.debug("Retrieving current MBUS master capture time for ObisCode: {}", obisCodeMbusMasterValue);
    final AttributeAddress mbusTime = new AttributeAddress(CLASS_ID_MBUS, obisCodeMbusMasterValue, ATTRIBUTE_ID_TIME);
    final AttributeAddress scalerUnit = new AttributeAddress(CLASS_ID_MBUS, this.masterValueForChannel(actualMeterReadsRequest.getChannel()), ATTRIBUTE_ID_SCALER_UNIT);
    conn.getDlmsMessageListener().setDescription("GetActualMeterReadsGas for channel " + actualMeterReadsRequest.getChannel() + ", retrieve attributes: " + JdlmsObjectToStringUtil.describeAttributes(mbusValue, mbusTime, scalerUnit));
    final List<GetResult> getResultList = this.dlmsHelper.getAndCheck(conn, device, "retrieve actual meter reads for mbus " + actualMeterReadsRequest.getChannel(), mbusValue, mbusTime, scalerUnit);
    final DlmsMeterValueDto consumption = this.dlmsHelper.getScaledMeterValue(getResultList.get(0), getResultList.get(2), "retrieve scaled value for mbus " + actualMeterReadsRequest.getChannel());
    final CosemDateTimeDto cosemDateTime = this.dlmsHelper.readDateTime(getResultList.get(1), "captureTime gas");
    final Date captureTime;
    if (cosemDateTime.isDateTimeSpecified()) {
        captureTime = cosemDateTime.asDateTime().toDate();
    } else {
        throw new ProtocolAdapterException("Unexpected null/unspecified value for M-Bus Capture Time");
    }
    return new MeterReadsGasResponseDto(new Date(), consumption, captureTime);
}
Also used : GetResult(org.openmuc.jdlms.GetResult) AttributeAddress(org.openmuc.jdlms.AttributeAddress) ObisCode(org.openmuc.jdlms.ObisCode) ProtocolAdapterException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ProtocolAdapterException) MeterReadsGasResponseDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.MeterReadsGasResponseDto) Date(java.util.Date) DlmsMeterValueDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.DlmsMeterValueDto) CosemDateTimeDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.CosemDateTimeDto)

Aggregations

CosemDateTimeDto (org.opensmartgridplatform.dto.valueobjects.smartmetering.CosemDateTimeDto)17 DateTime (org.joda.time.DateTime)6 GetResult (org.openmuc.jdlms.GetResult)6 ProtocolAdapterException (org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ProtocolAdapterException)6 Date (java.util.Date)5 Test (org.junit.jupiter.api.Test)5 AttributeAddress (org.openmuc.jdlms.AttributeAddress)4 DataObject (org.openmuc.jdlms.datatypes.DataObject)4 DlmsMeterValueDto (org.opensmartgridplatform.dto.valueobjects.smartmetering.DlmsMeterValueDto)4 ArrayList (java.util.ArrayList)3 AttributeAddressForProfile (org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.dlmsobjectconfig.AttributeAddressForProfile)3 CosemTimeDto (org.opensmartgridplatform.dto.valueobjects.smartmetering.CosemTimeDto)3 PeriodTypeDto (org.opensmartgridplatform.dto.valueobjects.smartmetering.PeriodTypeDto)3 LocalDateTime (java.time.LocalDateTime)2 CosemDateTime (org.openmuc.jdlms.datatypes.CosemDateTime)2 DlmsCaptureObject (org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.dlmsobjectconfig.DlmsCaptureObject)2 DlmsObjectType (org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.dlmsobjectconfig.DlmsObjectType)2 DlmsClock (org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.dlmsobjectconfig.model.DlmsClock)2 DlmsObject (org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.dlmsobjectconfig.model.DlmsObject)2 DlmsProfile (org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.dlmsobjectconfig.model.DlmsProfile)2