Search in sources :

Example 21 with DataObject

use of org.openmuc.jdlms.datatypes.DataObject in project open-smart-grid-platform by OSGP.

the class GetGsmDiagnosticCommandExecutorTest method testHappy.

@Test
public void testHappy() throws Exception {
    // SETUP
    when(this.dlmsObjectConfigService.getDlmsObjectForCommunicationMethod(this.device, DlmsObjectType.GSM_DIAGNOSTIC)).thenReturn(new DlmsObject(DlmsObjectType.GSM_DIAGNOSTIC, this.classId, this.obisCode));
    // SETUP - mock return data objects
    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 result6 = mock(GetResult.class);
    final GetResult result7 = mock(GetResult.class);
    when(result2.getResultCode()).thenReturn(AccessResultCode.SUCCESS);
    when(result3.getResultCode()).thenReturn(AccessResultCode.SUCCESS);
    when(result4.getResultCode()).thenReturn(AccessResultCode.SUCCESS);
    when(result5.getResultCode()).thenReturn(AccessResultCode.SUCCESS);
    when(result6.getResultCode()).thenReturn(AccessResultCode.SUCCESS);
    when(result7.getResultCode()).thenReturn(AccessResultCode.SUCCESS);
    final DataObject operator = mock(DataObject.class);
    when(result2.getResultData()).thenReturn(operator);
    when(operator.getValue()).thenReturn(new byte[] { 65, 66 });
    final DataObject modemRegistrationStatus = mock(DataObject.class);
    when(result3.getResultData()).thenReturn(modemRegistrationStatus);
    when(modemRegistrationStatus.getValue()).thenReturn(2);
    final DataObject csStatus = mock(DataObject.class);
    when(result4.getResultData()).thenReturn(csStatus);
    when(csStatus.getValue()).thenReturn(3);
    final DataObject psStatus = mock(DataObject.class);
    when(result5.getResultData()).thenReturn(psStatus);
    when(psStatus.getValue()).thenReturn(4);
    final DataObject cellInfo = mock(DataObject.class);
    when(result6.getResultData()).thenReturn(cellInfo);
    final DataObject cellId = mock(DataObject.class);
    final DataObject locationId = mock(DataObject.class);
    final DataObject signalQuality = mock(DataObject.class);
    final DataObject bitErrorRate = mock(DataObject.class);
    final DataObject mcc = mock(DataObject.class);
    final DataObject mnc = mock(DataObject.class);
    final DataObject channelNumber = mock(DataObject.class);
    when(cellInfo.getValue()).thenReturn(Arrays.asList(cellId, locationId, signalQuality, bitErrorRate, mcc, mnc, channelNumber));
    when(cellId.getValue()).thenReturn(128L);
    when(locationId.getValue()).thenReturn(1);
    when(signalQuality.getValue()).thenReturn((short) 2);
    when(bitErrorRate.getValue()).thenReturn((short) 3);
    when(mcc.getValue()).thenReturn(4);
    when(mnc.getValue()).thenReturn(5);
    when(channelNumber.getValue()).thenReturn(6L);
    final DataObject adjacentCells = mock(DataObject.class);
    when(result7.getResultData()).thenReturn(adjacentCells);
    final DataObject adjacentCell = mock(DataObject.class);
    when(adjacentCells.getValue()).thenReturn(Collections.singletonList(adjacentCell));
    final DataObject adjacentCellId = mock(DataObject.class);
    final DataObject adjacentCellSignalQuality = mock(DataObject.class);
    when(adjacentCell.getValue()).thenReturn(Arrays.asList(adjacentCellId, adjacentCellSignalQuality));
    when(adjacentCellId.getValue()).thenReturn(256L);
    when(adjacentCellSignalQuality.getValue()).thenReturn((short) 7);
    when(this.dlmsHelper.getAndCheck(eq(this.connectionManager), eq(this.device), eq("Get GsmDiagnostic"), any())).thenReturn(Arrays.asList(result2, result3, result4, result5, result6, result7));
    // CALL
    final GetGsmDiagnosticResponseDto result = this.executor.execute(this.connectionManager, this.device, this.request, this.messageMetadata);
    // VERIFY calls to mocks
    verify(this.dlmsMessageListener).setDescription(String.format("Get GsmDiagnostic, retrieve attributes: %s, %s, %s, %s, %s, %s", this.createAttributeAddress(2), this.createAttributeAddress(3), this.createAttributeAddress(4), this.createAttributeAddress(5), this.createAttributeAddress(6), this.createAttributeAddress(7)));
    verify(this.dlmsObjectConfigService).getDlmsObjectForCommunicationMethod(this.device, DlmsObjectType.GSM_DIAGNOSTIC);
    // VERIFY contents of the return value
    assertThat(result.getOperator()).isEqualTo("AB");
    assertThat(result.getModemRegistrationStatus()).isEqualTo(ModemRegistrationStatusDto.fromIndexValue(2));
    assertThat(result.getCircuitSwitchedStatus()).isEqualTo(CircuitSwitchedStatusDto.fromIndexValue(3));
    assertThat(result.getPacketSwitchedStatus()).isEqualTo(PacketSwitchedStatusDto.fromIndexValue(4));
    assertThat(result.getCellInfo().getCellId()).isEqualTo(128L);
    assertThat(result.getCellInfo().getLocationId()).isEqualTo(1);
    assertThat(result.getCellInfo().getSignalQuality()).isEqualTo(SignalQualityDto.fromIndexValue(2));
    assertThat(result.getCellInfo().getBitErrorRate()).isEqualTo(BitErrorRateDto.fromIndexValue(3));
    assertThat(result.getCellInfo().getMobileCountryCode()).isEqualTo(4);
    assertThat(result.getCellInfo().getMobileNetworkCode()).isEqualTo(5);
    assertThat(result.getCellInfo().getChannelNumber()).isEqualTo(6);
    assertThat(result.getAdjacentCells().size()).isEqualTo(1);
    assertThat(result.getAdjacentCells().get(0).getCellId()).isEqualTo(256L);
    assertThat(result.getAdjacentCells().get(0).getSignalQuality()).isEqualTo(SignalQualityDto.fromIndexValue(7));
}
Also used : GetGsmDiagnosticResponseDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.GetGsmDiagnosticResponseDto) DataObject(org.openmuc.jdlms.datatypes.DataObject) GetResult(org.openmuc.jdlms.GetResult) DlmsObject(org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.dlmsobjectconfig.model.DlmsObject) Test(org.junit.jupiter.api.Test)

