]> git.basschouten.com Git - openhab-addons.git/blob
10a4d610220b20d099585c5b6b5f7c34ee71fb08
[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.message;
14
15 import java.util.Map;
16
17 /**
18  * Helper class with common RPC funtions.
19  *
20  * @author Gerhard Riegler - Initial contribution
21  */
22 public class RpcUtils {
23
24     /**
25      * Dumps decoded RPC data.
26      */
27     public static String dumpRpcMessage(String methodName, Object[] responseData) {
28         StringBuilder sb = new StringBuilder();
29         if (methodName != null) {
30             sb.append(methodName);
31             sb.append("()\n");
32         }
33         dumpCollection(responseData, sb, 0);
34         return sb.toString();
35     }
36
37     private static void dumpCollection(Object[] c, StringBuilder sb, int indent) {
38         if (indent > 0) {
39             for (int in = 0; in < indent - 1; in++) {
40                 sb.append('\t');
41             }
42             sb.append("[\n");
43         }
44         for (Object o : c) {
45             if (o instanceof Map) {
46                 dumpMap((Map<?, ?>) o, sb, indent + 1);
47             } else if (o instanceof Object[]) {
48                 dumpCollection((Object[]) o, sb, indent + 1);
49             } else {
50                 for (int in = 0; in < indent; in++) {
51                     sb.append('\t');
52                 }
53                 sb.append(o);
54                 sb.append('\n');
55             }
56         }
57         if (indent > 0) {
58             for (int in = 0; in < indent - 1; in++) {
59                 sb.append('\t');
60             }
61             sb.append("]\n");
62         }
63     }
64
65     private static void dumpMap(Map<?, ?> c, StringBuilder sb, int indent) {
66         if (indent > 0) {
67             for (int in = 0; in < indent - 1; in++) {
68                 sb.append('\t');
69             }
70             sb.append("{\n");
71         }
72         for (Map.Entry<?, ?> me : c.entrySet()) {
73             Object o = me.getValue();
74             for (int in = 0; in < indent; in++) {
75                 sb.append('\t');
76             }
77             sb.append(me.getKey());
78             sb.append('=');
79             if (o instanceof Map<?, ?>) {
80                 sb.append("\n");
81                 dumpMap((Map<?, ?>) o, sb, indent + 1);
82             } else if (o instanceof Object[]) {
83                 sb.append("\n");
84                 dumpCollection((Object[]) o, sb, indent + 1);
85             } else {
86                 sb.append(o);
87                 sb.append('\n');
88             }
89         }
90         if (indent > 0) {
91             for (int in = 0; in < indent - 1; in++) {
92                 sb.append('\t');
93             }
94             sb.append("}\n");
95         }
96     }
97 }