]> git.basschouten.com Git - openhab-addons.git/blob
a6bf35396c045c8a6c7fbd62f2dae5408b7b9941
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.server;
14
15 import static org.openhab.binding.homematic.internal.misc.HomematicConstants.*;
16
17 import java.io.IOException;
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.Objects;
22
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;
30
31 /**
32  * Common RPC response methods.
33  *
34  * @author Gerhard Riegler - Initial contribution
35  */
36
37 public abstract class RpcResponseHandler<T> {
38     private final Logger logger = LoggerFactory.getLogger(RpcResponseHandler.class);
39
40     private RpcEventListener listener;
41
42     public RpcResponseHandler(RpcEventListener listener) {
43         this.listener = listener;
44     }
45
46     /**
47      * Returns a valid result of the method called by the Homematic gateway.
48      */
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;
65                 if (call != null) {
66                     String method = Objects.toString(call.get("methodName"), "");
67                     Object[] data = (Object[]) call.get("params");
68                     handleMethodCall(method, data);
69                 }
70             }
71             return getEmptyEventListResult();
72         } else if (RPC_METHODNAME_SET_CONFIG_READY.equals(methodName)) {
73             return getEmptyEventListResult();
74         } else {
75             logger.warn("Unknown method called by Homematic gateway: {}", methodName);
76             return getEmptyEventListResult();
77         }
78     }
79
80     /**
81      * Creates a BINRPC message with the supported method names.
82      */
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);
89         return events;
90     }
91
92     /**
93      * Populates the extracted event to the listener.
94      */
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();
100     }
101
102     /**
103      * Calls the listener when a devices has been detected.
104      */
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();
110     }
111
112     /**
113      * Calls the listener when devices has been deleted.
114      */
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();
120     }
121
122     /**
123      * Returns a predefined result for an empty string.
124      */
125     protected abstract T getEmptyStringResult();
126
127     /**
128      * Returns a predefined result for an empty array.
129      */
130     protected abstract T getEmptyArrayResult();
131
132     /**
133      * Returns a predefined result for an empty event list.
134      */
135     protected abstract T getEmptyEventListResult();
136
137     /**
138      * Creates a typed RpcRequest.
139      */
140     protected abstract RpcRequest<T> createRpcRequest();
141 }