working? worker commands

This commit is contained in:
Nicolás Sánchez 2024-12-25 16:18:44 -03:00
parent 0ad6500257
commit fe95c69201
5 changed files with 232 additions and 22 deletions

View File

@ -10,13 +10,16 @@ import android.os.Handler;
import android.os.Looper;
import android.text.InputType;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.Toast;
import java.io.IOException;
import java.util.Locale;
import com.google.android.material.materialswitch.MaterialSwitch;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
@ -153,6 +156,7 @@ public class WorkerInterface {
return valueToReturn;
}
public static JsonObject addTrader(String exchange, String pair, boolean retry) throws IOException {
String[] pairBaseAndQuote = pair.split("/");
String base = pairBaseAndQuote[0];
@ -195,6 +199,7 @@ public class WorkerInterface {
}
}
public static JsonObject removeTrader(String exchange, String pair, boolean retry) throws IOException {
String[] pairBaseAndQuote = pair.split("/");
String base = pairBaseAndQuote[0];
@ -237,6 +242,7 @@ public class WorkerInterface {
}
}
public static JsonObject restartTrader(String exchange, String pair, boolean retry) throws IOException {
String[] pairBaseAndQuote = pair.split("/");
String base = pairBaseAndQuote[0];
@ -279,6 +285,7 @@ public class WorkerInterface {
}
}
public static JsonObject importTrader(String exchange, String pair, boolean retry) throws IOException {
String[] pairBaseAndQuote = pair.split("/");
String base = pairBaseAndQuote[0];
@ -321,6 +328,7 @@ public class WorkerInterface {
}
}
public static JsonObject togglePause(String exchange, String pair, boolean retry) throws IOException {
String[] pairBaseAndQuote = pair.split("/");
String base = pairBaseAndQuote[0];
@ -363,6 +371,7 @@ public class WorkerInterface {
}
}
public static JsonObject toggleAutoswitch(String exchange, String pair, boolean retry) throws IOException {
String[] pairBaseAndQuote = pair.split("/");
String base = pairBaseAndQuote[0];
@ -405,6 +414,7 @@ public class WorkerInterface {
}
}
public static JsonObject toggleCleanup(String exchange, String pair, boolean retry) throws IOException {
String[] pairBaseAndQuote = pair.split("/");
String base = pairBaseAndQuote[0];
@ -490,6 +500,7 @@ public class WorkerInterface {
}
}
public static JsonObject switchToLong(String exchange, String pair, boolean calculateProfits, boolean retry) throws IOException {
String[] pairBaseAndQuote = pair.split("/");
String base = pairBaseAndQuote[0];
@ -537,6 +548,7 @@ public class WorkerInterface {
}
}
public static JsonObject switchToShort(String exchange, String pair, boolean retry) throws IOException {
String[] pairBaseAndQuote = pair.split("/");
String base = pairBaseAndQuote[0];
@ -579,6 +591,7 @@ public class WorkerInterface {
}
}
public static JsonObject switchQuoteCurrency(String exchange, String pair, String targetQuoteCurrency, boolean retry) throws IOException {
String[] pairBaseAndQuote = pair.split("/");
String base = pairBaseAndQuote[0];
@ -622,6 +635,7 @@ public class WorkerInterface {
}
}
public static JsonObject addSafetyOrders(String exchange, String pair, int amount, boolean retry) throws IOException {
String[] pairBaseAndQuote = pair.split("/");
String base = pairBaseAndQuote[0];
@ -665,6 +679,7 @@ public class WorkerInterface {
}
}
public static JsonObject addQuote(String exchange, String pair, double amount, boolean retry) throws IOException {
String[] pairBaseAndQuote = pair.split("/");
String base = pairBaseAndQuote[0];
@ -708,6 +723,7 @@ public class WorkerInterface {
}
}
public static void showToggleDialog(JsonObject result, Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
@ -719,6 +735,7 @@ public class WorkerInterface {
builder.show();
}
public static void sendTogglePause(String exchange, String pair, Context context) {
new Thread(() -> {
try {
@ -731,6 +748,7 @@ public class WorkerInterface {
}).start();
}
public static void sendToggleAutoswitch(String exchange, String pair, Context context) {
new Thread(() -> {
try {
@ -755,6 +773,7 @@ public class WorkerInterface {
}).start();
}
public static void sendToggleLastCall(String exchange, String pair, Context context) {
new Thread(() -> {
try {
@ -767,6 +786,67 @@ public class WorkerInterface {
}).start();
}
public static void sendSwitchToLongCall(String exchange, String pair, Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Switch "+ pair + " to long mode?");
builder.setPositiveButton("Switch to long", (dialog, which) -> {
new Thread(() -> {
try {
JsonObject response = WorkerInterface.switchToLong(exchange, pair, true,true);
new Handler(Looper.getMainLooper()).post(() -> {
showToggleDialog(response, context);
});
} catch (IOException e) {
e.printStackTrace();
// Show an error dialog on the main thread
new Handler(Looper.getMainLooper()).post(() -> {
AlertDialog.Builder errorBuilder = new AlertDialog.Builder(context);
errorBuilder.setTitle("Error");
errorBuilder.setMessage("Failed to switch to long: " + e.getMessage());
errorBuilder.setPositiveButton("OK", null);
errorBuilder.show();
});
}
}).start();
});
builder.setNegativeButton("Cancel", (dialog, which) -> dialog.cancel());
builder.show();
}
public static void sendSwitchToShortCall(String exchange, String pair, Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Switch "+ pair + " to short mode?");
builder.setPositiveButton("Switch to short", (dialog, which) -> {
new Thread(() -> {
try {
JsonObject response = WorkerInterface.switchToShort(exchange, pair, true);
new Handler(Looper.getMainLooper()).post(() -> {
showToggleDialog(response, context);
});
} catch (IOException e) {
e.printStackTrace();
// Show an error dialog on the main thread
new Handler(Looper.getMainLooper()).post(() -> {
AlertDialog.Builder errorBuilder = new AlertDialog.Builder(context);
errorBuilder.setTitle("Error");
errorBuilder.setMessage("Failed to switch to short: " + e.getMessage());
errorBuilder.setPositiveButton("OK", null);
errorBuilder.show();
});
}
}).start();
});
builder.setNegativeButton("Cancel", (dialog, which) -> dialog.cancel());
builder.show();
}
public static void sendAddTraderCall(String exchange, Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Enter pair to add to "+ exchange);
@ -803,6 +883,64 @@ public class WorkerInterface {
}
public static void sendRemoveTraderCall(String exchange, String pair, Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Remove "+ pair + " from instance?");
builder.setPositiveButton("Remove trader", (dialog, which) -> {
new Thread(() -> {
try {
JsonObject response = WorkerInterface.removeTrader(exchange, pair, true);
new Handler(Looper.getMainLooper()).post(() -> {
showToggleDialog(response, context);
});
} catch (IOException e) {
e.printStackTrace();
// Show an error dialog on the main thread
new Handler(Looper.getMainLooper()).post(() -> {
AlertDialog.Builder errorBuilder = new AlertDialog.Builder(context);
errorBuilder.setTitle("Error");
errorBuilder.setMessage("Failed to remove trader: " + e.getMessage());
errorBuilder.setPositiveButton("OK", null);
errorBuilder.show();
});
}
}).start();
});
builder.setNegativeButton("Cancel", (dialog, which) -> dialog.cancel());
builder.show();
}
public static void sendRestartTraderCall(String exchange, String pair, Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Restart "+ pair + "?");
builder.setPositiveButton("Restart trader", (dialog, which) -> {
new Thread(() -> {
try {
JsonObject response = WorkerInterface.restartTrader(exchange, pair, true);
new Handler(Looper.getMainLooper()).post(() -> {
showToggleDialog(response, context);
});
} catch (IOException e) {
e.printStackTrace();
// Show an error dialog on the main thread
new Handler(Looper.getMainLooper()).post(() -> {
AlertDialog.Builder errorBuilder = new AlertDialog.Builder(context);
errorBuilder.setTitle("Error");
errorBuilder.setMessage("Failed to restart trader: " + e.getMessage());
errorBuilder.setPositiveButton("OK", null);
errorBuilder.show();
});
}
}).start();
});
builder.setNegativeButton("Cancel", (dialog, which) -> dialog.cancel());
builder.show();
}
public static void sendImportTraderCall(String exchange, Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Enter pair to import to "+ exchange);
@ -841,7 +979,7 @@ public class WorkerInterface {
public static void sendSwitchQuoteCurrencyCall(String exchange, String pair, Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Enter new quote currency "+ exchange);
builder.setTitle("Enter new quote currency of"+ pair);
final EditText input = new EditText(context);
input.setInputType(InputType.TYPE_CLASS_TEXT);
@ -874,6 +1012,78 @@ public class WorkerInterface {
builder.show();
}
public static void sendAddSafetyOrdersCall(String exchange, String pair, Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Add safety orders to "+ pair);
final EditText input = new EditText(context);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
input.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
builder.setView(input);
builder.setPositiveButton("Add safety orders", (dialog, which) -> {
final int amountToAdd = Integer.parseInt(input.getText().toString());
new Thread(() -> {
try {
JsonObject response = WorkerInterface.addSafetyOrders(exchange, pair, amountToAdd,true);
new Handler(Looper.getMainLooper()).post(() -> {
showToggleDialog(response, context);
});
} catch (IOException e) {
e.printStackTrace();
// Show an error dialog on the main thread
new Handler(Looper.getMainLooper()).post(() -> {
AlertDialog.Builder errorBuilder = new AlertDialog.Builder(context);
errorBuilder.setTitle("Error");
errorBuilder.setMessage("Failed to add safety orders: " + e.getMessage());
errorBuilder.setPositiveButton("OK", null);
errorBuilder.show();
});
}
}).start();
});
builder.setNegativeButton("Cancel", (dialog, which) -> dialog.cancel());
builder.show();
}
public static void sendAddQuoteCall(String exchange, String pair, Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Add quote currency to "+ pair);
final EditText input = new EditText(context);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
input.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
builder.setView(input);
builder.setPositiveButton("Add quote", (dialog, which) -> {
final double amountToAdd = Double.parseDouble(input.getText().toString());
new Thread(() -> {
try {
JsonObject response = WorkerInterface.addQuote(exchange, pair, amountToAdd,true);
new Handler(Looper.getMainLooper()).post(() -> {
showToggleDialog(response, context);
});
} catch (IOException e) {
e.printStackTrace();
// Show an error dialog on the main thread
new Handler(Looper.getMainLooper()).post(() -> {
AlertDialog.Builder errorBuilder = new AlertDialog.Builder(context);
errorBuilder.setTitle("Error");
errorBuilder.setMessage("Failed to add quote currency: " + e.getMessage());
errorBuilder.setPositiveButton("OK", null);
errorBuilder.show();
});
}
}).start();
});
builder.setNegativeButton("Cancel", (dialog, which) -> dialog.cancel());
builder.show();
}
public static void fetchWorkerStats(String exchange, String pair, Context context) {
new Thread(() -> {
try {

View File

@ -79,10 +79,10 @@ public class BinanceFragment extends Fragment implements WorkerCardAdapter.OnCar
WorkerInterface.sendAddTraderCall("binance",getContext());
return true;
} else if (item.getItemId() == R.id.removeTrader) {
System.err.println(pair + " Remove trader option clicked");
WorkerInterface.sendRemoveTraderCall("binance", pair, getContext());
return true;
} else if (item.getItemId() == R.id.restartTrader) {
System.err.println(pair + " Restart option clicked");
WorkerInterface.sendRestartTraderCall("binance", pair, getContext());
return true;
} else if (item.getItemId() == R.id.importTrader) {
WorkerInterface.sendImportTraderCall("binance",getContext());
@ -100,19 +100,19 @@ public class BinanceFragment extends Fragment implements WorkerCardAdapter.OnCar
WorkerInterface.sendToggleLastCall("binance", pair, getContext());
return true;
} else if (item.getItemId() == R.id.switchToLong) {
System.err.println(pair + " Switch to long option clicked");
WorkerInterface.sendSwitchToLongCall("binance", pair, getContext());
return true;
} else if (item.getItemId() == R.id.switchToShort) {
System.err.println(pair + " Switch to short option clicked");
WorkerInterface.sendSwitchToShortCall("binance", pair, getContext());
return true;
} else if (item.getItemId() == R.id.switchQuoteCurrency) {
WorkerInterface.sendSwitchQuoteCurrencyCall("binance", pair, getContext());
return true;
} else if (item.getItemId() == R.id.addSafetyOrders) {
System.err.println(pair + " Add safety orders option clicked");
WorkerInterface.sendAddSafetyOrdersCall("binance", pair, getContext());
return true;
} else if (item.getItemId() == R.id.addQuote) {
System.err.println(pair + " Add quote option clicked");
WorkerInterface.sendAddQuoteCall("binance", pair, getContext());
return true;
}
return false;

View File

@ -93,19 +93,19 @@ public class GateioFragment extends Fragment implements WorkerCardAdapter.OnCard
WorkerInterface.sendToggleLastCall("gateio", pair, getContext());
return true;
} else if (item.getItemId() == R.id.switchToLong) {
System.err.println(pair + " Switch to long option clicked");
WorkerInterface.sendSwitchToLongCall("gateio", pair, getContext());
return true;
} else if (item.getItemId() == R.id.switchToShort) {
System.err.println(pair + " Switch to short option clicked");
WorkerInterface.sendSwitchToShortCall("gateio", pair, getContext());
return true;
} else if (item.getItemId() == R.id.switchQuoteCurrency) {
WorkerInterface.sendSwitchQuoteCurrencyCall("binance", pair, getContext());
WorkerInterface.sendSwitchQuoteCurrencyCall("gateio", pair, getContext());
return true;
} else if (item.getItemId() == R.id.addSafetyOrders) {
System.err.println(pair + " Add safety orders option clicked");
WorkerInterface.sendAddSafetyOrdersCall("gateio", pair, getContext());
return true;
} else if (item.getItemId() == R.id.addQuote) {
System.err.println(pair + " Add quote option clicked");
WorkerInterface.sendAddQuoteCall("gateio", pair, getContext());
return true;
}
return false;

View File

@ -93,19 +93,19 @@ public class KucoinFragment extends Fragment implements WorkerCardAdapter.OnCard
WorkerInterface.sendToggleLastCall("kucoin", pair, getContext());
return true;
} else if (item.getItemId() == R.id.switchToLong) {
System.err.println(pair + " Switch to long option clicked");
WorkerInterface.sendSwitchToLongCall("kucoin", pair, getContext());
return true;
} else if (item.getItemId() == R.id.switchToShort) {
System.err.println(pair + " Switch to short option clicked");
WorkerInterface.sendSwitchToShortCall("kucoin", pair, getContext());
return true;
} else if (item.getItemId() == R.id.switchQuoteCurrency) {
WorkerInterface.sendSwitchQuoteCurrencyCall("binance", pair, getContext());
WorkerInterface.sendSwitchQuoteCurrencyCall("kucoin", pair, getContext());
return true;
} else if (item.getItemId() == R.id.addSafetyOrders) {
System.err.println(pair + " Add safety orders option clicked");
WorkerInterface.sendAddSafetyOrdersCall("kucoin", pair, getContext());
return true;
} else if (item.getItemId() == R.id.addQuote) {
System.err.println(pair + " Add quote option clicked");
WorkerInterface.sendAddQuoteCall("kucoin", pair, getContext());
return true;
}
return false;

View File

@ -93,19 +93,19 @@ public class OkxFragment extends Fragment implements WorkerCardAdapter.OnCardLon
WorkerInterface.sendToggleLastCall("okex", pair, getContext());
return true;
} else if (item.getItemId() == R.id.switchToLong) {
System.err.println(pair + " Switch to long option clicked");
WorkerInterface.sendSwitchToLongCall("okex", pair, getContext());
return true;
} else if (item.getItemId() == R.id.switchToShort) {
System.err.println(pair + " Switch to short option clicked");
WorkerInterface.sendSwitchToShortCall("okex", pair, getContext());
return true;
} else if (item.getItemId() == R.id.switchQuoteCurrency) {
WorkerInterface.sendSwitchQuoteCurrencyCall("binance", pair, getContext());
WorkerInterface.sendSwitchQuoteCurrencyCall("okex", pair, getContext());
return true;
} else if (item.getItemId() == R.id.addSafetyOrders) {
System.err.println(pair + " Add safety orders option clicked");
WorkerInterface.sendAddSafetyOrdersCall("okex", pair, getContext());
return true;
} else if (item.getItemId() == R.id.addQuote) {
System.err.println(pair + " Add quote option clicked");
WorkerInterface.sendAddQuoteCall("okex", pair, getContext());
return true;
}
return false;