I hate json parsing.
This commit is contained in:
parent
02f55c715f
commit
f01a95f310
|
|
@ -2,6 +2,9 @@ package com.example.dcav2gui;
|
||||||
|
|
||||||
import static com.example.dcav2gui.MainActivity.globalSettings;
|
import static com.example.dcav2gui.MainActivity.globalSettings;
|
||||||
|
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.annotation.Nullable;
|
||||||
|
|
||||||
import com.google.gson.JsonArray;
|
import com.google.gson.JsonArray;
|
||||||
import com.google.gson.JsonElement;
|
import com.google.gson.JsonElement;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
|
|
@ -10,9 +13,13 @@ import com.google.gson.JsonSyntaxException;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.ListIterator;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
import okhttp3.OkHttpClient;
|
import okhttp3.OkHttpClient;
|
||||||
|
|
@ -277,6 +284,7 @@ public class InstanceInterface {
|
||||||
int onlineWorkers;
|
int onlineWorkers;
|
||||||
int longWorkers;
|
int longWorkers;
|
||||||
int shortWorkers;
|
int shortWorkers;
|
||||||
|
List<WorkerStatsData> workers;
|
||||||
|
|
||||||
|
|
||||||
// Semaphore
|
// Semaphore
|
||||||
|
|
@ -286,6 +294,8 @@ public class InstanceInterface {
|
||||||
fundsAvailable = getFundsAvailable(exchange, "USDT", true);
|
fundsAvailable = getFundsAvailable(exchange, "USDT", true);
|
||||||
|
|
||||||
//Individual worker status needed to calculate this
|
//Individual worker status needed to calculate this
|
||||||
|
workers = getAllWorkersStats("binance", true);
|
||||||
|
|
||||||
//Funds needed
|
//Funds needed
|
||||||
fundsNeeded = 0;
|
fundsNeeded = 0;
|
||||||
|
|
||||||
|
|
@ -332,8 +342,106 @@ public class InstanceInterface {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static List<WorkerStatsData> getAllWorkersStats(String exchange) throws IOException {
|
public static List<WorkerStatsData> getAllWorkersStats(String exchange, boolean retry) throws IOException {
|
||||||
return null;
|
List<WorkerStatsData> valueToReturn = new ArrayList<>();
|
||||||
|
|
||||||
|
Request workersStatsRequest = new Request.Builder()
|
||||||
|
.url(API_BASE_URL + "/" + exchange + "/get_all_worker_status")
|
||||||
|
.header("X-API-KEY", API_KEY)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
try (Response workersStatsResponse = httpClient.newCall(workersStatsRequest).execute()) {
|
||||||
|
if (!workersStatsResponse.isSuccessful()) {
|
||||||
|
if (workersStatsResponse.code() == 503 && retry) {
|
||||||
|
return getAllWorkersStats(exchange, false);
|
||||||
|
}
|
||||||
|
throw new IOException("Unexpected code " + workersStatsResponse);
|
||||||
|
}
|
||||||
|
String workersStatsResponseBody = workersStatsResponse.body().string();
|
||||||
|
JsonElement jsonElement = JsonParser.parseString(workersStatsResponseBody);
|
||||||
|
if (!jsonElement.isJsonObject()) {
|
||||||
|
System.err.println("The parsed JSON response is not a JsonObject.");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
JsonObject jsonObject = jsonElement.getAsJsonObject();
|
||||||
|
if (jsonObject.has("Error")) {
|
||||||
|
System.err.println("The parsed JSON response contains Error");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (String key : jsonObject.keySet()) {
|
||||||
|
JsonObject value = jsonObject.get(key).getAsJsonObject();
|
||||||
|
try {
|
||||||
|
boolean isShort = value.has("is_short") && value.get("is_short").isJsonPrimitive() && value.get("is_short").getAsBoolean();
|
||||||
|
boolean stopWhenProfit = value.has("stop_when_profit") && value.get("stop_when_profit").isJsonPrimitive() && value.get("stop_when_profit").getAsBoolean();
|
||||||
|
double orderSize = value.has("order_size") && value.get("order_size").isJsonPrimitive() ? value.get("order_size").getAsDouble() : 0.0;
|
||||||
|
double quoteSpent = value.has("quote_spent") && value.get("quote_spent").isJsonPrimitive() ? value.get("quote_spent").getAsDouble() : 0.0;
|
||||||
|
double baseBought = value.has("base_bought") && value.get("base_bought").isJsonPrimitive() ? value.get("base_bought").getAsDouble() : 0.0;
|
||||||
|
int soAmount = value.has("so_amount") && value.get("so_amount").isJsonPrimitive() ? value.get("so_amount").getAsInt() : 0;
|
||||||
|
int tpMode = value.has("tp_mode") && value.get("tp_mode").isJsonPrimitive() ? value.get("tp_mode").getAsInt() : 0;
|
||||||
|
String profitTable = value.has("profit_table") && value.get("profit_table").isJsonPrimitive() ? value.get("profit_table").getAsString() : null;
|
||||||
|
double startTime = value.has("start_time") && value.get("start_time").isJsonPrimitive() ? value.get("start_time").getAsDouble() : 0.0;
|
||||||
|
double startPrice = value.has("start_price") && value.get("start_price").isJsonPrimitive() ? value.get("start_price").getAsDouble() : 0.0;
|
||||||
|
double dealStartTime = value.has("deal_start_time") && value.get("deal_start_time").isJsonPrimitive() ? value.get("deal_start_time").getAsDouble() : 0.0;
|
||||||
|
double dealUptime = value.has("deal_uptime") && value.get("deal_uptime").isJsonPrimitive() ? value.get("deal_uptime").getAsDouble() : 0.0;
|
||||||
|
double totalUptime = value.has("total_uptime") && value.get("total_uptime").isJsonPrimitive() ? value.get("total_uptime").getAsDouble() : 0.0;
|
||||||
|
double price = value.has("price") && value.get("price").isJsonPrimitive() ? value.get("price").getAsDouble() : 0.0;
|
||||||
|
double takeProfitPrice = value.has("take_profit_price") && value.get("take_profit_price").isJsonPrimitive() ? value.get("take_profit_price").getAsDouble() : 0.0;
|
||||||
|
double nextSoPrice = value.has("next_so_price") && value.get("next_so_price").isJsonPrimitive() ? value.get("next_so_price").getAsDouble() : 0.0;
|
||||||
|
String tpOrderId = value.has("tp_order_id") && value.get("tp_order_id").isJsonPrimitive() ? value.get("tp_order_id").getAsString() : null;
|
||||||
|
//String takeProfitOrder = value.has("take_profit_order") && value.get("take_profit_order").isJsonPrimitive() ? value.get("take_profit_order").getAsString() : null;
|
||||||
|
JsonObject takeProfitOrder = value.get("take_profit_order").getAsJsonObject();
|
||||||
|
String safetyOrderId = value.has("safety_order_id") && value.get("safety_order_id").isJsonPrimitive() ? value.get("safety_order_id").getAsString() : null;
|
||||||
|
//String safetyOrder = value.has("safety_order") && value.get("safety_order").isJsonPrimitive() ? value.get("safety_order").getAsString() : null;
|
||||||
|
JsonObject safetyOrder = value.get("safety_order").getAsJsonObject();
|
||||||
|
double feesPaidInBase = value.has("fees_paid_in_base") && value.get("fees_paid_in_base").isJsonPrimitive() ? value.get("fees_paid_in_base").getAsDouble() : 0.0;
|
||||||
|
double feesPaidInQuote = value.has("fees_paid_in_quote") && value.get("fees_paid_in_quote").isJsonPrimitive() ? value.get("fees_paid_in_quote").getAsDouble() : 0.0;
|
||||||
|
double partialProfit = value.has("partial_profit") && value.get("partial_profit").isJsonPrimitive() ? value.get("partial_profit").getAsDouble() : 0.0;
|
||||||
|
String safetyPriceTable = value.has("safety_price_table") && value.get("safety_price_table").isJsonPrimitive() ? value.get("safety_price_table").getAsString() : null;
|
||||||
|
String dealOrderHistory = value.has("deal_order_history") && value.get("deal_order_history").isJsonPrimitive() ? value.get("deal_order_history").getAsString() : null;
|
||||||
|
String pauseReason = value.has("pause_reason") && value.get("pause_reason").isJsonPrimitive() ? value.get("pause_reason").getAsString() : null;
|
||||||
|
String statusString = value.has("status_string") && value.get("status_string").isJsonPrimitive() ? value.get("status_string").getAsString() : null;
|
||||||
|
JsonObject oldLong = null;
|
||||||
|
if (value.has("old_long")) {
|
||||||
|
oldLong = value.get("old_long").getAsJsonObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
valueToReturn.add(new WorkerStatsData(
|
||||||
|
isShort,
|
||||||
|
stopWhenProfit,
|
||||||
|
orderSize,
|
||||||
|
quoteSpent,
|
||||||
|
baseBought,
|
||||||
|
soAmount,
|
||||||
|
tpMode,
|
||||||
|
profitTable,
|
||||||
|
startTime,
|
||||||
|
startPrice,
|
||||||
|
dealStartTime,
|
||||||
|
dealUptime,
|
||||||
|
totalUptime,
|
||||||
|
price,
|
||||||
|
takeProfitPrice,
|
||||||
|
nextSoPrice,
|
||||||
|
tpOrderId,
|
||||||
|
takeProfitOrder,
|
||||||
|
safetyOrderId,
|
||||||
|
safetyOrder,
|
||||||
|
feesPaidInBase,
|
||||||
|
feesPaidInQuote,
|
||||||
|
partialProfit,
|
||||||
|
safetyPriceTable,
|
||||||
|
dealOrderHistory,
|
||||||
|
pauseReason,
|
||||||
|
oldLong,
|
||||||
|
statusString
|
||||||
|
));
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.err.println("Error processing JSON for key '" + key + "': " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return valueToReturn;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -354,26 +462,6 @@ public class InstanceInterface {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// private static double getUptime(String jsonResponse) {
|
|
||||||
// try {
|
|
||||||
// JsonElement jsonElement = JsonParser.parseString(jsonResponse);
|
|
||||||
// if (!jsonElement.isJsonObject()) {
|
|
||||||
// System.err.println("The parsed JSON response is not a JsonObject.");
|
|
||||||
// return 0.0;
|
|
||||||
// }
|
|
||||||
// JsonObject jsonObject = jsonElement.getAsJsonObject();
|
|
||||||
// if (jsonObject.has("Time")) {
|
|
||||||
// return jsonObject.get("Time").getAsDouble();
|
|
||||||
// } else {
|
|
||||||
// System.err.println("The parsed JSON response does not contain a 'Time' entry.");
|
|
||||||
// return 0.0;
|
|
||||||
// }
|
|
||||||
// } catch (Exception e) {
|
|
||||||
// System.err.println("Error processing uptime data: " + e.getMessage());
|
|
||||||
// return 0.0;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
private static InstanceGlobalStatsData getInstanceGlobalStatus(String jsonResponse) {
|
private static InstanceGlobalStatsData getInstanceGlobalStatus(String jsonResponse) {
|
||||||
try {
|
try {
|
||||||
JsonElement jsonElement = JsonParser.parseString(jsonResponse);
|
JsonElement jsonElement = JsonParser.parseString(jsonResponse);
|
||||||
|
|
@ -591,7 +679,7 @@ public class InstanceInterface {
|
||||||
private final double baseBought;
|
private final double baseBought;
|
||||||
private final int soAmount;
|
private final int soAmount;
|
||||||
private final int tpMode;
|
private final int tpMode;
|
||||||
private final double[] profitTable;
|
private final String profitTable;
|
||||||
private final double startTime;
|
private final double startTime;
|
||||||
private final double startPrice;
|
private final double startPrice;
|
||||||
private final double dealStartTime;
|
private final double dealStartTime;
|
||||||
|
|
@ -602,19 +690,19 @@ public class InstanceInterface {
|
||||||
private final double takeProfitPrice;
|
private final double takeProfitPrice;
|
||||||
private final double nextSoPrice;
|
private final double nextSoPrice;
|
||||||
private final String tpOrderId;
|
private final String tpOrderId;
|
||||||
private final Order takeProfitOrder;
|
private final JsonObject takeProfitOrder;
|
||||||
private final String soOrderId;
|
private final String soOrderId;
|
||||||
private final Order safetyOrder;
|
private final JsonObject safetyOrder;
|
||||||
private final double feesPaidInBase;
|
private final double feesPaidInBase;
|
||||||
private final double feesPaidInQuote;
|
private final double feesPaidInQuote;
|
||||||
private final double partialProfit;
|
private final double partialProfit;
|
||||||
private final double[] safetyPriceTable;
|
private final String safetyPriceTable;
|
||||||
private final List<String> dealOrderHistory;
|
private final String dealOrderHistory;
|
||||||
private final String pauseReason;
|
private final String pauseReason;
|
||||||
private final OldLongDictionary oldLongDictionary; //Change type
|
private final JsonObject oldLongDictionary; //Change type
|
||||||
private final String statusString;
|
private final String statusString;
|
||||||
|
|
||||||
public WorkerStatsData(boolean isShort, boolean stopWhenProfit, double orderSize, double quoteSpent, double baseBought, int soAmount, int tpMode, double[] profitTable, double startTime, double startPrice, double dealStartTime, double dealUptime, double totalUptime, double price, double takeProfitPrice, double nextSoPrice, String tpOrderId, Order takeProfitOrder, String soOrderId, Order safetyOrder, double feesPaidInBase, double feesPaidInQuote, double partialProfit, double[] safetyPriceTable, List<String> dealOrderHistory, String pauseReason, OldLongDictionary oldLong, String statusString) {
|
public WorkerStatsData(boolean isShort, boolean stopWhenProfit, double orderSize, double quoteSpent, double baseBought, int soAmount, int tpMode, String profitTable, double startTime, double startPrice, double dealStartTime, double dealUptime, double totalUptime, double price, double takeProfitPrice, double nextSoPrice, String tpOrderId, JsonObject takeProfitOrder, String soOrderId, JsonObject safetyOrder, double feesPaidInBase, double feesPaidInQuote, double partialProfit, String safetyPriceTable, String dealOrderHistory, String pauseReason, JsonObject oldLong, String statusString) {
|
||||||
this.isShort = isShort;
|
this.isShort = isShort;
|
||||||
this.stopWhenProfit = stopWhenProfit;
|
this.stopWhenProfit = stopWhenProfit;
|
||||||
this.orderSize = orderSize;
|
this.orderSize = orderSize;
|
||||||
|
|
@ -652,7 +740,7 @@ public class InstanceInterface {
|
||||||
public double getBaseBought() { return baseBought; }
|
public double getBaseBought() { return baseBought; }
|
||||||
public double getSoAmount() { return soAmount; }
|
public double getSoAmount() { return soAmount; }
|
||||||
public int getTpMode() { return tpMode; }
|
public int getTpMode() { return tpMode; }
|
||||||
public double[] getProfitTable() { return profitTable; }
|
public String getProfitTable() { return profitTable; }
|
||||||
public double getStartTime() { return startTime; }
|
public double getStartTime() { return startTime; }
|
||||||
public double getStartPrice() { return startPrice; }
|
public double getStartPrice() { return startPrice; }
|
||||||
public double getDealStartTime() { return dealStartTime; }
|
public double getDealStartTime() { return dealStartTime; }
|
||||||
|
|
@ -662,16 +750,16 @@ public class InstanceInterface {
|
||||||
public double getTakeProfitPrice() { return takeProfitPrice; }
|
public double getTakeProfitPrice() { return takeProfitPrice; }
|
||||||
public double getNextSoPrice() { return nextSoPrice; }
|
public double getNextSoPrice() { return nextSoPrice; }
|
||||||
public String getTpOrderId() { return tpOrderId; }
|
public String getTpOrderId() { return tpOrderId; }
|
||||||
public Order getTakeProfitOrder() { return takeProfitOrder; }
|
public JsonObject getTakeProfitOrder() { return takeProfitOrder; }
|
||||||
public String getSoOrderId() { return soOrderId; }
|
public String getSoOrderId() { return soOrderId; }
|
||||||
public Order getSafetyOrder() { return safetyOrder; }
|
public JsonObject getSafetyOrder() { return safetyOrder; }
|
||||||
public double getFeesPaidInBase() { return feesPaidInBase; }
|
public double getFeesPaidInBase() { return feesPaidInBase; }
|
||||||
public double getFeesPaidInQuote() { return feesPaidInQuote; }
|
public double getFeesPaidInQuote() { return feesPaidInQuote; }
|
||||||
public double getPartialProfit() { return partialProfit; }
|
public double getPartialProfit() { return partialProfit; }
|
||||||
public double[] getSafetyPriceTable() { return safetyPriceTable; }
|
public String getSafetyPriceTable() { return safetyPriceTable; }
|
||||||
public List<String> getDealOrderHistory() { return dealOrderHistory; }
|
public String getDealOrderHistory() { return dealOrderHistory; }
|
||||||
public String getPauseReason() { return pauseReason; }
|
public String getPauseReason() { return pauseReason; }
|
||||||
public OldLongDictionary getOldLongDictionary() { return oldLongDictionary; }
|
public JsonObject getOldLongDictionary() { return oldLongDictionary; }
|
||||||
public String getStatusString() { return statusString; }
|
public String getStatusString() { return statusString; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,6 @@ import java.util.concurrent.Executors;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
public class HomeFragment extends Fragment {
|
public class HomeFragment extends Fragment {
|
||||||
|
|
||||||
private HomeViewModel homeViewModel;
|
private HomeViewModel homeViewModel;
|
||||||
//private FragmentHomeBinding binding;
|
//private FragmentHomeBinding binding;
|
||||||
private final ExecutorService executorService = Executors.newSingleThreadExecutor();
|
private final ExecutorService executorService = Executors.newSingleThreadExecutor();
|
||||||
|
|
@ -41,6 +40,11 @@ public class HomeFragment extends Fragment {
|
||||||
private Handler handler = new Handler();
|
private Handler handler = new Handler();
|
||||||
private Runnable updateRunnable;
|
private Runnable updateRunnable;
|
||||||
|
|
||||||
|
public List<InstanceInterface.WorkerStatsData> binanceWorkerStatsData;
|
||||||
|
public List<InstanceInterface.WorkerStatsData> gateioWorkerStatsData;
|
||||||
|
public List<InstanceInterface.WorkerStatsData> kucoinWorkersStatsData;
|
||||||
|
public List<InstanceInterface.WorkerStatsData> okexWorkerStatsData;
|
||||||
|
|
||||||
//Tickers
|
//Tickers
|
||||||
private TextView pricePair1;
|
private TextView pricePair1;
|
||||||
private TextView pricePair124hPercentage ;
|
private TextView pricePair124hPercentage ;
|
||||||
|
|
@ -421,12 +425,22 @@ public class HomeFragment extends Fragment {
|
||||||
System.err.print(e.toString());
|
System.err.print(e.toString());
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
CompletableFuture<InstanceInterface.ExchangeStatsData> future14 = CompletableFuture.supplyAsync(() -> {
|
||||||
|
try {
|
||||||
|
return InstanceInterface.getExchangeStatsData("binance");
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.print(e.toString());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Wait for all futures to complete
|
// Wait for all futures to complete
|
||||||
CompletableFuture<Void> allFutures = CompletableFuture.allOf(
|
CompletableFuture<Void> allFutures = CompletableFuture.allOf(
|
||||||
future1, future2, future3, future4, future5, future6, future7, future8,
|
future1, future2, future3, future4, future5, future6, future7, future8,
|
||||||
future9, future10, future11, future12, future13);
|
future9, future10, future11, future12, future13, future14);
|
||||||
|
|
||||||
// Update UI
|
// Update UI
|
||||||
allFutures.thenAccept(voidResult -> {
|
allFutures.thenAccept(voidResult -> {
|
||||||
|
|
@ -639,6 +653,12 @@ public class HomeFragment extends Fragment {
|
||||||
log3Content.setText(logs3);
|
log3Content.setText(logs3);
|
||||||
log4Content.setText(logs4);
|
log4Content.setText(logs4);
|
||||||
|
|
||||||
|
// try {
|
||||||
|
// System.err.println(future14.get());
|
||||||
|
// } catch (ExecutionException | InterruptedException e) {
|
||||||
|
// throw new RuntimeException(e);
|
||||||
|
// }
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue