2 * Copyright (c) 2010-2024 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.osgi.service.component.annotations.Component;
28 import org.osgi.service.component.annotations.ServiceScope;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * The {@link OpenWebNetCENActions} defines CEN/CEN+ actions for the openwebnet binding.
35 * @author Massimo Valla - Initial contribution
38 @Component(scope = ServiceScope.PROTOTYPE, service = OpenWebNetCENActions.class)
39 @ThingActionsScope(name = "openwebnet")
41 public class OpenWebNetCENActions implements ThingActions {
43 private final Logger logger = LoggerFactory.getLogger(OpenWebNetCENActions.class);
44 private @Nullable OpenWebNetScenarioHandler scenarioHandler;
47 public void setThingHandler(@Nullable ThingHandler handler) {
48 this.scenarioHandler = (OpenWebNetScenarioHandler) handler;
52 public @Nullable ThingHandler getThingHandler() {
53 return scenarioHandler;
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!");
66 logger.warn("openwebnet OpenWebNetCENActions: press parameter is null!");
71 msg = handler.pressStrToMessage(press, button);
72 Response res = handler.send(msg);
74 logger.debug("Sent virtualPress '{}' to gateway. Response: {}", msg, res.getResponseMessages());
75 return res.isSuccess();
77 logger.debug("virtual press action returned null response");
79 } catch (IllegalArgumentException e) {
80 logger.warn("cannot execute virtual press action for thing {}: {}", handler.getThing().getUID(),
82 } catch (OWNException e) {
83 logger.warn("exception while sending virtual press message '{}' to gateway: {}", msg, e.getMessage());
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);
93 throw new IllegalArgumentException("Instance is not an OpenWebNetCENActions class.");