Search in sources :

Example 1 with AbstractBrokerHandler

use of org.eclipse.smarthome.binding.mqtt.handler.AbstractBrokerHandler in project smarthome by eclipse.

the class TopicSubscribeMultiConnection method add.

/**
 * Add thing if it is a bridge and has a handler that inherits from {@link AbstractBrokerHandler}.
 */
public void add(AbstractBrokerHandler handler) {
    final ThingUID bridgeUid = handler.getThing().getUID();
    handler.getConnectionAsync().thenAccept(connection -> {
        final TopicSubscribe o = new TopicSubscribe(connection, topic, messageReceivedListener, bridgeUid);
        observedBrokerHandlers.put(bridgeUid, o);
        o.start().exceptionally(e -> {
            logger.warn("Failed to MQTT subscribe for {} on topic {}", bridgeUid, topic);
            return false;
        }).thenRun(() -> {
            logger.trace("Found suitable bridge {} for listing to topic {}", bridgeUid, topic);
        });
    });
}
Also used : AbstractBrokerHandler(org.eclipse.smarthome.binding.mqtt.handler.AbstractBrokerHandler) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) Logger(org.slf4j.Logger) Map(java.util.Map) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) MQTTTopicDiscoveryParticipant(org.eclipse.smarthome.binding.mqtt.discovery.MQTTTopicDiscoveryParticipant) TopicSubscribe(org.eclipse.smarthome.binding.mqtt.discovery.TopicSubscribe) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) TopicSubscribe(org.eclipse.smarthome.binding.mqtt.discovery.TopicSubscribe)

Example 2 with AbstractBrokerHandler

use of org.eclipse.smarthome.binding.mqtt.handler.AbstractBrokerHandler in project smarthome by eclipse.

the class AbstractMQTTThingHandler method bridgeStatusChanged.

@Override
public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
    if (bridgeStatusInfo.getStatus() == ThingStatus.OFFLINE) {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
        stop();
        connection = null;
        return;
    }
    if (bridgeStatusInfo.getStatus() != ThingStatus.ONLINE) {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
        stop();
        return;
    }
    AbstractBrokerHandler h = getBridgeHandler();
    if (h == null) {
        logger.warn("Bridge handler not found!");
        return;
    }
    final MqttBrokerConnection connection;
    try {
        connection = h.getConnectionAsync().get(500, TimeUnit.MILLISECONDS);
    } catch (InterruptedException | ExecutionException | TimeoutException ignored) {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED, "Bridge handler has no valid broker connection!");
        return;
    }
    this.connection = connection;
    // class.
    try {
        start(connection).thenApply(e -> true).exceptionally(e -> {
            updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getLocalizedMessage());
            return null;
        }).get(subscribeTimeout, TimeUnit.MILLISECONDS);
    } catch (InterruptedException | ExecutionException | TimeoutException ignored) {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Did not receive all required topics");
    }
}
Also used : NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) Logger(org.slf4j.Logger) MqttBrokerConnection(org.eclipse.smarthome.io.transport.mqtt.MqttBrokerConnection) Bridge(org.eclipse.smarthome.core.thing.Bridge) RefreshType(org.eclipse.smarthome.core.types.RefreshType) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) UnDefType(org.eclipse.smarthome.core.types.UnDefType) CompletableFuture(java.util.concurrent.CompletableFuture) ThingStatusInfo(org.eclipse.smarthome.core.thing.ThingStatusInfo) ChannelState(org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelState) BaseThingHandler(org.eclipse.smarthome.core.thing.binding.BaseThingHandler) Command(org.eclipse.smarthome.core.types.Command) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) AbstractBrokerHandler(org.eclipse.smarthome.binding.mqtt.handler.AbstractBrokerHandler) ChannelUID(org.eclipse.smarthome.core.thing.ChannelUID) Nullable(org.eclipse.jdt.annotation.Nullable) ThingStatusDetail(org.eclipse.smarthome.core.thing.ThingStatusDetail) Thing(org.eclipse.smarthome.core.thing.Thing) ChannelStateUpdateListener(org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelStateUpdateListener) State(org.eclipse.smarthome.core.types.State) ThingStatus(org.eclipse.smarthome.core.thing.ThingStatus) MqttBrokerConnection(org.eclipse.smarthome.io.transport.mqtt.MqttBrokerConnection) AbstractBrokerHandler(org.eclipse.smarthome.binding.mqtt.handler.AbstractBrokerHandler) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 3 with AbstractBrokerHandler

use of org.eclipse.smarthome.binding.mqtt.handler.AbstractBrokerHandler in project smarthome by eclipse.

the class MQTTActions method publishMQTT.

