Skip to content

Commit

Permalink
Add new connection status and listerner to controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian S committed Jan 1, 2020
1 parent a01844e commit bcabce4
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 17 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<description>eBUS core library - This library handles the communication with heating engineering via the BUS specification. This protocol is used by many heating manufacturers in Europe.</description>
<groupId>de.cs-dev.ebus</groupId>
<artifactId>ebus-core</artifactId>
<version>1.0.0-SNAPSHOT</version>
<version>1.0.1-SNAPSHOT</version>
<url>https://github.com/csowada/ebus</url>
<packaging>bundle</packaging>

Expand Down Expand Up @@ -218,7 +218,7 @@
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
<version>1.7.25</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
*/
package de.csdev.ebus.core;

import de.csdev.ebus.core.IEBusController.ConnectionStatus;

/**
* @author Christian Sowada - Initial contribution
*
Expand Down Expand Up @@ -44,4 +46,9 @@ public void onTelegramException(EBusDataException exception, Integer sendQueueId
public void onConnectionException(Exception e) {
// noop
}

@Override
public void onConnectionStatusChanged(ConnectionStatus status) {
// noop
}
}
47 changes: 41 additions & 6 deletions src/main/java/de/csdev/ebus/core/EBusControllerBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ public abstract class EBusControllerBase extends Thread implements IEBusControll

protected EBusQueue queue = new EBusQueue();

private ConnectionStatus connectionStatus = ConnectionStatus.DISCONNECTED;

/*
* (non-Javadoc)
*
*
* @see de.csdev.ebus.core.IEBusController#addToSendQueue(byte[], int)
*/
@Override
Expand All @@ -59,7 +61,7 @@ public Integer addToSendQueue(byte[] buffer, int maxAttemps) throws EBusControll

/*
* (non-Javadoc)
*
*
* @see de.csdev.ebus.core.IEBusController#addToSendQueue(byte[])
*/
@Override
Expand All @@ -72,7 +74,7 @@ public Integer addToSendQueue(byte[] buffer) throws EBusControllerException {

/*
* (non-Javadoc)
*
*
* @see de.csdev.ebus.core.IEBusController#addEBusEventListener(de.csdev.ebus.core.IEBusConnectorEventListener)
*/
@Override
Expand All @@ -82,7 +84,7 @@ public void addEBusEventListener(IEBusConnectorEventListener listener) {

/*
* (non-Javadoc)
*
*
* @see de.csdev.ebus.core.IEBusController#removeEBusEventListener(de.csdev.ebus.core.IEBusConnectorEventListener)
*/
@Override
Expand Down Expand Up @@ -173,6 +175,30 @@ public void run() {
});
}

/**
* @param status
*/
protected void fireOnEBusConnectionStatusChange(ConnectionStatus status) {

if (threadPool == null || threadPool.isTerminated()) {
logger.warn("ThreadPool not ready!");
return;
}

threadPool.execute(new Runnable() {
@Override
public void run() {
for (IEBusConnectorEventListener listener : listeners) {
try {
listener.onConnectionStatusChanged(status);
} catch (Exception e) {
logger.error("Error while firing fireOnEBusConnectionStatusChange events!", e);
}
}
}
});
}

