use of org.openhab.io.transport.mqtt.MqttSenderChannel in project openhab1-addons by openhab.
the class MqttBrokerConnection method startProducer.
/**
* Start a registered producer, so that it can start sending messages.
*
* @param publisher
* to start.
*/
private void startProducer(MqttMessageProducer publisher) {
logger.trace("Starting message producer for broker '{}'", name);
publisher.setSenderChannel(new MqttSenderChannel() {
@Override
public void publish(String topic, byte[] payload) throws Exception {
if (!started) {
logger.warn("Broker connection not started. Cannot publish message to topic '{}'", topic);
return;
}
// Create and configure a message
MqttMessage message = new MqttMessage(payload);
message.setQos(qos);
message.setRetained(retain);
// publish message asynchronously
MqttTopic mqttTopic = client.getTopic(topic);
MqttDeliveryToken deliveryToken = mqttTopic.publish(message);
logger.debug("Publishing message {} to topic '{}'", deliveryToken.getMessageId(), topic);
if (!async) {
// wait for publish confirmation
deliveryToken.waitForCompletion(10000);
if (!deliveryToken.isComplete()) {
logger.error("Did not receive completion message within timeout limit whilst publishing to topic '{}'", topic);
}
}
}
});
}
use of org.openhab.io.transport.mqtt.MqttSenderChannel in project openhab1-addons by openhab.
the class MqttMessagePublisherTest method getPublishedMessage.
private String getPublishedMessage(String configString, String message) throws BindingConfigParseException {
final List<String> sentMessage = new ArrayList<String>();
MqttMessagePublisher publisher = new MqttMessagePublisher(configString);
publisher.setSenderChannel(new MqttSenderChannel() {
@Override
public void publish(String topic, byte[] message) {
sentMessage.add(new String(message));
}
});
publisher.publish(publisher.getTopic(), message.getBytes());
return sentMessage.get(0);
}
Aggregations