From f01a95f3109a8ff2286dd1225d4a84f9705e9ed6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20S=C3=A1nchez?= Date: Sat, 14 Dec 2024 17:59:50 -0300 Subject: [PATCH] I hate json parsing. --- .../example/dcav2gui/InstanceInterface.java | 158 ++++++++++++++---- .../dcav2gui/ui/home/HomeFragment.java | 24 ++- 2 files changed, 145 insertions(+), 37 deletions(-) diff --git a/app/src/main/java/com/example/dcav2gui/InstanceInterface.java b/app/src/main/java/com/example/dcav2gui/InstanceInterface.java index bd0c69e..58b65fa 100644 --- a/app/src/main/java/com/example/dcav2gui/InstanceInterface.java +++ b/app/src/main/java/com/example/dcav2gui/InstanceInterface.java @@ -2,6 +2,9 @@ package com.example.dcav2gui; import static com.example.dcav2gui.MainActivity.globalSettings; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; @@ -10,9 +13,13 @@ import com.google.gson.JsonSyntaxException; import java.io.IOException; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.Comparator; +import java.util.Iterator; import java.util.List; +import java.util.ListIterator; +import java.util.Map; import java.util.concurrent.CompletableFuture; import okhttp3.OkHttpClient; @@ -277,6 +284,7 @@ public class InstanceInterface { int onlineWorkers; int longWorkers; int shortWorkers; + List workers; // Semaphore @@ -286,6 +294,8 @@ public class InstanceInterface { fundsAvailable = getFundsAvailable(exchange, "USDT", true); //Individual worker status needed to calculate this + workers = getAllWorkersStats("binance", true); + //Funds needed fundsNeeded = 0; @@ -332,8 +342,106 @@ public class InstanceInterface { } - public static List getAllWorkersStats(String exchange) throws IOException { - return null; + public static List getAllWorkersStats(String exchange, boolean retry) throws IOException { + List 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) { try { JsonElement jsonElement = JsonParser.parseString(jsonResponse); @@ -591,7 +679,7 @@ public class InstanceInterface { private final double baseBought; private final int soAmount; private final int tpMode; - private final double[] profitTable; + private final String profitTable; private final double startTime; private final double startPrice; private final double dealStartTime; @@ -602,19 +690,19 @@ public class InstanceInterface { private final double takeProfitPrice; private final double nextSoPrice; private final String tpOrderId; - private final Order takeProfitOrder; + private final JsonObject takeProfitOrder; private final String soOrderId; - private final Order safetyOrder; + private final JsonObject safetyOrder; private final double feesPaidInBase; private final double feesPaidInQuote; private final double partialProfit; - private final double[] safetyPriceTable; - private final List dealOrderHistory; + private final String safetyPriceTable; + private final String dealOrderHistory; private final String pauseReason; - private final OldLongDictionary oldLongDictionary; //Change type + private final JsonObject oldLongDictionary; //Change type 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 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.stopWhenProfit = stopWhenProfit; this.orderSize = orderSize; @@ -652,7 +740,7 @@ public class InstanceInterface { public double getBaseBought() { return baseBought; } public double getSoAmount() { return soAmount; } public int getTpMode() { return tpMode; } - public double[] getProfitTable() { return profitTable; } + public String getProfitTable() { return profitTable; } public double getStartTime() { return startTime; } public double getStartPrice() { return startPrice; } public double getDealStartTime() { return dealStartTime; } @@ -662,16 +750,16 @@ public class InstanceInterface { public double getTakeProfitPrice() { return takeProfitPrice; } public double getNextSoPrice() { return nextSoPrice; } public String getTpOrderId() { return tpOrderId; } - public Order getTakeProfitOrder() { return takeProfitOrder; } + public JsonObject getTakeProfitOrder() { return takeProfitOrder; } public String getSoOrderId() { return soOrderId; } - public Order getSafetyOrder() { return safetyOrder; } + public JsonObject getSafetyOrder() { return safetyOrder; } public double getFeesPaidInBase() { return feesPaidInBase; } public double getFeesPaidInQuote() { return feesPaidInQuote; } public double getPartialProfit() { return partialProfit; } - public double[] getSafetyPriceTable() { return safetyPriceTable; } - public List getDealOrderHistory() { return dealOrderHistory; } + public String getSafetyPriceTable() { return safetyPriceTable; } + public String getDealOrderHistory() { return dealOrderHistory; } public String getPauseReason() { return pauseReason; } - public OldLongDictionary getOldLongDictionary() { return oldLongDictionary; } + public JsonObject getOldLongDictionary() { return oldLongDictionary; } public String getStatusString() { return statusString; } } diff --git a/app/src/main/java/com/example/dcav2gui/ui/home/HomeFragment.java b/app/src/main/java/com/example/dcav2gui/ui/home/HomeFragment.java index da94389..c9e1dfb 100644 --- a/app/src/main/java/com/example/dcav2gui/ui/home/HomeFragment.java +++ b/app/src/main/java/com/example/dcav2gui/ui/home/HomeFragment.java @@ -33,7 +33,6 @@ import java.util.concurrent.Executors; import java.io.IOException; public class HomeFragment extends Fragment { - private HomeViewModel homeViewModel; //private FragmentHomeBinding binding; private final ExecutorService executorService = Executors.newSingleThreadExecutor(); @@ -41,6 +40,11 @@ public class HomeFragment extends Fragment { private Handler handler = new Handler(); private Runnable updateRunnable; + public List binanceWorkerStatsData; + public List gateioWorkerStatsData; + public List kucoinWorkersStatsData; + public List okexWorkerStatsData; + //Tickers private TextView pricePair1; private TextView pricePair124hPercentage ; @@ -421,12 +425,22 @@ public class HomeFragment extends Fragment { System.err.print(e.toString()); return null; } + + }); + + CompletableFuture future14 = CompletableFuture.supplyAsync(() -> { + try { + return InstanceInterface.getExchangeStatsData("binance"); + } catch (IOException e) { + System.err.print(e.toString()); + return null; + } }); // Wait for all futures to complete CompletableFuture allFutures = CompletableFuture.allOf( future1, future2, future3, future4, future5, future6, future7, future8, - future9, future10, future11, future12, future13); + future9, future10, future11, future12, future13, future14); // Update UI allFutures.thenAccept(voidResult -> { @@ -639,6 +653,12 @@ public class HomeFragment extends Fragment { log3Content.setText(logs3); log4Content.setText(logs4); +// try { +// System.err.println(future14.get()); +// } catch (ExecutionException | InterruptedException e) { +// throw new RuntimeException(e); +// } + }); } catch (Exception e) {