Search in sources :

Example 1 with MqttSenderChannel

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);
                }
            }
        }
    });
}
Also used : MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) MqttTopic(org.eclipse.paho.client.mqttv3.MqttTopic) IMqttDeliveryToken(org.eclipse.paho.client.mqttv3.IMqttDeliveryToken) MqttDeliveryToken(org.eclipse.paho.client.mqttv3.MqttDeliveryToken) MqttSenderChannel(org.openhab.io.transport.mqtt.MqttSenderChannel) MqttException(org.eclipse.paho.client.mqttv3.MqttException)

Example 2 with MqttSenderChannel

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);
}
Also used : ArrayList(java.util.ArrayList) MqttSenderChannel(org.openhab.io.transport.mqtt.MqttSenderChannel)

Aggregations

MqttSenderChannel (org.openhab.io.transport.mqtt.MqttSenderChannel)2 ArrayList (java.util.ArrayList)1 IMqttDeliveryToken (org.eclipse.paho.client.mqttv3.IMqttDeliveryToken)1 MqttDeliveryToken (org.eclipse.paho.client.mqttv3.MqttDeliveryToken)1 MqttException (org.eclipse.paho.client.mqttv3.MqttException)1 MqttMessage (org.eclipse.paho.client.mqttv3.MqttMessage)1 MqttTopic (org.eclipse.paho.client.mqttv3.MqttTopic)1