Example 22 with DataObject

use of org.openmuc.jdlms.datatypes.DataObject 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 23 with DataObject

use of org.openmuc.jdlms.datatypes.DataObject in project open-smart-grid-platform by OSGP.

the class GetPeriodicMeterReadsCommandExecutorIntegrationTest method createAttributeAddress.

private AttributeAddress createAttributeAddress(final Protocol protocol, final PeriodTypeDto type, final Date timeFrom, final Date timeTo) throws Exception {
    final DataObject from = this.dlmsHelper.asDataObject(new DateTime(timeFrom));
    final DataObject to = this.dlmsHelper.asDataObject(new DateTime(timeTo));
    if (protocol == Protocol.DSMR_4_2_2) {
        if (type == PeriodTypeDto.DAILY) {
            return this.createAttributeAddressDsmr4Daily(from, to);
        } else if (type == PeriodTypeDto.MONTHLY) {
            return this.createAttributeAddressDsmr4Monthly(from, to);
        } else if (type == PeriodTypeDto.INTERVAL) {
            return this.createAttributeAddressDsmr4Interval(from, to);
        }
    } else if (protocol == Protocol.SMR_5_0_0 || protocol == Protocol.SMR_5_1) {
        if (type == PeriodTypeDto.DAILY) {
            return this.createAttributeAddressSmr5Daily(from, to);
        } else if (type == PeriodTypeDto.MONTHLY) {
            return this.createAttributeAddressSmr5Monthly(from, to);
        } else if (type == PeriodTypeDto.INTERVAL) {
            return this.createAttributeAddressSmr5Interval(from, to);
        }
    }
    throw new Exception("Invalid combination of protocol " + protocol.getName() + " and version " + protocol.getVersion());
}
Also used : DataObject(org.openmuc.jdlms.datatypes.DataObject) DateTime(org.joda.time.DateTime) CosemDateTime(org.openmuc.jdlms.datatypes.CosemDateTime)

