]> git.basschouten.com Git - openhab-addons.git/blob
534856167864de75b03bcf4ec94bec3ff608847e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.openwebnet.internal.actions;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.openwebnet.internal.handler.OpenWebNetScenarioHandler;
18 import org.openhab.core.automation.annotation.ActionInput;
19 import org.openhab.core.automation.annotation.ActionOutput;
20 import org.openhab.core.automation.annotation.RuleAction;
21 import org.openhab.core.thing.binding.ThingActions;
22 import org.openhab.core.thing.binding.ThingActionsScope;
23 import org.openhab.core.thing.binding.ThingHandler;
24 import org.openwebnet4j.communication.OWNException;
25 import org.openwebnet4j.communication.Response;
26 import org.openwebnet4j.message.CEN;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * The {@link OpenWebNetCENActions} defines CEN/CEN+ actions for the openwebnet binding.
32  *
33  * @author Massimo Valla - Initial contribution
34  */
35
36 @ThingActionsScope(name = "openwebnet")
37 @NonNullByDefault
38 public class OpenWebNetCENActions implements ThingActions {
39
40     private final Logger logger = LoggerFactory.getLogger(OpenWebNetCENActions.class);
41     private @Nullable OpenWebNetScenarioHandler scenarioHandler;
42
43     @Override
44     public void setThingHandler(@Nullable ThingHandler handler) {
45         this.scenarioHandler = (OpenWebNetScenarioHandler) handler;
46     }
47
48     @Override
49     public @Nullable ThingHandler getThingHandler() {
50         return scenarioHandler;
51     }
52
53     @RuleAction(label = "virtualPress", description = "Virtual press of the push button")
54     public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean virtualPress(
55             @ActionInput(name = "press", label = "press", description = "Type of press") @Nullable String press,
56             @ActionInput(name = "button", label = "button", description = "Button number") int button) {
57         OpenWebNetScenarioHandler handler = scenarioHandler;
58         if (handler == null) {
59             logger.warn("openwebnet OpenWebNetCENActions: scenarioHandler is null!");
60             return false;
61         }
62         if (press == null) {
63             logger.warn("openwebnet OpenWebNetCENActions: press parameter is null!");
64             return false;
65         }
66         CEN msg = null;
67         try {
68             msg = handler.pressStrToMessage(press, button);
69             Response res = handler.send(msg);
70             if (res != null) {
71                 logger.debug("Sent virtualPress '{}' to gateway. Response: {}", msg, res.getResponseMessages());
72                 return res.isSuccess();
73             } else {
74                 logger.debug("virtual press action returned null response");
75             }
76         } catch (IllegalArgumentException e) {
77             logger.warn("cannot execute virtual press action for thing {}: {}", handler.getThing().getUID(),
78                     e.getMessage());
79         } catch (OWNException e) {
80             logger.warn("exception while sending virtual press message '{}' to gateway: {}", msg, e.getMessage());
81         }
82         return false;
83     }
84
85     // legacy delegate methods
86     public static void virtualPress(@Nullable ThingActions actions, @Nullable String press, int button) {
87         if (actions instanceof OpenWebNetCENActions openWebNetCENActions) {
88             openWebNetCENActions.virtualPress(press, button);
89         } else {
90             throw new IllegalArgumentException("Instance is not an OpenWebNetCENActions class.");
91         }
92     }
93 }