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.emotiva.internal;
15 import static org.openhab.binding.emotiva.internal.EmotivaBindingConstants.*;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.emotiva.internal.protocol.EmotivaControlCommands;
21 import org.openhab.binding.emotiva.internal.protocol.EmotivaControlRequest;
22 import org.openhab.binding.emotiva.internal.protocol.EmotivaProtocolVersion;
23 import org.openhab.binding.emotiva.internal.protocol.EmotivaSubscriptionTags;
24 import org.openhab.binding.emotiva.internal.protocol.OHChannelToEmotivaCommand;
25 import org.openhab.core.library.types.PercentType;
28 * Helper class for Emotiva commands.
30 * @author Espen Fossen - Initial contribution
33 public class EmotivaCommandHelper {
35 public static PercentType volumeDecibelToPercentage(String volumeInDecibel) {
36 String volumeTrimmed = volumeInDecibel.replace("dB", "").trim();
37 int clampedValue = clamp(volumeTrimmed, DEFAULT_VOLUME_MIN_DECIBEL, DEFAULT_VOLUME_MAX_DECIBEL);
38 return new PercentType(Math.round((100 - ((float) Math.abs(clampedValue - DEFAULT_VOLUME_MAX_DECIBEL)
39 / Math.abs(DEFAULT_VOLUME_MIN_DECIBEL - DEFAULT_VOLUME_MAX_DECIBEL)) * 100)));
42 public static double integerToPercentage(int integer) {
43 int clampedValue = clamp(integer, 0, 100);
44 return Math.round((100 - ((float) Math.abs(clampedValue - 100) / Math.abs(-100)) * 100));
47 public static int volumePercentageToDecibel(int volumeInPercentage) {
48 int clampedValue = clamp(volumeInPercentage, 0, 100);
49 return (clampedValue * (DEFAULT_VOLUME_MAX_DECIBEL - DEFAULT_VOLUME_MIN_DECIBEL) / 100)
50 + DEFAULT_VOLUME_MIN_DECIBEL;
53 public static int volumePercentageToDecibel(String volumeInPercentage) {
54 String volumeInPercentageTrimmed = volumeInPercentage.replace("%", "").trim();
55 int clampedValue = clamp(volumeInPercentageTrimmed, 0, 100);
56 return (clampedValue * (DEFAULT_VOLUME_MAX_DECIBEL - DEFAULT_VOLUME_MIN_DECIBEL) / 100)
57 + DEFAULT_VOLUME_MIN_DECIBEL;
60 public static double clamp(Number value, double min, double max) {
61 return Math.min(Math.max(value.intValue(), min), max);
64 private static int clamp(String volumeInPercentage, int min, int max) {
65 return Math.min(Math.max(Double.valueOf(volumeInPercentage.trim()).intValue(), min), max);
68 private static int clamp(int volumeInPercentage, int min, int max) {
69 return Math.min(Math.max(Double.valueOf(volumeInPercentage).intValue(), min), max);
72 public static EmotivaControlRequest channelToControlRequest(String id,
73 Map<String, Map<EmotivaControlCommands, String>> commandMaps, EmotivaProtocolVersion protocolVersion) {
74 EmotivaSubscriptionTags channelSubscription = EmotivaSubscriptionTags.fromChannelUID(id);
75 EmotivaControlCommands channelFromCommand = OHChannelToEmotivaCommand.fromChannelUID(id);
76 return new EmotivaControlRequest(id, channelSubscription, channelFromCommand, commandMaps, protocolVersion);
79 public static String getMenuPanelRowLabel(int row) {
88 public static String getMenuPanelColumnLabel(int column) {
89 return switch (column) {
97 public static String updateProgress(double progressPercentage) {
99 StringBuilder sb = new StringBuilder();
103 for (; i <= (int) (progressPercentage * width); i++) {
106 for (; i < width; i++) {
110 return sb.toString();