Skip to content

Commit

Permalink
new feature and refactoring testing openhab#1
Browse files Browse the repository at this point in the history
  • Loading branch information
acioni committed Sep 18, 2016
1 parent ae5a7b4 commit 75f7da2
Show file tree
Hide file tree
Showing 15 changed files with 281 additions and 206 deletions.
2 changes: 1 addition & 1 deletion addons/binding/org.openhab.binding.mysensors/.classpath
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="output" path="target/classes"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ Bundle-ClassPath: .
Import-Package:
com.google.common.collect,
com.google.common.base,
gnu.io,
org.apache.commons.lang,
gnu.io,
org.openhab.binding.mysensors,
org.openhab.binding.mysensors.handler,
org.eclipse.smarthome.config.core,
Expand All @@ -28,4 +28,5 @@ Import-Package:
Service-Component: OSGI-INF/*.xml
Export-Package: org.openhab.binding.mysensors,
org.openhab.binding.mysensors.handler
Require-Bundle: org.eclipse.smarthome.core
Require-Bundle: org.eclipse.smarthome.core,
org.openhab.io.transport.serial
9 changes: 9 additions & 0 deletions addons/binding/org.openhab.binding.mysensors/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,14 @@

<name>MySensors Binding</name>
<packaging>eclipse-plugin</packaging>

<dependencies>
<dependency>
<groupId>com.neuronrobotics</groupId>
<artifactId>nrjavaserial</artifactId>
<version>3.11.0</version>
<type>bundle</type>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -48,8 +49,11 @@ public class MySensorsBridgeHandler extends BaseBridgeHandler {
// Configuration from thing file
private MySensorsBridgeConfiguration myConfiguration = null;

private ScheduledExecutorService connectorScheduler = null;

public MySensorsBridgeHandler(Bridge bridge) {
super(bridge);
connectorScheduler = Executors.newSingleThreadScheduledExecutor();
}

/*
Expand All @@ -66,7 +70,8 @@ public void initialize() {
logger.debug("Set skip check on startup to: {}", myConfiguration.skipStartupCheck);

mysConnector = new MySensorsNetworkConnector(this);
connectorFuture = getScheduler().scheduleWithFixedDelay(mysConnector, 0, 10, TimeUnit.SECONDS);
connectorFuture = connectorScheduler.scheduleWithFixedDelay(mysConnector, 0,
MySensorsNetworkConnector.CONNECTOR_INTERVAL_CHECK, TimeUnit.SECONDS);

notifyDisconnect();
}
Expand All @@ -79,7 +84,6 @@ public void initialize() {
@Override
public void dispose() {
disconnect();
notifyDisconnect();
}

/*
Expand Down Expand Up @@ -147,10 +151,6 @@ public MySensorsBridgeConfiguration getBridgeConfiguration() {
return myConfiguration;
}

public ScheduledExecutorService getScheduler() {
return scheduler;
}

public void notifyConnect() {
updateStatus(ThingStatus.ONLINE);
}
Expand All @@ -161,14 +161,20 @@ public void notifyDisconnect() {

private void disconnect() {

if (mysConnector != null) {
mysConnector.stop();
mysConnector = null;
}

if (connectorFuture != null) {
connectorFuture.cancel(true);
connectorFuture = null;
}

if (mysConnector != null) {
mysConnector.disconnect();
mysConnector = null;
if (connectorScheduler != null) {
connectorScheduler.shutdown();
connectorScheduler.shutdownNow();
connectorScheduler = null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,15 @@ public void initialize() {

@Override
public void handleRemoval() {
logger.trace("handleRemoval for thing: " + nodeId);
updateStatus(ThingStatus.OFFLINE);
getBridgeHandler().getBridgeConnector().removeUpdateListener(this);
super.handleRemoval();
}

@Override
public void bridgeHandlerDisposed(ThingHandler thingHandler, Bridge bridge) {
logger.trace("handleRemoval for thing: " + nodeId);
updateStatus(ThingStatus.OFFLINE);
super.bridgeHandlerDisposed(thingHandler, bridge);
}
Expand Down Expand Up @@ -298,11 +301,6 @@ public void statusUpdateReceived(MySensorsStatusUpdateEvent event) {
}
}

@Override
public void disconnectEvent() {

}

/**
* Returns the BridgeHandler of the bridge/gateway to the MySensors network
*
Expand All @@ -320,7 +318,7 @@ public synchronized MySensorsBridgeHandler getBridgeHandler() {
@Override
public void bridgeHandlerInitialized(ThingHandler thingHandler, Bridge bridge) {
MySensorsBridgeHandler bridgeHandler = (MySensorsBridgeHandler) thingHandler;
if (bridgeHandler.getBridgeConnector() == null) {
if (bridgeHandler.getBridgeConnector() == null || !bridgeHandler.getBridgeConnector().checkConnection()) {
logger.warn("Bridge connection not estblished yet - can't subscribe for node: {} child: {}", nodeId,
childId);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,4 @@ public interface MySensorsUpdateListener extends EventListener {
* Procedure for receive status update from MySensorsNetwork.
*/
public void statusUpdateReceived(MySensorsStatusUpdateEvent event);

