]> git.basschouten.com Git - openhab-addons.git/blob
21c93a8d8741a273511d96717ed68703fb02d068
[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.lifx.internal.dto;
14
15 /**
16  * The signal strength of a light.
17  *
18  * @author Wouter Born - Initial contribution
19  */
20 public class SignalStrength {
21
22     private double milliWatts;
23
24     public SignalStrength(double milliWatts) {
25         this.milliWatts = milliWatts;
26     }
27
28     /**
29      * Returns the signal strength.
30      *
31      * @return the signal strength in milliwatts (mW).
32      */
33     public double getMilliWatts() {
34         return milliWatts;
35     }
36
37     /**
38      * Returns the signal strength as a quality percentage:
39      * <ul>
40      * <li>RSSI <= -100: returns 0
41      * <li>-100 < RSSI < -50: returns a value between 0 and 1 (linearly distributed)
42      * <li>RSSI >= -50: returns 1
43      * <ul>
44      *
45      * @return a value between 0 and 1. 0 being worst strength and 1
46      *         being best strength.
47      */
48     public double toQualityPercentage() {
49         return Math.min(100, Math.max(0, 2 * (toRSSI() + 100))) / 100;
50     }
51
52     /**
53      * Returns the signal strength as a quality rating.
54      *
55      * @return one of the values: 0, 1, 2, 3 or 4. 0 being worst strength and 4
56      *         being best strength.
57      */
58     public byte toQualityRating() {
59         return (byte) Math.round(toQualityPercentage() * 4);
60     }
61
62     /**
63      * Returns the received signal strength indicator (RSSI).
64      *
65      * @return a value <= 0. 0 being best strength and more negative values indicate worser strength.
66      */
67     public double toRSSI() {
68         return 10 * Math.log10(milliWatts);
69     }
70
71     @Override
72     public String toString() {
73         return "SignalStrength [milliWatts=" + milliWatts + ", rssi=" + Math.round(toRSSI()) + "]";
74     }
75 }