]> git.basschouten.com Git - openhab-addons.git/blob
53bcc7844a6f1ecceed08c32fa743a31651c235d
[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 java.io.IOException;
16
17 import org.openhab.binding.homematic.internal.misc.HomematicClientException;
18 import org.openhab.binding.homematic.internal.model.HmChannel;
19 import org.openhab.binding.homematic.internal.model.HmDatapoint;
20 import org.openhab.binding.homematic.internal.model.HmDatapointConfig;
21 import org.openhab.binding.homematic.internal.model.HmDevice;
22 import org.openhab.binding.homematic.internal.model.HmParamsetType;
23 import org.openhab.binding.homematic.internal.model.HmValueType;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * Abstract base class for all virtual datapoints with common methods.
29  *
30  * @author Gerhard Riegler - Initial contribution
31  */
32 public abstract class AbstractVirtualDatapointHandler implements VirtualDatapointHandler {
33     private final Logger logger = LoggerFactory.getLogger(AbstractVirtualDatapointHandler.class);
34
35     @Override
36     public boolean canHandleCommand(HmDatapoint dp, Object value) {
37         return false;
38     }
39
40     @Override
41     public void handleCommand(VirtualGateway gateway, HmDatapoint dp, HmDatapointConfig dpConfig, Object value)
42             throws IOException, HomematicClientException {
43     }
44
45     @Override
46     public boolean canHandleEvent(HmDatapoint dp) {
47         return false;
48     }
49
50     @Override
51     public void handleEvent(VirtualGateway gateway, HmDatapoint dp) {
52     }
53
54     @Override
55     public HmDatapoint getVirtualDatapoint(HmChannel channel) {
56         return channel.getDatapoint(HmParamsetType.VALUES, getName());
57     }
58
59     /**
60      * Creates a new datapoint with the given parameters and adds it to the channel.
61      */
62     protected HmDatapoint addDatapoint(HmDevice device, Integer channelNumber, String datapointName,
63             HmValueType valueType, Object value, boolean readOnly) {
64         HmChannel channel = device.getChannel(channelNumber);
65         HmDatapoint dp = new HmDatapoint(datapointName, datapointName, valueType, value, readOnly,
66                 HmParamsetType.VALUES);
67         return addDatapoint(channel, dp);
68     }
69
70     /**
71      * Adds a new datapoint to the channel.
72      */
73     protected HmDatapoint addDatapoint(HmChannel channel, HmDatapoint dp) {
74         logger.trace("Adding virtual datapoint '{}' to device '{}' ({}) and channel {}", dp.getName(),
75                 channel.getDevice().getAddress(), channel.getDevice().getType(), channel.getNumber());
76         dp.setVirtual(true);
77         dp.setReadable(true);
78         channel.addDatapoint(dp);
79         return dp;
80     }
81 }