Skip to content

Commit

Permalink
refactoring of mqtt-client plugin - updated eclipse paho lib to 1.1.1…
Browse files Browse the repository at this point in the history
… version
  • Loading branch information
mcicolella committed Aug 8, 2017
1 parent 2406f1a commit 33e0f23
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 72 deletions.
10 changes: 2 additions & 8 deletions plugins/devices/mqtt-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,6 @@
<freedomotic.marketplace.username></freedomotic.marketplace.username>
<freedomotic.marketplace.password></freedomotic.marketplace.password>
</properties>
<repositories>
<repository>
<id>Eclipse Paho Repo</id>
<url>https://repo.eclipse.org/content/repositories/paho-releases/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
Expand All @@ -65,8 +59,8 @@
</dependency>
<dependency>
<groupId>org.eclipse.paho</groupId>
<artifactId>mqtt-client</artifactId>
<version>0.4.0</version>
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,27 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.freedomotic.events.ProtocolRead;
import java.util.logging.Level;

/**
* @author Mauro Cicolella
*/
public class Mqtt implements MqttCallback {

private static final Logger LOG = LoggerFactory.getLogger(Mqtt.class.getName());

MqttClient4FD pluginRef = null;
MqttClient myClient;
MqttConnectOptions connectionOptions;
MemoryPersistence persistence = new MemoryPersistence();

Mqtt(MqttClient4FD pluginRef) {
this.pluginRef = pluginRef;
}

/**
*
* startClient Class entry point
* Class entry point
*
* @param brokerUrl
* @param clientID
Expand All @@ -62,9 +63,9 @@ public class Mqtt implements MqttCallback {
*
*/
public boolean startClient(String brokerUrl, String clientID, String setCleanSession, Integer setKeepAliveInterval, String authenticationEnabled, String username, String password) {

MqttConnectOptions connectionOptions = new MqttConnectOptions();

connectionOptions.setCleanSession(Boolean.parseBoolean(setCleanSession));
connectionOptions.setKeepAliveInterval(setKeepAliveInterval);
// authentication requires username and password
Expand Down Expand Up @@ -92,9 +93,9 @@ public boolean startClient(String brokerUrl, String clientID, String setCleanSes
public void subscribeTopic(String topic) {
try {
myClient.subscribe(topic, 0);
LOG.info("Subscribed topic '{}'",topic);
LOG.info("Subscribed topic \"{}\"", topic);
} catch (MqttException ex) {
LOG.error("Unable to subscribe topic '{}' for {}", topic, ex);
LOG.error("Unable to subscribe topic \"{}\" for {}", topic, ex);
}
}

Expand All @@ -106,14 +107,14 @@ public void subscribeTopic(String topic) {
* @param pubQoS
*/
public void publish(String topic, String message, int subQoS, int pubQoS) {

MqttMessage messageToPublish = new MqttMessage(message.getBytes());
messageToPublish.setQos(pubQoS);
messageToPublish.setRetained(false);
try {
myClient.publish(topic, messageToPublish);
} catch (MqttException ex) {
LOG.error("Unable to publish message: '" + message + "' to " + topic + " for " + ex.getMessage());
LOG.error("Unable to publish message: \"{}\" to \"{}\"", message, topic);
}
}

Expand All @@ -139,8 +140,8 @@ public void messageArrived(String topic, MqttMessage message) throws Exception {
String protocol = "mqtt-client";
String address = topic;
String payload = new String(message.getPayload());
LOG.info("Received message '{}' on topic '{}'", payload, topic);

LOG.info("Received message \"{}\" on topic \"{}\"", payload, topic);
// create and notify a Freedomotic event
// the object address is equal to "topic"
ProtocolRead event = new ProtocolRead(this, protocol, topic);
Expand All @@ -155,19 +156,24 @@ public void messageArrived(String topic, MqttMessage message) throws Exception {
*/
@Override
public void deliveryComplete(IMqttDeliveryToken imdt) {
LOG.info("Message published");
try {
LOG.info("Message \"{}\" published on \"{}\" topic", imdt.getMessage().toString(), imdt.getTopics());
} catch (MqttException ex) {
LOG.error(ex.getMessage());
}
}

/**
*
* This callback is invoked upon loosing the MQTT connection.
* The client tries to reconnect.
*
* @param cause
*/
@Override
public void connectionLost(Throwable cause) {
LOG.error("Connection to Mqtt broker lost for {}", cause.getCause());
LOG.error("Reconnecting in progress ...");
LOG.info("Reconnecting in progress ...");
while (!myClient.isConnected()) {
try {
myClient.connect(connectionOptions);
Expand All @@ -176,6 +182,6 @@ public void connectionLost(Throwable cause) {
}
// set a delay before retrying
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@
package com.freedomotic.plugins.devices.mqttclient;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.freedomotic.api.EventTemplate;
import com.freedomotic.api.Protocol;
import com.freedomotic.exceptions.PluginStartupException;
Expand All @@ -37,18 +35,17 @@ public class MqttClient4FD extends Protocol {

private static final Logger LOG = LoggerFactory.getLogger(MqttClient4FD.class.getName());

private String BROKER_URL = configuration.getStringProperty("broker-url", "tcp://test.mosquitto.org:1883");
private String CLIENT_ID = configuration.getStringProperty("client-id", "freedomotic");
private String AUTHENTICATION_ENABLED = configuration.getStringProperty("authentication-enabled", "false");
private String USERNAME = configuration.getStringProperty("username", "admin");
private String PASSWORD = configuration.getStringProperty("password", "admin");
private String SET_CLEAN_SESSION = configuration.getStringProperty("set-clean-session", "true");
private Integer SET_KEEP_ALIVE_INTERVAL = configuration.getIntProperty("set-keep-alive-interval", 600);
private final String BROKER_URL = configuration.getStringProperty("broker-url", "tcp://test.mosquitto.org:1883");
private final String CLIENT_ID = configuration.getStringProperty("client-id", "freedomotic");
private final String AUTHENTICATION_ENABLED = configuration.getStringProperty("authentication-enabled", "false");
private final String USERNAME = configuration.getStringProperty("username", "admin");
private final String PASSWORD = configuration.getStringProperty("password", "admin");
private final String SET_CLEAN_SESSION = configuration.getStringProperty("set-clean-session", "true");
private final Integer SET_KEEP_ALIVE_INTERVAL = configuration.getIntProperty("set-keep-alive-interval", 600);
private Boolean connected = false;
private Mqtt mqttClient = null;

public MqttClient4FD() {
//every plugin needs a name and a manifest XML file
super("MQTT Client", "/mqtt-client/mqtt-client-manifest.xml");
setPollingWait(-1); //onRun() disabled
}
Expand Down Expand Up @@ -87,7 +84,7 @@ protected void onStop() {
mqttClient.disconnect();
setDescription("Mqtt Client disconnected");
}
LOG.info("Mqtt Client stopped");
LOG.info("Mqtt Client plugin stopped");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<timeout>0</timeout>
<properties>
<properties>
<property name="mqtt.topic" value="testtopic/1"/>
<property name="mqtt.topic" value="@owner.object.address"/>
<property name="mqtt.message" value="message to send"/>
<property name="mqtt.sub-qos" value="0"/>
<property name="mqtt.pub-qos" value="0"/>
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<trigger>
<name>Mqtt client reads a state change</name>
<description>Mqtt client reads relay state change</description>
<name>Mqtt client reads raw data</name>
<description>Mqtt client reads raw data</description>
<channel>app.event.sensor.protocol.read.mqtt-client</channel>
<payload>
<payload>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<trigger>
<name>Mqtt client reads a temperature</name>
<description>Mqtt client reads a temperature</description>
<channel>app.event.sensor.protocol.read.mqtt-client</channel>
<payload>
<payload>
<statement>
<logical>SET</logical>
<attribute>behaviorValue</attribute>
<operand>EQUALS</operand>
<value>= behaviorValue=Math.floor(@event.mqtt.message*10);</value>
</statement>
</payload>
</payload>
<hardwareLevel>true</hardwareLevel>
<delay>0</delay>
<priority>0</priority>
<maxExecutions>-1</maxExecutions>
<numberOfExecutions>0</numberOfExecutions>
<suspensionTime>0</suspensionTime>
</trigger>
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
<property name="description" value="A simple client for MQTT protocol"/>
<property name="category" value="protocol"/>
<property name="short-name" value="mqtt-client"/>
<property name="protocol.name" value="mqtt-client"/>
<property name="startup-time" value="on load"/>
<property name="broker-url" value="tcp://test.mosquitto.org:1883"/>
<property name="protocol.name"value="mqtt-client"/>
<property name="startup-time" value="on load"/>
<property name="broker-url" value="tcp://broker.hivemq.com:1883"/>
<property name="client-id" value="freedomotic"/>
<property name="set-clean-session" value="false"/>
<property name="set-keep-alive-interval" value="600"/>
Expand Down

0 comments on commit 33e0f23

Please sign in to comment.