]> git.basschouten.com Git - openhab-addons.git/blob
9ccff4951adb2e2c226ed80123b0550a1905dc16
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.communicator.client.UnknownParameterSetException;
19 import org.openhab.binding.homematic.internal.communicator.client.UnknownRpcFailureException;
20 import org.openhab.binding.homematic.internal.communicator.message.RpcRequest;
21
22 /**
23  * Parses the response from a RPC call, throws exception if fault response.
24  *
25  * @author Gerhard Riegler - Initial contribution
26  */
27 public class RpcResponseParser extends CommonRpcParser<Object[], Object[]> {
28     private RpcRequest<?> request;
29
30     public RpcResponseParser(RpcRequest<?> request) {
31         this.request = request;
32     }
33
34     @Override
35     @SuppressWarnings("unchecked")
36     public Object[] parse(Object[] message) throws IOException {
37         if (message != null && message.length > 0) {
38             Object responseData = message[0];
39             if (responseData instanceof Map) {
40                 Map<String, Object> map = (Map<String, Object>) responseData;
41                 if (map.containsKey("faultCode")) {
42                     Number faultCode = toNumber(map.get("faultCode"));
43                     String faultString = toString(map.get("faultString"));
44                     String faultMessage = String.format("%s %s (sending %s)", faultCode, faultString, request);
45                     if (faultCode.intValue() == -1 && "Failure".equals(faultString)) {
46                         throw new UnknownRpcFailureException(faultMessage);
47                     } else if (faultCode.intValue() == -3 && "Unknown paramset".equals(faultString)) {
48                         throw new UnknownParameterSetException(faultMessage);
49                     }
50                     throw new IOException(faultMessage);
51                 }
52             }
53             return message;
54         }
55         throw new IOException("Unknown Result: " + message);
56     }
57 }