mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2024-11-28 12:56:49 +01:00
Deduplicate code
This commit is contained in:
parent
d6f5e36e12
commit
f9e600d369
@ -78,38 +78,9 @@ public class AmazfitBipFetchLogsOperation extends AbstractFetchOperation {
|
||||
return;
|
||||
}
|
||||
|
||||
final String taskName = StringUtils.ensureNotNull(builder.getTaskName());
|
||||
GregorianCalendar sinceWhen = BLETypeConversions.createCalendar();
|
||||
sinceWhen.add(Calendar.DAY_OF_MONTH, -10);
|
||||
byte[] fetchBytes = BLETypeConversions.join(new byte[]{
|
||||
HuamiService.COMMAND_ACTIVITY_DATA_START_DATE,
|
||||
AmazfitBipService.COMMAND_ACTIVITY_DATA_TYPE_DEBUGLOGS},
|
||||
getSupport().getTimeBytes(sinceWhen, TimeUnit.MINUTES));
|
||||
builder.add(new AbstractGattListenerWriteAction(getQueue(), characteristicFetch, fetchBytes) {
|
||||
@Override
|
||||
protected boolean onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
|
||||
UUID characteristicUUID = characteristic.getUuid();
|
||||
if (HuamiService.UUID_UNKNOWN_CHARACTERISTIC4.equals(characteristicUUID)) {
|
||||
byte[] value = characteristic.getValue();
|
||||
|
||||
if (ArrayUtils.equals(value, HuamiService.RESPONSE_ACTIVITY_DATA_START_DATE_SUCCESS, 0)) {
|
||||
handleActivityMetadata(value);
|
||||
TransactionBuilder newBuilder = createTransactionBuilder(taskName + " Step 2");
|
||||
newBuilder.notify(characteristicActivityData, true);
|
||||
newBuilder.write(characteristicFetch, new byte[]{HuamiService.COMMAND_FETCH_DATA});
|
||||
try {
|
||||
performImmediately(newBuilder);
|
||||
} catch (IOException ex) {
|
||||
GB.toast(getContext(), "Error fetching debug logs: " + ex.getMessage(), Toast.LENGTH_LONG, GB.ERROR, ex);
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
handleActivityMetadata(value);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
});
|
||||
startFetching(builder, AmazfitBipService.COMMAND_ACTIVITY_DATA_TYPE_DEBUGLOGS, sinceWhen);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -22,6 +22,7 @@ import android.bluetooth.BluetoothGattCharacteristic;
|
||||
import android.content.SharedPreferences;
|
||||
import android.support.annotation.CallSuper;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.widget.Toast;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@ -32,18 +33,22 @@ import java.util.Arrays;
|
||||
import java.util.Calendar;
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
||||
import nodomain.freeyourgadget.gadgetbridge.Logging;
|
||||
import nodomain.freeyourgadget.gadgetbridge.R;
|
||||
import nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiService;
|
||||
import nodomain.freeyourgadget.gadgetbridge.devices.huami.amazfitbip.AmazfitBipService;
|
||||
import nodomain.freeyourgadget.gadgetbridge.service.btle.BLETypeConversions;
|
||||
import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder;
|
||||
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.AbstractGattListenerWriteAction;
|
||||
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceBusyAction;
|
||||
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.HuamiSupport;
|
||||
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.AbstractHuamiOperation;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.ArrayUtils;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.StringUtils;
|
||||
|
||||
/**
|
||||
* An operation that fetches activity data. For every fetch, a new operation must
|
||||
@ -137,6 +142,39 @@ public abstract class AbstractFetchOperation extends AbstractHuamiOperation {
|
||||
|
||||
protected abstract void bufferActivityData(byte[] value);
|
||||
|
||||
protected void startFetching(TransactionBuilder builder, byte fetchType, GregorianCalendar sinceWhen) {
|
||||
final String taskName = StringUtils.ensureNotNull(builder.getTaskName());
|
||||
byte[] fetchBytes = BLETypeConversions.join(new byte[]{
|
||||
HuamiService.COMMAND_ACTIVITY_DATA_START_DATE,
|
||||
fetchType},
|
||||
getSupport().getTimeBytes(sinceWhen, TimeUnit.MINUTES));
|
||||
builder.add(new AbstractGattListenerWriteAction(getQueue(), characteristicFetch, fetchBytes) {
|
||||
@Override
|
||||
protected boolean onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
|
||||
UUID characteristicUUID = characteristic.getUuid();
|
||||
if (HuamiService.UUID_UNKNOWN_CHARACTERISTIC4.equals(characteristicUUID)) {
|
||||
byte[] value = characteristic.getValue();
|
||||
|
||||
if (ArrayUtils.equals(value, HuamiService.RESPONSE_ACTIVITY_DATA_START_DATE_SUCCESS, 0)) {
|
||||
handleActivityMetadata(value);
|
||||
TransactionBuilder newBuilder = createTransactionBuilder(taskName + " Step 2");
|
||||
newBuilder.notify(characteristicActivityData, true);
|
||||
newBuilder.write(characteristicFetch, new byte[]{HuamiService.COMMAND_FETCH_DATA});
|
||||
try {
|
||||
performImmediately(newBuilder);
|
||||
} catch (IOException ex) {
|
||||
GB.toast(getContext(), "Error fetching debug logs: " + ex.getMessage(), Toast.LENGTH_LONG, GB.ERROR, ex);
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
handleActivityMetadata(value);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected void handleActivityMetadata(byte[] value) {
|
||||
if (value.length == 15) {
|
||||
// first two bytes are whether our request was accepted
|
||||
|
@ -79,32 +79,7 @@ public class FetchActivityOperation extends AbstractFetchOperation {
|
||||
protected void startFetching(TransactionBuilder builder) {
|
||||
final String taskName = StringUtils.ensureNotNull(builder.getTaskName());
|
||||
GregorianCalendar sinceWhen = getLastSuccessfulSyncTime();
|
||||
byte[] fetchBytes = BLETypeConversions.join(new byte[] { HuamiService.COMMAND_ACTIVITY_DATA_START_DATE, HuamiService.COMMAND_ACTIVITY_DATA_TYPE_ACTIVTY }, getSupport().getTimeBytes(sinceWhen, TimeUnit.MINUTES));
|
||||
builder.add(new AbstractGattListenerWriteAction(getQueue(), characteristicFetch, fetchBytes) {
|
||||
@Override
|
||||
protected boolean onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
|
||||
UUID characteristicUUID = characteristic.getUuid();
|
||||
if (HuamiService.UUID_UNKNOWN_CHARACTERISTIC4.equals(characteristicUUID)) {
|
||||
byte[] value = characteristic.getValue();
|
||||
|
||||
if (ArrayUtils.equals(value, HuamiService.RESPONSE_ACTIVITY_DATA_START_DATE_SUCCESS, 0)) {
|
||||
handleActivityMetadata(value);
|
||||
TransactionBuilder newBuilder = createTransactionBuilder(taskName + " Step 2");
|
||||
newBuilder.notify(characteristicActivityData, true);
|
||||
newBuilder.write(characteristicFetch, new byte[] { HuamiService.COMMAND_FETCH_DATA});
|
||||
try {
|
||||
performImmediately(newBuilder);
|
||||
} catch (IOException ex) {
|
||||
GB.toast(getContext(), "Error fetching activity data: " + ex.getMessage(), Toast.LENGTH_LONG, GB.ERROR, ex);
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
handleActivityMetadata(value);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
});
|
||||
startFetching(builder, HuamiService.COMMAND_ACTIVITY_DATA_TYPE_ACTIVTY, sinceWhen);
|
||||
}
|
||||
|
||||
protected void handleActivityFetchFinish(boolean success) {
|
||||
|
@ -71,14 +71,7 @@ public class FetchSportsDetailsOperation extends AbstractFetchOperation {
|
||||
LOG.info("start " + getName());
|
||||
buffer = new ByteArrayOutputStream(1024);
|
||||
GregorianCalendar sinceWhen = getLastSuccessfulSyncTime();
|
||||
|
||||
builder.write(characteristicFetch, BLETypeConversions.join(new byte[] {
|
||||
HuamiService.COMMAND_ACTIVITY_DATA_START_DATE,
|
||||
AmazfitBipService.COMMAND_ACTIVITY_DATA_TYPE_SPORTS_DETAILS},
|
||||
getSupport().getTimeBytes(sinceWhen, TimeUnit.MINUTES)));
|
||||
builder.add(new WaitAction(1000)); // TODO: actually wait for the success-reply
|
||||
builder.notify(characteristicActivityData, true);
|
||||
builder.write(characteristicFetch, new byte[] { HuamiService.COMMAND_FETCH_DATA });
|
||||
startFetching(builder, AmazfitBipService.COMMAND_ACTIVITY_DATA_TYPE_SPORTS_DETAILS, sinceWhen);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -67,13 +67,7 @@ public class FetchSportsSummaryOperation extends AbstractFetchOperation {
|
||||
protected void startFetching(TransactionBuilder builder) {
|
||||
LOG.info("start" + getName());
|
||||
GregorianCalendar sinceWhen = getLastSuccessfulSyncTime();
|
||||
builder.write(characteristicFetch, BLETypeConversions.join(new byte[] {
|
||||
HuamiService.COMMAND_ACTIVITY_DATA_START_DATE,
|
||||
AmazfitBipService.COMMAND_ACTIVITY_DATA_TYPE_SPORTS_SUMMARIES},
|
||||
getSupport().getTimeBytes(sinceWhen, TimeUnit.MINUTES)));
|
||||
builder.add(new WaitAction(1000)); // TODO: actually wait for the success-reply
|
||||
builder.notify(characteristicActivityData, true);
|
||||
builder.write(characteristicFetch, new byte[] { HuamiService.COMMAND_FETCH_DATA });
|
||||
startFetching(builder, AmazfitBipService.COMMAND_ACTIVITY_DATA_TYPE_SPORTS_SUMMARIES, sinceWhen);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Loading…
Reference in New Issue
Block a user