]> git.basschouten.com Git - openhab-addons.git/blob
6ef676257bf2cad51b9b69e2d4b24a102934d0df
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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
39     public PlayerItemConverter(Consumer<State> updateState, Consumer<Command> postCommand,
40             @Nullable Consumer<String> sendHttpValue, ValueTransformation stateTransformations,
41             ValueTransformation commandTransformations, HttpChannelConfig channelConfig) {
42         super(updateState, postCommand, sendHttpValue, stateTransformations, commandTransformations, channelConfig);
43         this.channelConfig = channelConfig;
44     }
45
46     @Override
47     public String toString(Command command) {
48         String string = channelConfig.commandToFixedValue(command);
49         if (string != null) {
50             return string;
51         }
52
53         throw new IllegalArgumentException("Command type '" + command.toString() + "' not supported");
54     }
55
56     @Override
57     protected @Nullable Command toCommand(String string) {
58         if (string.equals(channelConfig.playValue)) {
59             return PlayPauseType.PLAY;
60         } else if (string.equals(channelConfig.pauseValue)) {
61             return PlayPauseType.PAUSE;
62         } else if (string.equals(channelConfig.nextValue)) {
63             return NextPreviousType.NEXT;
64         } else if (string.equals(channelConfig.previousValue)) {
65             return NextPreviousType.PREVIOUS;
66         } else if (string.equals(channelConfig.rewindValue)) {
67             return RewindFastforwardType.REWIND;
68         } else if (string.equals(channelConfig.fastforwardValue)) {
69             return RewindFastforwardType.FASTFORWARD;
70         }
71
72         return null;
73     }
74
75     @Override
76     public State toState(String string) {
77         return UnDefType.UNDEF;
78     }
79 }