2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.openwebnet.internal.actions;
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;
31 * The {@link OpenWebNetCENActions} defines CEN/CEN+ actions for the openwebnet binding.
33 * @author Massimo Valla - Initial contribution
36 @ThingActionsScope(name = "openwebnet")
38 public class OpenWebNetCENActions implements ThingActions {
40 private final Logger logger = LoggerFactory.getLogger(OpenWebNetCENActions.class);
41 private @Nullable OpenWebNetScenarioHandler scenarioHandler;
44 public void setThingHandler(@Nullable ThingHandler handler) {
45 this.scenarioHandler = (OpenWebNetScenarioHandler) handler;
49 public @Nullable ThingHandler getThingHandler() {
50 return scenarioHandler;
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!");
63 logger.warn("openwebnet OpenWebNetCENActions: press parameter is null!");
68 msg = handler.pressStrToMessage(press, button);
69 Response res = handler.send(msg);
71 logger.debug("Sent virtualPress '{}' to gateway. Response: {}", msg, res.getResponseMessages());
72 return res.isSuccess();
74 logger.debug("virtual press action returned null response");
76 } catch (IllegalArgumentException e) {
77 logger.warn("cannot execute virtual press action for thing {}: {}", handler.getThing().getUID(),
79 } catch (OWNException e) {
80 logger.warn("exception while sending virtual press message '{}' to gateway: {}", msg, e.getMessage());
85 // legacy delegate methods
86 public static void virtualPress(@Nullable ThingActions actions, @Nullable String press, int button) {
87 if (actions instanceof OpenWebNetCENActions) {
88 ((OpenWebNetCENActions) actions).virtualPress(press, button);
90 throw new IllegalArgumentException("Instance is not an OpenWebNetCENActions class.");