]> git.basschouten.com Git - openhab-addons.git/blob
51354afdb84fffd436b4d8ab4bf0cf592ddbccf9
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.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;
25
26 /**
27  * The {@link AbstractTransformingItemConverter} is a base class for an item converter with transformations
28  *
29  * @author Jan N. Klug - Initial contribution
30  */
31 @NonNullByDefault
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;
38
39     protected HttpChannelConfig channelConfig;
40
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;
50     }
51
52     @Override
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);
59                 } else {
60                     updateState.accept(toState(transformedValue));
61                 }
62             });
63         } else {
64             throw new IllegalStateException("Write-only channel");
65         }
66     }
67
68     @Override
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);
73         } else {
74             throw new IllegalStateException("Read-only channel");
75         }
76     }
77
78     /**
79      * check if this converter received a value that needs to be sent as command
80      *
81      * @param value the value
82      * @return the command or null
83      */
84     protected abstract @Nullable Command toCommand(String value);
85
86     /**
87      * convert the received value to a state
88      *
89      * @param value the value
90      * @return the state that represents the value of UNDEF if conversion failed
91      */
92     protected abstract State toState(String value);
93
94     /**
95      * convert a command to a string
96      *
97      * @param command the command
98      * @return the string representation of the command
99      */
100     protected abstract String toString(Command command);
101
102     @FunctionalInterface
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);
107     }
108 }