Add possibility to set qos and retained state of MQTT messages

This commit is contained in:
fgather
2017-05-30 21:49:31 +02:00
parent fb9cc64839
commit a55c53299a
5 changed files with 43 additions and 21 deletions

View File

@@ -13,11 +13,12 @@ import org.slf4j.LoggerFactory;
import com.bwssystems.HABridge.NamedIP;
import java.util.Optional;
public class MQTTHandler {
private static final Logger log = LoggerFactory.getLogger(MQTTHandler.class);
private NamedIP myConfig;
private MqttClient myClient;
private int qos = 1;
public MQTTHandler(NamedIP aConfig) {
super();
@@ -40,20 +41,19 @@ public class MQTTHandler {
}
try {
myClient.connect(connOpts);
} catch (MqttSecurityException e) {
log.error("Could not connect MQTT client for name: " + myConfig.getName() + " and ip: " + myConfig.getIp() + " with message: " + e.getMessage());
} catch (MqttException e) {
log.error("Could not connect MQTT client for name: " + myConfig.getName() + " and ip: " + myConfig.getIp() + " with message: " + e.getMessage());
}
}
public void publishMessage(String topic, String content) {
public void publishMessage(String topic, String content, Integer qos, Boolean retain) {
MqttMessage message = new MqttMessage(StringEscapeUtils.unescapeJava(content).getBytes());
message.setQos(qos);
message.setQos(Optional.ofNullable(qos).orElse(1));
message.setRetained(Optional.ofNullable(retain).orElse(false));
try {
myClient.publish(topic, message);
} catch (MqttPersistenceException e) {
log.error("Could not publish to MQTT client for name: " + myConfig.getName() + " and ip: " + myConfig.getIp() + " with message: " + e.getMessage());
} catch (MqttException e) {
log.error("Could not publish to MQTT client for name: " + myConfig.getName() + " and ip: " + myConfig.getIp() + " with message: " + e.getMessage());
}

View File

@@ -37,9 +37,7 @@ public class MQTTHome implements Home {
return;
log.debug("Shutting down MQTT handlers.");
if(handlers != null && !handlers.isEmpty()) {
Iterator<String> keys = handlers.keySet().iterator();
while(keys.hasNext()) {
String key = keys.next();
for (String key : handlers.keySet()) {
handlers.get(key).shutdown();
}
}
@@ -108,7 +106,7 @@ public class MQTTHome implements Home {
if (mqttHandler == null) {
log.warn("Should not get here, no mqtt hanlder available");
} else {
mqttHandler.publishMessage(mqttMessages[y].getTopic(), mqttMessages[y].getMessage());
mqttHandler.publishMessage(mqttMessages[y].getTopic(), mqttMessages[y].getMessage(), mqttMessages[y].getQos(), mqttMessages[y].getRetain());
}
}
}
@@ -130,13 +128,10 @@ public class MQTTHome implements Home {
aGsonHandler =
new GsonBuilder()
.create();
handlers = new HashMap<String, MQTTHandler>();
Iterator<NamedIP> theList = bridgeSettings.getBridgeSettingsDescriptor().getMqttaddress().getDevices().iterator();
while(theList.hasNext()) {
NamedIP aClientConfig = theList.next();
handlers = new HashMap<>();
for (NamedIP aClientConfig : bridgeSettings.getBridgeSettingsDescriptor().getMqttaddress().getDevices()) {
MQTTHandler aHandler = new MQTTHandler(aClientConfig);
if(aHandler != null)
handlers.put(aClientConfig.getName(), aHandler);
handlers.put(aClientConfig.getName(), aHandler);
}
}
return this;

View File

@@ -6,6 +6,8 @@ public class MQTTMessage {
private String message;
private Integer delay;
private Integer count;
private Integer qos;
private Boolean retain;
public String getClientId() {
return clientId;
}
@@ -36,4 +38,19 @@ public class MQTTMessage {
public void setCount(Integer count) {
this.count = count;
}
public Integer getQos() {
return qos;
}
public void setQos(Integer qos) {
this.qos = qos;
}
public Boolean getRetain() {
return retain;
}
public void setRetain(Boolean retain) {
this.retain = retain;
}
}