1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-07-21 14:21:03 +02:00

Bip: improved logging, read expected number of bytes for progress monitoring during sync

This commit is contained in:
cpfeiffer 2017-11-01 23:04:52 +01:00
parent 331ca6a47e
commit fd159b7603
8 changed files with 54 additions and 22 deletions

View File

@ -43,6 +43,7 @@ import nodomain.freeyourgadget.gadgetbridge.service.devices.miband.operations.Op
public abstract class AbstractBTLEOperation<T extends AbstractBTLEDeviceSupport> implements GattCallback, BTLEOperation { public abstract class AbstractBTLEOperation<T extends AbstractBTLEDeviceSupport> implements GattCallback, BTLEOperation {
private final T mSupport; private final T mSupport;
protected OperationStatus operationStatus = OperationStatus.INITIAL; protected OperationStatus operationStatus = OperationStatus.INITIAL;
private String name;
protected AbstractBTLEOperation(T support) { protected AbstractBTLEOperation(T support) {
mSupport = support; mSupport = support;
@ -115,6 +116,22 @@ public abstract class AbstractBTLEOperation<T extends AbstractBTLEDeviceSupport>
return mSupport.getDevice(); return mSupport.getDevice();
} }
protected void setName(String name) {
this.name = name;
}
@Override
public String getName() {
if (name != null) {
return name;
}
String busyTask = getDevice().getBusyTask();
if (busyTask != null) {
return busyTask;
}
return getClass().getSimpleName();
}
protected BluetoothGattCharacteristic getCharacteristic(UUID uuid) { protected BluetoothGattCharacteristic getCharacteristic(UUID uuid) {
return mSupport.getCharacteristic(uuid); return mSupport.getCharacteristic(uuid);
} }

View File

@ -20,4 +20,9 @@ import java.io.IOException;
public interface BTLEOperation { public interface BTLEOperation {
void perform() throws IOException; void perform() throws IOException;
/**
* Returns a human readable name of this operation, to be used e.g. in log output.
*/
String getName();
} }

View File

@ -26,7 +26,6 @@ import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
@ -50,6 +49,7 @@ public class AmazfitBipFetchLogsOperation extends AbstractFetchOperation {
public AmazfitBipFetchLogsOperation(AmazfitBipSupport support) { public AmazfitBipFetchLogsOperation(AmazfitBipSupport support) {
super(support); super(support);
setName("fetch logs");
} }
@Override @Override
@ -90,7 +90,7 @@ public class AmazfitBipFetchLogsOperation extends AbstractFetchOperation {
@Override @Override
protected void handleActivityFetchFinish(boolean success) { protected void handleActivityFetchFinish(boolean success) {
LOG.info("Fetching log data has finished"); LOG.info(getName() +" data has finished");
try { try {
logOutputStream.close(); logOutputStream.close();
logOutputStream = null; logOutputStream = null;
@ -113,7 +113,7 @@ public class AmazfitBipFetchLogsOperation extends AbstractFetchOperation {
lastPacketCounter++; lastPacketCounter++;
bufferActivityData(value); bufferActivityData(value);
} else { } else {
GB.toast("Error fetching activity data, invalid package counter: " + value[0], Toast.LENGTH_LONG, GB.ERROR); GB.toast("Error " + getName() + " invalid package counter: " + value[0], Toast.LENGTH_LONG, GB.ERROR);
handleActivityFetchFinish(false); handleActivityFetchFinish(false);
} }
} }

View File

@ -57,6 +57,7 @@ public abstract class AbstractFetchOperation extends AbstractMiBand2Operation {
protected BluetoothGattCharacteristic characteristicActivityData; protected BluetoothGattCharacteristic characteristicActivityData;
protected BluetoothGattCharacteristic characteristicFetch; protected BluetoothGattCharacteristic characteristicFetch;
protected Calendar startTimestamp; protected Calendar startTimestamp;
protected int expectedDataLength;
public AbstractFetchOperation(MiBand2Support support) { public AbstractFetchOperation(MiBand2Support support) {
super(support); super(support);
@ -66,7 +67,8 @@ public abstract class AbstractFetchOperation extends AbstractMiBand2Operation {
protected void enableNeededNotifications(TransactionBuilder builder, boolean enable) { protected void enableNeededNotifications(TransactionBuilder builder, boolean enable) {
if (!enable) { if (!enable) {
// dynamically enabled, but always disabled on finish // dynamically enabled, but always disabled on finish
builder.notify(getCharacteristic(MiBand2Service.UUID_CHARACTERISTIC_5_ACTIVITY_DATA), enable); builder.notify(characteristicFetch, enable);
builder.notify(characteristicActivityData, enable);
} }
} }
@ -78,7 +80,7 @@ public abstract class AbstractFetchOperation extends AbstractMiBand2Operation {
protected void startFetching() throws IOException { protected void startFetching() throws IOException {
lastPacketCounter = -1; lastPacketCounter = -1;
TransactionBuilder builder = performInitialized("fetching activity data"); TransactionBuilder builder = performInitialized(getName());
getSupport().setLowLatency(builder); getSupport().setLowLatency(builder);
if (fetchCount == 0) { if (fetchCount == 0) {
builder.add(new SetDeviceBusyAction(getDevice(), getContext().getString(R.string.busy_task_fetch_activity_data), getContext())); builder.add(new SetDeviceBusyAction(getDevice(), getContext().getString(R.string.busy_task_fetch_activity_data), getContext()));
@ -139,7 +141,8 @@ public abstract class AbstractFetchOperation extends AbstractMiBand2Operation {
// first two bytes are whether our request was accepted // first two bytes are whether our request was accepted
if (ArrayUtils.equals(value, MiBand2Service.RESPONSE_ACTIVITY_DATA_START_DATE_SUCCESS, 0)) { if (ArrayUtils.equals(value, MiBand2Service.RESPONSE_ACTIVITY_DATA_START_DATE_SUCCESS, 0)) {
// the third byte (0x01 on success) = ? // the third byte (0x01 on success) = ?
// the 4th - 7th bytes probably somehow represent the number of bytes/packets to expect // the 4th - 7th bytes epresent the number of bytes/packets to expect, excluding the counter bytes
expectedDataLength = BLETypeConversions.toUint32(Arrays.copyOfRange(value, 3, 7));
// last 8 bytes are the start date // last 8 bytes are the start date
Calendar startTimestamp = getSupport().fromTimeBytes(Arrays.copyOfRange(value, 7, value.length)); Calendar startTimestamp = getSupport().fromTimeBytes(Arrays.copyOfRange(value, 7, value.length));

View File

@ -57,6 +57,7 @@ public class FetchActivityOperation extends AbstractFetchOperation {
public FetchActivityOperation(MiBand2Support support) { public FetchActivityOperation(MiBand2Support support) {
super(support); super(support);
setName("fetching activity data");
} }
@Override @Override
@ -75,14 +76,14 @@ public class FetchActivityOperation extends AbstractFetchOperation {
} }
protected void handleActivityFetchFinish(boolean success) { protected void handleActivityFetchFinish(boolean success) {
LOG.info("Fetching activity data has finished round " + fetchCount); LOG.info(getName() + " has finished round " + fetchCount);
GregorianCalendar lastSyncTimestamp = saveSamples(); GregorianCalendar lastSyncTimestamp = saveSamples();
if (lastSyncTimestamp != null && needsAnotherFetch(lastSyncTimestamp)) { if (lastSyncTimestamp != null && needsAnotherFetch(lastSyncTimestamp)) {
try { try {
startFetching(); startFetching();
return; return;
} catch (IOException ex) { } catch (IOException ex) {
LOG.error("Error starting another round of fetching activity data", ex); LOG.error("Error starting another round of " + getName(), ex);
} }
} }
@ -91,7 +92,7 @@ public class FetchActivityOperation extends AbstractFetchOperation {
private boolean needsAnotherFetch(GregorianCalendar lastSyncTimestamp) { private boolean needsAnotherFetch(GregorianCalendar lastSyncTimestamp) {
if (fetchCount > 5) { if (fetchCount > 5) {
LOG.warn("Already jave 5 fetch rounds, not doing another one."); LOG.warn("Already have 5 fetch rounds, not doing another one.");
return false; return false;
} }
@ -166,12 +167,12 @@ public class FetchActivityOperation extends AbstractFetchOperation {
lastPacketCounter++; lastPacketCounter++;
bufferActivityData(value); bufferActivityData(value);
} else { } else {
GB.toast("Error fetching activity data, invalid package counter: " + value[0], Toast.LENGTH_LONG, GB.ERROR); GB.toast("Error " + getName() + ", invalid package counter: " + value[0], Toast.LENGTH_LONG, GB.ERROR);
handleActivityFetchFinish(false); handleActivityFetchFinish(false);
return; return;
} }
} else { } else {
GB.toast("Error fetching activity data, unexpected package length: " + value.length, Toast.LENGTH_LONG, GB.ERROR); GB.toast("Error " + getName() + ", unexpected package length: " + value.length, Toast.LENGTH_LONG, GB.ERROR);
handleActivityFetchFinish(false); handleActivityFetchFinish(false);
} }
} }

View File

@ -56,11 +56,13 @@ public class FetchSportsDetailsOperation extends AbstractFetchOperation {
public FetchSportsDetailsOperation(BaseActivitySummary summary, MiBand2Support support) { public FetchSportsDetailsOperation(BaseActivitySummary summary, MiBand2Support support) {
super(support); super(support);
setName("fetching sport details");
this.summary = summary; this.summary = summary;
} }
@Override @Override
protected void startFetching(TransactionBuilder builder) { protected void startFetching(TransactionBuilder builder) {
LOG.info("start " + getName());
buffer = new ByteArrayOutputStream(1024); buffer = new ByteArrayOutputStream(1024);
GregorianCalendar sinceWhen = getLastSuccessfulSyncTime(); GregorianCalendar sinceWhen = getLastSuccessfulSyncTime();
builder.write(characteristicFetch, BLETypeConversions.join(new byte[] { builder.write(characteristicFetch, BLETypeConversions.join(new byte[] {
@ -74,7 +76,7 @@ public class FetchSportsDetailsOperation extends AbstractFetchOperation {
@Override @Override
protected void handleActivityFetchFinish(boolean success) { protected void handleActivityFetchFinish(boolean success) {
LOG.info("Fetching activity data has finished round " + fetchCount); LOG.info(getName() + " has finished round " + fetchCount);
// GregorianCalendar lastSyncTimestamp = saveSamples(); // GregorianCalendar lastSyncTimestamp = saveSamples();
// if (lastSyncTimestamp != null && needsAnotherFetch(lastSyncTimestamp)) { // if (lastSyncTimestamp != null && needsAnotherFetch(lastSyncTimestamp)) {
// try { // try {
@ -126,16 +128,16 @@ public class FetchSportsDetailsOperation extends AbstractFetchOperation {
*/ */
@Override @Override
protected void handleActivityNotif(byte[] value) { protected void handleActivityNotif(byte[] value) {
LOG.warn("sports data: " + Logging.formatBytes(value)); LOG.warn("sports details: " + Logging.formatBytes(value));
if (!isOperationRunning()) { if (!isOperationRunning()) {
LOG.error("ignoring activity data notification because operation is not running. Data length: " + value.length); LOG.error("ignoring sports details notification because operation is not running. Data length: " + value.length);
getSupport().logMessageContent(value); getSupport().logMessageContent(value);
return; return;
} }
if (value.length < 2) { if (value.length < 2) {
LOG.error("unexpected sports summary data length: " + value.length); LOG.error("unexpected sports details data length: " + value.length);
getSupport().logMessageContent(value); getSupport().logMessageContent(value);
return; return;
} }
@ -144,7 +146,7 @@ public class FetchSportsDetailsOperation extends AbstractFetchOperation {
lastPacketCounter++; lastPacketCounter++;
bufferActivityData(value); bufferActivityData(value);
} else { } else {
GB.toast("Error fetching activity data, invalid package counter: " + value[0] + ", last was: " + lastPacketCounter, Toast.LENGTH_LONG, GB.ERROR); GB.toast("Error " + getName() + ", invalid package counter: " + value[0] + ", last was: " + lastPacketCounter, Toast.LENGTH_LONG, GB.ERROR);
handleActivityFetchFinish(false); handleActivityFetchFinish(false);
return; return;
} }

View File

@ -38,7 +38,6 @@ import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
import nodomain.freeyourgadget.gadgetbridge.database.DBHelper; import nodomain.freeyourgadget.gadgetbridge.database.DBHelper;
import nodomain.freeyourgadget.gadgetbridge.devices.huami.amazfitbip.AmazfitBipService; import nodomain.freeyourgadget.gadgetbridge.devices.huami.amazfitbip.AmazfitBipService;
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBand2Service; import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBand2Service;
import nodomain.freeyourgadget.gadgetbridge.devices.miband2.MiBand2Const;
import nodomain.freeyourgadget.gadgetbridge.entities.BaseActivitySummary; import nodomain.freeyourgadget.gadgetbridge.entities.BaseActivitySummary;
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession; import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
import nodomain.freeyourgadget.gadgetbridge.entities.Device; import nodomain.freeyourgadget.gadgetbridge.entities.Device;
@ -60,14 +59,14 @@ public class FetchSportsSummaryOperation extends AbstractFetchOperation {
private ByteArrayOutputStream buffer = new ByteArrayOutputStream(140); private ByteArrayOutputStream buffer = new ByteArrayOutputStream(140);
// private List<MiBandActivitySample> samples = new ArrayList<>(60*24); // 1day per default
public FetchSportsSummaryOperation(MiBand2Support support) { public FetchSportsSummaryOperation(MiBand2Support support) {
super(support); super(support);
setName("fetching sport summaries");
} }
@Override @Override
protected void startFetching(TransactionBuilder builder) { protected void startFetching(TransactionBuilder builder) {
LOG.info("start" + getName());
GregorianCalendar sinceWhen = getLastSuccessfulSyncTime(); GregorianCalendar sinceWhen = getLastSuccessfulSyncTime();
builder.write(characteristicFetch, BLETypeConversions.join(new byte[] { builder.write(characteristicFetch, BLETypeConversions.join(new byte[] {
MiBand2Service.COMMAND_ACTIVITY_DATA_START_DATE, MiBand2Service.COMMAND_ACTIVITY_DATA_START_DATE,
@ -80,7 +79,7 @@ public class FetchSportsSummaryOperation extends AbstractFetchOperation {
@Override @Override
protected void handleActivityFetchFinish(boolean success) { protected void handleActivityFetchFinish(boolean success) {
LOG.info("Fetching activity data has finished round " + fetchCount); LOG.info(getName() + " has finished round " + fetchCount);
// GregorianCalendar lastSyncTimestamp = saveSamples(); // GregorianCalendar lastSyncTimestamp = saveSamples();
// if (lastSyncTimestamp != null && needsAnotherFetch(lastSyncTimestamp)) { // if (lastSyncTimestamp != null && needsAnotherFetch(lastSyncTimestamp)) {
@ -137,7 +136,7 @@ public class FetchSportsSummaryOperation extends AbstractFetchOperation {
*/ */
@Override @Override
protected void handleActivityNotif(byte[] value) { protected void handleActivityNotif(byte[] value) {
LOG.warn("sports data: " + Logging.formatBytes(value)); LOG.warn("sports summary data: " + Logging.formatBytes(value));
if (!isOperationRunning()) { if (!isOperationRunning()) {
LOG.error("ignoring activity data notification because operation is not running. Data length: " + value.length); LOG.error("ignoring activity data notification because operation is not running. Data length: " + value.length);
@ -155,7 +154,7 @@ public class FetchSportsSummaryOperation extends AbstractFetchOperation {
lastPacketCounter++; lastPacketCounter++;
bufferActivityData(value); bufferActivityData(value);
} else { } else {
GB.toast("Error fetching activity data, invalid package counter: " + value[0] + ", last was: " + lastPacketCounter, Toast.LENGTH_LONG, GB.ERROR); GB.toast("Error " + getName() + ", invalid package counter: " + value[0] + ", last was: " + lastPacketCounter, Toast.LENGTH_LONG, GB.ERROR);
handleActivityFetchFinish(false); handleActivityFetchFinish(false);
return; return;
} }

View File

@ -23,6 +23,11 @@ public class Tryout extends TestBase {
byte b = (byte) v; byte b = (byte) v;
LOG.info("v: " + v); LOG.info("v: " + v);
Logging.logBytes(LOG, new byte[] { b }); Logging.logBytes(LOG, new byte[] { b });
byte[] bs = new byte[] {(byte) 0xf0,0x28,0x00,0x00 };
LOG.warn("uint32: " + BLETypeConversions.toUint32(bs));
LOG.warn("uint16: " + BLETypeConversions.toUint16(bs));
} }
@Test @Test