]> git.basschouten.com Git - openhab-addons.git/blob
6923b54ba81892b84bd4c3c1971e34b9f1543270
[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.daikin.internal.api;
14
15 import java.io.UnsupportedEncodingException;
16 import java.net.URLDecoder;
17 import java.nio.charset.StandardCharsets;
18 import java.util.Map;
19 import java.util.Optional;
20 import java.util.stream.Collectors;
21 import java.util.stream.Stream;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * Class for parsing the comma separated values and array values returned by the Daikin Controller.
29  *
30  * @author Jimmy Tanagra - Initial Contribution
31  *         urldecode the parsed value
32  *
33  */
34 @NonNullByDefault
35 public class InfoParser {
36     private static final Logger LOGGER = LoggerFactory.getLogger(InfoParser.class);
37
38     private InfoParser() {
39     }
40
41     public static Map<String, String> parse(String response) {
42         return Stream.of(response.split(",")).filter(kv -> kv.contains("=")).map(kv -> {
43             String[] keyValue = kv.split("=");
44             String key = keyValue[0];
45             String value = keyValue.length > 1 ? urldecode(keyValue[1]) : "";
46             return new String[] { key, value };
47         }).collect(Collectors.toMap(x -> x[0], x -> x[1]));
48     }
49
50     public static Optional<Double> parseDouble(String value) {
51         if ("-".equals(value)) {
52             return Optional.empty();
53         }
54         try {
55             return Optional.of(Double.parseDouble(value));
56         } catch (NumberFormatException e) {
57             return Optional.empty();
58         }
59     }
60
61     public static Optional<Integer> parseInt(String value) {
62         if ("-".equals(value)) {
63             return Optional.empty();
64         }
65         try {
66             return Optional.of(Integer.parseInt(value));
67         } catch (NumberFormatException e) {
68             return Optional.empty();
69         }
70     }
71
72     public static Optional<Integer[]> parseArrayOfInt(String value) {
73         if ("-".equals(value)) {
74             return Optional.empty();
75         }
76         try {
77             return Optional.of(Stream.of(value.split("/")).map(val -> Integer.parseInt(val)).toArray(Integer[]::new));
78         } catch (NumberFormatException e) {
79             return Optional.empty();
80         }
81     }
82
83     public static Optional<Integer[]> parseArrayOfInt(String value, int expectedArraySize) {
84         Optional<Integer[]> result = parseArrayOfInt(value);
85         if (result.isPresent() && result.get().length == expectedArraySize) {
86             return result;
87         }
88         return Optional.empty();
89     }
90
91     public static String urldecode(String value) {
92         try {
93             return URLDecoder.decode(value, StandardCharsets.UTF_8.toString());
94         } catch (UnsupportedEncodingException e) {
95             LOGGER.warn("Unsupported encoding error in '{}'", value, e);
96             return value;
97         }
98     }
99 }