Example 24 with DataObject

use of org.openmuc.jdlms.datatypes.DataObject in project open-smart-grid-platform by OSGP.

the class GetPeriodicMeterReadsCommandExecutorIntegrationTest method createSelectiveAccessDescriptionDsmr4Daily.

private SelectiveAccessDescription createSelectiveAccessDescriptionDsmr4Daily(final DataObject from, final DataObject to) {
    final DataObject selectedValues = DataObject.newArrayData(Arrays.asList(this.CLOCK, this.STATUS, this.ACTIVE_ENERGY_IMPORT_RATE_1, this.ACTIVE_ENERGY_IMPORT_RATE_2, this.ACTIVE_ENERGY_EXPORT_RATE_1, this.ACTIVE_ENERGY_EXPORT_RATE_2));
    final DataObject expectedAccessParam = DataObject.newStructureData(Arrays.asList(this.CLOCK, from, to, selectedValues));
    return new SelectiveAccessDescription(1, expectedAccessParam);
}
Also used : DataObject(org.openmuc.jdlms.datatypes.DataObject) SelectiveAccessDescription(org.openmuc.jdlms.SelectiveAccessDescription)

Example 25 with DataObject

use of org.openmuc.jdlms.datatypes.DataObject in project open-smart-grid-platform by OSGP.

the class GetPeriodicMeterReadsGasCommandExecutorIntegrationTest method createSelectiveAccessDescriptionDsmr4Monthly.

private SelectiveAccessDescription createSelectiveAccessDescriptionDsmr4Monthly(final DataObject from, final DataObject to) {
    final DataObject selectedValues = DataObject.newArrayData(Arrays.asList(this.CLOCK, this.GAS_VALUE_DSMR4, this.GAS_CAPTURE_TIME_DSMR4));
    final DataObject expectedAccessParam = DataObject.newStructureData(Arrays.asList(this.CLOCK, from, to, selectedValues));
    return new SelectiveAccessDescription(1, expectedAccessParam);
}
Also used : DataObject(org.openmuc.jdlms.datatypes.DataObject) SelectiveAccessDescription(org.openmuc.jdlms.SelectiveAccessDescription)

Aggregations

DataObject (org.openmuc.jdlms.datatypes.DataObject)176 ArrayList (java.util.ArrayList)46 AttributeAddress (org.openmuc.jdlms.AttributeAddress)36 ProtocolAdapterException (org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ProtocolAdapterException)34 Test (org.junit.jupiter.api.Test)31 GetResult (org.openmuc.jdlms.GetResult)23 SelectiveAccessDescription (org.openmuc.jdlms.SelectiveAccessDescription)16 DateTime (org.joda.time.DateTime)15 CosemDateTime (org.openmuc.jdlms.datatypes.CosemDateTime)14 SetParameter (org.openmuc.jdlms.SetParameter)12 DlmsObject (org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.dlmsobjectconfig.model.DlmsObject)11 BitString (org.openmuc.jdlms.datatypes.BitString)10 ObisCode (org.openmuc.jdlms.ObisCode)9 IOException (java.io.IOException)8 ConnectionException (org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ConnectionException)7 GetResultImpl (org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.testutil.GetResultImpl)6 List (java.util.List)5 MethodResultCode (org.openmuc.jdlms.MethodResultCode)5 AttributeAddressForProfile (org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.dlmsobjectconfig.AttributeAddressForProfile)5 DlmsProfile (org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.dlmsobjectconfig.model.DlmsProfile)5