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.parser;
15 import static org.openhab.binding.homematic.internal.misc.HomematicConstants.*;
17 import java.io.IOException;
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.List;
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.HmDatapointInfo;
25 import org.openhab.binding.homematic.internal.model.HmParamsetType;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 * Extracts the possible options from the remote control metadata and parses the DISPLAY_OPTIONS virtual datapoint.
32 * @author Gerhard Riegler - Initial contribution
35 public class DisplayOptionsParser extends CommonRpcParser<Object, Void> {
36 private final Logger logger = LoggerFactory.getLogger(DisplayOptionsParser.class);
37 private static final String[] onOff = new String[] { "ON", "OFF" };
38 private static final int IDX_NOT_FOUND = -1;
39 private HmChannel channel;
42 private int backlight = 0;
44 private List<String> symbols = new ArrayList<>();
46 public DisplayOptionsParser(HmChannel channel) {
47 this.channel = channel;
51 public Void parse(Object value) throws IOException {
52 String valueString = toString(value);
53 String optionsString = valueString == null ? null : valueString.replace(" ", "");
54 if (optionsString != null) {
55 int idxFirstSep = optionsString.indexOf(",");
56 if (idxFirstSep == -1) {
60 text = optionsString.substring(0, idxFirstSep);
61 optionsString = optionsString.substring(idxFirstSep + 1);
64 String[] options = optionsString.split(",");
66 String[] availableSymbols = getAvailableSymbols(channel);
67 String[] availableBeepOptions = getAvailableOptions(channel, DATAPOINT_NAME_BEEP);
68 String[] availableBacklightOptions = getAvailableOptions(channel, DATAPOINT_NAME_BACKLIGHT);
69 String[] availableUnitOptions = getAvailableOptions(channel, DATAPOINT_NAME_UNIT);
71 String deviceAddress = channel.getDevice().getAddress();
72 if (logger.isDebugEnabled()) {
73 logger.debug("Remote control '{}' supports these beep options: {}", deviceAddress,
74 availableBeepOptions);
75 logger.debug("Remote control '{}' supports these backlight options: {}", deviceAddress,
76 availableBacklightOptions);
77 logger.debug("Remote control '{}' supports these unit options: {}", deviceAddress,
78 availableUnitOptions);
79 logger.debug("Remote control '{}' supports these symbols: {}", deviceAddress, symbols);
82 if (options != null) {
83 for (String parameter : options) {
84 logger.debug("Parsing remote control option '{}'", parameter);
85 beep = getIntParameter(availableBeepOptions, beep, parameter, DATAPOINT_NAME_BEEP, deviceAddress);
86 backlight = getIntParameter(availableBacklightOptions, backlight, parameter,
87 DATAPOINT_NAME_BACKLIGHT, deviceAddress);
88 unit = getIntParameter(availableUnitOptions, unit, parameter, DATAPOINT_NAME_UNIT, deviceAddress);
90 if (findInArray(availableSymbols, parameter) != IDX_NOT_FOUND) {
91 logger.debug("Symbol '{}' found for remote control '{}'", parameter, deviceAddress);
92 symbols.add(parameter);
101 * Returns the first found parameter index of the options.
103 private int getIntParameter(String[] options, int currentValue, String parameter, String parameterName,
104 String deviceAddress) {
105 int idx = findInArray(options, parameter);
106 if (idx != IDX_NOT_FOUND) {
107 if (currentValue == 0) {
108 logger.debug("{} option '{}' found at index {} for remote control '{}'", parameterName, parameter,
109 idx + 1, deviceAddress);
112 logger.warn("{} option already set for remote control '{}', ignoring '{}'!", parameterName,
113 deviceAddress, parameter);
122 * Returns all possible options from the given datapoint.
124 private String[] getAvailableOptions(HmChannel channel, String datapointName) {
125 HmDatapointInfo dpInfo = HmDatapointInfo.createValuesInfo(channel, datapointName);
126 HmDatapoint dp = channel.getDatapoint(dpInfo);
128 String[] dpOpts = dp.getOptions();
129 String[] options = new String[dpOpts.length - 1];
130 options = Arrays.copyOfRange(dpOpts, 1, dpOpts.length);
131 for (String onOffString : onOff) {
132 int onIdx = findInArray(options, onOffString);
133 if (onIdx != IDX_NOT_FOUND) {
134 options[onIdx] = datapointName + "_" + onOffString;
139 return new String[0];
142 private int findInArray(String[] arr, String searchString) {
143 if (arr.length == 0) {
144 return IDX_NOT_FOUND;
146 for (int i = 0; i < arr.length; i++) {
147 if (arr[i].equals(searchString)) {
151 return IDX_NOT_FOUND;
155 * Returns all possible symbols from the remote control.
157 private String[] getAvailableSymbols(HmChannel channel) {
158 List<String> symbols = new ArrayList<>();
159 for (HmDatapoint datapoint : channel.getDatapoints()) {
160 if (!datapoint.isReadOnly() && datapoint.isBooleanType()
161 && datapoint.getParamsetType() == HmParamsetType.VALUES
162 && !DATAPOINT_NAME_SUBMIT.equals(datapoint.getName())
163 && !DATAPOINT_NAME_INSTALL_TEST.equals(datapoint.getName())) {
164 symbols.add(datapoint.getName());
167 return symbols.toArray(new String[0]);
171 * Returns the parsed text.
173 public String getText() {
178 * Returns the parsed beep value.
180 public int getBeep() {
185 * Returns the parsed backlight value.
187 public int getBacklight() {
192 * Returns the parsed unit value.
194 public int getUnit() {
199 * Returns the parsed symbols.
201 public List<String> getSymbols() {