@RuleAction(label = "@text/actionLabel", description = "@text/actionDesc")
public void publishMQTT(@ActionInput(name = "topic", label = "@text/actionInputTopicLabel", description = "@text/actionInputTopicDesc") @Nullable String topic, @ActionInput(name = "value", label = "@text/actionInputValueLabel", description = "@text/actionInputValueDesc") @Nullable String value) {
    AbstractBrokerHandler brokerHandler = handler;
    if (brokerHandler == null) {
        logger.warn("MQTT Action service ThingHandler is null!");
        return;
    }
    MqttBrokerConnection connection = brokerHandler.getConnection();
    if (connection == null) {
        logger.warn("MQTT Action service ThingHandler connection is null!");
        return;
    }
    if (value == null) {
        logger.debug("skipping MQTT publishing to topic '{}' due to null value.", topic);
        return;
    }
    if (topic == null) {
        logger.debug("skipping MQTT publishing of value '{}' as topic is null.", value);
        return;
    }
    connection.publish(topic, value.getBytes()).thenRun(() -> {
        logger.debug("MQTT publish to {} performed", topic);
    }).exceptionally(e -> {
        logger.warn("MQTT publish to {} failed!", topic);
        return null;
    });
}
Also used : MqttBrokerConnection(org.eclipse.smarthome.io.transport.mqtt.MqttBrokerConnection) AbstractBrokerHandler(org.eclipse.smarthome.binding.mqtt.handler.AbstractBrokerHandler) RuleAction(org.eclipse.smarthome.automation.annotation.RuleAction)

Example 4 with AbstractBrokerHandler

use of org.eclipse.smarthome.binding.mqtt.handler.AbstractBrokerHandler in project smarthome by eclipse.

the class MqttBrokerHandlerFactory method createHandler.

@Override
@Nullable
protected ThingHandler createHandler(Thing thing) {
    if (mqttService == null) {
        throw new IllegalStateException("MqttService must be bound, before ThingHandlers can be created");
    }
    if (!(thing instanceof Bridge)) {
        throw new IllegalStateException("A bridge type is expected");
    }
    final ThingTypeUID thingTypeUID = thing.getThingTypeUID();
    final AbstractBrokerHandler handler;
    if (thingTypeUID.equals(MqttBindingConstants.BRIDGE_TYPE_SYSTEMBROKER)) {
        handler = new SystemBrokerHandler((Bridge) thing, mqttService);
    } else if (thingTypeUID.equals(MqttBindingConstants.BRIDGE_TYPE_BROKER)) {
        handler = new BrokerHandler((Bridge) thing);
    } else {
        throw new IllegalStateException("Not supported " + thingTypeUID.toString());
    }
    createdHandler(handler);
    return handler;
}
Also used : SystemBrokerHandler(org.eclipse.smarthome.binding.mqtt.handler.SystemBrokerHandler) AbstractBrokerHandler(org.eclipse.smarthome.binding.mqtt.handler.AbstractBrokerHandler) AbstractBrokerHandler(org.eclipse.smarthome.binding.mqtt.handler.AbstractBrokerHandler) BrokerHandler(org.eclipse.smarthome.binding.mqtt.handler.BrokerHandler) SystemBrokerHandler(org.eclipse.smarthome.binding.mqtt.handler.SystemBrokerHandler) ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) Bridge(org.eclipse.smarthome.core.thing.Bridge) Nullable(org.eclipse.jdt.annotation.Nullable)

Aggregations

AbstractBrokerHandler (org.eclipse.smarthome.binding.mqtt.handler.AbstractBrokerHandler)4 CompletableFuture (java.util.concurrent.CompletableFuture)2 NonNullByDefault (org.eclipse.jdt.annotation.NonNullByDefault)2 Nullable (org.eclipse.jdt.annotation.Nullable)2 Bridge (org.eclipse.smarthome.core.thing.Bridge)2 MqttBrokerConnection (org.eclipse.smarthome.io.transport.mqtt.MqttBrokerConnection)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeUnit (java.util.concurrent.TimeUnit)1 TimeoutException (java.util.concurrent.TimeoutException)1 RuleAction (org.eclipse.smarthome.automation.annotation.RuleAction)1 MQTTTopicDiscoveryParticipant (org.eclipse.smarthome.binding.mqtt.discovery.MQTTTopicDiscoveryParticipant)1 TopicSubscribe (org.eclipse.smarthome.binding.mqtt.discovery.TopicSubscribe)1 ChannelState (org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelState)1 ChannelStateUpdateListener (org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelStateUpdateListener)1 BrokerHandler (org.eclipse.smarthome.binding.mqtt.handler.BrokerHandler)1 SystemBrokerHandler (org.eclipse.smarthome.binding.mqtt.handler.SystemBrokerHandler)1