Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

My sensors #3

Merged
merged 5 commits into from
Feb 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
</description>
<default>100</default>
</parameter>
<parameter name="baudRate" type="integer" required="true" min="1" max="230400">
<context>baudrate</context>
<label>Baud rate</label>
<description>Specify the baud rate of the connected serial gateway. DEFAULT is 115200</description>
<default>115200</default>
</parameter>
<parameter name="baudRate" type="integer" required="false" min="300" max="250000">
<label>Baudrate</label>
<description>Baudrate for serial port
</description>
<default>115200</default>
</parameter>
</config-description>

</bridge-type>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,12 @@ public class MySensorsBindingConstants {

public static final String BINDING_ID = "mysensors";

public static final int DEFAULT_SERIAL_BAUDRATE = 115200;

public static final String PARAMETER_NODEID = "nodeId";
public static final String PARAMETER_CHILDID = "childId";
public static final String PARAMETER_IPADDRESS = "ipAddress";
public static final String PRAMETER_TCPPORT = "tcpPort";
public static final String PARAMETER_SENDDELAY = "sendDelay";
public static final String PARAMETER_BAUDRATE = "baudRate";
public static final String PARAMETER_REQUESTACK = "requestack";

public static final int MYSENSORS_MSG_TYPE_PRESENTATION = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,138 +9,150 @@
import org.eclipse.smarthome.config.discovery.DiscoveryResult;
import org.eclipse.smarthome.config.discovery.DiscoveryResultBuilder;
import org.eclipse.smarthome.core.thing.ThingUID;
import org.eclipse.smarthome.core.thing.binding.builder.GenericThingBuilder;
import org.openhab.binding.mysensors.handler.MySensorsBridgeHandler;
import org.openhab.binding.mysensors.handler.MySensorsStatusUpdateEvent;
import org.openhab.binding.mysensors.handler.MySensorsUpdateListener;
import org.openhab.binding.mysensors.internal.MySensorsMessage;
import org.openhab.binding.mysensors.service.DiscoveryThread;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;




/**
* @author Tim Oberföll
*
* Discoveryservice for MySensors devices
*
* Discoveryservice for MySensors devices
*/
public class MySensorsDiscoveryService extends AbstractDiscoveryService {

private Logger logger = LoggerFactory.getLogger(MySensorsDiscoveryService.class);

private MySensorsBridgeHandler bridgeHandler = null;

private DiscoveryThread discoThread = null;

public MySensorsDiscoveryService(MySensorsBridgeHandler bridgeHandler) {
super(SUPPORTED_THING_TYPES_UIDS, 3000, false);
this.bridgeHandler = bridgeHandler;
}

@Override
protected void startScan() {
if(discoThread == null)
discoThread = new DiscoveryThread(bridgeHandler.mysCon, this);
discoThread.start();
}

public void activate() {

}

public void deactivate() {
if(discoThread == null)
discoThread = new DiscoveryThread(bridgeHandler.mysCon, this);
discoThread.stop();
}

@Override
protected void stopScan() {
if(discoThread == null)
discoThread = new DiscoveryThread(bridgeHandler.mysCon, this);
discoThread.stop();
}

public void newDevicePresented(MySensorsMessage msg) {

// Representation Message?
if(msg.getMsgType() == MYSENSORS_MSG_TYPE_PRESENTATION) {
logger.debug("Representation Message received");
logger.debug("Preparing new thing for inbox");


ThingUID uid = null;

// uid must not contains dots
switch (msg.getSubType()) {
case MYSENSORS_SUBTYPE_S_HUM:
uid = new ThingUID(THING_TYPE_HUMIDITY, bridgeHandler.getThing().getUID(), "Humidity_" + msg.getNodeId() + "_" + msg.getChildId());
break;
case MYSENSORS_SUBTYPE_S_TEMP:
uid = new ThingUID(THING_TYPE_TEMPERATURE, bridgeHandler.getThing().getUID(), "Temperature_" + msg.getNodeId() + "_" + msg.getChildId());
break;
case MYSENSORS_SUBTYPE_S_MULTIMETER:
uid = new ThingUID(THING_TYPE_MULTIMETER, bridgeHandler.getThing().getUID(), "Multimeter_" + msg.getNodeId() + "_" + msg.getChildId());
break;
case MYSENSORS_SUBTYPE_S_LIGHT:
uid = new ThingUID(THING_TYPE_LIGHT, bridgeHandler.getThing().getUID(), "Light_" + msg.getNodeId() + "_" + msg.getChildId());
break;
case MYSENSORS_SUBTYPE_S_POWER:
uid = new ThingUID(THING_TYPE_POWER, bridgeHandler.getThing().getUID(), "Power_" + msg.getNodeId() + "_" + msg.getChildId());
break;
case MYSENSORS_SUBTYPE_S_BARO:
uid = new ThingUID(THING_TYPE_BARO, bridgeHandler.getThing().getUID(), "Baro_" + msg.getNodeId() + "_" + msg.getChildId());
break;
case MYSENSORS_SUBTYPE_S_DOOR:
uid = new ThingUID(THING_TYPE_DOOR, bridgeHandler.getThing().getUID(), "Door_" + msg.getNodeId() + "_" + msg.getChildId());
break;
case MYSENSORS_SUBTYPE_S_MOTION:
uid = new ThingUID(THING_TYPE_MOTION, bridgeHandler.getThing().getUID(), "Motion_" + msg.getNodeId() + "_" + msg.getChildId());
break;
case MYSENSORS_SUBTYPE_S_SMOKE:
uid = new ThingUID(THING_TYPE_SMOKE, bridgeHandler.getThing().getUID(), "Smoke_" + msg.getNodeId() + "_" + msg.getChildId());
break;
case MYSENSORS_SUBTYPE_S_DIMMER:
uid = new ThingUID(THING_TYPE_DIMMER, bridgeHandler.getThing().getUID(), "Dimmer_" + msg.getNodeId() + "_" + msg.getChildId());
break;
case MYSENSORS_SUBTYPE_S_COVER:
uid = new ThingUID(THING_TYPE_COVER, bridgeHandler.getThing().getUID(), "Cover_" + msg.getNodeId() + "_" + msg.getChildId());
break;
case MYSENSORS_SUBTYPE_S_WIND:
uid = new ThingUID(THING_TYPE_WIND, bridgeHandler.getThing().getUID(), "Wind_" + msg.getNodeId() + "_" + msg.getChildId());
break;
case MYSENSORS_SUBTYPE_S_RAIN:
uid = new ThingUID(THING_TYPE_RAIN, bridgeHandler.getThing().getUID(), "Rain_" + msg.getNodeId() + "_" + msg.getChildId());
break;
case MYSENSORS_SUBTYPE_S_UV:
uid = new ThingUID(THING_TYPE_UV, bridgeHandler.getThing().getUID(), "UV_" + msg.getNodeId() + "_" + msg.getChildId());
break;
case MYSENSORS_SUBTYPE_S_WEIGHT:
uid = new ThingUID(THING_TYPE_WEIGHT, bridgeHandler.getThing().getUID(), "Weight_" + msg.getNodeId() + "_" + msg.getChildId());
break;
case MYSENSORS_SUBTYPE_S_DISTANCE:
uid = new ThingUID(THING_TYPE_DISTANCE, bridgeHandler.getThing().getUID(), "Distance_" + msg.getNodeId() + "_" + msg.getChildId());
break;
case MYSENSORS_SUBTYPE_S_LIGHT_LEVEL:
uid = new ThingUID(THING_TYPE_LIGHT_LEVEL, bridgeHandler.getThing().getUID(), "Light_level_" + msg.getNodeId() + "_" + msg.getChildId());
break;
}
if(uid!=null) {
Map<String, Object> properties = new HashMap<>(2);
properties.put(PARAMETER_NODEID , ""+msg.getNodeId());
properties.put(PARAMETER_CHILDID, ""+msg.getChildId());
DiscoveryResult result = DiscoveryResultBuilder.create(uid)
.withProperties(properties)
.withLabel("MySensors Device (" + msg.getNodeId() + ";" + msg.getChildId() +")")
.withBridge(bridgeHandler.getThing().getUID())
.build();
thingDiscovered(result);

logger.debug("Discovered device submitted");
}
}
}

private Logger logger = LoggerFactory.getLogger(MySensorsDiscoveryService.class);

private MySensorsBridgeHandler bridgeHandler = null;

private DiscoveryThread discoThread = null;

public MySensorsDiscoveryService(MySensorsBridgeHandler bridgeHandler) {
super(SUPPORTED_THING_TYPES_UIDS, 3000, false);
this.bridgeHandler = bridgeHandler;
}

@Override
protected void startScan() {
if (discoThread == null) {
discoThread = new DiscoveryThread(bridgeHandler.getBridgeConnection(), this);
}
discoThread.start();
}

public void activate() {

}

@Override
public void deactivate() {
if (discoThread == null) {
discoThread = new DiscoveryThread(bridgeHandler.getBridgeConnection(), this);
}
discoThread.stop();
}

@Override
protected void stopScan() {
if (discoThread == null) {
discoThread = new DiscoveryThread(bridgeHandler.getBridgeConnection(), this);
}
discoThread.stop();
}

public void newDevicePresented(MySensorsMessage msg) {

// Representation Message?
if (msg.getMsgType() == MYSENSORS_MSG_TYPE_PRESENTATION) {
logger.debug("Representation Message received");
logger.debug("Preparing new thing for inbox");

ThingUID uid = null;

// uid must not contains dots
switch (msg.getSubType()) {
case MYSENSORS_SUBTYPE_S_HUM:
uid = new ThingUID(THING_TYPE_HUMIDITY, bridgeHandler.getThing().getUID(),
"Humidity_" + msg.getNodeId() + "_" + msg.getChildId());
break;
case MYSENSORS_SUBTYPE_S_TEMP:
uid = new ThingUID(THING_TYPE_TEMPERATURE, bridgeHandler.getThing().getUID(),
"Temperature_" + msg.getNodeId() + "_" + msg.getChildId());
break;
case MYSENSORS_SUBTYPE_S_MULTIMETER:
uid = new ThingUID(THING_TYPE_MULTIMETER, bridgeHandler.getThing().getUID(),
"Multimeter_" + msg.getNodeId() + "_" + msg.getChildId());
break;
case MYSENSORS_SUBTYPE_S_LIGHT:
uid = new ThingUID(THING_TYPE_LIGHT, bridgeHandler.getThing().getUID(),
"Light_" + msg.getNodeId() + "_" + msg.getChildId());
break;
case MYSENSORS_SUBTYPE_S_POWER:
uid = new ThingUID(THING_TYPE_POWER, bridgeHandler.getThing().getUID(),
"Power_" + msg.getNodeId() + "_" + msg.getChildId());
break;
case MYSENSORS_SUBTYPE_S_BARO:
uid = new ThingUID(THING_TYPE_BARO, bridgeHandler.getThing().getUID(),
"Baro_" + msg.getNodeId() + "_" + msg.getChildId());
break;
case MYSENSORS_SUBTYPE_S_DOOR:
uid = new ThingUID(THING_TYPE_DOOR, bridgeHandler.getThing().getUID(),
"Door_" + msg.getNodeId() + "_" + msg.getChildId());
break;
case MYSENSORS_SUBTYPE_S_MOTION:
uid = new ThingUID(THING_TYPE_MOTION, bridgeHandler.getThing().getUID(),
"Motion_" + msg.getNodeId() + "_" + msg.getChildId());
break;
case MYSENSORS_SUBTYPE_S_SMOKE:
uid = new ThingUID(THING_TYPE_SMOKE, bridgeHandler.getThing().getUID(),
"Smoke_" + msg.getNodeId() + "_" + msg.getChildId());
break;
case MYSENSORS_SUBTYPE_S_DIMMER:
uid = new ThingUID(THING_TYPE_DIMMER, bridgeHandler.getThing().getUID(),
"Dimmer_" + msg.getNodeId() + "_" + msg.getChildId());
break;
case MYSENSORS_SUBTYPE_S_COVER:
uid = new ThingUID(THING_TYPE_COVER, bridgeHandler.getThing().getUID(),
"Cover_" + msg.getNodeId() + "_" + msg.getChildId());
break;
case MYSENSORS_SUBTYPE_S_WIND:
uid = new ThingUID(THING_TYPE_WIND, bridgeHandler.getThing().getUID(),
"Wind_" + msg.getNodeId() + "_" + msg.getChildId());
break;
case MYSENSORS_SUBTYPE_S_RAIN:
uid = new ThingUID(THING_TYPE_RAIN, bridgeHandler.getThing().getUID(),
"Rain_" + msg.getNodeId() + "_" + msg.getChildId());
break;
case MYSENSORS_SUBTYPE_S_UV:
uid = new ThingUID(THING_TYPE_UV, bridgeHandler.getThing().getUID(),
"UV_" + msg.getNodeId() + "_" + msg.getChildId());
break;
case MYSENSORS_SUBTYPE_S_WEIGHT:
uid = new ThingUID(THING_TYPE_WEIGHT, bridgeHandler.getThing().getUID(),
"Weight_" + msg.getNodeId() + "_" + msg.getChildId());
break;
case MYSENSORS_SUBTYPE_S_DISTANCE:
uid = new ThingUID(THING_TYPE_DISTANCE, bridgeHandler.getThing().getUID(),
"Distance_" + msg.getNodeId() + "_" + msg.getChildId());
break;
case MYSENSORS_SUBTYPE_S_LIGHT_LEVEL:
uid = new ThingUID(THING_TYPE_LIGHT_LEVEL, bridgeHandler.getThing().getUID(),
"Light_level_" + msg.getNodeId() + "_" + msg.getChildId());
break;
}
if (uid != null) {
Map<String, Object> properties = new HashMap<>(2);
properties.put(PARAMETER_NODEID, "" + msg.getNodeId());
properties.put(PARAMETER_CHILDID, "" + msg.getChildId());
DiscoveryResult result = DiscoveryResultBuilder.create(uid).withProperties(properties)
.withLabel("MySensors Device (" + msg.getNodeId() + ";" + msg.getChildId() + ")")
.withBridge(bridgeHandler.getThing().getUID()).build();
thingDiscovered(result);

logger.debug("Discovered device submitted");
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import org.eclipse.smarthome.config.core.Configuration;
import org.eclipse.smarthome.core.thing.Bridge;
Expand Down Expand Up @@ -36,13 +39,16 @@ public class MySensorsBridgeHandler extends BaseBridgeHandler implements MySenso

private Logger logger = LoggerFactory.getLogger(MySensorsBridgeHandler.class);

public MySensorsBridgeConnection mysCon = null;

public Collection<Thing> connectedThings = Lists.newArrayList();

// List of Ids that OpenHAB has given, in response to an id request from a sensor node
private List<Number> givenIds = new ArrayList<Number>();

// Executor that hold the bridge reader thread
private ExecutorService executor = Executors.newSingleThreadExecutor();
private Future<?> future = null;
private MySensorsBridgeConnection mysCon = null;

public MySensorsBridgeHandler(Bridge bridge) {
super(bridge);
}
Expand All @@ -59,15 +65,14 @@ public void initialize() {
MySensorsBridgeConfiguration configuration = getConfigAs(MySensorsBridgeConfiguration.class);

if (getThing().getThingTypeUID().equals(THING_TYPE_BRIDGE_SER)) {
mysCon = new MySensorsSerialConnection(configuration.serialPort,
(configuration.baudRate == null) ? DEFAULT_SERIAL_BAUDRATE : configuration.baudRate,
mysCon = new MySensorsSerialConnection(configuration.serialPort, configuration.baudRate,
configuration.sendDelay);
} else if (getThing().getThingTypeUID().equals(THING_TYPE_BRIDGE_ETH)) {
mysCon = new MySensorsIpConnection(configuration.ipAddress, configuration.tcpPort, configuration.sendDelay);
}

if (mysCon.connected) {
mysCon.start();
if (mysCon.connect()) {
future = executor.submit(mysCon);
mysCon.addUpdateListener(this);
updateStatus(ThingStatus.ONLINE);
} else {
Expand All @@ -87,7 +92,16 @@ public void initialize() {
@Override
public void dispose() {
if (mysCon != null) {
mysCon.stopReader();
mysCon.disconnect();
}

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

if (executor != null) {
executor.shutdown();
executor.shutdownNow();
}
}

Expand Down Expand Up @@ -187,4 +201,8 @@ public void revertToOldStatus(MySensorsStatusUpdateEvent event) {

}

public MySensorsBridgeConnection getBridgeConnection() {
return mysCon;
}

}
Loading