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.caddx.internal.action;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.caddx.internal.handler.ThingHandlerKeypad;
18 import org.openhab.core.automation.annotation.ActionInput;
19 import org.openhab.core.automation.annotation.RuleAction;
20 import org.openhab.core.thing.binding.ThingActions;
21 import org.openhab.core.thing.binding.ThingActionsScope;
22 import org.openhab.core.thing.binding.ThingHandler;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
27 * This is the automation engine action handler service for the
28 * caddx bridge actions.
30 * @author Georgios Moutsos - Initial contribution
32 @ThingActionsScope(name = "caddx")
34 public class CaddxKeypadActions implements ThingActions {
35 private final Logger logger = LoggerFactory.getLogger(CaddxKeypadActions.class);
37 private static final String HANDLER_IS_NULL = "ThingHandlerKeypad is null!";
38 private static final String TEXT_IS_NULL = "The value for the text is null. Action not executed.";
39 private static final String DISPLAY_LOCATION_IS_NULL = "The value for the display location is null. Action not executed.";
40 private static final String DISPLAY_LOCATION_IS_INVALID = "The value for the display location [{}] is invalid. Action not executed.";
42 private @Nullable ThingHandlerKeypad handler;
45 public void setThingHandler(@Nullable ThingHandler handler) {
46 if (handler instanceof ThingHandlerKeypad keypadHandler) {
47 this.handler = keypadHandler;
52 public @Nullable ThingHandler getThingHandler() {
56 @RuleAction(label = "enterTerminalMode", description = "Enter terminal mode on the selected keypad")
57 public void enterTerminalMode() {
58 ThingHandlerKeypad thingHandler = this.handler;
59 if (thingHandler == null) {
60 logger.debug(HANDLER_IS_NULL);
64 thingHandler.enterTerminalMode();
67 public static void enterTerminalMode(ThingActions actions) {
68 ((CaddxKeypadActions) actions).enterTerminalMode();
71 @RuleAction(label = "sendKeypadTextMessage", description = "Display a message on the Keypad")
72 public void sendKeypadTextMessage(
73 @ActionInput(name = "displayLocation", label = "Display Location", description = "Display storage location (0=top left corner)") @Nullable String displayLocation,
74 @ActionInput(name = "text", label = "Text", description = "The text to be displayed") @Nullable String text) {
75 ThingHandlerKeypad thingHandler = handler;
76 if (thingHandler == null) {
77 logger.debug(HANDLER_IS_NULL);
82 logger.debug(TEXT_IS_NULL);
86 if (displayLocation == null) {
87 logger.debug(DISPLAY_LOCATION_IS_NULL);
91 if (!displayLocation.matches("^\\d$")) {
92 logger.debug(DISPLAY_LOCATION_IS_INVALID, displayLocation);
97 String paddedText = text + " ";
98 paddedText = paddedText.substring(0, 8);
101 thingHandler.sendKeypadTextMessage(displayLocation, text);
104 public static void sendKeypadTextMessage(ThingActions actions, @Nullable String displayLocation,
105 @Nullable String text) {
106 ((CaddxKeypadActions) actions).sendKeypadTextMessage(displayLocation, text);