use of nodomain.freeyourgadget.gadgetbridge.entities.Device in project Gadgetbridge by Freeyourgadget.
the class FetchActivityOperation method saveSamples.
private void saveSamples() {
if (samples.size() > 0) {
// save all the samples that we got
try (DBHandler handler = GBApplication.acquireDB()) {
DaoSession session = handler.getDaoSession();
SampleProvider<MiBandActivitySample> sampleProvider = new MiBandSampleProvider(getDevice(), session);
Device device = DBHelper.getDevice(getDevice(), session);
User user = DBHelper.getUser(session);
GregorianCalendar timestamp = (GregorianCalendar) startTimestamp.clone();
for (MiBandActivitySample sample : samples) {
sample.setDevice(device);
sample.setUser(user);
sample.setTimestamp((int) (timestamp.getTimeInMillis() / 1000));
sample.setProvider(sampleProvider);
if (LOG.isDebugEnabled()) {
// LOG.debug("sample: " + sample);
}
timestamp.add(Calendar.MINUTE, 1);
}
sampleProvider.addGBActivitySamples(samples.toArray(new MiBandActivitySample[0]));
saveLastSyncTimestamp(timestamp);
LOG.info("Mi2 activity data: last sample timestamp: " + DateTimeUtils.formatDateTime(timestamp.getTime()));
} catch (Exception ex) {
GB.toast(getContext(), "Error saving activity samples", Toast.LENGTH_LONG, GB.ERROR);
} finally {
samples.clear();
}
}
}
use of nodomain.freeyourgadget.gadgetbridge.entities.Device in project Gadgetbridge by Freeyourgadget.
the class EntitiesTest method testDBHelper.
@Test
public void testDBHelper() {
GBDevice dummyGBDevice = createDummyGDevice("00:00:00:00:01");
Device device = DBHelper.getDevice(dummyGBDevice, daoSession);
assertNotNull(device);
assertEquals("00:00:00:00:01", device.getIdentifier());
assertEquals("Testie", device.getName());
assertEquals("4.0", device.getModel());
assertEquals(DeviceType.TEST.getKey(), device.getType());
DeviceAttributes attributes = device.getDeviceAttributesList().get(0);
assertNotNull(attributes);
assertEquals("1.2.3", attributes.getFirmwareVersion1());
}
use of nodomain.freeyourgadget.gadgetbridge.entities.Device in project Gadgetbridge by Freeyourgadget.
the class HPlusHealthSampleProvider method getAllActivitySamples.
@NonNull
@Override
public List<HPlusHealthActivitySample> getAllActivitySamples(int timestamp_from, int timestamp_to) {
List<HPlusHealthActivitySample> samples = super.getGBActivitySamples(timestamp_from, timestamp_to, ActivityKind.TYPE_ALL);
Device dbDevice = DBHelper.findDevice(getDevice(), getSession());
if (dbDevice == null) {
return Collections.emptyList();
}
QueryBuilder<HPlusHealthActivityOverlay> qb = getSession().getHPlusHealthActivityOverlayDao().queryBuilder();
qb.where(HPlusHealthActivityOverlayDao.Properties.DeviceId.eq(dbDevice.getId()), HPlusHealthActivityOverlayDao.Properties.TimestampFrom.ge(timestamp_from - 3600 * 24), HPlusHealthActivityOverlayDao.Properties.TimestampTo.le(timestamp_to), HPlusHealthActivityOverlayDao.Properties.TimestampTo.ge(timestamp_from));
List<HPlusHealthActivityOverlay> overlayRecords = qb.build().list();
//Todays sample steps will come from the Day Slots messages
//Historical steps will be provided by Day Summaries messages
//This will allow both week and current day results to be consistent
Calendar today = GregorianCalendar.getInstance();
today.set(Calendar.HOUR_OF_DAY, 0);
today.set(Calendar.MINUTE, 0);
today.set(Calendar.SECOND, 0);
today.set(Calendar.MILLISECOND, 0);
int stepsTodayMax = 0;
int stepsTodayCount = 0;
HPlusHealthActivitySample lastSample = null;
for (HPlusHealthActivitySample sample : samples) {
if (sample.getTimestamp() >= today.getTimeInMillis() / 1000) {
if (sample.getRawKind() == HPlusDataRecord.TYPE_REALTIME) {
stepsTodayMax = Math.max(stepsTodayMax, sample.getSteps());
} else if (sample.getRawKind() == HPlusDataRecord.TYPE_DAY_SLOT) {
stepsTodayCount += sample.getSteps();
}
sample.setSteps(ActivitySample.NOT_MEASURED);
lastSample = sample;
} else {
if (sample.getRawKind() != HPlusDataRecord.TYPE_DAY_SUMMARY) {
sample.setSteps(ActivitySample.NOT_MEASURED);
}
}
}
if (lastSample != null)
lastSample.setSteps(Math.max(stepsTodayCount, stepsTodayMax));
for (HPlusHealthActivityOverlay overlay : overlayRecords) {
//Create fake events to improve activity counters if there are no events around the overlay
//timestamp boundaries
//Insert one before, one at the beginning, one at the end, and one 1s after.
insertVirtualItem(samples, Math.max(overlay.getTimestampFrom() - 1, timestamp_from), overlay.getDeviceId(), overlay.getUserId());
insertVirtualItem(samples, Math.max(overlay.getTimestampFrom(), timestamp_from), overlay.getDeviceId(), overlay.getUserId());
insertVirtualItem(samples, Math.min(overlay.getTimestampTo() - 1, timestamp_to - 1), overlay.getDeviceId(), overlay.getUserId());
insertVirtualItem(samples, Math.min(overlay.getTimestampTo(), timestamp_to), overlay.getDeviceId(), overlay.getUserId());
for (HPlusHealthActivitySample sample : samples) {
if (sample.getTimestamp() >= overlay.getTimestampFrom() && sample.getTimestamp() < overlay.getTimestampTo()) {
sample.setRawKind(overlay.getRawKind());
}
}
}
detachFromSession();
Collections.sort(samples, new Comparator<HPlusHealthActivitySample>() {
public int compare(HPlusHealthActivitySample one, HPlusHealthActivitySample other) {
return one.getTimestamp() - other.getTimestamp();
}
});
return samples;
}
use of nodomain.freeyourgadget.gadgetbridge.entities.Device in project Gadgetbridge by Freeyourgadget.
the class PebbleHealthSampleProvider method getAllActivitySamples.
@Override
public List<PebbleHealthActivitySample> getAllActivitySamples(int timestamp_from, int timestamp_to) {
List<PebbleHealthActivitySample> samples = super.getGBActivitySamples(timestamp_from, timestamp_to, ActivityKind.TYPE_ALL);
Device dbDevice = DBHelper.findDevice(getDevice(), getSession());
if (dbDevice == null) {
// no device, no samples
return Collections.emptyList();
}
QueryBuilder<PebbleHealthActivityOverlay> qb = getSession().getPebbleHealthActivityOverlayDao().queryBuilder();
// I assume it returns the records by id ascending ... (last overlay is dominant)
qb.where(PebbleHealthActivityOverlayDao.Properties.DeviceId.eq(dbDevice.getId()), PebbleHealthActivityOverlayDao.Properties.TimestampTo.ge(timestamp_from)).where(PebbleHealthActivityOverlayDao.Properties.TimestampFrom.le(timestamp_to));
List<PebbleHealthActivityOverlay> overlayRecords = qb.build().list();
for (PebbleHealthActivityOverlay overlay : overlayRecords) {
for (PebbleHealthActivitySample sample : samples) {
if (overlay.getTimestampFrom() <= sample.getTimestamp() && sample.getTimestamp() < overlay.getTimestampTo()) {
// patch in the raw kind
sample.setRawKind(overlay.getRawKind());
}
}
}
detachFromSession();
return samples;
}
use of nodomain.freeyourgadget.gadgetbridge.entities.Device in project Gadgetbridge by Freeyourgadget.
the class DBHelper method createDevice.
private static Device createDevice(GBDevice gbDevice, DaoSession session) {
Device device = new Device();
ensureDeviceUpToDate(device, gbDevice, session);
return device;
}
Aggregations