Skip to content

Commit

Permalink
Issue/s7h v2 (#1219)
Browse files Browse the repository at this point in the history
* Adjusts your own TCP channel configuration. PLC clock reading and adjustment validated.

* fix: Fixed the problem with using the default port.

---------

Co-authored-by: Cesar Garcia <[email protected]>
Co-authored-by: Christofer Dutz <[email protected]>
  • Loading branch information
3 people authored Nov 23, 2023
1 parent d023f06 commit 0981362
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -89,7 +89,6 @@ public class S7Configuration implements Configuration {
@IntDefaultValue(0)
public int retryTime = 0;


public int getLocalRack() {
return localRack;
}
Expand Down Expand Up @@ -217,7 +216,7 @@ public int getRetryTime() {
public void setRetryTime(int retryTime) {
this.retryTime = retryTime;
}

@Override
public String toString() {
return "Configuration{" +
Expand All @@ -229,6 +228,7 @@ public String toString() {
", remote-rack2=" + remoteRack2 +
", remote-slot2=" + remoteSlot2 +
", remote-tsap=" + remoteTsap +
", port=" + getDefaultPort() +
", pduSize=" + pduSize +
", maxAmqCaller=" + maxAmqCaller +
", maxAmqCallee=" + maxAmqCallee +
Expand All @@ -240,6 +240,6 @@ public String toString() {
'\'' +
'}';
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public class S7HGeneratedDriverBase extends GeneratedDriverBase<TPKTPacket> 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);
Expand All @@ -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");
Expand All @@ -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<? extends TransportConfiguration> transportConfigurationType = transport.getTransportConfigType();
if(this instanceof TransportConfigurationTypeProvider) {
TransportConfigurationTypeProvider transportConfigurationTypeProvider =
(TransportConfigurationTypeProvider) this;
Class<? extends TransportConfiguration> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ByteBuf, ByteBuf> s7hmux;

protected int slice_ping = 0;
protected int slice_retry_time = 0;
protected int slicePing = 0;
protected int sliceRetryTime = 0;

public S7HPlcConnection(
boolean canPing,
Expand Down Expand Up @@ -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.");
}
Expand Down Expand Up @@ -217,24 +217,24 @@ 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());
}
}
}

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();
}
}

Expand All @@ -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);
}
}
}
Expand Down Expand Up @@ -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;
}

/*
Expand All @@ -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);
}
}
}
Expand All @@ -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);
}
}
}
Expand All @@ -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"
+ "*************************************************");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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("*****************************************************");
Expand Down

0 comments on commit 0981362

Please sign in to comment.