mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2024-08-29 02:25:14 +02:00
73 lines
2.7 KiB
Java
73 lines
2.7 KiB
Java
|
/* Copyright (C) 2023 José Rebelo
|
||
|
|
||
|
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.service.devices.huami.operations;
|
||
|
|
||
|
import org.slf4j.Logger;
|
||
|
import org.slf4j.LoggerFactory;
|
||
|
|
||
|
import java.nio.ByteBuffer;
|
||
|
import java.nio.ByteOrder;
|
||
|
import java.util.GregorianCalendar;
|
||
|
|
||
|
import nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiService;
|
||
|
import nodomain.freeyourgadget.gadgetbridge.service.btle.BLETypeConversions;
|
||
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.HuamiSupport;
|
||
|
|
||
|
/**
|
||
|
* An operation that fetches manual stress data.
|
||
|
*/
|
||
|
public class FetchStressManualOperation extends AbstractRepeatingFetchOperation {
|
||
|
private static final Logger LOG = LoggerFactory.getLogger(FetchStressManualOperation.class);
|
||
|
|
||
|
public FetchStressManualOperation(final HuamiSupport support) {
|
||
|
super(support, HuamiService.COMMAND_ACTIVITY_DATA_TYPE_STRESS_MANUAL, "manual stress data");
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
protected boolean handleActivityData(final GregorianCalendar timestamp, final byte[] bytes) {
|
||
|
if (bytes.length % 5 != 0) {
|
||
|
LOG.info("Unexpected buffered stress data size {} is not a multiple of 5", bytes.length);
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
final ByteBuffer buffer = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN);
|
||
|
final GregorianCalendar lastSyncTimestamp = new GregorianCalendar();
|
||
|
|
||
|
while (buffer.position() < bytes.length) {
|
||
|
final long currentTimestamp = BLETypeConversions.toUnsigned(buffer.getInt()) * 1000;
|
||
|
|
||
|
// 0-39 = relaxed
|
||
|
// 40-59 = mild
|
||
|
// 60-79 = moderate
|
||
|
// 80-100 = high
|
||
|
final int stress = buffer.get() & 0xff;
|
||
|
timestamp.setTimeInMillis(currentTimestamp);
|
||
|
|
||
|
LOG.info("Stress (manual) at {}: {}", lastSyncTimestamp.getTime(), stress);
|
||
|
|
||
|
// TODO: Save stress data
|
||
|
}
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
protected String getLastSyncTimeKey() {
|
||
|
return "lastStressManualTimeMillis";
|
||
|
}
|
||
|
}
|