]> git.basschouten.com Git - openhab-addons.git/blob
30c0f474e0736c227f6c9b30f6b97920e1bb056e
[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.homematic.internal.communicator.virtual;
14
15 import static org.openhab.binding.homematic.internal.misc.HomematicConstants.VIRTUAL_DATAPOINT_NAME_SIGNAL_STRENGTH;
16
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;
21
22 /**
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.
25  *
26  * @author Gerhard Riegler - Initial contribution
27  */
28
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;
33
34     @Override
35     public String getName() {
36         return VIRTUAL_DATAPOINT_NAME_SIGNAL_STRENGTH;
37     }
38
39     @Override
40     public void initialize(HmDevice device) {
41         if (isWirelessDevice(device)) {
42             addDatapoint(device, 0, getName(), HmValueType.INTEGER, getRssiValue(device.getChannel(0)), true);
43         }
44     }
45
46     @Override
47     public void handleEvent(VirtualGateway gateway, HmDatapoint dp) {
48         HmChannel channel = dp.getChannel();
49         Integer value = getRssiValue(channel);
50
51         if (value != null) {
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);
55
56             HmDatapoint vdpRssi = getVirtualDatapoint(channel);
57             vdpRssi.setValue(strength);
58         }
59     }
60 }