]> git.basschouten.com Git - openhab-addons.git/blob
5cdb93d81b6e99706b3408f78f814675fc69ac8b
[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.parser;
14
15 import java.io.IOException;
16
17 import org.apache.commons.lang.StringUtils;
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.HmParamsetType;
21 import org.openhab.binding.homematic.internal.model.HmValueType;
22 import org.openhab.binding.homematic.internal.model.TclScriptDataEntry;
23 import org.openhab.binding.homematic.internal.model.TclScriptDataList;
24
25 /**
26  * Parses a TclRega script result containing variables and scripts.
27  *
28  * @author Gerhard Riegler - Initial contribution
29  */
30 public class CcuVariablesAndScriptsParser extends CommonRpcParser<TclScriptDataList, Void> {
31     private HmChannel channel;
32
33     public CcuVariablesAndScriptsParser(HmChannel channel) {
34         this.channel = channel;
35     }
36
37     @Override
38     public Void parse(TclScriptDataList resultList) throws IOException {
39         if (resultList.getEntries() != null) {
40             for (TclScriptDataEntry entry : resultList.getEntries()) {
41                 HmDatapoint dp = channel.getDatapoint(HmParamsetType.VALUES, entry.name);
42                 if (dp != null) {
43                     dp.setValue(convertToType(entry.value));
44                 } else {
45                     dp = new HmDatapoint();
46                     dp.setName(entry.name);
47                     dp.setInfo(entry.name);
48                     dp.setDescription(entry.description);
49                     dp.setType(HmValueType.parse(entry.valueType));
50                     dp.setValue(convertToType(entry.value));
51                     if (dp.isIntegerType()) {
52                         dp.setMinValue(toInteger(entry.minValue));
53                         dp.setMaxValue(toInteger(entry.maxValue));
54                     } else {
55                         dp.setMinValue(toDouble(entry.minValue));
56                         dp.setMaxValue(toDouble(entry.maxValue));
57                     }
58                     dp.setReadOnly(entry.readOnly);
59                     dp.setUnit(entry.unit);
60
61                     String[] result = StringUtils.splitByWholeSeparatorPreserveAllTokens(entry.options, ";");
62                     dp.setOptions(result == null || result.length == 0 ? null : result);
63
64                     if (dp.getOptions() != null) {
65                         dp.setMinValue(0);
66                         dp.setMaxValue(dp.getOptions().length - 1);
67                     }
68
69                     dp.setParamsetType(HmParamsetType.VALUES);
70                     channel.addDatapoint(dp);
71                 }
72             }
73         }
74         return null;
75     }
76 }