2 * Copyright (c) 2010-2024 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
14 * This file is based on:
19 * Copyright (c) 2014 LG Electronics.
20 * Created by Hyun Kook Khang on 19 Jan 2014
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
26 * http://www.apache.org/licenses/LICENSE-2.0
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.
35 package org.openhab.binding.lgwebos.internal.handler.command;
37 import java.util.function.Function;
39 import org.eclipse.jdt.annotation.Nullable;
40 import org.openhab.binding.lgwebos.internal.handler.core.ResponseListener;
42 import com.google.gson.JsonElement;
43 import com.google.gson.JsonObject;
46 * Internal implementation of ServiceCommand for URL-based commands
48 * @author Hyun Kook Khang - Connect SDK initial contribution
49 * @author Sebastian Prehn - Adoption for openHAB
51 public class ServiceCommand<T> {
59 protected JsonObject payload;
60 protected String target;
61 protected Function<JsonObject, @Nullable T> converter;
63 ResponseListener<T> responseListener;
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;
74 public JsonElement getPayload() {
78 public String getType() {
82 public String getTarget() {
86 public void processResponse(JsonObject response) {
87 this.getResponseListener().onSuccess(this.converter.apply(response));
90 public void processError(String error) {
91 this.getResponseListener().onError(error);
94 public ResponseListener<T> getResponseListener() {
95 return responseListener;
99 public String toString() {
100 return "ServiceCommand [type=" + type + ", target=" + target + ", payload=" + payload + "]";