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.anel.internal.state;
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;
25 * Convert an openhab command to an ANEL UDP command message.
27 * @author Patrick Koenemann - Initial contribution
30 public class AnelCommandHandler {
32 private final Logger logger = LoggerFactory.getLogger(AnelCommandHandler.class);
34 public @Nullable State getLockedState(@Nullable AnelState state, String channelId) {
35 if (IAnelConstants.CHANNEL_RELAY_STATE.contains(channelId)) {
37 return null; // assume unlocked
40 final int index = IAnelConstants.getIndexFromChannel(channelId);
42 final @Nullable Boolean locked = state.relayLocked[index];
43 if (locked == null || !locked.booleanValue()) {
44 return null; // no lock information or unlocked
47 final @Nullable Boolean lockedState = state.relayState[index];
48 if (lockedState == null) {
49 return null; // no state information available
52 return OnOffType.from(lockedState.booleanValue());
55 if (IAnelConstants.CHANNEL_IO_STATE.contains(channelId)) {
57 return null; // assume unlocked
60 final int index = IAnelConstants.getIndexFromChannel(channelId);
62 final @Nullable Boolean isInput = state.ioIsInput[index];
63 if (isInput == null || !isInput.booleanValue()) {
64 return null; // no direction infmoration or output port
67 final @Nullable Boolean ioState = state.ioState[index];
68 if (ioState == null) {
69 return null; // no state information available
71 return OnOffType.from(ioState.booleanValue());
73 return null; // all other channels are read-only!
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);
85 // unset anel state which enforces a channel state update
87 state.relayState[index] = null;
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);
95 logger.warn("Relay {} is locked; skipping command {}.", index + 1, command);
97 } else if (IAnelConstants.CHANNEL_IO_STATE.contains(channelId)) {
98 final int index = IAnelConstants.getIndexFromChannel(channelId);
100 // unset anel state which enforces a channel state update
102 state.ioState[index] = null;
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);
110 logger.warn("IO {} has direction input, not output; skipping command {}.", index + 1, command);
114 return null; // all other channels are read-only