[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.Map;
import java.util.Set; import java.util.Set;
import li.strolch.plc.model.ConnectionState;
import li.strolch.plc.model.PlcAddress; import li.strolch.plc.model.PlcAddress;
import li.strolch.plc.model.PlcAddressType; import li.strolch.plc.model.PlcAddressType;
import li.strolch.utils.ExecutorPool; import li.strolch.utils.ExecutorPool;
@ -106,15 +105,17 @@ public class DefaultPlc implements Plc {
private PlcConnection validateConnection(PlcAddress plcAddress) { private PlcConnection validateConnection(PlcAddress plcAddress) {
PlcConnection connection = getConnection(plcAddress); PlcConnection connection = getConnection(plcAddress);
if (connection.getState() == ConnectionState.Connected) if (!connection.isAutoConnect() || connection.isConnected())
return connection; return connection;
connection.connect(); connection.connect();
if (connection.getState() == ConnectionState.Connected) if (connection.isConnected())
return connection; 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 @Override
@ -133,7 +134,7 @@ public class DefaultPlc implements Plc {
@Override @Override
public void start() { public void start() {
this.executorPool = new ExecutorPool(); 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 @Override

View File

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

View File

@ -2,6 +2,7 @@ package li.strolch.plc.core.hw.connections;
import static java.util.Collections.singletonList; import static java.util.Collections.singletonList;
import static li.strolch.utils.helper.ExceptionHelper.getExceptionMessageWithCauses; import static li.strolch.utils.helper.ExceptionHelper.getExceptionMessageWithCauses;
import static li.strolch.utils.helper.StringHelper.isEmpty;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -16,7 +17,6 @@ import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import li.strolch.plc.core.hw.Plc; import li.strolch.plc.core.hw.Plc;
import li.strolch.plc.model.ConnectionState;
import li.strolch.utils.helper.AsciiHelper; import li.strolch.utils.helper.AsciiHelper;
public class DataLogicScannerConnection extends SimplePlcConnection { public class DataLogicScannerConnection extends SimplePlcConnection {
@ -39,7 +39,7 @@ public class DataLogicScannerConnection extends SimplePlcConnection {
} }
@Override @Override
public boolean isConnectAtStartup() { public boolean isAutoConnect() {
return false; return false;
} }
@ -57,7 +57,7 @@ public class DataLogicScannerConnection extends SimplePlcConnection {
@Override @Override
public boolean connect() { public boolean connect() {
if (getState() == ConnectionState.Connected) if (isConnected())
return true; return true;
try { try {
@ -190,6 +190,9 @@ public class DataLogicScannerConnection extends SimplePlcConnection {
public void send(String address, Object value) { public void send(String address, Object value) {
String command = (String) 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 { try {
switch (command) { switch (command) {
@ -203,7 +206,7 @@ public class DataLogicScannerConnection extends SimplePlcConnection {
break; break;
case CMD_STOP: case CMD_STOP:
if (getState() == ConnectionState.Connected) { if (isConnected()) {
sendStopTrigger(); sendStopTrigger();
disconnect(); 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.Plc;
import li.strolch.plc.core.hw.connections.SimplePlcConnection; import li.strolch.plc.core.hw.connections.SimplePlcConnection;
import li.strolch.plc.core.hw.gpio.PlcGpioController; import li.strolch.plc.core.hw.gpio.PlcGpioController;
import li.strolch.plc.model.ConnectionState;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -87,7 +86,7 @@ public class PCF8574InputConnection extends SimplePlcConnection {
@Override @Override
public boolean connect() { public boolean connect() {
if (this.connectionState == ConnectionState.Connected) { if (isConnected()) {
logger.warn(this.id + ": Already connected"); logger.warn(this.id + ": Already connected");
return true; return true;
} }

View File

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