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.homematic.internal.communicator.virtual;
15 import static org.openhab.binding.homematic.internal.misc.HomematicConstants.VIRTUAL_DATAPOINT_NAME_SIGNAL_STRENGTH;
17 import org.openhab.binding.homematic.internal.model.HmChannel;
18 import org.openhab.binding.homematic.internal.model.HmDatapoint;
19 import org.openhab.binding.homematic.internal.model.HmDevice;
20 import org.openhab.binding.homematic.internal.model.HmValueType;
23 * A virtual datapoint that represents signal strength of a device as a number with values 0, 1, 2, 3 or 4, 0 being
24 * worst strength and 4 being best strength.
26 * @author Gerhard Riegler - Initial contribution
29 public class SignalStrengthVirtualDatapointHandler extends RssiVirtualDatapointHandler {
30 private static final int RSSI_START = 40;
31 private static final int RSSI_STEP = 25;
32 private static final int RSSI_UNITS = 4;
35 public String getName() {
36 return VIRTUAL_DATAPOINT_NAME_SIGNAL_STRENGTH;
40 public void initialize(HmDevice device) {
41 if (isWirelessDevice(device)) {
42 addDatapoint(device, 0, getName(), HmValueType.INTEGER, getRssiValue(device.getChannel(0)), true);
47 public void handleEvent(VirtualGateway gateway, HmDatapoint dp) {
48 HmChannel channel = dp.getChannel();
49 Integer value = getRssiValue(channel);
52 Integer strength = Math.max(Math.abs(value), RSSI_START);
53 strength = strength > RSSI_START + RSSI_STEP * RSSI_UNITS ? 0
54 : RSSI_UNITS - ((strength - RSSI_START) / RSSI_STEP);
56 HmDatapoint vdpRssi = getVirtualDatapoint(channel);
57 vdpRssi.setValue(strength);