Skip to content
Snippets Groups Projects
Commit 3473de36 authored by René Schöne's avatar René Schöne
Browse files

Reworked binding to openHAB version 2.4.0.

- Changed connection to	mqtt broker to old way using MqttService
- Changed processing of	numbers	to double instead of float
parent bc94880d
No related branches found
No related tags found
No related merge requests found
...@@ -18,7 +18,6 @@ Import-Package: ...@@ -18,7 +18,6 @@ Import-Package:
org.eclipse.smarthome.core.thing.type, org.eclipse.smarthome.core.thing.type,
org.eclipse.smarthome.core.types, org.eclipse.smarthome.core.types,
org.eclipse.smarthome.io.transport.mqtt, org.eclipse.smarthome.io.transport.mqtt,
org.openhab.binding.mqtt.handler,
org.osgi.framework, org.osgi.framework,
org.osgi.service.component, org.osgi.service.component,
org.slf4j org.slf4j
......
...@@ -3,7 +3,7 @@ package org.openhab.binding.openlicht.handler; ...@@ -3,7 +3,7 @@ package org.openhab.binding.openlicht.handler;
import static org.openhab.binding.openlicht.BindingConstants.*; import static org.openhab.binding.openlicht.BindingConstants.*;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.nio.ByteBuffer; import java.util.Map.Entry;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
...@@ -12,26 +12,27 @@ import java.util.concurrent.locks.ReentrantLock; ...@@ -12,26 +12,27 @@ import java.util.concurrent.locks.ReentrantLock;
import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.core.common.registry.RegistryChangeListener;
import org.eclipse.smarthome.core.thing.Thing; import org.eclipse.smarthome.core.thing.Thing;
import org.eclipse.smarthome.core.thing.ThingRegistry;
import org.eclipse.smarthome.core.thing.ThingStatus; import org.eclipse.smarthome.core.thing.ThingStatus;
import org.eclipse.smarthome.core.thing.ThingStatusDetail; import org.eclipse.smarthome.core.thing.ThingStatusDetail;
import org.eclipse.smarthome.core.thing.binding.BaseThingHandler; import org.eclipse.smarthome.core.thing.binding.BaseThingHandler;
import org.eclipse.smarthome.core.thing.binding.ThingHandler; //import org.openhab.binding.mqtt.handler.AbstractBrokerHandler;
//import org.openhab.binding.openlicht.internal.ConfigurationHolder;
import org.eclipse.smarthome.io.transport.mqtt.MqttBrokerConnection; import org.eclipse.smarthome.io.transport.mqtt.MqttBrokerConnection;
import org.eclipse.smarthome.io.transport.mqtt.MqttMessageSubscriber; import org.eclipse.smarthome.io.transport.mqtt.MqttMessageSubscriber;
import org.openhab.binding.mqtt.handler.AbstractBrokerHandler; import org.eclipse.smarthome.io.transport.mqtt.MqttService;
import org.eclipse.smarthome.io.transport.mqtt.MqttServiceObserver;
import org.openhab.binding.openlicht.internal.ConfigurationHolder; import org.openhab.binding.openlicht.internal.ConfigurationHolder;
import org.osgi.framework.Version; import org.osgi.framework.Version;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
public abstract class AbstractMqttHandler extends BaseThingHandler public abstract class AbstractMqttHandler extends BaseThingHandler
implements MqttMessageSubscriber, RegistryChangeListener<Thing> { implements MqttMessageSubscriber, MqttServiceObserver/* , RegistryChangeListener<Thing> */ {
protected final @NonNull Logger logger = LoggerFactory.getLogger(AbstractMqttHandler.class); protected final @NonNull Logger logger = LoggerFactory.getLogger(AbstractMqttHandler.class);
private @Nullable ThingRegistry thingRegistry; // private @Nullable ThingRegistry thingRegistry;
private @Nullable MqttService mqttService;
private int usedTopicLength = 0; private int usedTopicLength = 0;
private MqttBrokerConnection currentBrokerConnection = null; private MqttBrokerConnection currentBrokerConnection = null;
private boolean warnNoBrokerConnection = true; private boolean warnNoBrokerConnection = true;
...@@ -43,50 +44,70 @@ public abstract class AbstractMqttHandler extends BaseThingHandler ...@@ -43,50 +44,70 @@ public abstract class AbstractMqttHandler extends BaseThingHandler
public AbstractMqttHandler(Thing thing, ConfigurationHolder configurationHolder) { public AbstractMqttHandler(Thing thing, ConfigurationHolder configurationHolder) {
super(thing); super(thing);
this.thingRegistry = configurationHolder.getThingRegistry(); // this.thingRegistry = configurationHolder.getThingRegistry();
this.thingRegistry.addRegistryChangeListener(this); // this.thingRegistry.addRegistryChangeListener(this);
this.mqttService = configurationHolder.getMqttService();
if (this.mqttService != null) {
this.mqttService.addBrokersListener(this);
}
this.executor = configurationHolder.getExecutor(); this.executor = configurationHolder.getExecutor();
this.version = configurationHolder.getVersion(); this.version = configurationHolder.getVersion();
this.configUpdateLock = new ReentrantLock(); this.configUpdateLock = new ReentrantLock();
logger.info("Started handler " + this.getClass().getSimpleName() + " in version " + version);
} }
@Override // @Override
public void added(Thing element) { // public void added(Thing element) {
if (thingIsMyMqttBroker(element)) { // if (thingIsMyMqttBroker(element)) {
addConnectionOf(element); // addConnectionOf(element);
} // }
} // }
//
// @Override
// public void removed(Thing element) {
// if (thingIsMyMqttBroker(element)) {
// removeMyBroker();
// }
// }
//
// @Override
// public void updated(Thing oldElement, Thing element) {
// if (thingIsMyMqttBroker(element)) {
// removeMyBroker();
// addConnectionOf(element);
// }
// }
@Override @Override
public void removed(Thing element) { public void brokerAdded(String brokerID, MqttBrokerConnection broker) {
if (thingIsMyMqttBroker(element)) { if (brokerID.equals(myBrokerName)) {
removeMyBroker(); myBrokerAdded(broker);
} }
} }
@Override @Override
public void updated(Thing oldElement, Thing element) { public void brokerRemoved(String brokerID, MqttBrokerConnection broker) {
if (thingIsMyMqttBroker(element)) { if (brokerID.equals(myBrokerName)) {
removeMyBroker(); removeMyBroker();
addConnectionOf(element);
} }
} }
private boolean thingIsMyMqttBroker(Thing thing) { // private boolean thingIsMyMqttBroker(Thing thing) {
if (!"mqtt:systemBroker".equals(thing.getThingTypeUID().getAsString()) // if (!"mqtt:systemBroker".equals(thing.getThingTypeUID().getAsString())
&& !"mqtt:broker".equals(thing.getThingTypeUID().getAsString())) { // && !"mqtt:broker".equals(thing.getThingTypeUID().getAsString())) {
return false; // return false;
} // }
return myBrokerName.equals(thing.getUID().getAsString()); // return myBrokerName.equals(thing.getUID().getAsString());
} // }
private void addConnectionOf(Thing mqttBroker) { // private void addConnectionOf(Thing mqttBroker) {
ThingHandler handler = mqttBroker.getHandler(); // ThingHandler handler = mqttBroker.getHandler();
if (handler instanceof AbstractBrokerHandler) { // this.mqttService.addBrokerConnection(myBrokerName, currentBrokerConnection)
AbstractBrokerHandler abh = (AbstractBrokerHandler) handler; // if (handler instanceof AbstractBrokerHandler) {
myBrokerAdded(abh.getConnection()); // AbstractBrokerHandler abh = (AbstractBrokerHandler) handler;
} // myBrokerAdded(abh.getConnection());
} // }
// }
@Override @Override
public void initialize() { public void initialize() {
...@@ -103,9 +124,18 @@ public abstract class AbstractMqttHandler extends BaseThingHandler ...@@ -103,9 +124,18 @@ public abstract class AbstractMqttHandler extends BaseThingHandler
myBrokerName = brokerName; myBrokerName = brokerName;
logger.debug("Setting myBrokerName to '{}'", myBrokerName); logger.debug("Setting myBrokerName to '{}'", myBrokerName);
for (Thing thing : this.thingRegistry.getAll()) { // for (Thing thing : this.thingRegistry.getAll()) {
if (thingIsMyMqttBroker(thing)) { // if (thingIsMyMqttBroker(thing)) {
addConnectionOf(thing); // addConnectionOf(thing);
// }
// }
MqttService service = this.mqttService;
if (service == null) {
logger.debug("No mqtt service yet");
} else {
for (Entry<@NonNull String, @NonNull MqttBrokerConnection> entry : service.getAllBrokerConnections()
.entrySet()) {
brokerAdded(entry.getKey(), entry.getValue());
} }
} }
} finally { } finally {
...@@ -160,6 +190,9 @@ public abstract class AbstractMqttHandler extends BaseThingHandler ...@@ -160,6 +190,9 @@ public abstract class AbstractMqttHandler extends BaseThingHandler
public void removeMyBroker() { public void removeMyBroker() {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_PENDING, "Broker is offline"); updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_PENDING, "Broker is offline");
if (this.mqttService != null) {
this.mqttService.removeBrokerConnection(myBrokerName);
}
currentBrokerConnection = null; currentBrokerConnection = null;
} }
...@@ -215,17 +248,17 @@ public abstract class AbstractMqttHandler extends BaseThingHandler ...@@ -215,17 +248,17 @@ public abstract class AbstractMqttHandler extends BaseThingHandler
protected boolean subscribeSubTopics() { protected boolean subscribeSubTopics() {
return true; return true;
} }
//
/** // /**
* Writes a float array from the given input based on the length of the targeted output. // * Writes a float array from the given input based on the length of the targeted output.
* // *
* @param input the buffer to read from // * @param input the buffer to read from
* @param output the array to write in // * @param output the array to write in
*/ // */
protected static void writeFloatArray(ByteBuffer input, float[] output) { // protected static void writeFloatArray(ByteBuffer input, float[] output) {
for (int i = 0; i < output.length; i++) { // for (int i = 0; i < output.length; i++) {
output[i] = input.getFloat(); // output[i] = input.getFloat();
} // }
} // }
} }
...@@ -15,7 +15,7 @@ package org.openhab.binding.openlicht.handler; ...@@ -15,7 +15,7 @@ package org.openhab.binding.openlicht.handler;
import static org.openhab.binding.openlicht.BindingConstants.*; import static org.openhab.binding.openlicht.BindingConstants.*;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.FloatBuffer; import java.nio.DoubleBuffer;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
...@@ -82,21 +82,21 @@ public abstract class AbstractSmartWatchHandler extends AbstractMqttHandler { ...@@ -82,21 +82,21 @@ public abstract class AbstractSmartWatchHandler extends AbstractMqttHandler {
String category = topic.substring(getTopicLength()); String category = topic.substring(getTopicLength());
if (byteBaseMessages) { if (byteBaseMessages) {
ByteBuffer buffer = ByteBuffer.wrap(payload); ByteBuffer buffer = ByteBuffer.wrap(payload);
FloatBuffer floatBuffer; DoubleBuffer doubleBuffer;
switch (category) { switch (category) {
case MQTT_CATEGORY_BRIGHTNESS: case MQTT_CATEGORY_BRIGHTNESS:
updateState(CHANNEL_BRIGHTNESS, new DecimalType(buffer.getFloat())); updateState(CHANNEL_BRIGHTNESS, new DecimalType(buffer.getFloat()));
break; break;
case MQTT_CATEGORY_ACCELERATION: case MQTT_CATEGORY_ACCELERATION:
floatBuffer = buffer.asFloatBuffer(); doubleBuffer = buffer.asDoubleBuffer();
updateState(CHANNEL_ACCELERATION_X, new DecimalType(floatBuffer.get(0))); updateState(CHANNEL_ACCELERATION_X, new DecimalType(doubleBuffer.get(0)));
updateState(CHANNEL_ACCELERATION_Y, new DecimalType(floatBuffer.get(1))); updateState(CHANNEL_ACCELERATION_Y, new DecimalType(doubleBuffer.get(1)));
updateState(CHANNEL_ACCELERATION_Z, new DecimalType(floatBuffer.get(2))); updateState(CHANNEL_ACCELERATION_Z, new DecimalType(doubleBuffer.get(2)));
case MQTT_CATEGORY_ROTATION: case MQTT_CATEGORY_ROTATION:
floatBuffer = buffer.asFloatBuffer(); doubleBuffer = buffer.asDoubleBuffer();
updateState(CHANNEL_ROTATION_X, new DecimalType(floatBuffer.get(0))); updateState(CHANNEL_ROTATION_X, new DecimalType(doubleBuffer.get(0)));
updateState(CHANNEL_ROTATION_Y, new DecimalType(floatBuffer.get(1))); updateState(CHANNEL_ROTATION_Y, new DecimalType(doubleBuffer.get(1)));
updateState(CHANNEL_ROTATION_Z, new DecimalType(floatBuffer.get(2))); updateState(CHANNEL_ROTATION_Z, new DecimalType(doubleBuffer.get(2)));
default: default:
logUnsupportedCategory(category); logUnsupportedCategory(category);
} }
...@@ -104,7 +104,7 @@ public abstract class AbstractSmartWatchHandler extends AbstractMqttHandler { ...@@ -104,7 +104,7 @@ public abstract class AbstractSmartWatchHandler extends AbstractMqttHandler {
String message = new String(payload); String message = new String(payload);
switch (category) { switch (category) {
case MQTT_CATEGORY_BRIGHTNESS: case MQTT_CATEGORY_BRIGHTNESS:
updateState(CHANNEL_BRIGHTNESS, new DecimalType(Float.parseFloat(message))); updateState(CHANNEL_BRIGHTNESS, new DecimalType(Double.parseDouble(message)));
break; break;
case MQTT_CATEGORY_ACCELERATION: case MQTT_CATEGORY_ACCELERATION:
MqttUtils.handleAcceleration(message, this::updateState); MqttUtils.handleAcceleration(message, this::updateState);
......
...@@ -15,7 +15,7 @@ package org.openhab.binding.openlicht.handler; ...@@ -15,7 +15,7 @@ package org.openhab.binding.openlicht.handler;
import static org.openhab.binding.openlicht.BindingConstants.*; import static org.openhab.binding.openlicht.BindingConstants.*;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.FloatBuffer; import java.nio.DoubleBuffer;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
...@@ -46,13 +46,7 @@ public abstract class AbstractSmartphoneHandler extends AbstractMqttHandler { ...@@ -46,13 +46,7 @@ public abstract class AbstractSmartphoneHandler extends AbstractMqttHandler {
@Override @Override
public void handleCommand(ChannelUID channelUID, Command command) { public void handleCommand(ChannelUID channelUID, Command command) {
String channelId = channelUID.getId(); logger.info("Got command for read-only thing: {} {}", channelUID.getAsString(), command.toFullString());
switch (channelId) {
case CHANNEL_BRIGHTNESS:
break;
default:
logger.warn("Unknown channel id {}!", channelId);
}
} }
@Override @Override
...@@ -61,16 +55,16 @@ public abstract class AbstractSmartphoneHandler extends AbstractMqttHandler { ...@@ -61,16 +55,16 @@ public abstract class AbstractSmartphoneHandler extends AbstractMqttHandler {
String category = topic.substring(getTopicLength()); String category = topic.substring(getTopicLength());
if (byteBaseMessages) { if (byteBaseMessages) {
ByteBuffer buffer = ByteBuffer.wrap(payload); ByteBuffer buffer = ByteBuffer.wrap(payload);
FloatBuffer floatBuffer; DoubleBuffer doubleBuffer;
switch (category) { switch (category) {
case MQTT_CATEGORY_BRIGHTNESS: case MQTT_CATEGORY_BRIGHTNESS:
updateState(CHANNEL_BRIGHTNESS, new DecimalType(buffer.getFloat())); updateState(CHANNEL_BRIGHTNESS, new DecimalType(buffer.getFloat()));
break; break;
case MQTT_CATEGORY_ROTATION: case MQTT_CATEGORY_ROTATION:
floatBuffer = buffer.asFloatBuffer(); doubleBuffer = buffer.asDoubleBuffer();
updateState(CHANNEL_ROTATION_X, new DecimalType(floatBuffer.get(0))); updateState(CHANNEL_ROTATION_X, new DecimalType(doubleBuffer.get(0)));
updateState(CHANNEL_ROTATION_Y, new DecimalType(floatBuffer.get(1))); updateState(CHANNEL_ROTATION_Y, new DecimalType(doubleBuffer.get(1)));
updateState(CHANNEL_ROTATION_Z, new DecimalType(floatBuffer.get(2))); updateState(CHANNEL_ROTATION_Z, new DecimalType(doubleBuffer.get(2)));
default: default:
logUnsupportedCategory(category); logUnsupportedCategory(category);
} }
...@@ -78,7 +72,7 @@ public abstract class AbstractSmartphoneHandler extends AbstractMqttHandler { ...@@ -78,7 +72,7 @@ public abstract class AbstractSmartphoneHandler extends AbstractMqttHandler {
String message = new String(payload); String message = new String(payload);
switch (category) { switch (category) {
case MQTT_CATEGORY_BRIGHTNESS: case MQTT_CATEGORY_BRIGHTNESS:
updateState(CHANNEL_BRIGHTNESS, new DecimalType(Float.parseFloat(message))); updateState(CHANNEL_BRIGHTNESS, new DecimalType(Double.parseDouble(message)));
break; break;
case MQTT_CATEGORY_ROTATION: case MQTT_CATEGORY_ROTATION:
MqttUtils.handleRotation(message, this::updateState); MqttUtils.handleRotation(message, this::updateState);
......
...@@ -24,7 +24,7 @@ public class MqttUtils { ...@@ -24,7 +24,7 @@ public class MqttUtils {
for (String tok : tokens) { for (String tok : tokens) {
String[] keyValue = tok.split("="); String[] keyValue = tok.split("=");
String key = keyValue[0].trim(); String key = keyValue[0].trim();
DecimalType value = new DecimalType(Float.parseFloat(keyValue[1].trim())); DecimalType value = new DecimalType(Double.parseDouble(keyValue[1].trim()));
result.put(key, value); result.put(key, value);
} }
return result; return result;
......
...@@ -31,12 +31,6 @@ public class SkyWriterHATHandler extends AbstractMqttHandler { ...@@ -31,12 +31,6 @@ public class SkyWriterHATHandler extends AbstractMqttHandler {
@Override @Override
public void handleCommand(ChannelUID channelUID, Command command) { public void handleCommand(ChannelUID channelUID, Command command) {
logger.info("Got command for read-only thing: {} {}", channelUID.getAsString(), command.toFullString()); logger.info("Got command for read-only thing: {} {}", channelUID.getAsString(), command.toFullString());
// TODO: handle command
// Note: if communication with thing fails for some reason,
// indicate that by setting the status with detail information
// updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
// "Could not control device at IP address x.x.x.x");
} }
@Override @Override
......
...@@ -132,12 +132,12 @@ public class OpenLichtHandlerFactory extends BaseThingHandlerFactory implements ...@@ -132,12 +132,12 @@ public class OpenLichtHandlerFactory extends BaseThingHandlerFactory implements
@Reference(cardinality = ReferenceCardinality.MANDATORY, policy = ReferencePolicy.STATIC) @Reference(cardinality = ReferenceCardinality.MANDATORY, policy = ReferencePolicy.STATIC)
public void setThingRegistry(ThingRegistry thingRegistry) { public void setThingRegistry(ThingRegistry thingRegistry) {
LoggerFactory.getLogger(OpenLichtHandlerFactory.class).info("Setting mqtt service to {}", thingRegistry); LoggerFactory.getLogger(OpenLichtHandlerFactory.class).info("Setting thing registry to {}", thingRegistry);
this.thingRegistry = thingRegistry; this.thingRegistry = thingRegistry;
} }
public void unsetThingRegistry(ThingRegistry service) { public void unsetThingRegistry(ThingRegistry service) {
LoggerFactory.getLogger(OpenLichtHandlerFactory.class).info("Deleting mqtt service {}", service); LoggerFactory.getLogger(OpenLichtHandlerFactory.class).info("Deleting thing registry {}", service);
this.thingRegistry = null; this.thingRegistry = null;
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment