fetchWorkerData, but using handlers

This commit is contained in:
Nicolás Sánchez 2024-12-18 11:50:46 -03:00
parent d47567e226
commit 529e0bdd47
6 changed files with 78 additions and 42 deletions

View File

@ -4,10 +4,10 @@
<selectionStates> <selectionStates>
<SelectionState runConfigName="app"> <SelectionState runConfigName="app">
<option name="selectionMode" value="DROPDOWN" /> <option name="selectionMode" value="DROPDOWN" />
<DropdownSelection timestamp="2024-12-18T13:29:16.439093075Z"> <DropdownSelection timestamp="2024-12-18T13:48:59.828906476Z">
<Target type="DEFAULT_BOOT"> <Target type="DEFAULT_BOOT">
<handle> <handle>
<DeviceId pluginId="LocalEmulator" identifier="path=/home/nicolas/.android/avd/Pixel_6_Pro_API_34.avd" /> <DeviceId pluginId="PhysicalDevice" identifier="serial=ZY22FN7MHQ" />
</handle> </handle>
</Target> </Target>
</DropdownSelection> </DropdownSelection>

View File

@ -651,16 +651,6 @@ public class InstanceInterface {
} }
public static List<WorkerData> fetchWorkersDataForCards(String exchange) throws IOException {
// Implement this method to fetch actual data
// call List<WorkerStatsData> getAllWorkersStats from InstanceInterface and
// translate all the WorkerStatsData objects to WorkerData objects!
// Example implementation:
List<InstanceInterface.WorkerStatsData> workerStatsDataList = InstanceInterface.getAllWorkersStats(exchange, true);
return translateToWorkerData(workerStatsDataList);
}
public static class DealData { public static class DealData {
private final double timestamp; private final double timestamp;
private final String pair; private final String pair;

View File

@ -1,6 +1,8 @@
package com.example.dcav2gui.ui.exchanges; package com.example.dcav2gui.ui.exchanges;
import android.app.Application; import android.app.Application;
import android.util.Log; import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel; import androidx.lifecycle.AndroidViewModel;
@ -34,19 +36,27 @@ public class BinanceViewModel extends AndroidViewModel {
} }
private void fetchWorkerData() { private void fetchWorkerData() {
new Thread(() -> { HandlerThread handlerThread = new HandlerThread("FetchWorkerDataThread");
while (true) { handlerThread.start();
Handler handler = new Handler(handlerThread.getLooper());
handler.post(new Runnable() {
@Override
public void run() {
try { try {
Thread.sleep((long) settingsData.timeBetweenQueries*1000);
// Fetch and update the data // Fetch and update the data
List<WorkerData> newData = InstanceInterface.fetchWorkersDataForCards("binance"); List<WorkerData> newData = InstanceInterface.translateToWorkerData(InstanceInterface.getAllWorkersStats("binance", true));
//System.err.println(newData.toString());
workerDataList.postValue(newData); workerDataList.postValue(newData);
} catch (InterruptedException | IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
// Schedule the next execution
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
fetchWorkerData();
} }
}).start(); }, (long) settingsData.timeBetweenQueries * 1000);
}
});
} }
} }

View File

@ -1,5 +1,8 @@
package com.example.dcav2gui.ui.exchanges; package com.example.dcav2gui.ui.exchanges;
import android.app.Application; import android.app.Application;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel; import androidx.lifecycle.AndroidViewModel;
@ -33,19 +36,28 @@ public class GateioViewModel extends AndroidViewModel {
} }
private void fetchWorkerData() { private void fetchWorkerData() {
new Thread(() -> { HandlerThread handlerThread = new HandlerThread("FetchWorkerDataThread");
while (true) { handlerThread.start();
Handler handler = new Handler(handlerThread.getLooper());
handler.post(new Runnable() {
@Override
public void run() {
try { try {
Thread.sleep((long) settingsData.timeBetweenQueries*1000);
// Fetch and update the data // Fetch and update the data
List<WorkerData> newData = InstanceInterface.fetchWorkersDataForCards("gateio"); List<WorkerData> newData = InstanceInterface.translateToWorkerData(InstanceInterface.getAllWorkersStats("gateio", true));
//System.err.println(newData.toString());
workerDataList.postValue(newData); workerDataList.postValue(newData);
} catch (InterruptedException | IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
// Schedule the next execution
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
fetchWorkerData();
} }
}).start(); }, (long) settingsData.timeBetweenQueries * 1000);
}
});
} }
} }

View File

@ -1,5 +1,8 @@
package com.example.dcav2gui.ui.exchanges; package com.example.dcav2gui.ui.exchanges;
import android.app.Application; import android.app.Application;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel; import androidx.lifecycle.AndroidViewModel;
@ -33,19 +36,28 @@ public class KucoinViewModel extends AndroidViewModel {
} }
private void fetchWorkerData() { private void fetchWorkerData() {
new Thread(() -> { HandlerThread handlerThread = new HandlerThread("FetchWorkerDataThread");
while (true) { handlerThread.start();
Handler handler = new Handler(handlerThread.getLooper());
handler.post(new Runnable() {
@Override
public void run() {
try { try {
Thread.sleep((long) settingsData.timeBetweenQueries*1000);
// Fetch and update the data // Fetch and update the data
List<WorkerData> newData = InstanceInterface.fetchWorkersDataForCards("kucoin"); List<WorkerData> newData = InstanceInterface.translateToWorkerData(InstanceInterface.getAllWorkersStats("kucoin", true));
//System.err.println(newData.toString());
workerDataList.postValue(newData); workerDataList.postValue(newData);
} catch (InterruptedException | IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
// Schedule the next execution
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
fetchWorkerData();
} }
}).start(); }, (long) settingsData.timeBetweenQueries * 1000);
}
});
} }
} }

View File

@ -1,5 +1,8 @@
package com.example.dcav2gui.ui.exchanges; package com.example.dcav2gui.ui.exchanges;
import android.app.Application; import android.app.Application;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel; import androidx.lifecycle.AndroidViewModel;
@ -33,19 +36,28 @@ public class OkxViewModel extends AndroidViewModel {
} }
private void fetchWorkerData() { private void fetchWorkerData() {
new Thread(() -> { HandlerThread handlerThread = new HandlerThread("FetchWorkerDataThread");
while (true) { handlerThread.start();
Handler handler = new Handler(handlerThread.getLooper());
handler.post(new Runnable() {
@Override
public void run() {
try { try {
Thread.sleep((long) settingsData.timeBetweenQueries*1000);
// Fetch and update the data // Fetch and update the data
List<WorkerData> newData = InstanceInterface.fetchWorkersDataForCards("okex"); List<WorkerData> newData = InstanceInterface.translateToWorkerData(InstanceInterface.getAllWorkersStats("okex", true));
//System.err.println(newData.toString());
workerDataList.postValue(newData); workerDataList.postValue(newData);
} catch (InterruptedException | IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
// Schedule the next execution
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
fetchWorkerData();
} }
}).start(); }, (long) settingsData.timeBetweenQueries * 1000);
}
});
} }
} }