]> git.basschouten.com Git - openhab-addons.git/blob
dc4b4379fed6f26b10915797a889a801a2e33afd
[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.osgi.service.component.annotations.Component;
28 import org.osgi.service.component.annotations.ServiceScope;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * The {@link OpenWebNetCENActions} defines CEN/CEN+ actions for the openwebnet binding.
34  *
35  * @author Massimo Valla - Initial contribution
36  */
37
38 @Component(scope = ServiceScope.PROTOTYPE, service = OpenWebNetCENActions.class)
39 @ThingActionsScope(name = "openwebnet")
40 @NonNullByDefault
41 public class OpenWebNetCENActions implements ThingActions {
42
43     private final Logger logger = LoggerFactory.getLogger(OpenWebNetCENActions.class);
44     private @Nullable OpenWebNetScenarioHandler scenarioHandler;
45
46     @Override
47     public void setThingHandler(@Nullable ThingHandler handler) {
48         this.scenarioHandler = (OpenWebNetScenarioHandler) handler;
49     }
50
51     @Override
52     public @Nullable ThingHandler getThingHandler() {
53         return scenarioHandler;
54     }
55
56     @RuleAction(label = "virtualPress", description = "Virtual press of the push button")
57     public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean virtualPress(
58             @ActionInput(name = "press", label = "press", description = "Type of press") @Nullable String press,
59             @ActionInput(name = "button", label = "button", description = "Button number") int button) {
60         OpenWebNetScenarioHandler handler = scenarioHandler;
61         if (handler == null) {
62             logger.warn("openwebnet OpenWebNetCENActions: scenarioHandler is null!");
63             return false;
64         }
65         if (press == null) {
66             logger.warn("openwebnet OpenWebNetCENActions: press parameter is null!");
67             return false;
68         }
69         CEN msg = null;
70         try {
71             msg = handler.pressStrToMessage(press, button);
72             Response res = handler.send(msg);
73             if (res != null) {
74                 logger.debug("Sent virtualPress '{}' to gateway. Response: {}", msg, res.getResponseMessages());
75                 return res.isSuccess();
76             } else {
77                 logger.debug("virtual press action returned null response");
78             }
79         } catch (IllegalArgumentException e) {
80             logger.warn("cannot execute virtual press action for thing {}: {}", handler.getThing().getUID(),
81                     e.getMessage());
82         } catch (OWNException e) {
83             logger.warn("exception while sending virtual press message '{}' to gateway: {}", msg, e.getMessage());
84         }
85         return false;
86     }
87
88     // legacy delegate methods
89     public static void virtualPress(@Nullable ThingActions actions, @Nullable String press, int button) {
90         if (actions instanceof OpenWebNetCENActions openWebNetCENActions) {
91             openWebNetCENActions.virtualPress(press, button);
92         } else {
93             throw new IllegalArgumentException("Instance is not an OpenWebNetCENActions class.");
94         }
95     }
96 }