]> git.basschouten.com Git - openhab-addons.git/blob
b03cf27d633760c32125a72470d3ad1a299f7722
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.types.Command;
22 import org.openhab.core.types.State;
23 import org.openhab.core.types.UnDefType;
24
25 /**
26  * The {@link FixedValueMappingItemConverter} implements mapping conversions for different item-types
27  *
28  * @author Jan N. Klug - Initial contribution
29  */
30
31 @NonNullByDefault
32 public class FixedValueMappingItemConverter extends AbstractTransformingItemConverter {
33
34     public FixedValueMappingItemConverter(Consumer<State> updateState, Consumer<Command> postCommand,
35             @Nullable Consumer<String> sendHttpValue, ValueTransformation stateTransformations,
36             ValueTransformation commandTransformations, HttpChannelConfig channelConfig) {
37         super(updateState, postCommand, sendHttpValue, stateTransformations, commandTransformations, channelConfig);
38     }
39
40     @Override
41     protected @Nullable Command toCommand(String value) {
42         return null;
43     }
44
45     @Override
46     public String toString(Command command) {
47         String value = channelConfig.commandToFixedValue(command);
48         if (value != null) {
49             return value;
50         }
51
52         throw new IllegalArgumentException(
53                 "Command type '" + command.toString() + "' not supported or mapping not defined.");
54     }
55
56     @Override
57     public State toState(String string) {
58         State state = channelConfig.fixedValueToState(string);
59
60         return state != null ? state : UnDefType.UNDEF;
61     }
62 }