]> git.basschouten.com Git - openhab-addons.git/blob
ec79969e8531021a7ff0d0e37b57dd2dacda1453
[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.emotiva.internal;
14
15 import static org.openhab.binding.emotiva.internal.EmotivaBindingConstants.*;
16
17 import java.util.Map;
18
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;
26
27 /**
28  * Helper class for Emotiva commands.
29  *
30  * @author Espen Fossen - Initial contribution
31  */
32 @NonNullByDefault
33 public class EmotivaCommandHelper {
34
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)));
40     }
41
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));
45     }
46
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;
51     }
52
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;
58     }
59
60     public static double clamp(Number value, double min, double max) {
61         return Math.min(Math.max(value.intValue(), min), max);
62     }
63
64     private static int clamp(String volumeInPercentage, int min, int max) {
65         return Math.min(Math.max(Double.valueOf(volumeInPercentage.trim()).intValue(), min), max);
66     }
67
68     private static int clamp(int volumeInPercentage, int min, int max) {
69         return Math.min(Math.max(Double.valueOf(volumeInPercentage).intValue(), min), max);
70     }
71
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);
77     }
78
79     public static String getMenuPanelRowLabel(int row) {
80         return switch (row) {
81             case 4 -> "top";
82             case 5 -> "middle";
83             case 6 -> "bottom";
84             default -> "";
85         };
86     }
87
88     public static String getMenuPanelColumnLabel(int column) {
89         return switch (column) {
90             case 0 -> "start";
91             case 1 -> "center";
92             case 2 -> "end";
93             default -> "";
94         };
95     }
96
97     public static String updateProgress(double progressPercentage) {
98         final int width = 30;
99         StringBuilder sb = new StringBuilder();
100
101         sb.append("[");
102         int i = 0;
103         for (; i <= (int) (progressPercentage * width); i++) {
104             sb.append(".");
105         }
106         for (; i < width; i++) {
107             sb.append(" ");
108         }
109         sb.append("]");
110         return sb.toString();
111     }
112 }