]> git.basschouten.com Git - openhab-addons.git/blob
51088c9dd6e96d0589c5d9662166223f1f95e994
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.eclipse.jdt.annotation.Nullable;
40 import org.openhab.binding.lgwebos.internal.handler.core.ResponseListener;
41
42 import com.google.gson.JsonElement;
43 import com.google.gson.JsonObject;
44
45 /**
46  * Internal implementation of ServiceCommand for URL-based commands
47  *
48  * @author Hyun Kook Khang - Connect SDK initial contribution
49  * @author Sebastian Prehn - Adoption for openHAB
50  */
51 public class ServiceCommand<T> {
52
53     protected enum Type {
54         request,
55         subscribe
56     }
57
58     protected Type type;
59     protected JsonObject payload;
60     protected String target;
61     protected Function<JsonObject, @Nullable T> converter;
62
63     ResponseListener<T> responseListener;
64
65     public ServiceCommand(String targetURL, JsonObject payload, Function<JsonObject, @Nullable T> converter,
66             ResponseListener<T> listener) {
67         this.target = targetURL;
68         this.payload = payload;
69         this.converter = converter;
70         this.responseListener = listener;
71         this.type = Type.request;
72     }
73
74     public JsonElement getPayload() {
75         return payload;
76     }
77
78     public String getType() {
79         return type.name();
80     }
81
82     public String getTarget() {
83         return target;
84     }
85
86     public void processResponse(JsonObject response) {
87         this.getResponseListener().onSuccess(this.converter.apply(response));
88     }
89
90     public void processError(String error) {
91         this.getResponseListener().onError(error);
92     }
93
94     public ResponseListener<T> getResponseListener() {
95         return responseListener;
96     }
97
98     @Override
99     public String toString() {
100         return "ServiceCommand [type=" + type + ", target=" + target + ", payload=" + payload + "]";
101     }
102 }