]> git.basschouten.com Git - openhab-addons.git/blob
e291104166da6c5adeab995eaaf090f8f7d25aa6
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.anel.internal.state;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.anel.internal.IAnelConstants;
18 import org.openhab.core.library.types.OnOffType;
19 import org.openhab.core.types.Command;
20 import org.openhab.core.types.State;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * Convert an openhab command to an ANEL UDP command message.
26  *
27  * @author Patrick Koenemann - Initial contribution
28  */
29 @NonNullByDefault
30 public class AnelCommandHandler {
31
32     private final Logger logger = LoggerFactory.getLogger(AnelCommandHandler.class);
33
34     public @Nullable State getLockedState(@Nullable AnelState state, String channelId) {
35         if (IAnelConstants.CHANNEL_RELAY_STATE.contains(channelId)) {
36             if (state == null) {
37                 return null; // assume unlocked
38             }
39
40             final int index = IAnelConstants.getIndexFromChannel(channelId);
41
42             final @Nullable Boolean locked = state.relayLocked[index];
43             if (locked == null || !locked.booleanValue()) {
44                 return null; // no lock information or unlocked
45             }
46
47             final @Nullable Boolean lockedState = state.relayState[index];
48             if (lockedState == null) {
49                 return null; // no state information available
50             }
51
52             return OnOffType.from(lockedState.booleanValue());
53         }
54
55         if (IAnelConstants.CHANNEL_IO_STATE.contains(channelId)) {
56             if (state == null) {
57                 return null; // assume unlocked
58             }
59
60             final int index = IAnelConstants.getIndexFromChannel(channelId);
61
62             final @Nullable Boolean isInput = state.ioIsInput[index];
63             if (isInput == null || !isInput.booleanValue()) {
64                 return null; // no direction infmoration or output port
65             }
66
67             final @Nullable Boolean ioState = state.ioState[index];
68             if (ioState == null) {
69                 return null; // no state information available
70             }
71             return OnOffType.from(ioState.booleanValue());
72         }
73         return null; // all other channels are read-only!
74     }
75
76     public @Nullable String toAnelCommandAndUnsetState(@Nullable AnelState state, String channelId, Command command,
77             String authentication) {
78         if (!(command instanceof OnOffType)) {
79             // only relay states and io states can be changed, all other channels are read-only
80             logger.warn("Anel binding only support ON/OFF and Refresh commands, not {}: {}",
81                     command.getClass().getSimpleName(), command);
82         } else if (IAnelConstants.CHANNEL_RELAY_STATE.contains(channelId)) {
83             final int index = IAnelConstants.getIndexFromChannel(channelId);
84
85             // unset anel state which enforces a channel state update
86             if (state != null) {
87                 state.relayState[index] = null;
88             }
89
90             @Nullable
91             final Boolean locked = state == null ? null : state.relayLocked[index];
92             if (locked == null || !locked.booleanValue()) {
93                 return String.format("Sw_%s%d%s", command.toString().toLowerCase(), index + 1, authentication);
94             } else {
95                 logger.warn("Relay {} is locked; skipping command {}.", index + 1, command);
96             }
97         } else if (IAnelConstants.CHANNEL_IO_STATE.contains(channelId)) {
98             final int index = IAnelConstants.getIndexFromChannel(channelId);
99
100             // unset anel state which enforces a channel state update
101             if (state != null) {
102                 state.ioState[index] = null;
103             }
104
105             @Nullable
106             final Boolean isInput = state == null ? null : state.ioIsInput[index];
107             if (isInput == null || !isInput.booleanValue()) {
108                 return String.format("IO_%s%d%s", command.toString().toLowerCase(), index + 1, authentication);
109             } else {
110                 logger.warn("IO {} has direction input, not output; skipping command {}.", index + 1, command);
111             }
112         }
113
114         return null; // all other channels are read-only
115     }
116 }