From aee5944f4bec7984270f6b6f8b2932f54bb1c14d Mon Sep 17 00:00:00 2001 From: Klaudiusz Staniek Date: Thu, 20 Aug 2015 12:00:41 +0200 Subject: [PATCH] MQTT Action bundle added action to designer and enriched the error message --- .../action/org.openhab.action.mqtt/.classpath | 8 ++ .../action/org.openhab.action.mqtt/.project | 33 ++++++++ .../META-INF/MANIFEST.MF | 28 +++++++ .../OSGI-INF/action.xml | 23 ++++++ .../org.openhab.action.mqtt/build.properties | 6 ++ .../action/org.openhab.action.mqtt/pom.xml | 35 +++++++++ .../openhab/action/mqtt/internal/Mqtt.java | 59 +++++++++++++++ .../mqtt/internal/MqttActionService.java | 75 +++++++++++++++++++ .../action/mqtt/internal/MqttActivator.java | 53 +++++++++++++ .../mqtt/internal/MqttMessagePublisher.java | 43 +++++++++++ .../src/main/resources/readme.txt | 1 + bundles/action/pom.xml | 2 + .../org.openhab.designer.feature/feature.xml | 7 ++ 13 files changed, 373 insertions(+) create mode 100644 bundles/action/org.openhab.action.mqtt/.classpath create mode 100644 bundles/action/org.openhab.action.mqtt/.project create mode 100644 bundles/action/org.openhab.action.mqtt/META-INF/MANIFEST.MF create mode 100644 bundles/action/org.openhab.action.mqtt/OSGI-INF/action.xml create mode 100644 bundles/action/org.openhab.action.mqtt/build.properties create mode 100644 bundles/action/org.openhab.action.mqtt/pom.xml create mode 100644 bundles/action/org.openhab.action.mqtt/src/main/java/org/openhab/action/mqtt/internal/Mqtt.java create mode 100644 bundles/action/org.openhab.action.mqtt/src/main/java/org/openhab/action/mqtt/internal/MqttActionService.java create mode 100644 bundles/action/org.openhab.action.mqtt/src/main/java/org/openhab/action/mqtt/internal/MqttActivator.java create mode 100644 bundles/action/org.openhab.action.mqtt/src/main/java/org/openhab/action/mqtt/internal/MqttMessagePublisher.java create mode 100644 bundles/action/org.openhab.action.mqtt/src/main/resources/readme.txt diff --git a/bundles/action/org.openhab.action.mqtt/.classpath b/bundles/action/org.openhab.action.mqtt/.classpath new file mode 100644 index 00000000000..6d224cd3d0d --- /dev/null +++ b/bundles/action/org.openhab.action.mqtt/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/bundles/action/org.openhab.action.mqtt/.project b/bundles/action/org.openhab.action.mqtt/.project new file mode 100644 index 00000000000..969a17b6cd2 --- /dev/null +++ b/bundles/action/org.openhab.action.mqtt/.project @@ -0,0 +1,33 @@ + + + org.openhab.action.mqtt + This is the ${binding-name} binding of the open Home Automation Bus (openHAB) + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.pde.ManifestBuilder + + + + + org.eclipse.pde.SchemaBuilder + + + + + org.eclipse.pde.ds.core.builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.pde.PluginNature + + diff --git a/bundles/action/org.openhab.action.mqtt/META-INF/MANIFEST.MF b/bundles/action/org.openhab.action.mqtt/META-INF/MANIFEST.MF new file mode 100644 index 00000000000..457a88f8ec7 --- /dev/null +++ b/bundles/action/org.openhab.action.mqtt/META-INF/MANIFEST.MF @@ -0,0 +1,28 @@ +Manifest-Version: 1.0 +Private-Package: org.openhab.action.mqtt.internal +Ignore-Package: org.openhab.action.mqtt.internal +Bundle-License: http://www.eclipse.org/legal/epl-v10.html +Bundle-Name: openHAB Mqtt Action +Bundle-SymbolicName: org.openhab.action.mqtt +Bundle-Vendor: openHAB.org +Bundle-Version: 1.8.0.qualifier +Bundle-Activator: org.openhab.action.mqtt.internal.MqttActivator +Bundle-ManifestVersion: 2 +Bundle-Description: This is the Mqtt action of the open Home Aut + omation Bus (openHAB) +Import-Package: org.apache.commons.lang, + org.openhab.core.items, + org.openhab.core.library.items, + org.openhab.core.library.types, + org.openhab.core.scriptengine.action, + org.openhab.core.types, + org.openhab.io.transport.mqtt, + org.osgi.framework, + org.osgi.service.cm, + org.osgi.service.component, + org.slf4j +Bundle-DocURL: http://www.openhab.org +Bundle-RequiredExecutionEnvironment: J2SE-1.5 +Service-Component: OSGI-INF/action.xml +Bundle-ClassPath: . +Bundle-ActivationPolicy: lazy diff --git a/bundles/action/org.openhab.action.mqtt/OSGI-INF/action.xml b/bundles/action/org.openhab.action.mqtt/OSGI-INF/action.xml new file mode 100644 index 00000000000..ea07938c644 --- /dev/null +++ b/bundles/action/org.openhab.action.mqtt/OSGI-INF/action.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + diff --git a/bundles/action/org.openhab.action.mqtt/build.properties b/bundles/action/org.openhab.action.mqtt/build.properties new file mode 100644 index 00000000000..30f33af0ad2 --- /dev/null +++ b/bundles/action/org.openhab.action.mqtt/build.properties @@ -0,0 +1,6 @@ +source.. = src/main/java/,\ + src/main/resources/ +bin.includes = META-INF/,\ + .,\ + OSGI-INF/ +output.. = target/classes/ diff --git a/bundles/action/org.openhab.action.mqtt/pom.xml b/bundles/action/org.openhab.action.mqtt/pom.xml new file mode 100644 index 00000000000..08912b55d63 --- /dev/null +++ b/bundles/action/org.openhab.action.mqtt/pom.xml @@ -0,0 +1,35 @@ + + + + + org.openhab.bundles + action + 1.8.0-SNAPSHOT + + + + org.openhab.action.mqtt + org.openhab.action.mqtt + openhab-addon-action-mqtt + ${project.name} + + + 4.0.0 + org.openhab.action + org.openhab.action.mqtt + + openHAB MQTT Action + + eclipse-plugin + + + + + org.vafer + jdeb + + + + + diff --git a/bundles/action/org.openhab.action.mqtt/src/main/java/org/openhab/action/mqtt/internal/Mqtt.java b/bundles/action/org.openhab.action.mqtt/src/main/java/org/openhab/action/mqtt/internal/Mqtt.java new file mode 100644 index 00000000000..e67cabeb348 --- /dev/null +++ b/bundles/action/org.openhab.action.mqtt/src/main/java/org/openhab/action/mqtt/internal/Mqtt.java @@ -0,0 +1,59 @@ +/** + * Copyright (c) 2010-2015, openHAB.org and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + */ +package org.openhab.action.mqtt.internal; + +import org.openhab.action.mqtt.internal.MqttMessagePublisher; +import org.openhab.core.scriptengine.action.ActionDoc; +import org.openhab.core.scriptengine.action.ParamDoc; + +import java.util.concurrent.ConcurrentHashMap; + +import org.openhab.io.transport.mqtt.MqttService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * This class contains the methods that are made available in scripts and rules for MQTT Action. + * + * @author Klaudiusz Staniek + * @since 1.8.0 + */ +public class Mqtt { + + private static final Logger logger = LoggerFactory.getLogger(Mqtt.class); + public static MqttService mqttTransportService; + private static ConcurrentHashMap messagePublishers = new ConcurrentHashMap(); + + + @ActionDoc(text = "Publishes the message to topic using specified MQTT broker") + public static boolean publish( + @ParamDoc(name = "brokerName", text = "The name of the MQTT broker defined in configuration") String brokerName, + @ParamDoc(name = "topic", text = "The MQTT topic") String topic, + @ParamDoc(name = "message", text = "The message to be published") String message) { + + if (!MqttActionService.isProperlyConfigured) { + logger.debug("Mqtt action is not yet configured - execution aborted!"); + return false; + } + logger.debug("Message to broker: {}, topic: {}", brokerName, topic); + + MqttMessagePublisher publisher = messagePublishers.get(brokerName); + if (publisher == null) { + publisher = new MqttMessagePublisher(); + messagePublishers.put(brokerName, publisher); + mqttTransportService.registerMessageProducer(brokerName, publisher); + } + + publisher.publish(topic, message); + + return true; + } + +} diff --git a/bundles/action/org.openhab.action.mqtt/src/main/java/org/openhab/action/mqtt/internal/MqttActionService.java b/bundles/action/org.openhab.action.mqtt/src/main/java/org/openhab/action/mqtt/internal/MqttActionService.java new file mode 100644 index 00000000000..1acd24c0dbe --- /dev/null +++ b/bundles/action/org.openhab.action.mqtt/src/main/java/org/openhab/action/mqtt/internal/MqttActionService.java @@ -0,0 +1,75 @@ +/** + * Copyright (c) 2010-2015, openHAB.org and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + */ +package org.openhab.action.mqtt.internal; + +import java.util.Dictionary; + +import org.openhab.core.scriptengine.action.ActionService; +import org.openhab.io.transport.mqtt.MqttService; +import org.osgi.service.cm.ConfigurationException; +import org.osgi.service.cm.ManagedService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * This class registers an OSGi service for the MQTT Action. + * + * @author Klaudiusz Staniek + * @since 1.8.0 + */ +public class MqttActionService implements ActionService, ManagedService { + + private static final Logger logger = LoggerFactory.getLogger(MqttActionService.class); + + /** + * Indicates whether this action is properly configured which means all + * necessary configurations are set. This flag can be checked by the + * action methods before executing code. + */ + /* default */ static boolean isProperlyConfigured = true; + + public MqttActionService() { + } + + public void activate() { + } + + public void deactivate() { + // deallocate Resources here that are no longer needed and + // should be reset when activating this binding again + } + + @Override + public String getActionClassName() { + return Mqtt.class.getCanonicalName(); + } + + @Override + public Class getActionClass() { + return Mqtt.class; + } + + /** + * @{inheritDoc} + */ + @Override + public void updated(Dictionary config) throws ConfigurationException { + isProperlyConfigured = true; + } + + public void setMqttTransportService(MqttService mQTTService) { + Mqtt.mqttTransportService = mQTTService; + } + + public void unsetMqttTransportService(MqttService mQTTService) { + Mqtt.mqttTransportService = null; + } + +} diff --git a/bundles/action/org.openhab.action.mqtt/src/main/java/org/openhab/action/mqtt/internal/MqttActivator.java b/bundles/action/org.openhab.action.mqtt/src/main/java/org/openhab/action/mqtt/internal/MqttActivator.java new file mode 100644 index 00000000000..49412951c30 --- /dev/null +++ b/bundles/action/org.openhab.action.mqtt/src/main/java/org/openhab/action/mqtt/internal/MqttActivator.java @@ -0,0 +1,53 @@ +/** + * Copyright (c) 2010-2015, openHAB.org and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + */ +package org.openhab.action.mqtt.internal; + +import org.osgi.framework.BundleActivator; +import org.osgi.framework.BundleContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * Extension of the default OSGi bundle activator + * + * @author Klaudiusz Staniek + * @since 1.8.0 + */ +public final class MqttActivator implements BundleActivator { + + private static Logger logger = LoggerFactory.getLogger(MqttActivator.class); + + private static BundleContext context; + + /** + * Called whenever the OSGi framework starts our bundle + */ + public void start(BundleContext bc) throws Exception { + context = bc; + logger.debug("Mqtt action has been started."); + } + + /** + * Called whenever the OSGi framework stops our bundle + */ + public void stop(BundleContext bc) throws Exception { + context = null; + logger.debug("Mqtt action has been stopped."); + } + + /** + * Returns the bundle context of this bundle + * @return the bundle context + */ + public static BundleContext getContext() { + return context; + } + +} diff --git a/bundles/action/org.openhab.action.mqtt/src/main/java/org/openhab/action/mqtt/internal/MqttMessagePublisher.java b/bundles/action/org.openhab.action.mqtt/src/main/java/org/openhab/action/mqtt/internal/MqttMessagePublisher.java new file mode 100644 index 00000000000..395dc1fbe9e --- /dev/null +++ b/bundles/action/org.openhab.action.mqtt/src/main/java/org/openhab/action/mqtt/internal/MqttMessagePublisher.java @@ -0,0 +1,43 @@ +/** + * Copyright (c) 2010-2015, openHAB.org and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + */ +package org.openhab.action.mqtt.internal; + +import org.openhab.io.transport.mqtt.MqttMessageProducer; +import org.openhab.io.transport.mqtt.MqttSenderChannel; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This class implementing Message Producer from MQTT IO service. + * + * @author Klaudiusz Staniek + * @since 1.8.0 + */ +public class MqttMessagePublisher implements MqttMessageProducer { + + private static final Logger logger = LoggerFactory.getLogger(MqttMessagePublisher.class); + + private MqttSenderChannel senderChannel; + + public void publish(String topic, String message) { + if (senderChannel == null) { + return; + } + try { + senderChannel.publish(topic, message.getBytes()); + } catch (Exception e) { + logger.error("Error publishing message: {}", e.getMessage()); + } + } + + @Override + public void setSenderChannel(MqttSenderChannel channel) { + senderChannel = channel; + } +} diff --git a/bundles/action/org.openhab.action.mqtt/src/main/resources/readme.txt b/bundles/action/org.openhab.action.mqtt/src/main/resources/readme.txt new file mode 100644 index 00000000000..98698c670dc --- /dev/null +++ b/bundles/action/org.openhab.action.mqtt/src/main/resources/readme.txt @@ -0,0 +1 @@ +Bundle resources go in here! \ No newline at end of file diff --git a/bundles/action/pom.xml b/bundles/action/pom.xml index afca1a84c6d..ad009899199 100644 --- a/bundles/action/pom.xml +++ b/bundles/action/pom.xml @@ -32,6 +32,8 @@ org.openhab.action.astro org.openhab.action.tinkerforge org.openhab.action.harmonyhub + org.openhab.action.mqtt + diff --git a/features/org.openhab.designer.feature/feature.xml b/features/org.openhab.designer.feature/feature.xml index 6f0ffa6c247..07fc1dd9b9a 100644 --- a/features/org.openhab.designer.feature/feature.xml +++ b/features/org.openhab.designer.feature/feature.xml @@ -404,6 +404,13 @@ version="0.0.0" unpack="false"/> + +