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