]> git.basschouten.com Git - openhab-addons.git/blob
2fa168418b975ecb798a18224f219818701eb0e9
[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 import java.util.Map;
17
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
23 /**
24  * Parses a Homegear message with variables and generates the datapoints.
25  *
26  * @author Gerhard Riegler - Initial contribution
27  */
28 public class GetAllSystemVariablesParser extends CommonRpcParser<Object[], Void> {
29     private HmChannel channel;
30
31     public GetAllSystemVariablesParser(HmChannel channel) {
32         this.channel = channel;
33     }
34
35     @Override
36     @SuppressWarnings("unchecked")
37     public Void parse(Object[] message) throws IOException {
38         Map<String, ?> mapMessage = (Map<String, ?>) message[0];
39         for (String variableName : mapMessage.keySet()) {
40             Object value = mapMessage.get(variableName);
41             HmDatapoint dp = channel.getDatapoint(HmParamsetType.VALUES, variableName);
42             if (dp != null) {
43                 dp.setValue(value);
44             } else {
45                 HmDatapoint dpVariable = new HmDatapoint(variableName, variableName, guessType(value), value, false,
46                         HmParamsetType.VALUES);
47                 dpVariable.setInfo(variableName);
48                 channel.addDatapoint(dpVariable);
49             }
50         }
51         return null;
52     }
53
54     /**
55      * Guesses the value type.
56      */
57     private HmValueType guessType(Object value) {
58         if (value == null) {
59             return HmValueType.UNKNOWN;
60         } else if (value instanceof Boolean) {
61             return HmValueType.BOOL;
62         } else if (value instanceof Integer || value instanceof Long) {
63             return HmValueType.INTEGER;
64         } else if (value instanceof Number) {
65             return HmValueType.FLOAT;
66         } else {
67             return HmValueType.STRING;
68         }
69     }
70 }