diff --git a/app/src/main/java/com/example/dcav2gui/InstanceInterface.java b/app/src/main/java/com/example/dcav2gui/InstanceInterface.java index 5fd5f08..160fba2 100644 --- a/app/src/main/java/com/example/dcav2gui/InstanceInterface.java +++ b/app/src/main/java/com/example/dcav2gui/InstanceInterface.java @@ -48,6 +48,47 @@ public class InstanceInterface { } } + private static double getTodaysProfit(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("combined")) { + JsonArray combinedArray = jsonObject.get("combined").getAsJsonArray(); + return combinedArray.get(0).getAsDouble(); + } else { + System.err.println("The parsed JSON response does not contain a 'combined' array."); + return 0.0; + } + } catch (Exception e) { + System.err.println("Error processing combined profit data: " + e.getMessage()); + return 0.0; + } + } + + private static double getThisMonthsProfit(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("combined")) { + JsonArray combinedArray = jsonObject.get("combined").getAsJsonArray(); + return combinedArray.get(1).getAsDouble(); + } else { + System.err.println("The parsed JSON response does not contain a 'combined' array."); + return 0.0; + } + } catch (Exception e) { + System.err.println("Error processing combined profit data: " + e.getMessage()); + return 0.0; + } + } public static double getTraderTime(String exchange) throws IOException { Request uptimeRequest = new Request.Builder() @@ -61,7 +102,23 @@ public class InstanceInterface { } String stockResponseBody = statsResponse.body().string(); - return getUptime(stockResponseBody); + try { + JsonElement jsonElement = JsonParser.parseString(stockResponseBody); + 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; + } } } @@ -107,11 +164,6 @@ public class InstanceInterface { List kucoinDeals = kucoinFuture.join(); List okxDeals = okxFuture.join(); -// ListbinanceDeals = getLastTradesFromExchange("binance"); -// ListgateioDeals = getLastTradesFromExchange("gateio"); -// ListkucoinDeals = getLastTradesFromExchange("kucoin"); -// ListokxDeals = getLastTradesFromExchange("okex"); - List allDeals = new ArrayList<>(); allDeals.addAll(binanceDeals); allDeals.addAll(gateioDeals); @@ -192,6 +244,7 @@ public class InstanceInterface { System.err.println("The parsed JSON response does not contain the logs key."); return null; } + JsonArray logsArray = jsonObject.getAsJsonArray("Logs"); List logList = new ArrayList<>(); for (int i = logsArray.size()-1; i >= 0; i--) { @@ -206,12 +259,65 @@ public class InstanceInterface { public static ExchangeStatsData getExchangeStatsData(String exchange) throws IOException { - //This needs to do A LOT of work - return null; + double lastSeen; + double fundsAvailable; + double fundsNeeded; + int onlineWorkers; + int longWorkers; + int shortWorkers; + + + // Semaphore + lastSeen = getTraderTime(exchange); + + //Funds available + fundsAvailable = getFundsAvailable(exchange, "USDT"); + + //Individual worker status needed to calculate this + //Funds needed + fundsNeeded = 0; + + //Online workers + onlineWorkers = 0; + + //Long workers + longWorkers = 0; + + //Short workers + shortWorkers = 0; + + return new ExchangeStatsData(lastSeen,fundsAvailable,fundsNeeded,onlineWorkers,longWorkers,shortWorkers); } - public static List getAllWorkersStats(String exchange) throws IOException { + private static double getFundsAvailable(String exchange, String coin) throws IOException { + Request fundsRequest = new Request.Builder() + .url(API_BASE_URL + "/" + exchange + "/get_balance?coin=" + coin) + .header("X-API-KEY", API_KEY) + .build(); + + try (Response statsResponse = httpClient.newCall(fundsRequest).execute()) { + if (!statsResponse.isSuccessful()) { + throw new IOException("Unexpected code " + statsResponse); + } + String fundsResponseBody = statsResponse.body().string(); + JsonElement jsonElement = JsonParser.parseString(fundsResponseBody); + if (!jsonElement.isJsonObject()) { + System.err.println("The parsed JSON response is not a JsonObject."); + return 0.0; + } + JsonObject jsonObject = jsonElement.getAsJsonObject(); + if (!jsonObject.has(coin)) { + System.err.println("The parsed JSON response does not contain the coin requested."); + return 0.0; + } + + return jsonObject.get(coin).getAsDouble(); + } + } + + + public static List getAllWorkersStats(String exchange) throws IOException { return null; } @@ -233,67 +339,25 @@ public class InstanceInterface { } - private static double getTodaysProfit(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("combined")) { - JsonArray combinedArray = jsonObject.get("combined").getAsJsonArray(); - return combinedArray.get(0).getAsDouble(); - } else { - System.err.println("The parsed JSON response does not contain a 'combined' array."); - return 0.0; - } - } catch (Exception e) { - System.err.println("Error processing combined profit data: " + e.getMessage()); - return 0.0; - } - } - - private static double getThisMonthsProfit(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("combined")) { - JsonArray combinedArray = jsonObject.get("combined").getAsJsonArray(); - return combinedArray.get(1).getAsDouble(); - } else { - System.err.println("The parsed JSON response does not contain a 'combined' array."); - return 0.0; - } - } catch (Exception e) { - System.err.println("Error processing combined profit data: " + e.getMessage()); - return 0.0; - } - } - - 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 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 { @@ -480,15 +544,15 @@ public class InstanceInterface { public static class ExchangeStatsData { - private final int semaphore; + private final double lastSeen; private final double fundsAvailable; private final double fundsNeeded; private final int onlineWorkers; private final int longWorkers; private final int shortWorkers; - public ExchangeStatsData(int semaphore, double fundsAvailable, double fundsNeeded, int onlineWorkers, int longWorkers, int shortWorkers) { - this.semaphore = semaphore; + public ExchangeStatsData(double lastSeen, double fundsAvailable, double fundsNeeded, int onlineWorkers, int longWorkers, int shortWorkers) { + this.lastSeen = lastSeen; this.fundsAvailable = fundsAvailable; this.fundsNeeded = fundsNeeded; this.onlineWorkers = onlineWorkers; @@ -496,7 +560,7 @@ public class InstanceInterface { this.shortWorkers = shortWorkers; } - public int getSemaphore() { return semaphore; } + public double getLastSeen() { return lastSeen; } public double getFundsAvailable() { return fundsAvailable; } public double getFundsNeeded() { return fundsNeeded; } public int getOnlineWorkers() { return onlineWorkers; } 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 aa5ec07..780c61c 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 @@ -387,16 +387,54 @@ public class HomeFragment extends Fragment { } }); + CompletableFuture future10 = CompletableFuture.supplyAsync(() -> { + try { + return InstanceInterface.getExchangeStatsData("binance"); + } catch (IOException e) { + System.err.print(e.toString()); + return null; + } + }); + + CompletableFuture future11 = CompletableFuture.supplyAsync(() -> { + try { + return InstanceInterface.getExchangeStatsData("gateio"); + } catch (IOException e) { + System.err.print(e.toString()); + return null; + } + }); + + CompletableFuture future12 = CompletableFuture.supplyAsync(() -> { + try { + return InstanceInterface.getExchangeStatsData("kucoin"); + } catch (IOException e) { + System.err.print(e.toString()); + return null; + } + }); + + CompletableFuture future13 = CompletableFuture.supplyAsync(() -> { + try { + return InstanceInterface.getExchangeStatsData("okex"); + } 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); + CompletableFuture allFutures = CompletableFuture.allOf( + future1, future2, future3, future4, future5, future6, future7, future8, + future9, future10, future11, future12, future13); // Update UI allFutures.thenAccept(voidResult -> { try { requireActivity().runOnUiThread(() -> { - TickerTracker.PriceChangeData priceData = null; - TickerTracker.PriceChangeData priceData2 = null; - TickerTracker.PriceChangeData priceData3 = null; + TickerTracker.PriceChangeData priceData; + TickerTracker.PriceChangeData priceData2; + TickerTracker.PriceChangeData priceData3; try { priceData = future1.get(); priceData2 = future2.get(); @@ -412,10 +450,10 @@ public class HomeFragment extends Fragment { throw new RuntimeException(e); } - String logs1 = null; - String logs2 = null; - String logs3 = null; - String logs4 = null; + String logs1; + String logs2; + String logs3; + String logs4; try { logs1 = future5.get(); logs2 = future6.get(); @@ -425,6 +463,48 @@ public class HomeFragment extends Fragment { throw new RuntimeException(e); } + //Populate deals page + try { + List deals = future9.get(); + if (deals != null) { + StringBuilder dealsList = new StringBuilder(); + for (InstanceInterface.DealData deal : deals) { + String timestamp = timeStampConverter(deal.getTimestamp()); + String pair = deal.getPair(); + String amount = String.format(Locale.ROOT,"%.2f", deal.getAmount()); + String exchange = Character.toUpperCase(deal.getExchangeName().charAt(0)) + deal.getExchangeName().substring(1); + dealsList.append(timestamp) + .append(" | ") + .append(pair) + .append(" | ") + .append(amount) + .append(" USDT | ") + .append(exchange).append("\n"); + } + lastTrades.setText(dealsList); + } + } catch (ExecutionException e) { + throw new RuntimeException(e); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + + + InstanceInterface.ExchangeStatsData binanceStats; + InstanceInterface.ExchangeStatsData gateioStats; + InstanceInterface.ExchangeStatsData kucoinStats; + InstanceInterface.ExchangeStatsData okexStats; + try { + binanceStats = future10.get(); + gateioStats = future11.get(); + kucoinStats = future12.get(); + okexStats = future13.get(); + } catch (ExecutionException | InterruptedException e) { + throw new RuntimeException(e); + } + + + pricePair1.setText(String.format(Locale.ROOT, "%.2f", priceData.getCurrentPrice())); pricePair124hPercentage.setText(formatPercentage(priceData.getPriceChangePercent24h())); pricePair17dPercentage.setText(formatPercentage(priceData.getPriceChangePercent7d())); @@ -452,32 +532,87 @@ public class HomeFragment extends Fragment { profitsToday.setText(String.format(Locale.ROOT, "%.2f", profitsData.getProfitsToday())); profitsThisMonth.setText(String.format(Locale.ROOT, "%.2f", profitsData.getProfitsThisMonth())); - - //Populate deals page - try { - List deals = future9.get(); - if (deals != null) { - StringBuilder dealsList = new StringBuilder(); - for (InstanceInterface.DealData deal : deals) { - String timestamp = timeStampConverter(deal.getTimestamp()); - String pair = deal.getPair(); - String amount = String.format(Locale.ROOT,"%.2f", deal.getAmount()); - String exchange = Character.toUpperCase(deal.getExchangeName().charAt(0)) + deal.getExchangeName().substring(1); - dealsList.append(timestamp) - .append(" | ") - .append(pair) - .append(" | ") - .append(amount) - .append(" USDT | ") - .append(exchange).append("\n"); - } - lastTrades.setText(dealsList); - } - } catch (ExecutionException e) { - throw new RuntimeException(e); - } catch (InterruptedException e) { - throw new RuntimeException(e); + //Exchange stats + int timeoutForYellow = 30*1000; //30 seconds + int timeoutForRed = 300*1000; //300 seconds + //Icons + if (binanceStats.getLastSeen()*1000+timeoutForRed < System.currentTimeMillis()) { + exchange1Status.setImageResource(R.drawable.ic_red_circle_48); + System.err.println(binanceStats.getLastSeen());} + else if (binanceStats.getLastSeen()*1000+timeoutForYellow < System.currentTimeMillis()) { + exchange1Status.setImageResource(R.drawable.ic_yellow_circle_48);} + else { + exchange1Status.setImageResource(R.drawable.ic_green_circle_48); } + if (gateioStats.getLastSeen()*1000+timeoutForRed < System.currentTimeMillis()) { + exchange2Status.setImageResource(R.drawable.ic_red_circle_48); + System.err.println(gateioStats.getLastSeen());} + else if (gateioStats.getLastSeen()*1000+timeoutForYellow < System.currentTimeMillis()) { + exchange2Status.setImageResource(R.drawable.ic_yellow_circle_48);} + else { + exchange2Status.setImageResource(R.drawable.ic_green_circle_48); + } + if (kucoinStats.getLastSeen()*1000+timeoutForRed < System.currentTimeMillis()) { + exchange3Status.setImageResource(R.drawable.ic_red_circle_48); + System.err.println(kucoinStats.getLastSeen());} + else if (kucoinStats.getLastSeen()*1000+timeoutForYellow < System.currentTimeMillis()) { + exchange3Status.setImageResource(R.drawable.ic_yellow_circle_48);} + else { + exchange3Status.setImageResource(R.drawable.ic_green_circle_48); + } + if (okexStats.getLastSeen()*1000+timeoutForRed < System.currentTimeMillis()) { + exchange4Status.setImageResource(R.drawable.ic_red_circle_48); + System.err.println(okexStats.getLastSeen()); + } + else if (okexStats.getLastSeen()*1000+timeoutForYellow < System.currentTimeMillis()) { + exchange4Status.setImageResource(R.drawable.ic_yellow_circle_48);} + else { + exchange4Status.setImageResource(R.drawable.ic_green_circle_48); + } + //Funds available + String binanceFundsAvailable = String.format(Locale.ROOT,"%.2f",binanceStats.getFundsAvailable()); + String binanceFundsNeeded = String.format(Locale.ROOT,"%.2f",binanceStats.getFundsNeeded()); + String gateioFundsAvailable = String.format(Locale.ROOT,"%.2f",gateioStats.getFundsAvailable()); + String gateioFundsNeeded = String.format(Locale.ROOT,"%.2f",gateioStats.getFundsNeeded()); + String kucoinFundsAvailable = String.format(Locale.ROOT,"%.2f",kucoinStats.getFundsAvailable()); + String kucoinFundsNeeded = String.format(Locale.ROOT,"%.2f",kucoinStats.getFundsNeeded()); + String okexFundsAvailable = String.format(Locale.ROOT,"%.2f",okexStats.getFundsAvailable()); + String okexFundsNeeded = String.format(Locale.ROOT,"%.2f",okexStats.getFundsNeeded()); + String binanceFunds = binanceFundsAvailable+"/"+binanceFundsNeeded; + String gateioFunds = gateioFundsAvailable+"/"+gateioFundsNeeded; + String kucoinFunds = kucoinFundsAvailable+"/"+kucoinFundsNeeded; + String okexFunds = okexFundsAvailable+"/"+okexFundsNeeded; + double binanceFundsPercentage = 69.4231337; + if (binanceStats.getFundsNeeded()!=0) { + binanceFundsPercentage = 100 - (binanceStats.getFundsNeeded() - binanceStats.getFundsAvailable()) / binanceStats.getFundsNeeded() * 100; + } + double gateioFundsPercentage = 69.4231337; + if (gateioStats.getFundsNeeded()!=0) { + gateioFundsPercentage = 100 - (gateioStats.getFundsNeeded() - gateioStats.getFundsAvailable()) / gateioStats.getFundsNeeded() * 100; + } + double kucoinFundsPercentage = 69.4231337; + if (kucoinStats.getFundsNeeded()!=0) { + kucoinFundsPercentage = 100 - (kucoinStats.getFundsNeeded() - kucoinStats.getFundsAvailable()) / kucoinStats.getFundsNeeded() * 100; + } + double okexFundsPercentage = 69.4231337; + if (okexStats.getFundsNeeded()!=0) { + okexFundsPercentage = 100 - (okexStats.getFundsNeeded() - okexStats.getFundsAvailable()) / okexStats.getFundsNeeded() * 100; + } + String binanceFundsPercentageString = String.format(Locale.ROOT,"%.2f",binanceFundsPercentage)+"%"; + String gateioFundsPercentageString = String.format(Locale.ROOT,"%.2f",gateioFundsPercentage)+"%"; + String kucoinFundsPercentageString = String.format(Locale.ROOT,"%.2f",kucoinFundsPercentage)+"%"; + String okexFundsPercentageString = String.format(Locale.ROOT,"%.2f",okexFundsPercentage)+"%"; + exchange1Funds.setText(binanceFunds); + exchange2Funds.setText(gateioFunds); + exchange3Funds.setText(kucoinFunds); + exchange4Funds.setText(okexFunds); + exchange1FundsPercentage.setText(binanceFundsPercentageString); + exchange2FundsPercentage.setText(gateioFundsPercentageString); + exchange3FundsPercentage.setText(kucoinFundsPercentageString); + exchange4FundsPercentage.setText(okexFundsPercentageString); + + + //Populate logs log1Content.setText(logs1);