2 * Copyright (c) 2010-2023 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
13 package org.openhab.binding.tapocontrol.internal.helpers;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
17 import com.google.gson.Gson;
18 import com.google.gson.JsonObject;
22 * Generates payload for TapoHttp request
24 * @author Christian Wild - Initial contribution
27 public class PayloadBuilder {
28 public String method = "";
29 private JsonObject parameters = new JsonObject();
34 * @param command command (method) to send
36 public void setCommand(String command) {
37 this.method = command;
43 * @param name parameter name
44 * @param value parameter value (typeOf Bool,Number or String)
46 public void addParameter(String name, Object value) {
47 if (value instanceof Boolean) {
48 this.parameters.addProperty(name, (Boolean) value);
49 } else if (value instanceof Number) {
50 this.parameters.addProperty(name, (Number) value);
52 this.parameters.addProperty(name, value.toString());
57 * Get JSON Payload (STRING)
59 * @return String JSON-Payload
61 public String getPayload() {
62 Gson gson = new Gson();
63 JsonObject payload = getJsonPayload();
64 return gson.toJson(payload);
68 * Get JSON Payload (JSON-Object)
70 * @return JsonObject JSON-Payload
72 public JsonObject getJsonPayload() {
73 JsonObject payload = new JsonObject();
74 long timeMils = System.currentTimeMillis();// * 1000;
76 payload.addProperty("method", this.method);
77 if (this.parameters.size() > 0) {
78 payload.add("params", this.parameters);
80 payload.addProperty("requestTimeMils", timeMils);
87 * remove all parameters
89 public void flushParameters(String command) {
90 this.parameters = new JsonObject();