2 * Copyright (c) 2010-2022 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.server;
15 import static org.openhab.binding.homematic.internal.misc.HomematicConstants.*;
17 import java.io.IOException;
18 import java.util.ArrayList;
19 import java.util.List;
21 import java.util.Objects;
23 import org.openhab.binding.homematic.internal.communicator.message.RpcRequest;
24 import org.openhab.binding.homematic.internal.communicator.parser.DeleteDevicesParser;
25 import org.openhab.binding.homematic.internal.communicator.parser.EventParser;
26 import org.openhab.binding.homematic.internal.communicator.parser.NewDevicesParser;
27 import org.openhab.binding.homematic.internal.model.HmDatapointInfo;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * Common RPC response methods.
34 * @author Gerhard Riegler - Initial contribution
37 public abstract class RpcResponseHandler<T> {
38 private final Logger logger = LoggerFactory.getLogger(RpcResponseHandler.class);
40 private RpcEventListener listener;
42 public RpcResponseHandler(RpcEventListener listener) {
43 this.listener = listener;
47 * Returns a valid result of the method called by the Homematic gateway.
49 public T handleMethodCall(String methodName, Object[] responseData) throws IOException {
50 if (RPC_METHODNAME_EVENT.equals(methodName)) {
51 return handleEvent(responseData);
52 } else if (RPC_METHODNAME_LIST_DEVICES.equals(methodName) || RPC_METHODNAME_UPDATE_DEVICE.equals(methodName)) {
53 return getEmptyArrayResult();
54 } else if (RPC_METHODNAME_DELETE_DEVICES.equals(methodName)) {
55 return handleDeleteDevice(responseData);
56 } else if (RPC_METHODNAME_NEW_DEVICES.equals(methodName)) {
57 return handleNewDevice(responseData);
58 } else if (RPC_METHODNAME_SYSTEM_LISTMETHODS.equals(methodName)) {
59 RpcRequest<T> msg = createRpcRequest();
60 msg.addArg(getListMethods());
61 return msg.createMessage();
62 } else if (RPC_METHODNAME_SYSTEM_MULTICALL.equals(methodName)) {
63 for (Object o : (Object[]) responseData[0]) {
64 Map<?, ?> call = (Map<?, ?>) o;
66 String method = Objects.toString(call.get("methodName"), "");
67 Object[] data = (Object[]) call.get("params");
68 handleMethodCall(method, data);
71 return getEmptyEventListResult();
72 } else if (RPC_METHODNAME_SET_CONFIG_READY.equals(methodName)) {
73 return getEmptyEventListResult();
75 logger.warn("Unknown method called by Homematic gateway: {}", methodName);
76 return getEmptyEventListResult();
81 * Creates a BINRPC message with the supported method names.
83 private List<String> getListMethods() {
84 List<String> events = new ArrayList<>();
85 events.add(RPC_METHODNAME_SYSTEM_MULTICALL);
86 events.add(RPC_METHODNAME_EVENT);
87 events.add(RPC_METHODNAME_DELETE_DEVICES);
88 events.add(RPC_METHODNAME_NEW_DEVICES);
93 * Populates the extracted event to the listener.
95 private T handleEvent(Object[] message) throws IOException {
96 EventParser eventParser = new EventParser();
97 HmDatapointInfo dpInfo = eventParser.parse(message);
98 listener.eventReceived(dpInfo, eventParser.getValue());
99 return getEmptyStringResult();
103 * Calls the listener when a devices has been detected.
105 private T handleNewDevice(Object[] message) throws IOException {
106 NewDevicesParser ndParser = new NewDevicesParser();
107 List<String> adresses = ndParser.parse(message);
108 listener.newDevices(adresses);
109 return getEmptyArrayResult();
113 * Calls the listener when devices has been deleted.
115 private T handleDeleteDevice(Object[] message) throws IOException {
116 DeleteDevicesParser ddParser = new DeleteDevicesParser();
117 List<String> adresses = ddParser.parse(message);
118 listener.deleteDevices(adresses);
119 return getEmptyArrayResult();
123 * Returns a predefined result for an empty string.
125 protected abstract T getEmptyStringResult();
128 * Returns a predefined result for an empty array.
130 protected abstract T getEmptyArrayResult();
133 * Returns a predefined result for an empty event list.
135 protected abstract T getEmptyEventListResult();
138 * Creates a typed RpcRequest.
140 protected abstract RpcRequest<T> createRpcRequest();