[Major] Renamed PlcConnection.isConnectAtStartup() to isAutoConnect(), and use respectively

This commit is contained in:
Robert von Burg 2020-02-26 20:02:52 +01:00
parent 6f5fec4478
commit 7cd97f4c4c
5 changed files with 16 additions and 13 deletions

View File

@ -5,7 +5,6 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import li.strolch.plc.model.ConnectionState;
import li.strolch.plc.model.PlcAddress;
import li.strolch.plc.model.PlcAddressType;
import li.strolch.utils.ExecutorPool;
@ -106,15 +105,17 @@ public class DefaultPlc implements Plc {
private PlcConnection validateConnection(PlcAddress plcAddress) {
PlcConnection connection = getConnection(plcAddress);
if (connection.getState() == ConnectionState.Connected)
if (!connection.isAutoConnect() || connection.isConnected())
return connection;
connection.connect();
if (connection.getState() == ConnectionState.Connected)
if (connection.isConnected())
return connection;
throw new IllegalStateException("PlcConnection " + connection.getId() + " is disconnected for " + plcAddress);
throw new IllegalStateException(
"PlcConnection " + connection.getId() + " could not connect to " + plcAddress + " due to " + connection
.getStateMsg());
}
@Override
@ -133,7 +134,7 @@ public class DefaultPlc implements Plc {
@Override
public void start() {
this.executorPool = new ExecutorPool();
this.connections.values().stream().filter(PlcConnection::isConnectAtStartup).forEach(PlcConnection::connect);
this.connections.values().stream().filter(PlcConnection::isAutoConnect).forEach(PlcConnection::connect);
}
@Override

View File

@ -49,7 +49,7 @@ public abstract class PlcConnection {
throw new IllegalStateException("PlcConnection " + this.id + " is not yet connected!");
}
public boolean isConnectAtStartup() {
public boolean isAutoConnect() {
return true;
}

View File

@ -2,6 +2,7 @@ package li.strolch.plc.core.hw.connections;
import static java.util.Collections.singletonList;
import static li.strolch.utils.helper.ExceptionHelper.getExceptionMessageWithCauses;
import static li.strolch.utils.helper.StringHelper.isEmpty;
import java.io.IOException;
import java.io.InputStream;
@ -16,7 +17,6 @@ import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import li.strolch.plc.core.hw.Plc;
import li.strolch.plc.model.ConnectionState;
import li.strolch.utils.helper.AsciiHelper;
public class DataLogicScannerConnection extends SimplePlcConnection {
@ -39,7 +39,7 @@ public class DataLogicScannerConnection extends SimplePlcConnection {
}
@Override
public boolean isConnectAtStartup() {
public boolean isAutoConnect() {
return false;
}
@ -57,7 +57,7 @@ public class DataLogicScannerConnection extends SimplePlcConnection {
@Override
public boolean connect() {
if (getState() == ConnectionState.Connected)
if (isConnected())
return true;
try {
@ -190,6 +190,9 @@ public class DataLogicScannerConnection extends SimplePlcConnection {
public void send(String address, Object value) {
String command = (String) value;
if (isEmpty(command))
throw new IllegalArgumentException(
"PlcAddress " + address + " command empty. Must be one of " + CMD_START + " or " + CMD_STOP);
try {
switch (command) {
@ -203,7 +206,7 @@ public class DataLogicScannerConnection extends SimplePlcConnection {
break;
case CMD_STOP:
if (getState() == ConnectionState.Connected) {
if (isConnected()) {
sendStopTrigger();
disconnect();
}

View File

@ -18,7 +18,6 @@ import com.pi4j.io.i2c.I2CFactory;
import li.strolch.plc.core.hw.Plc;
import li.strolch.plc.core.hw.connections.SimplePlcConnection;
import li.strolch.plc.core.hw.gpio.PlcGpioController;
import li.strolch.plc.model.ConnectionState;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -87,7 +86,7 @@ public class PCF8574InputConnection extends SimplePlcConnection {
@Override
public boolean connect() {
if (this.connectionState == ConnectionState.Connected) {
if (isConnected()) {
logger.warn(this.id + ": Already connected");
return true;
}

View File

@ -63,7 +63,7 @@ public class PCF8574OutputConnection extends SimplePlcConnection {
@Override
public boolean connect() {
if (this.connectionState == ConnectionState.Connected) {
if (isConnected()) {
logger.warn(this.id + ": Already connected");
return true;
}