2 * Copyright (c) 2010-2023 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.samsungtv.internal.service;
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;
24 * The {@link DataConverters} provides utils for converting openHAB commands to
25 * Samsung TV specific values.
27 * @author Pauli Anttila - Initial contribution
30 public class DataConverters {
33 * Convert openHAB command to int.
41 public static int convertCommandToIntValue(Command command, int min, int max, int currentValue) {
42 if (command instanceof IncreaseDecreaseType || command instanceof DecimalType) {
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();
51 throw new NumberFormatException("Command '" + command + "' not supported");
57 throw new NumberFormatException("Command '" + command + "' not supported");
62 * Convert openHAB command to boolean.
67 public static boolean convertCommandToBooleanValue(Command command) {
68 if (command instanceof OnOffType || command instanceof OpenClosedType || command instanceof UpDownType) {
71 if (command.equals(OnOffType.ON) || command.equals(UpDownType.UP) || command.equals(OpenClosedType.OPEN)) {
73 } else if (command.equals(OnOffType.OFF) || command.equals(UpDownType.DOWN)
74 || command.equals(OpenClosedType.CLOSED)) {
77 throw new NumberFormatException("Command '" + command + "' not supported");
83 throw new NumberFormatException("Command '" + command + "' not supported for channel");