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