Skip to content

Commit

Permalink
Merge pull request #7 from DerTomm/development
Browse files Browse the repository at this point in the history
Merge development changes into master branch for 1.0.2 release
  • Loading branch information
DerTomm authored Jan 13, 2018
2 parents f0cdb33 + 36c1f83 commit faf3c11
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 5 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ The file contains the following parameters:
<td>serialgateway-in/#</td>
<td>This is the topic the bridge subscribes for messages. Wildcards '#' and '+' are supported.</td>
</tr>
<tr>
<td>mqtt.qosSubscribe</td>
<td>0</td>
<td>QoS which should be used for subscriptions.</td>
</tr>
<tr>
<td>mqtt.qosPublish</td>
<td>0</td>
<td>QoS which should be used for publications.</td>
</tr>
<tr>
<td colspan="3"><b>Logging properties</b></td>
</tr>
Expand Down
5 changes: 2 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>de.reitho</groupId>
<artifactId>serialMqttBridge</artifactId>
<version>1.0.1</version>
<version>1.0.2</version>
<name>SerialMqttBridge</name>

<properties>
Expand Down Expand Up @@ -95,10 +95,9 @@
<dependency>
<groupId>org.eclipse.paho</groupId>
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
<version>1.1.0</version>
<version>1.2.0</version>
</dependency>


</dependencies>

</project>
18 changes: 18 additions & 0 deletions src/main/java/de/reitho/serialMqttBridge/config/ConfigHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class ConfigHandler {
private String mqttClientId;
private String mqttTopicPublish;
private String mqttTopicSubscribe;
private int mqttQosSubscribe;
private int mqttQosPublish;

/* Logging properties */
private boolean logSerialInbound;
Expand Down Expand Up @@ -89,6 +91,8 @@ private void readConfigurationFile() throws Exception {
mqttClientId = prop.getProperty("mqtt.clientId").trim();
mqttTopicPublish = prop.getProperty("mqtt.topicPublish").trim();
mqttTopicSubscribe = prop.getProperty("mqtt.topicSubscribe").trim();
mqttQosSubscribe = Integer.parseInt(prop.getProperty("mqtt.qosSubscribe", "0"));
mqttQosPublish = Integer.parseInt(prop.getProperty("mqtt.qosPublish", "0"));

/* Parse logging properties */
logSerialInbound = Boolean.parseBoolean(prop.getProperty("logging.serialInbound").trim());
Expand Down Expand Up @@ -231,4 +235,18 @@ public String getMqttTopicPublish() {
return mqttTopicPublish;
}

/*********************************************************************************************************************************************************************
* @return
*/
public int getMqttQosSubscribe() {
return mqttQosSubscribe;
}

/*********************************************************************************************************************************************************************
* @return
*/
public int getMqttQosPublish() {
return mqttQosPublish;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private void connectAndSubscribe() throws Exception {
mqttClient.connect(connOpts);

// Subscribe to defined inbound topic
mqttClient.subscribe(configHandler.getMqttTopicSubscribe());
mqttClient.subscribe(configHandler.getMqttTopicSubscribe(), configHandler.getMqttQosSubscribe());
}

/*********************************************************************************************************************************************************************
Expand All @@ -74,7 +74,7 @@ private void connectAndSubscribe() throws Exception {
public void publishMessage(String messagePath, String message) {

MqttMessage mqttMessage = new MqttMessage(message.getBytes());
mqttMessage.setQos(2);
mqttMessage.setQos(serialMqttBridge.getConfigHandler().getMqttQosPublish());

String configuredTopicPublish = serialMqttBridge.getConfigHandler().getMqttTopicPublish();
String mqttPublishTopic = configuredTopicPublish + "/" + messagePath;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package de.reitho.serialMqttBridge.plugins;

public class MySensorsCustomSerialMqttTimestampAdapter implements MqttPublishPreprocessingPlugin {

private String mqttTopicPath;
private String mqttPublishMessage;

/* ********************************************************************************************************************************************************************
* (non-Javadoc)
* @see de.reitho.serialMqttBridge.plugins.MqttPreprocessingPlugin#getPluginName()
*/
@Override
public String getPluginName() {
return "MySensors Serial to MQTT adapter plugin with timestamp addition";
}

/* ********************************************************************************************************************************************************************
* (non-Javadoc)
* @see de.reitho.serialMqttBridge.plugins.AbstractPreprocessorPlugin#getPluginDescription()
*/
@Override
public String getPluginDescription() {
return "This plugin converts converts MySensors serial message format into topic and message for MQTT publishing (adding timestamp to payload).";
}

/* ********************************************************************************************************************************************************************
* (non-Javadoc)
* @see de.reitho.serialMqttBridge.plugins.AbstractPreprocessorPlugin#processMessage(java.lang.String)
*/
@Override
public boolean processSerialMessage(String message) throws Exception {

if (message.length() == 0 || !message.contains(";")) {
return false;
}

mqttTopicPath = "";
mqttPublishMessage = "";

String[] topicTokens = message.split(";");
for (int i = 0; i < topicTokens.length - 1; i++) {
if (i > 0) {
mqttTopicPath += "/";
}
mqttTopicPath += topicTokens[i];
}

mqttPublishMessage = topicTokens[topicTokens.length - 1];

// If message is of type V_TRIPPED (16): add timestamp to payload
if (mqttTopicPath.endsWith("/16")) {
mqttPublishMessage += ";" + System.currentTimeMillis();
}

return true;
}

/* ********************************************************************************************************************************************************************
* (non-Javadoc)
* @see de.reitho.serialMqttBridge.plugins.AbstractMqttPreprocessorPlugin#getPublishTopic()
*/
@Override
public String getPublishTopic() {
return mqttTopicPath;
}

/* ********************************************************************************************************************************************************************
* (non-Javadoc)
* @see de.reitho.serialMqttBridge.plugins.AbstractMqttPreprocessorPlugin#getPublishMessage()
*/
@Override
public String getPublishMessage() {
return mqttPublishMessage;
}
}
2 changes: 2 additions & 0 deletions src/main/resources/config.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ mqtt.brokerPassword=0
mqtt.clientId=SerialMqttBridge-42
mqtt.topicPublish=topic-out
mqtt.topicSubscribe=topic-in/#
mqtt.qosSubscribe=0
mqtt.qosPublish=0

## Logging properties ##
logging.serialInbound=false
Expand Down

0 comments on commit faf3c11

Please sign in to comment.