]> git.basschouten.com Git - openhab-addons.git/blob
e563f7ddac08f9945e46d14f2ccb4dfd81cf8460
[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.http.internal.converter;
14
15 import java.util.function.Consumer;
16
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;
27
28 /**
29  * The {@link PlayerItemConverter} implements {@link org.openhab.core.library.items.RollershutterItem}
30  * conversions
31  *
32  * @author Jan N. Klug - Initial contribution
33  */
34
35 @NonNullByDefault
36 public class PlayerItemConverter extends AbstractTransformingItemConverter {
37     private final HttpChannelConfig channelConfig;
38     private @Nullable String lastCommand; // store last command to prevent duplicate commands
39
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;
45     }
46
47     @Override
48     public String toString(Command command) {
49         String string = channelConfig.commandToFixedValue(command);
50         if (string != null) {
51             return string;
52         }
53
54         throw new IllegalArgumentException("Command type '" + command.toString() + "' not supported");
55     }
56
57     @Override
58     protected @Nullable Command toCommand(String string) {
59         if (string.equals(lastCommand)) {
60             // only send commands once
61             return null;
62         }
63         lastCommand = string;
64
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;
77         }
78
79         return null;
80     }
81
82     @Override
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;
92         }
93
94         return UnDefType.UNDEF;
95     }
96 }