]> git.basschouten.com Git - openhab-addons.git/blob
47aa56fb506ad888588f48b281c377c2c8b2b733
[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 package org.openhab.binding.miio.internal;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17
18 import com.google.gson.JsonArray;
19 import com.google.gson.JsonElement;
20 import com.google.gson.JsonObject;
21
22 /**
23  * Commands to be send
24  *
25  * @author Marcel Verpaalen - Initial contribution
26  */
27 @NonNullByDefault
28 public class MiIoSendCommand {
29
30     private final int id;
31     private final MiIoCommand command;
32     private final JsonObject commandJson;
33     private @Nullable JsonObject response;
34
35     public void setResponse(JsonObject response) {
36         this.response = response;
37     }
38
39     public MiIoSendCommand(int id, MiIoCommand command, JsonObject fullCommand) {
40         this.id = id;
41         this.command = command;
42         this.commandJson = fullCommand;
43     }
44
45     public int getId() {
46         return id;
47     }
48
49     public MiIoCommand getCommand() {
50         return command;
51     }
52
53     public JsonObject getCommandJson() {
54         return commandJson;
55     }
56
57     public String getCommandString() {
58         return commandJson.toString();
59     }
60
61     public String getMethod() {
62         return commandJson.has("method") ? commandJson.get("method").getAsString() : "";
63     }
64
65     public JsonElement getParams() {
66         return commandJson.has("params") ? commandJson.get("params").getAsJsonArray() : new JsonArray();
67     }
68
69     public JsonObject getResponse() {
70         final @Nullable JsonObject response = this.response;
71         return response != null ? response : new JsonObject();
72     }
73
74     public boolean isError() {
75         final @Nullable JsonObject response = this.response;
76         if (response != null) {
77             return response.has("error");
78         }
79         return true;
80     }
81
82     public JsonElement getResult() {
83         final @Nullable JsonObject response = this.response;
84         if (response != null && response.has("result")) {
85             return response.get("result");
86         }
87         return new JsonObject();
88     }
89 }