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.http.internal.converter;
15 import java.util.function.Consumer;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.http.internal.config.HttpChannelConfig;
20 import org.openhab.binding.http.internal.transform.ValueTransformation;
21 import org.openhab.core.library.types.NextPreviousType;
22 import org.openhab.core.library.types.PlayPauseType;
23 import org.openhab.core.library.types.RewindFastforwardType;
24 import org.openhab.core.types.Command;
25 import org.openhab.core.types.State;
26 import org.openhab.core.types.UnDefType;
29 * The {@link PlayerItemConverter} implements {@link org.openhab.core.library.items.RollershutterItem}
32 * @author Jan N. Klug - Initial contribution
36 public class PlayerItemConverter extends AbstractTransformingItemConverter {
37 private final HttpChannelConfig channelConfig;
38 private @Nullable String lastCommand; // store last command to prevent duplicate commands
40 public PlayerItemConverter(Consumer<State> updateState, Consumer<Command> postCommand,
41 @Nullable Consumer<String> sendHttpValue, ValueTransformation stateTransformations,
42 ValueTransformation commandTransformations, HttpChannelConfig channelConfig) {
43 super(updateState, postCommand, sendHttpValue, stateTransformations, commandTransformations, channelConfig);
44 this.channelConfig = channelConfig;
48 public String toString(Command command) {
49 String string = channelConfig.commandToFixedValue(command);
54 throw new IllegalArgumentException("Command type '" + command.toString() + "' not supported");
58 protected @Nullable Command toCommand(String string) {
59 if (string.equals(lastCommand)) {
60 // only send commands once
65 if (string.equals(channelConfig.playValue)) {
66 return PlayPauseType.PLAY;
67 } else if (string.equals(channelConfig.pauseValue)) {
68 return PlayPauseType.PAUSE;
69 } else if (string.equals(channelConfig.nextValue)) {
70 return NextPreviousType.NEXT;
71 } else if (string.equals(channelConfig.previousValue)) {
72 return NextPreviousType.PREVIOUS;
73 } else if (string.equals(channelConfig.rewindValue)) {
74 return RewindFastforwardType.REWIND;
75 } else if (string.equals(channelConfig.fastforwardValue)) {
76 return RewindFastforwardType.FASTFORWARD;
83 public State toState(String string) {
84 if (string.equals(channelConfig.playValue)) {
85 return PlayPauseType.PLAY;
86 } else if (string.equals(channelConfig.pauseValue)) {
87 return PlayPauseType.PAUSE;
88 } else if (string.equals(channelConfig.rewindValue)) {
89 return RewindFastforwardType.REWIND;
90 } else if (string.equals(channelConfig.fastforwardValue)) {
91 return RewindFastforwardType.FASTFORWARD;
94 return UnDefType.UNDEF;