]> git.basschouten.com Git - openhab-addons.git/blob
b07e5ebb0c675514947923df94b1f24a92b5b771
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.samsungtv.internal.service;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.core.library.types.DecimalType;
17 import org.openhab.core.library.types.IncreaseDecreaseType;
18 import org.openhab.core.library.types.OnOffType;
19 import org.openhab.core.library.types.OpenClosedType;
20 import org.openhab.core.library.types.UpDownType;
21 import org.openhab.core.types.Command;
22
23 /**
24  * The {@link DataConverters} provides utils for converting openHAB commands to
25  * Samsung TV specific values.
26  *
27  * @author Pauli Anttila - Initial contribution
28  */
29 @NonNullByDefault
30 public class DataConverters {
31
32     /**
33      * Convert openHAB command to int.
34      *
35      * @param command
36      * @param min
37      * @param max
38      * @param currentValue
39      * @return
40      */
41     public static int convertCommandToIntValue(Command command, int min, int max, int currentValue) {
42         if (command instanceof IncreaseDecreaseType || command instanceof DecimalType) {
43             int value;
44             if (command instanceof IncreaseDecreaseType && command == IncreaseDecreaseType.INCREASE) {
45                 value = Math.min(max, currentValue + 1);
46             } else if (command instanceof IncreaseDecreaseType && command == IncreaseDecreaseType.DECREASE) {
47                 value = Math.max(min, currentValue - 1);
48             } else if (command instanceof DecimalType decimalCommand) {
49                 value = decimalCommand.intValue();
50             } else {
51                 throw new NumberFormatException("Command '" + command + "' not supported");
52             }
53
54             return value;
55
56         } else {
57             throw new NumberFormatException("Command '" + command + "' not supported");
58         }
59     }
60
61     /**
62      * Convert openHAB command to boolean.
63      *
64      * @param command
65      * @return
66      */
67     public static boolean convertCommandToBooleanValue(Command command) {
68         if (command instanceof OnOffType || command instanceof OpenClosedType || command instanceof UpDownType) {
69             boolean newValue;
70
71             if (command.equals(OnOffType.ON) || command.equals(UpDownType.UP) || command.equals(OpenClosedType.OPEN)) {
72                 newValue = true;
73             } else if (command.equals(OnOffType.OFF) || command.equals(UpDownType.DOWN)
74                     || command.equals(OpenClosedType.CLOSED)) {
75                 newValue = false;
76             } else {
77                 throw new NumberFormatException("Command '" + command + "' not supported");
78             }
79
80             return newValue;
81
82         } else {
83             throw new NumberFormatException("Command '" + command + "' not supported for channel");
84         }
85     }
86 }