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);
});
});
}
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");
}
}
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;
});
}
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;
}
Aggregations