diff --git a/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/S7Driver.java b/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/S7Driver.java index 307eb9adcbb..88a757d1ce2 100644 --- a/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/S7Driver.java +++ b/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/S7Driver.java @@ -30,6 +30,7 @@ import org.apache.plc4x.java.spi.configuration.Configuration; import org.apache.plc4x.java.spi.connection.ProtocolStackConfigurer; import org.apache.plc4x.java.spi.optimizer.BaseOptimizer; +import org.apache.plc4x.java.spi.transport.TransportConfigurationTypeProvider; import org.apache.plc4x.java.spi.values.PlcValueHandler; import java.util.function.Consumer; diff --git a/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/configuration/S7Configuration.java b/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/configuration/S7Configuration.java index a3e42c7cf02..d97ccd6bc88 100644 --- a/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/configuration/S7Configuration.java +++ b/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/configuration/S7Configuration.java @@ -23,7 +23,7 @@ import org.apache.plc4x.java.spi.configuration.annotations.defaults.BooleanDefaultValue; import org.apache.plc4x.java.spi.configuration.annotations.defaults.IntDefaultValue; -public class S7Configuration implements Configuration { +public class S7Configuration extends S7TcpTransportConfiguration { @ConfigurationParameter("local-rack") @IntDefaultValue(1) @@ -89,7 +89,6 @@ public class S7Configuration implements Configuration { @IntDefaultValue(0) public int retryTime = 0; - public int getLocalRack() { return localRack; } @@ -217,7 +216,7 @@ public int getRetryTime() { public void setRetryTime(int retryTime) { this.retryTime = retryTime; } - + @Override public String toString() { return "Configuration{" + @@ -229,6 +228,7 @@ public String toString() { ", remote-rack2=" + remoteRack2 + ", remote-slot2=" + remoteSlot2 + ", remote-tsap=" + remoteTsap + + ", port=" + getDefaultPort() + ", pduSize=" + pduSize + ", maxAmqCaller=" + maxAmqCaller + ", maxAmqCallee=" + maxAmqCallee + @@ -240,6 +240,6 @@ public String toString() { '\'' + '}'; } - + } diff --git a/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7HGeneratedDriverBase.java b/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7HGeneratedDriverBase.java index 66dbfb5ebab..66f47cda383 100644 --- a/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7HGeneratedDriverBase.java +++ b/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7HGeneratedDriverBase.java @@ -54,6 +54,7 @@ public class S7HGeneratedDriverBase extends GeneratedDriverBase impl @Override public PlcConnection getConnection(String connectionString) throws PlcConnectionException { + ConfigurationFactory configurationFactory = new ConfigurationFactory(); // Split up the connection string into it's individual segments. Matcher smatcher = URI_PATTERN.matcher(connectionString); Matcher hmatcher = URI_H_PATTERN.matcher(connectionString); @@ -79,7 +80,7 @@ public PlcConnection getConnection(String connectionString) throws PlcConnection } // Create the configuration object. - Configuration configuration = new ConfigurationFactory().createConfiguration( + Configuration configuration = configurationFactory.createConfiguration( getConfigurationType(), protocolCode, transportCode, transportConfig, paramString); if (configuration == null) { throw new PlcConnectionException("Unsupported configuration"); @@ -99,8 +100,24 @@ public PlcConnection getConnection(String connectionString) throws PlcConnection throw new PlcConnectionException("Unsupported transport " + transportCode); } - // Inject the configuration into the transport. - configure(configuration, transport); + // Find out the type of the transport configuration. + Class transportConfigurationType = transport.getTransportConfigType(); + if(this instanceof TransportConfigurationTypeProvider) { + TransportConfigurationTypeProvider transportConfigurationTypeProvider = + (TransportConfigurationTypeProvider) this; + Class driverTransportConfigurationType = + transportConfigurationTypeProvider.getTransportConfigurationType(transportCode); + if(driverTransportConfigurationType != null) { + transportConfigurationType = driverTransportConfigurationType; + } + } + // Use the transport configuration type to actually configure the transport instance. + if(transportConfigurationType != null) { + Configuration transportConfiguration = configurationFactory + .createPrefixedConfiguration(transportConfigurationType, + transportCode, protocolCode, transportCode, transportConfig, paramString); + configure(transportConfiguration, transport); + } // Create an instance of the communication channel which the driver should use. ChannelFactory channelFactory = transport.createChannelFactory(transportConfig); diff --git a/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7HPlcConnection.java b/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7HPlcConnection.java index 91f96e517ae..17d20ec4f8e 100644 --- a/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7HPlcConnection.java +++ b/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7HPlcConnection.java @@ -72,12 +72,12 @@ public class S7HPlcConnection extends DefaultNettyPlcConnection implements Runna .build()); protected final ChannelFactory secondaryChannelFactory; - protected Channel primary_channel = null; - protected Channel secondary_channel = null; + protected Channel primaryChannel = null; + protected Channel secondaryChannel = null; protected final MessageToMessageCodec s7hmux; - protected int slice_ping = 0; - protected int slice_retry_time = 0; + protected int slicePing = 0; + protected int sliceRetryTime = 0; public S7HPlcConnection( boolean canPing, @@ -164,9 +164,9 @@ public void connect() throws PlcConnectionException { } //If it is not possible to generate a TCP connection. - //Safety shutdownn all executors in the channels. - if (primary_channel == null) { - if (secondary_channel == null) { + //Safety shutdown all executors in the channels. + if (primaryChannel == null) { + if (secondaryChannel == null) { sendChannelDisconectEvent(); throw new PlcConnectionException("Connection is not possible."); } @@ -217,12 +217,12 @@ public void close() throws PlcConnectionException { } catch (Exception ex) { logger.info(ex.toString()); } - if (primary_channel != null) { - if (primary_channel.isActive()) { + if (primaryChannel != null) { + if (primaryChannel.isActive()) { try { - primary_channel.pipeline().remove(MULTIPLEXER); - primary_channel.pipeline().fireUserEventTriggered(new CloseConnectionEvent()); - primary_channel.eventLoop().shutdownGracefully(); + primaryChannel.pipeline().remove(MULTIPLEXER); + primaryChannel.pipeline().fireUserEventTriggered(new CloseConnectionEvent()); + primaryChannel.eventLoop().shutdownGracefully(); } catch (Exception ex) { logger.info(ex.toString()); @@ -230,11 +230,11 @@ public void close() throws PlcConnectionException { } } - if (secondary_channel != null) { - if (secondary_channel.isActive()) { - secondary_channel.pipeline().remove(MULTIPLEXER); - secondary_channel.pipeline().fireUserEventTriggered(new CloseConnectionEvent()); - secondary_channel.eventLoop().shutdownGracefully(); + if (secondaryChannel != null) { + if (secondaryChannel.isActive()) { + secondaryChannel.pipeline().remove(MULTIPLEXER); + secondaryChannel.pipeline().fireUserEventTriggered(new CloseConnectionEvent()); + secondaryChannel.eventLoop().shutdownGracefully(); } } @@ -252,30 +252,30 @@ public boolean isConnected() { public void doPrimaryTcpConnections() { try { - primary_channel = channelFactory.createChannel(new LoggingHandler(LogLevel.TRACE)); + primaryChannel = channelFactory.createChannel(new LoggingHandler(LogLevel.TRACE)); } catch (Exception ex) { - primary_channel = null; + primaryChannel = null; logger.info(ex.toString()); } - if (primary_channel != null) { - if (primary_channel.isActive()) { - primary_channel.pipeline().addFirst(MULTIPLEXER, s7hmux); - ((S7HMux) s7hmux).setPrimaryChannel(primary_channel); + if (primaryChannel != null) { + if (primaryChannel.isActive()) { + primaryChannel.pipeline().addFirst(MULTIPLEXER, s7hmux); + ((S7HMux) s7hmux).setPrimaryChannel(primaryChannel); } } } public void doSecondaryTcpConnections() { try { - secondary_channel = secondaryChannelFactory.createChannel(new LoggingHandler(LogLevel.TRACE)); + secondaryChannel = secondaryChannelFactory.createChannel(new LoggingHandler(LogLevel.TRACE)); } catch (Exception ex) { - secondary_channel = null; + secondaryChannel = null; logger.info(ex.toString()); } - if (secondary_channel != null) { - if (secondary_channel.isActive()) { - secondary_channel.pipeline().addFirst(MULTIPLEXER, s7hmux); - ((S7HMux) s7hmux).setSecondaryChannel(secondary_channel); + if (secondaryChannel != null) { + if (secondaryChannel.isActive()) { + secondaryChannel.pipeline().addFirst(MULTIPLEXER, s7hmux); + ((S7HMux) s7hmux).setSecondaryChannel(secondaryChannel); } } } @@ -317,13 +317,13 @@ public void run() { * the TCP channel active, for very long sampling periods. */ if (channel.attr(S7HMuxImpl.IS_PING_ACTIVE).get()) { - if (slice_ping >= channel.attr(S7HMuxImpl.PING_TIME).get()) { + if (slicePing >= channel.attr(S7HMuxImpl.PING_TIME).get()) { ping(); - slice_ping = 0; + slicePing = 0; } - slice_ping++; + slicePing++; } else { - slice_ping = 0; + slicePing = 0; } /* @@ -333,19 +333,19 @@ public void run() { * and if the other is down, the connection must be lifted. * S7HMuxImpl, includes the switching logic between the TCP links. */ - if (slice_retry_time >= channel.attr(S7HMuxImpl.RETRY_TIME).get()) { + if (sliceRetryTime >= channel.attr(S7HMuxImpl.RETRY_TIME).get()) { - if (primary_channel != null) { - if (!primary_channel.isActive()) { + if (primaryChannel != null) { + if (!primaryChannel.isActive()) { logger.info("Creating prymary connection."); - primary_channel.eventLoop().shutdownGracefully(); + primaryChannel.eventLoop().shutdownGracefully(); doPrimaryTcpConnections(); - } else if (null == secondary_channel) { + } else if (null == secondaryChannel) { if (channel.attr(S7HMuxImpl.WAS_CONNECTED).get() && !channel.attr(S7HMuxImpl.IS_CONNECTED).get()) { logger.info("Reconnecting primary channel."); if (null != ((S7HMux) s7hmux).getTCPChannel()) { - ((S7HMux) s7hmux).setPrimaryChannel(primary_channel); + ((S7HMux) s7hmux).setPrimaryChannel(primaryChannel); } } } @@ -354,17 +354,17 @@ public void run() { doPrimaryTcpConnections(); } - if (secondary_channel != null) { - if (!secondary_channel.isActive()) { + if (secondaryChannel != null) { + if (!secondaryChannel.isActive()) { logger.info("Creating secondary connection."); - secondary_channel.eventLoop().shutdownGracefully(); + secondaryChannel.eventLoop().shutdownGracefully(); doSecondaryTcpConnections(); - } else if (null == primary_channel) { + } else if (null == primaryChannel) { if ((channel.attr(S7HMuxImpl.WAS_CONNECTED).get()) && (!channel.attr(S7HMuxImpl.IS_CONNECTED).get())) { logger.info("Reconnecting secondary channel."); if (null != ((S7HMux) s7hmux).getTCPChannel()) { - ((S7HMux) s7hmux).setSecondaryChannel(secondary_channel); + ((S7HMux) s7hmux).setSecondaryChannel(secondaryChannel); } } } @@ -374,22 +374,22 @@ public void run() { doSecondaryTcpConnections(); } } - slice_retry_time = 0; + sliceRetryTime = 0; } if (channel.attr(S7HMuxImpl.RETRY_TIME).get() > 0) { - slice_retry_time++; + sliceRetryTime++; } connected = channel.attr(S7HMuxImpl.IS_CONNECTED).get(); logger.trace("*************************************************\r\n" - + "INSTAMCIA PRIMARIO : " + ((null == primary_channel) ? "NULL" : primary_channel.toString()) + "\r\n" - + "ACTIVO PRIMARIO : " + ((null == primary_channel) ? "NULL" : primary_channel.isActive()) + "\r\n" - + "INSTAMCIA SECUNDARIO : " + ((null == secondary_channel) ? "NULL" : secondary_channel.toString()) + "\r\n" - + "ACTIVO SECUNDARIO : " + ((null == secondary_channel) ? "NULL" : secondary_channel.isActive()) + "\r\n" + + "INSTAMCIA PRIMARIO : " + ((null == primaryChannel) ? "NULL" : primaryChannel.toString()) + "\r\n" + + "ACTIVO PRIMARIO : " + ((null == primaryChannel) ? "NULL" : primaryChannel.isActive()) + "\r\n" + + "INSTAMCIA SECUNDARIO : " + ((null == secondaryChannel) ? "NULL" : secondaryChannel.toString()) + "\r\n" + + "ACTIVO SECUNDARIO : " + ((null == secondaryChannel) ? "NULL" : secondaryChannel.isActive()) + "\r\n" + "CANAL CONECTADO? : " + channel.attr(S7HMuxImpl.IS_CONNECTED).get() + "\r\n" + "CANAL ESTUVO CONECTADO? : " + channel.attr(S7HMuxImpl.WAS_CONNECTED).get() + "\r\n" - + "CONTADORES : " + slice_ping + " : " + slice_retry_time + "\r\n" + + "CONTADORES : " + slicePing + " : " + sliceRetryTime + "\r\n" + "*************************************************"); } diff --git a/plc4j/examples/plc4j-s7event/src/main/java/org/apache/plc4x/examples/plc4j/s7event/PlcReadDataS7400H.java b/plc4j/examples/plc4j-s7event/src/main/java/org/apache/plc4x/examples/plc4j/s7event/PlcReadDataS7400H.java index 49cf838d0ef..604ab800456 100644 --- a/plc4j/examples/plc4j-s7event/src/main/java/org/apache/plc4x/examples/plc4j/s7event/PlcReadDataS7400H.java +++ b/plc4j/examples/plc4j-s7event/src/main/java/org/apache/plc4x/examples/plc4j/s7event/PlcReadDataS7400H.java @@ -77,7 +77,7 @@ public void run() throws IOException { OpenConnection("s7://10.10.1.80/10.10.1.81?remote-rack=0&" + "remote-slot=3&remote-rack2=0&remote-slot=4&" - + "controller-type=S7_400&read-timeout=8&" + + "controller-type=S7_400&read-timeout=8&" + "ping=true&ping-time=2&retry-time=3"); //(01) logger.info("*****************************************************");