Search in sources :

Example 11 with CosemDateTimeDto

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

the class GetPeriodicMeterReadsGasCommandExecutor method readCaptureTime.

private Date readCaptureTime(final ConversionContext ctx, final Optional<Date> previousCaptureTime) throws ProtocolAdapterException, BufferedDateTimeValidationException {
    final List<DataObject> bufferedObjects = ctx.bufferedObjects;
    final AttributeAddressForProfile attributeAddressForProfile = ctx.attributeAddressForProfile;
    final Integer captureTimeIndex = attributeAddressForProfile.getIndex(DlmsObjectType.MBUS_MASTER_VALUE, 5);
    if (captureTimeIndex != null) {
        final CosemDateTimeDto cosemDateTime = this.dlmsHelper.readDateTime(bufferedObjects.get(captureTimeIndex), "Clock from mbus interval extended register");
        if (cosemDateTime != null) {
            if (cosemDateTime.isDateTimeSpecified()) {
                return cosemDateTime.asDateTime().toDate();
            } else {
                throw new ProtocolAdapterException(UNEXPECTED_VALUE);
            }
        } else {
            return this.calculateIntervalTimeBasedOnPreviousValue(ctx.periodicMeterReadsQuery.getPeriodType(), previousCaptureTime, ctx.intervalTime);
        }
    }
    return null;
}
Also used : DataObject(org.openmuc.jdlms.datatypes.DataObject) AttributeAddressForProfile(org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.dlmsobjectconfig.AttributeAddressForProfile) ProtocolAdapterException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ProtocolAdapterException) CosemDateTimeDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.CosemDateTimeDto)

Example 12 with CosemDateTimeDto

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

the class DlmsHelperTest method testFromByteArraySummerTime.

@Test
public void testFromByteArraySummerTime() throws Exception {
    final CosemDateTimeDto cosemDateTime = this.dlmsHelper.fromDateTimeValue(this.byteArraySummerTime());
    assertThat(cosemDateTime.isDateTimeSpecified()).isTrue();
    final DateTime dateInSummerTime = cosemDateTime.asDateTime();
    assertThat(ISODateTimeFormat.dateTime().print(dateInSummerTime)).isEqualTo("2015-07-21T14:53:07.230+02:00");
}
Also used : DateTime(org.joda.time.DateTime) CosemDateTime(org.openmuc.jdlms.datatypes.CosemDateTime) CosemDateTimeDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.CosemDateTimeDto) Test(org.junit.jupiter.api.Test)

Example 13 with CosemDateTimeDto

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

the class DlmsHelperTest method testFromByteArrayUnspecifiedTime.

@Test
public void testFromByteArrayUnspecifiedTime() throws Exception {
    final CosemDateTimeDto cosemDateTime = this.dlmsHelper.fromDateTimeValue(this.byteArrayUnspecifiedTime());
    assertThat(cosemDateTime.isDateTimeSpecified()).isFalse();
    assertThat(cosemDateTime.isLocalDateSpecified()).isFalse();
    assertThat(cosemDateTime.isLocalDateTimeSpecified()).isFalse();
    assertThat(cosemDateTime.isLocalTimeSpecified()).isFalse();
    assertThat(cosemDateTime.isDeviationSpecified()).isFalse();
    assertThat(cosemDateTime.asDateTime()).isNull();
}
Also used : CosemDateTimeDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.CosemDateTimeDto) Test(org.junit.jupiter.api.Test)

Example 14 with CosemDateTimeDto

use of org.opensmartgridplatform.dto.valueobjects.smartmetering.CosemDateTimeDto 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);
}
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) ChannelDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.ChannelDto) AttributeAddress(org.openmuc.jdlms.AttributeAddress) 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) DlmsExtendedRegister(org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.dlmsobjectconfig.model.DlmsExtendedRegister) Medium(org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.dlmsobjectconfig.model.Medium) DlmsProfile(org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.dlmsobjectconfig.model.DlmsProfile) PeriodicMeterReadGasResponseDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.PeriodicMeterReadGasResponseDto) 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) DlmsMeterValueDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.DlmsMeterValueDto) Test(org.junit.jupiter.api.Test)

Example 15 with CosemDateTimeDto

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

the class ActivityCalendarValidatorTest method createActivityCalendarDto.

