]> git.basschouten.com Git - openhab-addons.git/blob
4fac814dfcb6554cd1e94738b20582af51114d62
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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     private String cloudServer = "";
35
36     public void setResponse(JsonObject response) {
37         this.response = response;
38     }
39
40     public MiIoSendCommand(int id, MiIoCommand command, JsonObject fullCommand) {
41         this.id = id;
42         this.command = command;
43         this.commandJson = fullCommand;
44     }
45
46     public MiIoSendCommand(int id, MiIoCommand command, JsonObject fullCommand, String cloudServer) {
47         this.id = id;
48         this.command = command;
49         this.commandJson = fullCommand;
50         this.cloudServer = cloudServer;
51     }
52
53     public int getId() {
54         return id;
55     }
56
57     public MiIoCommand getCommand() {
58         return command;
59     }
60
61     public JsonObject getCommandJson() {
62         return commandJson;
63     }
64
65     public String getCommandString() {
66         return commandJson.toString();
67     }
68
69     public String getMethod() {
70         return commandJson.has("method") ? commandJson.get("method").getAsString() : "";
71     }
72
73     public JsonElement getParams() {
74         return commandJson.has("params") ? commandJson.get("params").getAsJsonArray() : new JsonArray();
75     }
76
77     public JsonObject getResponse() {
78         final @Nullable JsonObject response = this.response;
79         return response != null ? response : new JsonObject();
80     }
81
82     public boolean isError() {
83         final @Nullable JsonObject response = this.response;
84         if (response != null) {
85             return response.has("error");
86         }
87         return true;
88     }
89
90     public JsonElement getResult() {
91         final @Nullable JsonObject response = this.response;
92         if (response != null && response.has("result")) {
93             return response.get("result");
94         }
95         return new JsonObject();
96     }
97
98     public String getCloudServer() {
99         return cloudServer;
100     }
101
102     public void setCloudServer(String cloudServer) {
103         this.cloudServer = cloudServer;
104     }
105 }