2 * Copyright (c) 2010-2023 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.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;
23 * Parses the response from a RPC call, throws exception if fault response.
25 * @author Gerhard Riegler - Initial contribution
27 public class RpcResponseParser extends CommonRpcParser<Object[], Object[]> {
28 private RpcRequest<?> request;
30 public RpcResponseParser(RpcRequest<?> request) {
31 this.request = request;
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);
50 throw new IOException(faultMessage);
55 throw new IOException("Unknown Result: " + message);