use of nodomain.freeyourgadget.gadgetbridge.entities.MiBandActivitySample in project Gadgetbridge by Freeyourgadget.
the class FetchActivityOperation method flushActivityDataHolder.
/**
* empty the local buffer for activity data, arrange the values received in groups of three and
* store them in the DB
*/
private void flushActivityDataHolder() {
if (activityStruct == null) {
LOG.debug("nothing to flush, struct is already null");
return;
}
int bpm = getBytesPerMinuteOfActivityData();
LOG.debug("flushing activity data samples: " + activityStruct.activityDataHolderProgress / bpm);
byte category, intensity, steps, heartrate = 0;
try (DBHandler dbHandler = GBApplication.acquireDB()) {
MiBandSampleProvider provider = new MiBandSampleProvider(getDevice(), dbHandler.getDaoSession());
User user = DBHelper.getUser(dbHandler.getDaoSession());
Device device = DBHelper.getDevice(getDevice(), dbHandler.getDaoSession());
int minutes = 0;
try {
int timestampInSeconds = (int) (activityStruct.activityDataTimestampProgress.getTimeInMillis() / 1000);
if ((activityStruct.activityDataHolderProgress % bpm) != 0) {
throw new IllegalStateException("Unexpected data, progress should be multiple of " + bpm + ": " + activityStruct.activityDataHolderProgress);
}
int numSamples = activityStruct.activityDataHolderProgress / bpm;
MiBandActivitySample[] samples = new MiBandActivitySample[numSamples];
for (int i = 0; i < activityStruct.activityDataHolderProgress; i += bpm) {
category = activityStruct.activityDataHolder[i];
intensity = activityStruct.activityDataHolder[i + 1];
steps = activityStruct.activityDataHolder[i + 2];
if (hasExtendedActivityData) {
heartrate = activityStruct.activityDataHolder[i + 3];
// LOG.debug("heartrate received: " + (heartrate & 0xff));
}
MiBandActivitySample sample = getSupport().createActivitySample(device, user, timestampInSeconds, provider);
sample.setRawIntensity(intensity & 0xff);
sample.setSteps(steps & 0xff);
sample.setRawKind(category & 0xff);
sample.setHeartRate(heartrate & 0xff);
samples[minutes] = sample;
if (LOG.isDebugEnabled()) {
LOG.debug("sample: " + samples[minutes]);
}
// next minute
minutes++;
timestampInSeconds += 60;
}
provider.addGBActivitySamples(samples);
} finally {
activityStruct.bufferFlushed(minutes);
}
} catch (Exception ex) {
GB.toast(getContext(), ex.getMessage(), Toast.LENGTH_LONG, GB.ERROR, ex);
}
}
use of nodomain.freeyourgadget.gadgetbridge.entities.MiBandActivitySample in project Gadgetbridge by Freeyourgadget.
the class FetchActivityOperation method bufferActivityData.
/**
* Creates samples from the given 17-length array
* @param value
*/
private void bufferActivityData(byte[] value) {
int len = value.length;
if (len % 4 != 1) {
throw new AssertionError("Unexpected activity array size: " + value);
}
for (int i = 1; i < len; i += 4) {
MiBandActivitySample sample = createSample(value[i], value[i + 1], value[i + 2], value[i + 3]);
samples.add(sample);
}
}
use of nodomain.freeyourgadget.gadgetbridge.entities.MiBandActivitySample in project Gadgetbridge by Freeyourgadget.
the class MiBandSupport method createActivitySample.
public MiBandActivitySample createActivitySample(Device device, User user, int timestampInSeconds, SampleProvider provider) {
MiBandActivitySample sample = new MiBandActivitySample();
sample.setDevice(device);
sample.setUser(user);
sample.setTimestamp(timestampInSeconds);
sample.setProvider(provider);
return sample;
}
Aggregations