ActivityCalendarDto createActivityCalendarDto(final TestAnomaly testAnomaly) throws FunctionalException {
    final DayProfileActionDto dayAction1 = new DayProfileActionDto(1, new CosemTimeDto());
    final DayProfileActionDto dayAction2 = new DayProfileActionDto(2, new CosemTimeDto());
    final DayProfileActionDto dayAction3 = new DayProfileActionDto(3, new CosemTimeDto());
    final DayProfileActionDto dayAction4 = new DayProfileActionDto(4, new CosemTimeDto());
    final DayProfileActionDto dayAction5 = new DayProfileActionDto(5, new CosemTimeDto());
    final List<DayProfileActionDto> dayActions1 = Arrays.asList(dayAction1, dayAction2);
    final List<DayProfileActionDto> dayActions2 = Arrays.asList(dayAction3, dayAction4, dayAction5);
    int dayId = 2;
    if (testAnomaly == TestAnomaly.DUPLICATE_DAY) {
        // a second day with id 1 will be created with different day actions
        dayId = 1;
    }
    final DayProfileDto dayProfile1 = new DayProfileDto(1, dayActions1);
    final DayProfileDto dayProfile2 = new DayProfileDto(dayId, dayActions2);
    final DayProfileDto dayProfile3 = new DayProfileDto(3, dayActions2);
    final DayProfileDto dayProfile4 = new DayProfileDto(4, dayActions2);
    final DayProfileDto dayProfile5 = new DayProfileDto(5, dayActions2);
    final WeekProfileDto weekProfileDto1 = this.createWeekProfile("1", dayProfile1, dayProfile2);
    final SeasonProfileDto seasonProfileDto1 = new SeasonProfileDto("1", new CosemDateTimeDto(), weekProfileDto1);
    String weekProfileName = "2";
    if (testAnomaly == TestAnomaly.DUPLICATE_WEEK) {
        // a second week with name 'Week___1' will be created with different day profiles
        weekProfileName = "1";
    } else if (testAnomaly == TestAnomaly.INVALID_WEEK_NAME) {
        weekProfileName = "Week01";
    }
    final Builder weekProfileBuilder2 = WeekProfileDto.newBuilder();
    weekProfileBuilder2.withWeekProfileName(weekProfileName);
    weekProfileBuilder2.withMonday(dayProfile3);
    weekProfileBuilder2.withTuesday(dayProfile3);
    weekProfileBuilder2.withWednesday(dayProfile3);
    weekProfileBuilder2.withThursday(dayProfile3);
    weekProfileBuilder2.withFriday(dayProfile3);
    weekProfileBuilder2.withSaturday(dayProfile4);
    if (testAnomaly == TestAnomaly.FIFTH_DAY) {
        // an overall fifth day profile will be added
        weekProfileBuilder2.withSunday(dayProfile5);
    } else {
        weekProfileBuilder2.withSunday(dayProfile4);
    }
    final WeekProfileDto weekProfileDto2 = weekProfileBuilder2.build();
    String seasonProfileName = "2";
    if (testAnomaly == TestAnomaly.DUPLICATE_SEASON) {
        // a second season with name 'Season_1' will be created with a different week profile
        seasonProfileName = "1";
    } else if (testAnomaly == TestAnomaly.INVALID_SEASON_NAME) {
        seasonProfileName = "Season_1";
    }
    WeekProfileDto weekProfileDto = weekProfileDto2;
    if (testAnomaly == TestAnomaly.MULTIPLE_USES_SAME_WEEK) {
        // the same week profile is used in multiple seasons
        weekProfileDto = weekProfileDto1;
    }
    final SeasonProfileDto seasonProfileDto2 = new SeasonProfileDto(seasonProfileName, new CosemDateTimeDto(), weekProfileDto);
    final List<SeasonProfileDto> seasonList = new ArrayList<>();
    seasonList.addAll(Arrays.asList(seasonProfileDto1, seasonProfileDto2));
    if (testAnomaly == TestAnomaly.FIFTH_SEASON) {
        // a third, fourth and fifth season will be added to the activity calendar
        seasonList.addAll(Arrays.asList(new SeasonProfileDto("3", new CosemDateTimeDto(), WeekProfileDto.newBuilder().withWeekProfileName("3").build()), new SeasonProfileDto("4", new CosemDateTimeDto(), WeekProfileDto.newBuilder().withWeekProfileName("4").build()), new SeasonProfileDto("5", new CosemDateTimeDto(), WeekProfileDto.newBuilder().withWeekProfileName("5").build())));
    }
    return new ActivityCalendarDto("Calendar", new CosemDateTimeDto(), seasonList);
}
Also used : Builder(org.opensmartgridplatform.dto.valueobjects.smartmetering.WeekProfileDto.Builder) ArrayList(java.util.ArrayList) DayProfileActionDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.DayProfileActionDto) CosemTimeDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.CosemTimeDto) CosemDateTimeDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.CosemDateTimeDto) DayProfileDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.DayProfileDto) WeekProfileDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.WeekProfileDto) SeasonProfileDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.SeasonProfileDto) ActivityCalendarDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.ActivityCalendarDto)

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