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