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.lifx.internal.dto;
16 * The signal strength of a light.
18 * @author Wouter Born - Initial contribution
20 public class SignalStrength {
22 private double milliWatts;
24 public SignalStrength(double milliWatts) {
25 this.milliWatts = milliWatts;
29 * Returns the signal strength.
31 * @return the signal strength in milliwatts (mW).
33 public double getMilliWatts() {
38 * Returns the signal strength as a quality percentage:
40 * <li>{@code RSSI <= -100}: returns 0
41 * <li>{@code -100 < RSSI < -50}: returns a value between 0 and 1 (linearly distributed)
42 * <li>{@code RSSI >= -50}: returns 1
45 * @return a value between 0 and 1. 0 being worst strength and 1
46 * being best strength.
48 public double toQualityPercentage() {
49 return Math.min(100, Math.max(0, 2 * (toRSSI() + 100))) / 100;
53 * Returns the signal strength as a quality rating.
55 * @return one of the values: 0, 1, 2, 3 or 4. 0 being worst strength and 4
56 * being best strength.
58 public byte toQualityRating() {
59 return (byte) Math.round(toQualityPercentage() * 4);
63 * Returns the received signal strength indicator (RSSI).
65 * @return a value {@code <= 0. 0} being best strength and more negative values indicate worser strength.
67 public double toRSSI() {
68 return 10 * Math.log10(milliWatts);
72 public String toString() {
73 return "SignalStrength [milliWatts=" + milliWatts + ", rssi=" + Math.round(toRSSI()) + "]";