]> git.basschouten.com Git - openhab-addons.git/blob
e080663fefa08f6c951e67b047d7d83e80274033
[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.caddx.internal.action;
14
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;
25
26 /**
27  * This is the automation engine action handler service for the
28  * caddx bridge actions.
29  *
30  * @author Georgios Moutsos - Initial contribution
31  */
32 @ThingActionsScope(name = "caddx")
33 @NonNullByDefault
34 public class CaddxKeypadActions implements ThingActions {
35     private final Logger logger = LoggerFactory.getLogger(CaddxKeypadActions.class);
36
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.";
41
42     private @Nullable ThingHandlerKeypad handler;
43
44     @Override
45     public void setThingHandler(@Nullable ThingHandler handler) {
46         if (handler instanceof ThingHandlerKeypad keypadHandler) {
47             this.handler = keypadHandler;
48         }
49     }
50
51     @Override
52     public @Nullable ThingHandler getThingHandler() {
53         return this.handler;
54     }
55
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);
61             return;
62         }
63
64         thingHandler.enterTerminalMode();
65     }
66
67     public static void enterTerminalMode(ThingActions actions) {
68         ((CaddxKeypadActions) actions).enterTerminalMode();
69     }
70
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);
78             return;
79         }
80
81         if (text == null) {
82             logger.debug(TEXT_IS_NULL);
83             return;
84         }
85
86         if (displayLocation == null) {
87             logger.debug(DISPLAY_LOCATION_IS_NULL);
88             return;
89         }
90
91         if (!displayLocation.matches("^\\d$")) {
92             logger.debug(DISPLAY_LOCATION_IS_INVALID, displayLocation);
93             return;
94         }
95
96         // Adjust parameters
97         String paddedText = text + "        ";
98         paddedText = paddedText.substring(0, 8);
99
100         // Build the command
101         thingHandler.sendKeypadTextMessage(displayLocation, text);
102     }
103
104     public static void sendKeypadTextMessage(ThingActions actions, @Nullable String displayLocation,
105             @Nullable String text) {
106         ((CaddxKeypadActions) actions).sendKeypadTextMessage(displayLocation, text);
107     }
108 }