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.*;
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;
24 * A virtual datapoint that unifies the device and peer rssi datapoints.
26 * @author Gerhard Riegler - Initial contribution
28 public class RssiVirtualDatapointHandler extends AbstractVirtualDatapointHandler {
30 public String getName() {
31 return VIRTUAL_DATAPOINT_NAME_RSSI;
35 public void initialize(HmDevice device) {
36 if (isWirelessDevice(device)) {
37 HmDatapoint dp = addDatapoint(device, 0, getName(), HmValueType.INTEGER, getRssiValue(device.getChannel(0)),
40 dp.setMinValue(Integer.MIN_VALUE);
41 dp.setMaxValue(Integer.MAX_VALUE);
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()));
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);
60 * Returns either the device or the peer rssi value.
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);
66 Integer deviceValue = getDatapointValue(dpRssiDevice);
67 Integer peerValue = getDatapointValue(dpRssiPeer);
69 if ((deviceValue == null || deviceValue == 0) && peerValue != null) {
75 private Integer getDatapointValue(HmDatapoint dp) {
76 if (dp == null || dp.getValue() == null || (Integer) dp.getValue() == 0) {
80 return (Integer) dp.getValue();
83 protected boolean isWirelessDevice(HmDevice device) {
84 return device.getChannel(0).getDatapoint(HmParamsetType.VALUES, DATAPOINT_NAME_RSSI_DEVICE) != null;