use of org.openmuc.jdlms.GetResult in project open-smart-grid-platform by OSGP.
the class AbstractCommandExecutor method getValidatedResultData.
/**
* Retrieves connection, gets result data and validates it before returning.
*
* @param conn holds connection
* @param getParameter for attribute to retrieve result data from
* @return dataObject
*/
public DataObject getValidatedResultData(final DlmsConnectionManager conn, final AttributeAddress getParameter) throws ProtocolAdapterException {
final GetResult getResult;
try {
getResult = conn.getConnection().get(getParameter);
} catch (final IOException e) {
throw new ConnectionException(e);
}
if (getResult == null) {
throw new ProtocolAdapterException("No GetResult received while retrieving M-Bus encryption key status.");
}
final DataObject dataObject = getResult.getResultData();
if (!dataObject.isNumber()) {
throw new ProtocolAdapterException("Received unexpected result data.");
}
return dataObject;
}
use of org.openmuc.jdlms.GetResult in project open-smart-grid-platform by OSGP.
the class SetAlarmNotificationsCommandExecutor method retrieveCurrentAlarmNotifications.
private AlarmNotificationsDto retrieveCurrentAlarmNotifications(final DlmsConnectionManager conn, final AttributeAddress alarmFilterValue, final DlmsObjectType alarmRegisterDlmsObjectType) throws IOException, ProtocolAdapterException {
conn.getDlmsMessageListener().setDescription("SetAlarmNotifications retrieve current value, retrieve attribute: " + JdlmsObjectToStringUtil.describeAttributes(alarmFilterValue));
LOGGER.info("Retrieving current alarm filter by issuing get request for for address: {}", alarmFilterValue);
final GetResult getResult = conn.getConnection().get(alarmFilterValue);
if (getResult == null) {
throw new ProtocolAdapterException("No GetResult received while retrieving current alarm filter.");
}
return this.alarmNotifications(getResult.getResultData(), alarmRegisterDlmsObjectType);
}
use of org.openmuc.jdlms.GetResult 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));
}
use of org.openmuc.jdlms.GetResult in project open-smart-grid-platform by OSGP.
the class GetGsmDiagnosticCommandExecutorTest method testUnhappy.
@Test
public void testUnhappy() throws Exception {
// SETUP
when(this.dlmsObjectConfigService.getDlmsObjectForCommunicationMethod(this.device, DlmsObjectType.GSM_DIAGNOSTIC)).thenReturn(new DlmsObject(DlmsObjectType.GSM_DIAGNOSTIC, this.classId, this.obisCode));
final GetResult result = mock(GetResult.class);
when(result.getResultCode()).thenReturn(AccessResultCode.HARDWARE_FAULT);
when(this.dlmsHelper.getAndCheck(eq(this.connectionManager), eq(this.device), eq("Get GsmDiagnostic"), any())).thenReturn(Collections.singletonList(result));
// CALL
try {
this.executor.execute(this.connectionManager, this.device, this.request, this.messageMetadata);
fail("When result contains failure, then execute should fail");
} catch (final ProtocolAdapterException e) {
assertThat(e.getMessage()).isEqualTo("Get gsm diagnostic failed for " + this.device.getDeviceId());
}
}
use of org.openmuc.jdlms.GetResult 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());
}
Aggregations