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