2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.homematic.internal.communicator.parser;
15 import java.io.IOException;
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;
24 * Parses the response from a RPC call, throws exception if fault response.
26 * @author Gerhard Riegler - Initial contribution
28 public class RpcResponseParser extends CommonRpcParser<Object[], Object[]> {
29 private RpcRequest<?> request;
31 public RpcResponseParser(RpcRequest<?> request) {
32 this.request = request;
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);
51 throw new IOException(faultMessage);
56 throw new IOException("Unknown Result: " + message);