use of org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ConnectionException in project open-smart-grid-platform by OSGP.
the class DlmsConnectionHelperTest method initializesInvocationCounterWhenInvocationCounterIsOutOfSyncForLAndGDevice.
@Test
void initializesInvocationCounterWhenInvocationCounterIsOutOfSyncForLAndGDevice() throws Exception {
final DlmsDevice device = new DlmsDeviceBuilder().withHls5Active(true).withProtocol("SMR").withInvocationCounter(123L).build();
final DlmsMessageListener listener = new InvocationCountingDlmsMessageListener();
final ConnectionException exception = new ConnectionException("Error creating connection for device E0051004228715518 with Ip address:62.133.88.34 Port:null " + "UseHdlc:false UseSn:false Message:Socket was closed by remote host.");
doThrow(exception).when(this.connectionFactory).createAndHandleConnection(this.messageMetadata, device, listener, null, this.task);
assertThrows(ConnectionException.class, () -> this.helper.createAndHandleConnectionForDevice(this.messageMetadata, device, listener, null, this.task));
verify(this.invocationCounterManager).initializeInvocationCounter(this.messageMetadata, device);
verify(this.connectionFactory, times(2)).createAndHandleConnection(this.messageMetadata, device, listener, null, this.task);
}
use of org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ConnectionException in project open-smart-grid-platform by OSGP.
the class DlmsConnectionHelperTest method doesNotResetInvocationCounterWhenInvocationCounterIsOutOfSyncForDeviceThatNeedsNoInvocationCounter.
@Test
void doesNotResetInvocationCounterWhenInvocationCounterIsOutOfSyncForDeviceThatNeedsNoInvocationCounter() throws Exception {
final DlmsDevice device = new DlmsDeviceBuilder().withHls5Active(true).withProtocol("DSMR").withInvocationCounter(123L).build();
final DlmsMessageListener listener = new InvocationCountingDlmsMessageListener();
final ConnectionException exception = new ConnectionException("Error creating connection for device E0051004228715518 with Ip address:62.133.88.34 Port:null " + "UseHdlc:false UseSn:false Message:Socket was closed by remote host.");
doThrow(exception).when(this.connectionFactory).createAndHandleConnection(this.messageMetadata, device, listener, null, this.task);
assertThrows(ConnectionException.class, () -> this.helper.createAndHandleConnectionForDevice(this.messageMetadata, device, listener, this.task));
verifyNoMoreInteractions(this.invocationCounterManager);
}
use of org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ConnectionException in project open-smart-grid-platform by OSGP.
the class DlmsHelper method getWithList.
public List<GetResult> getWithList(final DlmsConnectionManager conn, final DlmsDevice device, final AttributeAddress... params) throws ProtocolAdapterException {
try {
if (device.isWithListSupported()) {
// Getting a too large list of attribute addresses in one get
// from the DlmsConnection
// might result in a SCOPE_OF_ACCESS_VIOLATED error
final List<GetResult> getResults = new ArrayList<>();
final List<AttributeAddress[]> maximizedSubsetsOfParams = this.getMaximizedSubsetsOfParams(params);
for (final AttributeAddress[] maximizedSubsetOfParams : maximizedSubsetsOfParams) {
getResults.addAll(conn.getConnection().get(Arrays.asList(maximizedSubsetOfParams)));
}
return getResults;
} else {
return this.getWithListWorkaround(conn, params);
}
} catch (final IOException | NullPointerException e) {
// JDlmsException).
throw new ConnectionException("Connection error retrieving values with-list for device: " + device.getDeviceIdentification(), e);
} catch (final Exception e) {
throw new ProtocolAdapterException("Error retrieving values with-list for device: " + device.getDeviceIdentification() + ", with-list: " + (device.isWithListSupported() ? "supported" : "not supported"), e);
}
}
use of org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ConnectionException in project open-smart-grid-platform by OSGP.
the class DlmsHelper method getAttributeValue.
/**
* Gets a single result from a meter, and returns the result data if retrieval was successful
* (resultCode of the GetResult equals AccessResultCode.SUCCESS).
*
* @return a result from trying to retrieve the value for the attribute identified by {@code
* attributeAddress}.
*/
public DataObject getAttributeValue(final DlmsConnectionManager conn, final AttributeAddress attributeAddress) throws FunctionalException {
Objects.requireNonNull(conn, "conn must not be null");
Objects.requireNonNull(attributeAddress, "attributeAddress must not be null");
try {
final GetResult getResult = conn.getConnection().get(attributeAddress);
final AccessResultCode resultCode = getResult.getResultCode();
if (AccessResultCode.SUCCESS == resultCode) {
return getResult.getResultData();
}
final String errorMessage = String.format("Retrieving attribute value for { %d, %s, %d }. Result: resultCode(%d), with data: %s", attributeAddress.getClassId(), attributeAddress.getInstanceId().asShortObisCodeString(), attributeAddress.getId(), resultCode.getCode(), this.getDebugInfo(getResult.getResultData()));
LOGGER.error(errorMessage);
throw new FunctionalException(FunctionalExceptionType.ERROR_RETRIEVING_ATTRIBUTE_VALUE, ComponentType.PROTOCOL_DLMS, new OsgpException(ComponentType.PROTOCOL_DLMS, errorMessage));
} catch (final IOException e) {
throw new ConnectionException(e);
}
}
use of org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ConnectionException in project open-smart-grid-platform by OSGP.
the class Lls1Connector method connect.
@Override
public DlmsConnection connect(final MessageMetadata messageMetadata, final DlmsDevice device, final DlmsMessageListener dlmsMessageListener) throws OsgpException {
// Make sure neither device or device.getIpAddress() is null.
this.checkDevice(device);
this.checkIpAddress(device);
try {
return this.createConnection(messageMetadata, device, dlmsMessageListener, this.secretManagementService::getKeys);
} catch (final UnknownHostException e) {
LOGGER.warn("The IP address is not found: {}", device.getIpAddress(), e);
// Unknown IP, unrecoverable.
throw new TechnicalException(ComponentType.PROTOCOL_DLMS, "The IP address is not found: " + device.getIpAddress());
} catch (final IOException e) {
throw new ConnectionException(e);
} catch (final EncrypterException e) {
LOGGER.error("decryption of security keys failed for device: {}", device.getDeviceIdentification(), e);
throw new TechnicalException(ComponentType.PROTOCOL_DLMS, "decryption of security keys failed for device: " + device.getDeviceIdentification());
}
}
Aggregations