public void disconnectEvent();
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,36 +30,52 @@ public abstract class MySensorsBridgeConnection {

private BlockingQueue<MySensorsMessage> outboundMessageQueue = null;

// Update listener
private List<MySensorsUpdateListener> updateListeners = null;
private boolean connected = false;

protected boolean connected = false;
private boolean requestDisconnection = false;

private MySensorsBridgeConnection waitingObj = null;
private boolean iVersionResponse = false;

private boolean skipStartupCheck = false;

protected MySensorsWriter mysConWriter = null;
protected MySensorsReader mysConReader = null;

// Update listener
private List<MySensorsUpdateListener> updateListeners = null;

public MySensorsBridgeConnection(boolean skipStartupCheck) {
outboundMessageQueue = new LinkedBlockingQueue<MySensorsMessage>();
updateListeners = new ArrayList<>();

this.skipStartupCheck = skipStartupCheck;
updateListeners = new ArrayList<>();
}

/**
* startup connection with bridge
* Startup connection with bridge
*
* @return
*/
public abstract boolean connect();
public boolean connect() {
connected = _connect();
return connected;
}

public abstract boolean _connect();

/**
* shutodown method that allows the correct disconnection with the used bridge
* Shutdown method that allows the correct disconnection with the used bridge
*
* @return
*/
public abstract void disconnect();
public void disconnect() {
removeAllUpdateListener();
clearOutboundMessagesQueue();
_disconnect();
connected = false;
}

public abstract void _disconnect();

/**
* Start thread managing the incoming/outgoing messages. It also have the task to test the connection to gateway by
Expand Down Expand Up @@ -102,41 +118,27 @@ public MySensorsMessage pollMySensorsOutboundQueue() throws InterruptedException
return outboundMessageQueue.poll(1, TimeUnit.DAYS);
}

private void clearOutboundMessagesQueue() {
synchronized (outboundMessageQueue) {
outboundMessageQueue.clear();
}
}

public void addMySensorsOutboundMessage(MySensorsMessage msg) {
addMySensorsOutboundMessage(msg, 1);
}

public void addMySensorsOutboundMessage(MySensorsMessage msg, int copy) {
try {
for (int i = 0; i < copy; i++) {
outboundMessageQueue.put(msg);
}
} catch (InterruptedException e) {
logger.error("Interrupted message while ruuning");
}
}

public void removeMySensorsOutboundMessage(MySensorsMessage msg) {

pauseWriter = true;

Iterator<MySensorsMessage> iterator = outboundMessageQueue.iterator();
if (iterator != null) {
while (iterator.hasNext()) {
MySensorsMessage msgInQueue = iterator.next();
// logger.debug("Msg in Queue: " + msgInQueue.getDebugInfo());
if (msgInQueue.getNodeId() == msg.getNodeId() && msgInQueue.getChildId() == msg.getChildId()
&& msgInQueue.getMsgType() == msg.getMsgType() && msgInQueue.getSubType() == msg.getSubType()
&& msgInQueue.getAck() == msg.getAck() && msgInQueue.getMsg().equals(msg.getMsg())) {
iterator.remove();
// logger.debug("Message removed: " + msg.getDebugInfo());
} else {
logger.debug("Message NOT removed: " + msg.getDebugInfo());
synchronized (outboundMessageQueue) {
try {
for (int i = 0; i < copy; i++) {
outboundMessageQueue.put(msg);
}
} catch (InterruptedException e) {
logger.error("Interrupted message while ruuning");
}
}

pauseWriter = false;
}

/**
Expand All @@ -158,24 +160,45 @@ public void removeUpdateListener(MySensorsUpdateListener listener) {
}
}

private void removeAllUpdateListener() {
synchronized (updateListeners) {
updateListeners.clear();
}
}

public List<MySensorsUpdateListener> getUpdateListeners() {
return updateListeners;
}

public void broadCastDisconnect() {
public void broadCastEvent(MySensorsStatusUpdateEvent event) {
synchronized (updateListeners) {
for (MySensorsUpdateListener mySensorsEventListener : updateListeners) {
mySensorsEventListener.disconnectEvent();
mySensorsEventListener.statusUpdateReceived(event);
}
}
}

public void broadCastEvent(MySensorsStatusUpdateEvent event) {
synchronized (updateListeners) {
for (MySensorsUpdateListener mySensorsEventListener : updateListeners) {
mySensorsEventListener.statusUpdateReceived(event);
public void removeMySensorsOutboundMessage(MySensorsMessage msg) {

pauseWriter = true;

Iterator<MySensorsMessage> iterator = outboundMessageQueue.iterator();
if (iterator != null) {
while (iterator.hasNext()) {
MySensorsMessage msgInQueue = iterator.next();
// logger.debug("Msg in Queue: " + msgInQueue.getDebugInfo());
if (msgInQueue.getNodeId() == msg.getNodeId() && msgInQueue.getChildId() == msg.getChildId()
&& msgInQueue.getMsgType() == msg.getMsgType() && msgInQueue.getSubType() == msg.getSubType()
&& msgInQueue.getAck() == msg.getAck() && msgInQueue.getMsg().equals(msg.getMsg())) {
iterator.remove();
// logger.debug("Message removed: " + msg.getDebugInfo());
} else {
logger.debug("Message NOT removed: " + msg.getDebugInfo());
}
}
}

pauseWriter = false;
}

public void iVersionMessageReceived(String msg) {
Expand All @@ -196,4 +219,13 @@ public boolean isWriterPaused() {
public boolean isConnected() {
return connected;
}

public boolean requestingDisconnection() {
return requestDisconnection;
}

public void requestDisconnection(boolean flag) {
logger.debug("Request disconnection flag setted to: " + flag);
requestDisconnection = flag;
}
}
Loading

0 comments on commit 75f7da2

Please sign in to comment.