2021-01-10 23:37:09 +01:00
|
|
|
/* Copyright (C) 2018-2021 Daniele Gobbetti, Gordon Williams, Vadim Kaushan
|
2020-12-04 16:14:00 +01:00
|
|
|
|
|
|
|
This file is part of Gadgetbridge.
|
|
|
|
|
|
|
|
Gadgetbridge is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published
|
|
|
|
by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Gadgetbridge is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
|
|
|
package nodomain.freeyourgadget.gadgetbridge.devices.banglejs;
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
|
2023-08-08 23:11:14 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
2020-12-04 16:14:00 +01:00
|
|
|
import de.greenrobot.dao.AbstractDao;
|
|
|
|
import de.greenrobot.dao.Property;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.AbstractSampleProvider;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.BangleJSActivitySample;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.BangleJSActivitySampleDao;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
2020-12-04 17:30:19 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
|
2020-12-04 16:14:00 +01:00
|
|
|
|
|
|
|
public class BangleJSSampleProvider extends AbstractSampleProvider<BangleJSActivitySample> {
|
2023-08-08 23:11:14 +02:00
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(BangleJSSampleProvider.class);
|
|
|
|
|
2020-12-04 16:14:00 +01:00
|
|
|
public BangleJSSampleProvider(GBDevice device, DaoSession session) {
|
|
|
|
super(device, session);
|
|
|
|
}
|
|
|
|
|
2020-12-04 17:30:19 +01:00
|
|
|
public static final int TYPE_ACTIVITY = 0;
|
|
|
|
|
2020-12-04 16:14:00 +01:00
|
|
|
@Override
|
|
|
|
public AbstractDao<BangleJSActivitySample, ?> getSampleDao() {
|
|
|
|
return getSession().getBangleJSActivitySampleDao();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
protected Property getRawKindSampleProperty() {
|
|
|
|
return BangleJSActivitySampleDao.Properties.RawKind;
|
|
|
|
}
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
@Override
|
|
|
|
protected Property getTimestampSampleProperty() {
|
|
|
|
return BangleJSActivitySampleDao.Properties.Timestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
@Override
|
|
|
|
protected Property getDeviceIdentifierSampleProperty() {
|
|
|
|
return BangleJSActivitySampleDao.Properties.DeviceId;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int normalizeType(int rawType) {
|
2020-12-04 17:30:19 +01:00
|
|
|
switch (rawType) {
|
|
|
|
case TYPE_ACTIVITY:
|
|
|
|
return ActivityKind.TYPE_ACTIVITY;
|
|
|
|
default: // fall through
|
|
|
|
return ActivityKind.TYPE_UNKNOWN;
|
|
|
|
}
|
2020-12-04 16:14:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int toRawActivityKind(int activityKind) {
|
2020-12-04 17:30:19 +01:00
|
|
|
return TYPE_ACTIVITY;
|
2020-12-04 16:14:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public float normalizeIntensity(int rawIntensity) {
|
2023-08-08 23:11:14 +02:00
|
|
|
return rawIntensity / 256.0f;
|
2020-12-04 16:14:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public BangleJSActivitySample createActivitySample() {
|
|
|
|
return new BangleJSActivitySample();
|
|
|
|
}
|
2023-08-08 23:11:14 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Upserts a sample in the database, avoiding duplicated samples if a sample already exists in a
|
|
|
|
* close timestamp (within 2 minutes);
|
|
|
|
*/
|
|
|
|
public void upsertSample(final BangleJSActivitySample sample) {
|
|
|
|
final List<BangleJSActivitySample> nearSamples = getGBActivitySamples(
|
|
|
|
sample.getTimestamp() - 60 * 2,
|
|
|
|
sample.getTimestamp() + 60 * 2,
|
|
|
|
normalizeType(sample.getRawKind())
|
|
|
|
);
|
|
|
|
|
|
|
|
if (nearSamples.isEmpty()) {
|
|
|
|
// No nearest sample, just insert
|
|
|
|
LOG.debug("No duplicate found at {}, inserting", sample.getTimestamp());
|
|
|
|
addGBActivitySample(sample);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
BangleJSActivitySample nearestSample = nearSamples.get(0);
|
|
|
|
|
|
|
|
for (final BangleJSActivitySample s : nearSamples) {
|
|
|
|
final int curDist = Math.abs(nearestSample.getTimestamp() - s.getTimestamp());
|
|
|
|
final int newDist = Math.abs(sample.getTimestamp() - s.getTimestamp());
|
|
|
|
if (newDist < curDist) {
|
|
|
|
nearestSample = s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG.debug("Found {} duplicates for {}, updating nearest sample at {}", nearSamples.size(), sample.getTimestamp(), nearestSample.getTimestamp());
|
|
|
|
|
|
|
|
if (sample.getHeartRate() != 0) {
|
|
|
|
nearestSample.setHeartRate(sample.getHeartRate());
|
|
|
|
}
|
|
|
|
if (sample.getSteps() != 0) {
|
|
|
|
nearestSample.setSteps(sample.getSteps());
|
|
|
|
}
|
|
|
|
if (sample.getRawIntensity() != 0) {
|
|
|
|
nearestSample.setRawIntensity(sample.getRawIntensity());
|
|
|
|
}
|
|
|
|
|
|
|
|
addGBActivitySample(nearestSample);
|
|
|
|
}
|
2020-12-04 16:14:00 +01:00
|
|
|
}
|