2 * Copyright (c) 2010-2022 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.config.HttpChannelMode;
21 import org.openhab.binding.http.internal.http.Content;
22 import org.openhab.binding.http.internal.transform.ValueTransformation;
23 import org.openhab.core.types.Command;
24 import org.openhab.core.types.State;
27 * The {@link AbstractTransformingItemConverter} is a base class for an item converter with transformations
29 * @author Jan N. Klug - Initial contribution
32 public abstract class AbstractTransformingItemConverter implements ItemValueConverter {
33 private final Consumer<State> updateState;
34 private final Consumer<Command> postCommand;
35 private final @Nullable Consumer<String> sendHttpValue;
36 private final ValueTransformation stateTransformations;
37 private final ValueTransformation commandTransformations;
39 protected HttpChannelConfig channelConfig;
41 public AbstractTransformingItemConverter(Consumer<State> updateState, Consumer<Command> postCommand,
42 @Nullable Consumer<String> sendHttpValue, ValueTransformation stateTransformations,
43 ValueTransformation commandTransformations, HttpChannelConfig channelConfig) {
44 this.updateState = updateState;
45 this.postCommand = postCommand;
46 this.sendHttpValue = sendHttpValue;
47 this.stateTransformations = stateTransformations;
48 this.commandTransformations = commandTransformations;
49 this.channelConfig = channelConfig;
53 public void process(Content content) {
54 if (channelConfig.mode != HttpChannelMode.WRITEONLY) {
55 stateTransformations.apply(content.getAsString()).ifPresent(transformedValue -> {
56 Command command = toCommand(transformedValue);
57 if (command != null) {
58 postCommand.accept(command);
60 updateState.accept(toState(transformedValue));
64 throw new IllegalStateException("Write-only channel");
69 public void send(Command command) {
70 Consumer<String> sendHttpValue = this.sendHttpValue;
71 if (sendHttpValue != null && channelConfig.mode != HttpChannelMode.READONLY) {
72 commandTransformations.apply(toString(command)).ifPresent(sendHttpValue);
74 throw new IllegalStateException("Read-only channel");
79 * check if this converter received a value that needs to be sent as command
81 * @param value the value
82 * @return the command or null
84 protected abstract @Nullable Command toCommand(String value);
87 * convert the received value to a state
89 * @param value the value
90 * @return the state that represents the value of UNDEF if conversion failed
92 protected abstract State toState(String value);
95 * convert a command to a string
97 * @param command the command
98 * @return the string representation of the command
100 protected abstract String toString(Command command);
103 public interface Factory {
104 ItemValueConverter create(Consumer<State> updateState, Consumer<Command> postCommand,
105 @Nullable Consumer<String> sendHttpValue, ValueTransformation stateTransformations,
106 ValueTransformation commandTransformations, HttpChannelConfig channelConfig);