Skip to content

Commit

Permalink
Merge pull request openhab#23 from andreacioni/MySensors
Browse files Browse the repository at this point in the history
Minor bugfixing and little refactoring
  • Loading branch information
tobof authored Aug 2, 2016
2 parents 5212440 + c50e57a commit 4aac271
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.Set;

import org.eclipse.smarthome.core.thing.ThingTypeUID;
import org.openhab.binding.mysensors.internal.MySensorsMessage;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
Expand Down Expand Up @@ -202,7 +203,10 @@ public class MySensorsBindingConstants {
public final static String CHANNEL_LAST_UPDATE = "lastupdate";

// Wait time Arduino reset
public final static int RESET_TIME = 5000;
public final static int RESET_TIME = 3000;

// I version message for startup check
public static final MySensorsMessage I_VERSION_MESSAGE = new MySensorsMessage(0, 0, 3, 0, false, 2, "");

public final static Map<Number, String> CHANNEL_MAP = new HashMap<Number, String>() {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,6 @@ public class MySensorsBridgeHandler extends BaseBridgeHandler implements MySenso

public MySensorsBridgeHandler(Bridge bridge) {
super(bridge);

boolean imperial = getConfigAs(MySensorsBridgeConfiguration.class).imperial;
iConfig = imperial ? "I" : "M";

logger.info("Using {} measure unit", (imperial ? "Imperial" : "Metric"));

skipStartupCheck = getConfigAs(MySensorsBridgeConfiguration.class).skipStartupCheck;

logger.debug("Set skip check on startup to: {}", skipStartupCheck);
}

/*
Expand All @@ -79,6 +70,15 @@ public void initialize() {

MySensorsBridgeConfiguration configuration = getConfigAs(MySensorsBridgeConfiguration.class);

boolean imperial = configuration.imperial;
iConfig = imperial ? "I" : "M";

logger.debug("Using {} measure unit", (imperial ? "Imperial" : "Metric"));

skipStartupCheck = configuration.skipStartupCheck;

logger.debug("Set skip check on startup to: {}", skipStartupCheck);

if (getThing().getThingTypeUID().equals(THING_TYPE_BRIDGE_SER)) {
mysCon = new MySensorsSerialConnection(configuration.serialPort, configuration.baudRate,
configuration.sendDelay, skipStartupCheck);
Expand All @@ -91,14 +91,15 @@ public void initialize() {

if (mysCon.connect()) {
updateStatus(ThingStatus.ONLINE);

// Start discovery service
MySensorsDiscoveryService discoveryService = new MySensorsDiscoveryService(this);
discoveryService.activate();
} else {
mysCon.removeUpdateListener(this);
updateStatus(ThingStatus.OFFLINE);
}

// Start discovery service
MySensorsDiscoveryService discoveryService = new MySensorsDiscoveryService(this);
discoveryService.activate();
}

/*
Expand Down Expand Up @@ -212,7 +213,7 @@ private void answerITimeMessage(MySensorsMessage msg) {
* @param msg, the incoming I_CONFIG message from sensor
*/
private void answerIConfigMessage(MySensorsMessage msg) {
logger.info("I_CONFIG request received from {}, answering...", msg.nodeId);
logger.debug("I_CONFIG request received from {}, answering...", msg.nodeId);

MySensorsMessage newMsg = new MySensorsMessage(msg.nodeId, msg.childId, MYSENSORS_MSG_TYPE_INTERNAL, 0, false,
MYSENSORS_SUBTYPE_I_CONFIG, iConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,15 @@ public void bridgeHandlerDisposed(ThingHandler thingHandler, Bridge bridge) {
*/
@Override
public void handleCommand(ChannelUID channelUID, Command command) {

/* We don't handle refresh commands yet
*
*/
if(command == RefreshType.REFRESH)
return;



/*
* We don't handle refresh commands yet
*
*/
if (command == RefreshType.REFRESH) {
return;
}

String msgPayload = "";
int subType = 0;
int int_requestack = 0;
Expand Down Expand Up @@ -226,7 +227,7 @@ public void handleUpdate(ChannelUID channelUID, org.eclipse.smarthome.core.types
*/
@Override
public void statusUpdateReceived(MySensorsStatusUpdateEvent event) {
MySensorsMessage msg = event.getData();
MySensorsMessage msg = event.getData();

// or is this an update message?
if (nodeId == msg.getNodeId()) { // is this message for me?
Expand Down Expand Up @@ -315,8 +316,10 @@ public synchronized MySensorsBridgeHandler getBridgeHandler() {
public void bridgeHandlerInitialized(ThingHandler thingHandler, Bridge bridge) {
MySensorsBridgeHandler bridgeHandler = (MySensorsBridgeHandler) thingHandler;
if (bridgeHandler.getBridgeConnection() == null) {
logger.error("Bridge connection not estblished yet - can't subscribe for updates");
logger.warn("Bridge connection not estblished yet - can't subscribe for node: {} child: {}", nodeId,
childId);
} else {
logger.debug("Bridge connection estblished - subscribing update for node: {} child: {}", nodeId, childId);
bridgeHandler.getBridgeConnection().addUpdateListener(this);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

import org.openhab.binding.mysensors.MySensorsBindingConstants;
import org.openhab.binding.mysensors.handler.MySensorsUpdateListener;
import org.openhab.binding.mysensors.protocol.MySensorsReader;
import org.openhab.binding.mysensors.protocol.MySensorsWriter;
Expand All @@ -31,7 +32,7 @@ public abstract class MySensorsBridgeConnection {

protected boolean connected = false;

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

private boolean skipStartupCheck = false;
Expand Down Expand Up @@ -68,17 +69,19 @@ protected boolean startReaderWriterThread(MySensorsReader reader, MySensorsWrite
reader.startReader();
writer.startWriter();

holdingThread = this;

if (!skipStartupCheck) {
synchronized (holdingThread) {
try {
if (!iVersionResponse) {
this.wait(5 * 1000); // wait 2s the reply for the I_VERSION message
try {
int i = 0;
synchronized (this) {
while (!iVersionResponse && i < 5) {
addMySensorsOutboundMessage(MySensorsBindingConstants.I_VERSION_MESSAGE);
waitingObj = this;
waitingObj.wait(1000);
i++;
}
} catch (Exception e) {
logger.error("Exception on waiting for I_VERSION message", e);
}
} catch (Exception e) {
logger.error("Exception on waiting for I_VERSION message", e);
}
} else {
logger.warn("Skipping I_VERSION connection test, not recommended...");
Expand All @@ -87,7 +90,6 @@ protected boolean startReaderWriterThread(MySensorsReader reader, MySensorsWrite

if (!iVersionResponse) {
logger.error("Cannot start reading/writing thread, probably sync message (I_VERSION) not received");
disconnect();
}

return iVersionResponse;
Expand Down Expand Up @@ -153,8 +155,9 @@ public void removeMySensorsOutboundMessage(MySensorsMessage msg) {

public void iVersionMessageReceived(String msg) {
iVersionResponse = true;
synchronized (holdingThread) {
holdingThread.notify();
synchronized (waitingObj) {
waitingObj.notifyAll();
waitingObj = null;
}
logger.debug("Good,Gateway is up and running! (Ver:{})", msg);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void run() {
}
}
} catch (Exception e) {
logger.error("exception on reading from serial port, message: {}", e.getMessage());
logger.error("({}) on reading from serial port, message: {}", e, getClass(), e.getMessage());
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,10 @@ public abstract class MySensorsWriter implements MySensorsUpdateListener, Runnab
protected ExecutorService executor = Executors.newSingleThreadExecutor();
protected Future<?> future = null;

private static final MySensorsMessage I_VERSION_MESSAGE = new MySensorsMessage(0, 0, 3, 0, false, 2, "");

protected int sendDelay = 1000;

public void startWriter() {
future = executor.submit(this);

// Send the I_VERSION message
mysCon.addMySensorsOutboundMessage(I_VERSION_MESSAGE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ public void disconnect() {
mysConWriter.stopWriting();
}

mysConReader.stopReader();
if (mysConReader != null) {
mysConReader.stopReader();
}

// Shut down socket
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public class MySensorsSerialConnection extends MySensorsBridgeConnection {

private String serialPort = "";
private int baudRate = 115200;
public int sendDelay = 0;
private int sendDelay = 0;
private boolean skipStartupCheck = false;

private NRSerialPort serialConnection = null;

Expand All @@ -44,6 +45,7 @@ public MySensorsSerialConnection(String serialPort, int baudRate, int sendDelay,
this.serialPort = serialPort;
this.baudRate = baudRate;
this.sendDelay = sendDelay;
this.skipStartupCheck = skipStartupCheck;

}

Expand All @@ -54,7 +56,7 @@ public boolean connect() {
updateSerialProperties(serialPort);
serialConnection = new NRSerialPort(serialPort, baudRate);
if (serialConnection.connect()) {
logger.debug("Successfully connected to serial port.");
logger.info("Successfully connected to serial port.");

try {
logger.debug("Waiting {} seconds to allow correct reset trigger on serial connection opening",
Expand All @@ -77,7 +79,7 @@ public boolean connect() {

@Override
public void disconnect() {
logger.debug("Shutting down serial connection!");
logger.info("Shutting down serial connection!");

if (mysConWriter != null) {
mysConWriter.stopWriting();
Expand Down

0 comments on commit 4aac271

Please sign in to comment.