]> git.basschouten.com Git - openhab-addons.git/blob
4dbc86d69e0b0aedf0013c49ba1f41ac6c09b304
[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 /*
14  * This file is based on:
15  *
16  * ServiceCommand
17  * Connect SDK
18  *
19  * Copyright (c) 2014 LG Electronics.
20  * Created by Hyun Kook Khang on 19 Jan 2014
21  *
22  * Licensed under the Apache License, Version 2.0 (the "License");
23  * you may not use this file except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *     http://www.apache.org/licenses/LICENSE-2.0
27  *
28  * Unless required by applicable law or agreed to in writing, software
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  */
34
35 package org.openhab.binding.lgwebos.internal.handler.command;
36
37 import java.util.function.Function;
38
39 import org.openhab.binding.lgwebos.internal.handler.core.ResponseListener;
40
41 import com.google.gson.JsonElement;
42 import com.google.gson.JsonObject;
43
44 /**
45  * Internal implementation of ServiceCommand for URL-based commands
46  *
47  * @author Hyun Kook Khang - Connect SDK initial contribution
48  * @author Sebastian Prehn - Adoption for openHAB
49  */
50 public class ServiceCommand<T> {
51
52     protected enum Type {
53         request,
54         subscribe
55     }
56
57     protected Type type;
58     protected JsonObject payload;
59     protected String target;
60     protected Function<JsonObject, T> converter;
61
62     ResponseListener<T> responseListener;
63
64     public ServiceCommand(String targetURL, JsonObject payload, Function<JsonObject, T> converter,
65             ResponseListener<T> listener) {
66         this.target = targetURL;
67         this.payload = payload;
68         this.converter = converter;
69         this.responseListener = listener;
70         this.type = Type.request;
71     }
72
73     public JsonElement getPayload() {
74         return payload;
75     }
76
77     public String getType() {
78         return type.name();
79     }
80
81     public String getTarget() {
82         return target;
83     }
84
85     public void processResponse(JsonObject response) {
86         this.getResponseListener().onSuccess(this.converter.apply(response));
87     }
88
89     public void processError(String error) {
90         this.getResponseListener().onError(error);
91     }
92
93     public ResponseListener<T> getResponseListener() {
94         return responseListener;
95     }
96
97     @Override
98     public String toString() {
99         return "ServiceCommand [type=" + type + ", target=" + target + ", payload=" + payload + "]";
100     }
101 }