use of org.opensmartgridplatform.adapter.protocol.iec60870.domain.services.ClientConnection in project open-smart-grid-platform by OSGP.
the class Iec60870Client method connect.
@Override
public ClientConnection connect(final ConnectionParameters connectionParameters, final ConnectionEventListener asduListener) throws ConnectionFailureException {
final InetAddress address = this.convertIpAddress(connectionParameters.getIpAddress());
final String deviceIdentification = connectionParameters.getDeviceIdentification();
final int port = connectionParameters.getPort() == null ? IEC60870_DEFAULT_PORT : connectionParameters.getPort();
final ClientConnectionBuilder clientConnectionBuilder = new ClientConnectionBuilder(address).setPort(port).setConnectionTimeout(this.connectionTimeout);
try {
LOGGER.info("Connecting to device: {}...", deviceIdentification);
final Connection connection = clientConnectionBuilder.build();
connection.startDataTransfer(asduListener);
LOGGER.info("Connected to device: {}", deviceIdentification);
return new DeviceConnection(connection, connectionParameters);
} catch (final IOException e) {
final String errorMessage = "Unable to connect to remote host: " + connectionParameters.getIpAddress();
LOGGER.error(errorMessage, e);
throw new ConnectionFailureException(ComponentType.PROTOCOL_IEC60870, errorMessage);
}
}
use of org.opensmartgridplatform.adapter.protocol.iec60870.domain.services.ClientConnection in project open-smart-grid-platform by OSGP.
the class AbstractMessageProcessor method processMessage.
@Override
public void processMessage(final ObjectMessage message) {
LOGGER.info("Processing message.");
MessageMetadata messageMetadata = null;
try {
messageMetadata = MessageMetadata.fromMessage(message);
final RequestMetadata requestMetadata = RequestMetadata.newBuilder().messageMetadata(messageMetadata).build();
final ClientConnection deviceConnection = this.iec60870DeviceConnectionService.getConnection(requestMetadata);
this.process(deviceConnection, requestMetadata);
} catch (final ProtocolAdapterException e) {
this.handleError(messageMetadata, e);
} catch (final Exception e) {
LOGGER.error("UNRECOVERABLE ERROR, unable to read ObjectMessage instance, giving up.", e);
}
}
use of org.opensmartgridplatform.adapter.protocol.iec60870.domain.services.ClientConnection in project open-smart-grid-platform by OSGP.
the class GeneralInterrogationService method sendGeneralInterrogation.
public void sendGeneralInterrogation(final ClientConnection deviceConnection, final RequestMetadata requestMetadata) throws IOException {
final String connectedDevice = deviceConnection.getConnectionParameters().getDeviceIdentification();
final int commonAddress = deviceConnection.getConnectionParameters().getCommonAddress();
deviceConnection.getConnection().interrogation(commonAddress, CauseOfTransmission.ACTIVATION, new IeQualifierOfInterrogation(QUALIFIER_OF_INTERROGATION_ID));
// interrogation command creates this asdu internally, however we
// need it here as well for logging...
final ASdu asdu = new ASdu(ASduType.C_IC_NA_1, false, CauseOfTransmission.ACTIVATION, false, false, ORIGINATOR_ADDRESS, commonAddress, new InformationObject(0, new IeQualifierOfInterrogation(QUALIFIER_OF_INTERROGATION_ID)));
final LogItem logItem = new LogItem(connectedDevice, requestMetadata.getOrganisationIdentification(), false, asdu.toString());
this.loggingService.log(logItem);
}
use of org.opensmartgridplatform.adapter.protocol.iec60870.domain.services.ClientConnection in project open-smart-grid-platform by OSGP.
the class ClientConnectionFactory method forDevice.
public static ClientConnection forDevice(final String deviceIdentification) {
final Connection connection = Mockito.mock(Connection.class);
final ConnectionParameters connectionParameters = new ConnectionParameters.Builder().deviceIdentification(deviceIdentification).build();
return new DeviceConnection(connection, connectionParameters);
}
use of org.opensmartgridplatform.adapter.protocol.iec60870.domain.services.ClientConnection in project open-smart-grid-platform by OSGP.
the class ClientConnectionServiceTest method testGetConnectionShouldReturnNewConnectionWhenNotInCache.
@Test
void testGetConnectionShouldReturnNewConnectionWhenNotInCache() throws Exception {
// Arrange
final String deviceIdentification = "DA_DVC_1";
final Iec60870Device device = Iec60870DeviceFactory.createDistributionAutomationDevice(deviceIdentification);
when(this.iec60870DeviceRepository.findByDeviceIdentification(deviceIdentification)).thenReturn(Optional.of(device));
final RequestMetadata requestMetadata = RequestMetadataFactory.forDevice(deviceIdentification);
final ClientConnection expectedConnection = ClientConnectionFactory.forDevice(deviceIdentification);
when(this.iec60870Client.connect(eq(expectedConnection.getConnectionParameters()), any(ConnectionEventListener.class))).thenReturn(expectedConnection);
// Act
final ClientConnection actualConnection = this.clientConnectionService.getConnection(requestMetadata);
// Assert
assertThat(actualConnection).isEqualTo(expectedConnection);
}
Aggregations