/**
*
*/
Expand Down Expand Up @@ -210,7 +236,7 @@ protected void shutdownThreadPool() {

/*
* (non-Javadoc)
*
*
* @see de.csdev.ebus.core.IEBusController#isRunning()
*/
@Override
Expand Down Expand Up @@ -265,7 +291,7 @@ public void run() {

/*
* (non-Javadoc)
*
*
* @see de.csdev.ebus.core.IEBusController#setWatchdogTimerTimeout(int)
*/
@Override
Expand All @@ -283,4 +309,13 @@ protected void fireWatchDogTimer() {
// }
}

protected void setConnectionStatus(ConnectionStatus status) {
this.connectionStatus = status;
fireOnEBusConnectionStatusChange(status);
}

@Override
public ConnectionStatus getConnectionStatus() {
return this.connectionStatus;
}
}
10 changes: 10 additions & 0 deletions src/main/java/de/csdev/ebus/core/EBusEbusdController.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public class EBusEbusdController extends EBusControllerBase {

private Thread senderThread = null;

// private ConnectionStatus connectionStatus = ConnectionStatus.DISCONNECTED;

public EBusEbusdController(String hostname, int port) {
this.hostname = hostname;
this.port = port;
Expand Down Expand Up @@ -136,6 +138,8 @@ private ByteBuffer parseLine(String readLine) throws IOException {

directMode = true;

setConnectionStatus(ConnectionStatus.CONNECTED);

} else if (!directMode) {
logger.info("Switch ebusd to direct mode ...");
// switch to direct mode
Expand Down Expand Up @@ -300,6 +304,9 @@ private void reconnect() throws IOException {

logger.info("Try to reconnect to ebusd daemon ...");

// set connection status to connecting
setConnectionStatus(ConnectionStatus.CONNECTING);

if (reConnectCounter > 10) {
reConnectCounter = -1;
this.interrupt();
Expand Down Expand Up @@ -339,6 +346,9 @@ private void disconnect() {
socket = null;

directMode = false;

// set connection status to disconnected
setConnectionStatus(ConnectionStatus.DISCONNECTED);
}

private boolean connect() throws UnknownHostException, IOException {
Expand Down
18 changes: 17 additions & 1 deletion src/main/java/de/csdev/ebus/core/EBusLowLevelController.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ private void reconnect() throws IOException {

logger.info("Try to reconnect to eBUS adapter ...");

// set connection status to connecting
setConnectionStatus(ConnectionStatus.CONNECTING);

if (reConnectCounter > 10) {
reConnectCounter = -1;
this.interrupt();
Expand Down Expand Up @@ -166,6 +169,9 @@ public void run() {

try {
if (!connection.isOpen()) {

setConnectionStatus(ConnectionStatus.CONNECTING);

connection.open();
}
} catch (IOException e) {
Expand All @@ -184,6 +190,9 @@ public void run() {

} else {

// the connection is now connected
setConnectionStatus(ConnectionStatus.CONNECTED);

// read byte from connector
read = connection.readBytes(buffer);

Expand All @@ -200,7 +209,6 @@ public void run() {
// reset with received data
resetWatchdogTimer();
reConnectCounter = 0;

}
}

Expand Down Expand Up @@ -419,6 +427,9 @@ protected void dispose() {
} catch (IOException e) {
logger.error(e.toString(), e);
}

// set connection status to disconnected
setConnectionStatus(ConnectionStatus.DISCONNECTED);
}

@Override
Expand All @@ -432,4 +443,9 @@ protected void fireWatchDogTimer() {
}
}

// @Override
// public ConnectionStatus getConnectionStatus() {
// return this.connectionStatus;
// }

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
*/
package de.csdev.ebus.core;

import de.csdev.ebus.core.IEBusController.ConnectionStatus;

/**
* This listener is called if the connector received a valid eBUS telegram.
*
Expand Down Expand Up @@ -38,4 +40,11 @@ public interface IEBusConnectorEventListener {
* @param e
*/
public void onConnectionException(Exception e);

/**
* The connection status has changed
*
* @param status ConnectionStatus.CONNECTING, ConnectionStatus.CONNECTED or ConnectionStatus.DISCONNECTED
*/
public void onConnectionStatusChanged(ConnectionStatus status);
}
8 changes: 8 additions & 0 deletions src/main/java/de/csdev/ebus/core/IEBusController.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,12 @@ public interface IEBusController {
boolean isInterrupted();

void interrupt();

enum ConnectionStatus {
DISCONNECTED,
CONNECTING,
CONNECTED
}

ConnectionStatus getConnectionStatus();
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
import java.util.Map;

import de.csdev.ebus.command.IEBusCommandMethod;
import de.csdev.ebus.core.EBusConnectorEventListener;
import de.csdev.ebus.core.EBusDataException;
import de.csdev.ebus.core.EBusDataException.EBusError;
import de.csdev.ebus.core.IEBusConnectorEventListener;
import de.csdev.ebus.service.parser.IEBusParserListener;

/**
* @author Christian Sowada - Initial contribution
*
*/
public class EBusMetricsService implements IEBusParserListener, IEBusConnectorEventListener {
public class EBusMetricsService extends EBusConnectorEventListener implements IEBusParserListener {

private static final BigDecimal HUNDRED = BigDecimal.valueOf(100);

Expand Down
9 changes: 8 additions & 1 deletion src/test/java/de/csdev/ebus/wip/ClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
import de.csdev.ebus.command.EBusCommandRegistry;
import de.csdev.ebus.command.IEBusCommandMethod;
import de.csdev.ebus.command.datatypes.EBusTypeException;
import de.csdev.ebus.core.EBusLowLevelController;
import de.csdev.ebus.core.EBusDataException;
import de.csdev.ebus.core.EBusLowLevelController;
import de.csdev.ebus.core.EBusStateMachineTest;
import de.csdev.ebus.core.IEBusConnectorEventListener;
import de.csdev.ebus.core.IEBusController.ConnectionStatus;
import de.csdev.ebus.core.connection.EBusEmulatorConnection;
import de.csdev.ebus.service.parser.IEBusParserListener;
import de.csdev.ebus.utils.EBusUtils;
Expand Down Expand Up @@ -74,6 +75,12 @@ public void onConnectionException(Exception e) {
logger.error("error!", e);
fail("No ConnectionException expected!");
}

@Override
public void onConnectionStatusChanged(ConnectionStatus status) {
// logger.error("error!", e);
fail("No ConnectionException expected!");
}
});

client.getResolverService().addEBusParserListener(new IEBusParserListener() {
Expand Down
11 changes: 8 additions & 3 deletions src/test/java/de/csdev/ebus/wip/ClientTest2.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@
import de.csdev.ebus.command.EBusCommandRegistry;
import de.csdev.ebus.command.IEBusCommandMethod;
import de.csdev.ebus.command.datatypes.EBusTypeException;
import de.csdev.ebus.core.EBusLowLevelController;
import de.csdev.ebus.core.EBusControllerException;
import de.csdev.ebus.core.EBusDataException;
import de.csdev.ebus.core.EBusLowLevelController;
import de.csdev.ebus.core.EBusStateMachineTest;
import de.csdev.ebus.core.IEBusConnectorEventListener;
import de.csdev.ebus.core.IEBusController.ConnectionStatus;
import de.csdev.ebus.core.connection.EBusEmulatorConnection;
import de.csdev.ebus.service.parser.IEBusParserListener;
import de.csdev.ebus.utils.EBusUtils;
Expand Down Expand Up @@ -56,7 +57,6 @@ public void testNoSlaveResponse()

@Override
public void onTelegramReceived(byte[] receivedData, Integer sendQueueId) {
// TODO Auto-generated method stub
logger.error("ClientTest.xxx().new EBusConnectorEventListener() {...}.onTelegramReceived()");
}

Expand All @@ -69,9 +69,14 @@ public void onTelegramException(EBusDataException exception, Integer sendQueueId

@Override
public void onConnectionException(Exception e) {
// TODO Auto-generated method stub
logger.error("ClientTest.xxx().new EBusConnectorEventListener() {...}.onConnectionException()");
}

@Override
public void onConnectionStatusChanged(ConnectionStatus status) {
logger.error(
"ClientTest2.testNoSlaveResponse().new IEBusConnectorEventListener() {...}.onConnectionStatusChanged()");
}
});

client.getResolverService().addEBusParserListener(new IEBusParserListener() {
Expand Down
10 changes: 8 additions & 2 deletions src/test/java/de/csdev/ebus/wip/ClientTest3.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@
import de.csdev.ebus.client.EBusClient;
import de.csdev.ebus.command.EBusCommandRegistry;
import de.csdev.ebus.command.datatypes.EBusTypeException;
import de.csdev.ebus.core.EBusLowLevelController;
import de.csdev.ebus.core.EBusControllerException;
import de.csdev.ebus.core.EBusDataException;
import de.csdev.ebus.core.EBusLowLevelController;
import de.csdev.ebus.core.EBusStateMachineTest;
import de.csdev.ebus.core.IEBusConnectorEventListener;
import de.csdev.ebus.core.IEBusController.ConnectionStatus;
import de.csdev.ebus.core.connection.EBusTCPConnection;
import de.csdev.ebus.core.connection.IEBusConnection;
import de.csdev.ebus.utils.EBusUtils;
Expand Down Expand Up @@ -71,9 +72,14 @@ public void onTelegramException(EBusDataException exception, Integer sendQueueId

@Override
public void onConnectionException(Exception e) {
// TODO Auto-generated method stub
logger.info("ClientTest.xxx().new EBusConnectorEventListener() {...}.onConnectionException()");
}

@Override
public void onConnectionStatusChanged(ConnectionStatus status) {
logger.info(
"ClientTest3.testNoSlaveResponse().new IEBusConnectorEventListener() {...}.onConnectionStatusChanged()");
}
});

// client.getResolverService().addEBusParserListener(new IEBusParserListener() {
Expand Down

0 comments on commit bcabce4

Please sign in to comment.