]> git.basschouten.com Git - openhab-addons.git/blob
26be40f341d3b016b47e32ffa5efc232a1fecfee
[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.Optional;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.serial.internal.transform.ValueTransformationProvider;
19 import org.openhab.core.library.types.IncreaseDecreaseType;
20 import org.openhab.core.library.types.OnOffType;
21 import org.openhab.core.types.Command;
22
23 /**
24  * The {@link DimmerChannel} channel applies a format followed by a transform.
25  *
26  * @author Mike Major - Initial contribution
27  */
28 @NonNullByDefault
29 public class DimmerChannel extends SwitchChannel {
30
31     public DimmerChannel(final ValueTransformationProvider valueTransformationProvider, final ChannelConfig config) {
32         super(valueTransformationProvider, config);
33     }
34
35     @Override
36     public Optional<String> mapCommand(final Command command) {
37         Optional<String> result;
38
39         if (command instanceof OnOffType) {
40             result = super.mapCommand(command);
41         } else {
42             String data;
43
44             final String increaseValue = config.increaseValue;
45             final String decreaseValue = config.decreaseValue;
46
47             if (command instanceof IncreaseDecreaseType) {
48                 if (increaseValue != null && IncreaseDecreaseType.INCREASE.equals(command)) {
49                     data = increaseValue;
50                 } else if (decreaseValue != null && IncreaseDecreaseType.DECREASE.equals(command)) {
51                     data = decreaseValue;
52                 } else {
53                     data = command.toFullString();
54                 }
55             } else {
56                 data = formatCommand(command);
57             }
58
59             result = transformCommand(data);
60
61             logger.debug("Mapped command is '{}'", result.orElse(null));
62         }
63
64         return result;
65     }
66 }