Search in sources :

Example 1 with RuleAction

use of org.eclipse.smarthome.automation.annotation.RuleAction 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 2 with RuleAction

use of org.eclipse.smarthome.automation.annotation.RuleAction in project smarthome by eclipse.

the class AnnotationActionModuleTypeHelper method parseAnnotations.

@SuppressWarnings({ "rawtypes" })
public Collection<ModuleInformation> parseAnnotations(String name, Object actionProvider) {
    Collection<ModuleInformation> moduleInformation = new ArrayList<>();
    Class clazz = actionProvider.getClass();
    Method[] methods = clazz.getDeclaredMethods();
    for (Method method : methods) {
        if (method.isAnnotationPresent(RuleAction.class)) {
            List<Input> inputs = getInputsFromAction(method);
            List<Output> outputs = getOutputsFromMethod(method);
            RuleAction ruleAction = method.getAnnotation(RuleAction.class);
            String uid = name + "." + method.getName();
            Set<String> tags = new HashSet<>(Arrays.asList(ruleAction.tags()));
            ModuleInformation mi = new ModuleInformation(uid, actionProvider, method);
            mi.setLabel(ruleAction.label());
            mi.setDescription(ruleAction.description());
            mi.setVisibility(ruleAction.visibility());
            mi.setInputs(inputs);
            mi.setOutputs(outputs);
            mi.setTags(tags);
            moduleInformation.add(mi);
        }
    }
    return moduleInformation;
}
Also used : RuleAction(org.eclipse.smarthome.automation.annotation.RuleAction) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) ActionInput(org.eclipse.smarthome.automation.annotation.ActionInput) Input(org.eclipse.smarthome.automation.type.Input) Output(org.eclipse.smarthome.automation.type.Output) ActionOutput(org.eclipse.smarthome.automation.annotation.ActionOutput) HashSet(java.util.HashSet)

Aggregations

RuleAction (org.eclipse.smarthome.automation.annotation.RuleAction)2 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 ActionInput (org.eclipse.smarthome.automation.annotation.ActionInput)1 ActionOutput (org.eclipse.smarthome.automation.annotation.ActionOutput)1 Input (org.eclipse.smarthome.automation.type.Input)1 Output (org.eclipse.smarthome.automation.type.Output)1 AbstractBrokerHandler (org.eclipse.smarthome.binding.mqtt.handler.AbstractBrokerHandler)1 MqttBrokerConnection (org.eclipse.smarthome.io.transport.mqtt.MqttBrokerConnection)1