]> git.basschouten.com Git - openhab-addons.git/blob
e723bed0bab1c78cf8f065302d19e69cf28646cd
[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.*;
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.HmParamsetType;
21 import org.openhab.binding.homematic.internal.model.HmValueType;
22
23 /**
24  * A virtual datapoint that unifies the device and peer rssi datapoints.
25  *
26  * @author Gerhard Riegler - Initial contribution
27  */
28 public class RssiVirtualDatapointHandler extends AbstractVirtualDatapointHandler {
29     @Override
30     public String getName() {
31         return VIRTUAL_DATAPOINT_NAME_RSSI;
32     }
33
34     @Override
35     public void initialize(HmDevice device) {
36         if (isWirelessDevice(device)) {
37             HmDatapoint dp = addDatapoint(device, 0, getName(), HmValueType.INTEGER, getRssiValue(device.getChannel(0)),
38                     true);
39             dp.setUnit("dBm");
40             dp.setMinValue(Integer.MIN_VALUE);
41             dp.setMaxValue(Integer.MAX_VALUE);
42         }
43     }
44
45     @Override
46     public boolean canHandleEvent(HmDatapoint dp) {
47         return isWirelessDevice(dp.getChannel().getDevice())
48                 && (DATAPOINT_NAME_RSSI_DEVICE.equals(dp.getName()) || DATAPOINT_NAME_RSSI_PEER.equals(dp.getName()));
49     }
50
51     @Override
52     public void handleEvent(VirtualGateway gateway, HmDatapoint dp) {
53         HmChannel channel = dp.getChannel();
54         Object value = getRssiValue(channel);
55         HmDatapoint vdpRssi = getVirtualDatapoint(channel);
56         vdpRssi.setValue(value);
57     }
58
59     /**
60      * Returns either the device or the peer rssi value.
61      */
62     protected Integer getRssiValue(HmChannel channel) {
63         HmDatapoint dpRssiDevice = channel.getDatapoint(HmParamsetType.VALUES, DATAPOINT_NAME_RSSI_DEVICE);
64         HmDatapoint dpRssiPeer = channel.getDatapoint(HmParamsetType.VALUES, DATAPOINT_NAME_RSSI_PEER);
65
66         Integer deviceValue = getDatapointValue(dpRssiDevice);
67         Integer peerValue = getDatapointValue(dpRssiPeer);
68
69         if ((deviceValue == null || deviceValue == 0) && peerValue != null) {
70             return peerValue;
71         }
72         return deviceValue;
73     }
74
75     private Integer getDatapointValue(HmDatapoint dp) {
76         if (dp == null || dp.getValue() == null || (Integer) dp.getValue() == 0) {
77             return null;
78         }
79
80         return (Integer) dp.getValue();
81     }
82
83     protected boolean isWirelessDevice(HmDevice device) {
84         return device.getChannel(0).getDatapoint(HmParamsetType.VALUES, DATAPOINT_NAME_RSSI_DEVICE) != null;
85     }
86 }