]> git.basschouten.com Git - openhab-addons.git/blob
7d488d75409cc53ad77885001145080648b0f7aa
[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.serial.internal.channel;
14
15 import java.util.IllegalFormatException;
16 import java.util.Optional;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.binding.serial.internal.transform.ValueTransformation;
20 import org.openhab.binding.serial.internal.transform.ValueTransformationProvider;
21 import org.openhab.core.types.Command;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * The {@link DeviceChannel} is the abstract class for handling a channel. Provides
27  * the ability to transform the device data into the channel state.
28  *
29  * @author Mike Major - Initial contribution
30  */
31 @NonNullByDefault
32 public abstract class DeviceChannel {
33     protected final Logger logger = LoggerFactory.getLogger(DeviceChannel.class);
34
35     protected final ChannelConfig config;
36
37     private final ValueTransformation stateTransform;
38     private final ValueTransformation commandTransform;
39
40     protected DeviceChannel(final ValueTransformationProvider valueTransformationProvider, final ChannelConfig config) {
41         this.config = config;
42         stateTransform = valueTransformationProvider.getValueTransformation(config.stateTransformation);
43         commandTransform = valueTransformationProvider.getValueTransformation(config.commandTransformation);
44     }
45
46     /**
47      * Map the supplied command into the data to send to the device by
48      * applying a format followed by a transform.
49      * 
50      * @param command the command to map
51      * @return the mapped data if the mapping produced a result.
52      */
53     public Optional<String> mapCommand(final Command command) {
54         final Optional<String> result = transformCommand(formatCommand(command));
55
56         logger.debug("Mapped command is '{}'", result.orElse(null));
57
58         return result;
59     }
60
61     /**
62      * Transform the data using the configured transform
63      * 
64      * @param data the data to transform
65      * @return the transformed data if the transform produced a result.
66      */
67     public Optional<String> transformData(final String data) {
68         return stateTransform.apply(data);
69     }
70
71     /**
72      * Transform the data using the configured command transform
73      * 
74      * @param data the command to transform
75      * @return the transformed data if the transform produced a result.
76      */
77     protected Optional<String> transformCommand(final String data) {
78         return commandTransform.apply(data);
79     }
80
81     /**
82      * Format the commnd using the configured format
83      * 
84      * @param command the command to transform
85      * @return the formatted data. The orginal data is returned if there is no format string
86      *         or if there is an error performing the format.
87      */
88     protected String formatCommand(final Command command) {
89         String data;
90
91         final String commandFormat = config.commandFormat;
92         if (commandFormat != null) {
93             try {
94                 data = command.format(commandFormat);
95             } catch (final IllegalFormatException e) {
96                 logger.warn("Couldn't format commmand because format string '{}' is invalid", commandFormat);
97                 data = command.toFullString();
98             }
99         } else {
100             data = command.toFullString();
101         }
102
103         return data;
104     }
105 }