2 * Copyright (c) 2010-2024 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.serial.internal.channel;
15 import java.util.IllegalFormatException;
16 import java.util.Optional;
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;
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.
29 * @author Mike Major - Initial contribution
32 public abstract class DeviceChannel {
33 protected final Logger logger = LoggerFactory.getLogger(DeviceChannel.class);
35 protected final ChannelConfig config;
37 private final ValueTransformation stateTransform;
38 private final ValueTransformation commandTransform;
40 protected DeviceChannel(final ValueTransformationProvider valueTransformationProvider, final ChannelConfig config) {
42 stateTransform = valueTransformationProvider.getValueTransformation(config.stateTransformation);
43 commandTransform = valueTransformationProvider.getValueTransformation(config.commandTransformation);
47 * Map the supplied command into the data to send to the device by
48 * applying a format followed by a transform.
50 * @param command the command to map
51 * @return the mapped data if the mapping produced a result.
53 public Optional<String> mapCommand(final Command command) {
54 final Optional<String> result = transformCommand(formatCommand(command));
56 logger.debug("Mapped command is '{}'", result.orElse(null));
62 * Transform the data using the configured transform
64 * @param data the data to transform
65 * @return the transformed data if the transform produced a result.
67 public Optional<String> transformData(final String data) {
68 return stateTransform.apply(data);
72 * Transform the data using the configured command transform
74 * @param data the command to transform
75 * @return the transformed data if the transform produced a result.
77 protected Optional<String> transformCommand(final String data) {
78 return commandTransform.apply(data);
82 * Format the commnd using the configured format
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.
88 protected String formatCommand(final Command command) {
91 final String commandFormat = config.commandFormat;
92 if (commandFormat != null) {
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();
100 data = command.toFullString();