]> git.basschouten.com Git - openhab-addons.git/blob
347f8eb4ca3e56a5444cc0626972944944356cb2
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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 java.io.IOException;
18
19 import org.apache.commons.lang.StringUtils;
20 import org.openhab.binding.homematic.internal.communicator.parser.DisplayOptionsParser;
21 import org.openhab.binding.homematic.internal.misc.HomematicClientException;
22 import org.openhab.binding.homematic.internal.model.HmChannel;
23 import org.openhab.binding.homematic.internal.model.HmDatapoint;
24 import org.openhab.binding.homematic.internal.model.HmDatapointConfig;
25 import org.openhab.binding.homematic.internal.model.HmDatapointInfo;
26 import org.openhab.binding.homematic.internal.model.HmDevice;
27 import org.openhab.binding.homematic.internal.model.HmInterface;
28 import org.openhab.binding.homematic.internal.model.HmValueType;
29
30 /**
31  * A virtual String datapoint to control the display of a 19 button remote control. You can send a text and/or show
32  * symbols, turn on the backlight and let the remote control beep.
33  *
34  * @author Gerhard Riegler - Initial contribution
35  */
36 public class DisplayOptionsVirtualDatapointHandler extends AbstractVirtualDatapointHandler {
37     @Override
38     public String getName() {
39         return VIRTUAL_DATAPOINT_NAME_DISPLAY_OPTIONS;
40     }
41
42     @Override
43     public void initialize(HmDevice device) {
44         if (device.getType().startsWith(DEVICE_TYPE_19_REMOTE_CONTROL)
45                 && !(device.getHmInterface() == HmInterface.CUXD)) {
46             addDatapoint(device, 18, getName(), HmValueType.STRING, null, false);
47         }
48     }
49
50     @Override
51     public boolean canHandleCommand(HmDatapoint dp, Object value) {
52         return getName().equals(dp.getName());
53     }
54
55     @Override
56     public void handleCommand(VirtualGateway gateway, HmDatapoint dp, HmDatapointConfig dpConfig, Object value)
57             throws IOException, HomematicClientException {
58         HmChannel channel = dp.getChannel();
59
60         DisplayOptionsParser rcOptionsParser = new DisplayOptionsParser(channel);
61         rcOptionsParser.parse(value);
62
63         if (StringUtils.isNotBlank(rcOptionsParser.getText())) {
64             sendDatapoint(gateway, channel, DATAPOINT_NAME_TEXT, rcOptionsParser.getText());
65         }
66
67         sendDatapoint(gateway, channel, DATAPOINT_NAME_BEEP, rcOptionsParser.getBeep());
68         sendDatapoint(gateway, channel, DATAPOINT_NAME_UNIT, rcOptionsParser.getUnit());
69         sendDatapoint(gateway, channel, DATAPOINT_NAME_BACKLIGHT, rcOptionsParser.getBacklight());
70
71         for (String symbol : rcOptionsParser.getSymbols()) {
72             sendDatapoint(gateway, channel, symbol, Boolean.TRUE);
73         }
74
75         sendDatapoint(gateway, channel, DATAPOINT_NAME_SUBMIT, Boolean.TRUE);
76         dp.setValue(value);
77     }
78
79     private void sendDatapoint(VirtualGateway gateway, HmChannel channel, String dpName, Object newValue)
80             throws IOException, HomematicClientException {
81         HmDatapointInfo dpInfo = HmDatapointInfo.createValuesInfo(channel, dpName);
82         HmDatapoint dp = gateway.getDatapoint(dpInfo);
83         gateway.sendDatapoint(dp, new HmDatapointConfig(), newValue, null);
84     }
85 }