Moyoung: Improve activity syncing status

This commit is contained in:
Arjan Schrijver
2025-06-11 20:58:00 +02:00
parent 9aab704fe7
commit bfeb2b5aa0
2 changed files with 22 additions and 10 deletions
@@ -84,6 +84,7 @@ public class FetchDataOperation extends AbstractBTLEOperation<MoyoungDeviceSuppo
if (charUuid.equals(MoyoungConstants.UUID_CHARACTERISTIC_STEPS)) {
byte[] data = characteristic.getValue();
LOG.info("TODAY STEPS data: " + Logging.formatBytes(data));
receivedSteps[0] = true;
decodeSteps(0, data);
return true;
}
@@ -123,6 +124,7 @@ public class FetchDataOperation extends AbstractBTLEOperation<MoyoungDeviceSuppo
private boolean handlePacket(byte packetType, byte[] payload) {
if (packetType == MoyoungConstants.CMD_SYNC_SLEEP) {
LOG.info("TODAY SLEEP data: " + Logging.formatBytes(payload));
receivedSleep[0] = true;
decodeSleep(0, payload);
return true;
}
@@ -143,21 +145,25 @@ public class FetchDataOperation extends AbstractBTLEOperation<MoyoungDeviceSuppo
if (dataType == MoyoungConstants.ARG_SYNC_DAY_BEFORE_YESTERDAY_STEPS) {
LOG.info("2 DAYS AGO STEPS data: " + Logging.formatBytes(data));
receivedSteps[2] = true;
decodeSteps(2, data);
return true;
}
else if (dataType == MoyoungConstants.ARG_SYNC_YESTERDAY_STEPS) {
LOG.info("YESTERDAY STEPS data: " + Logging.formatBytes(data));
receivedSteps[1] = true;
decodeSteps(1, data);
return true;
}
else if (dataType == MoyoungConstants.ARG_SYNC_DAY_BEFORE_YESTERDAY_SLEEP) {
LOG.info("2 DAYS AGO SLEEP data: " + Logging.formatBytes(data));
receivedSleep[2] = true;
decodeSleep(2 - daysAgoOffset, data);
return true;
}
else if (dataType == MoyoungConstants.ARG_SYNC_YESTERDAY_SLEEP) {
LOG.info("YESTERDAY SLEEP data: " + Logging.formatBytes(data));
receivedSleep[1] = true;
decodeSleep(1 - daysAgoOffset, data);
return true;
}
@@ -172,14 +178,12 @@ public class FetchDataOperation extends AbstractBTLEOperation<MoyoungDeviceSuppo
private void decodeSteps(int daysAgo, byte[] data)
{
getSupport().handleStepsHistory(daysAgo, data, false);
receivedSteps[daysAgo] = true;
updateProgressAndCheckFinish();
}
private void decodeSleep(int daysAgo, byte[] data)
{
getSupport().handleSleepHistory(daysAgo, data);
receivedSleep[daysAgo] = true;
updateProgressAndCheckFinish();
}
@@ -192,18 +196,21 @@ public class FetchDataOperation extends AbstractBTLEOperation<MoyoungDeviceSuppo
private void updateProgressAndCheckFinish()
{
int count = 0;
int count_steps = 0;
int count_sleep = 0;
int count_training = 0;
int total = receivedSteps.length + receivedSleep.length;
for(int i = 0; i < receivedSteps.length; i++)
for (int i = 0; i < receivedSteps.length; i++)
if (receivedSteps[i])
++count;
for(int i = 0; i < receivedSleep.length; i++)
++count_steps;
for (int i = 0; i < receivedSleep.length; i++)
if (receivedSleep[i])
++count;
++count_sleep;
if (receivedTrainingData)
++count;
++count_training;
int count = count_steps + count_sleep + count_training;
GB.updateTransferNotification(null, getContext().getString(R.string.busy_task_fetch_activity_data), true, 100 * count / total, getContext());
LOG.debug("Fetching activity data status: {} out of {}", count, total);
LOG.debug("Fetching activity data status: {} out of {} steps packets and {} out of {} sleep packets", count_steps, receivedSteps.length, count_sleep, receivedSleep.length);
if (count == total)
operationFinished();
}
@@ -1127,13 +1127,18 @@ public class MoyoungDeviceSupport extends AbstractBTLEDeviceSupport {
sample.setUserId(userId);
}
LOG.debug("Will persist {} HR samples", hrSamples.size());
sampleProvider.addSamples(hrSamples);
} catch (Exception e) {
LOG.error("Error acquiring database for recording heart rate samples", e);
}
// Request next batch
if (packetIndex >= 7) return; // 8 packets = 2 days, the maximum
if (packetIndex >= 7) {
// 8 packets = 2 days, the maximum
LOG.info("Last HR history packet received, HR sync finished.");
return;
}
try {
TransactionBuilder builder = performInitialized("FetchHROperation");
sendPacket(builder, MoyoungPacketOut.buildPacket(getMtu(), MoyoungConstants.CMD_QUERY_PAST_HEART_RATE_1, new byte[]{(byte) (packetIndex+1)}));