2021-01-10 23:37:09 +01:00
|
|
|
/* Copyright (C) 2017-2021 Andreas Shimokawa, Carsten Pfeiffer, Daniele
|
|
|
|
Gobbetti, Petr Vaněk
|
2017-09-08 23:56:05 +02: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/>. */
|
2018-08-01 22:56:01 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.service.devices.huami.operations;
|
2017-09-08 23:56:05 +02:00
|
|
|
|
|
|
|
import android.bluetooth.BluetoothGatt;
|
|
|
|
import android.bluetooth.BluetoothGattCharacteristic;
|
|
|
|
import android.content.SharedPreferences;
|
2018-09-15 22:38:20 +02:00
|
|
|
import android.widget.Toast;
|
2017-09-08 23:56:05 +02:00
|
|
|
|
2019-05-20 16:36:06 +02:00
|
|
|
import androidx.annotation.CallSuper;
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
|
2017-09-08 23:56:05 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.text.DateFormat;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.GregorianCalendar;
|
|
|
|
import java.util.UUID;
|
2018-09-15 22:38:20 +02:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2017-09-08 23:56:05 +02:00
|
|
|
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.Logging;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
2018-08-02 10:55:30 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiService;
|
2017-09-08 23:56:05 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.btle.BLETypeConversions;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder;
|
2018-09-15 22:38:20 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.AbstractGattListenerWriteAction;
|
2017-09-08 23:56:05 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceBusyAction;
|
2018-08-01 22:56:01 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.AbstractHuamiOperation;
|
2019-01-26 15:52:40 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.HuamiSupport;
|
2017-09-08 23:56:05 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.ArrayUtils;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
2018-09-15 22:38:20 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.StringUtils;
|
2017-09-08 23:56:05 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* An operation that fetches activity data. For every fetch, a new operation must
|
|
|
|
* be created, i.e. an operation may not be reused for multiple fetches.
|
|
|
|
*/
|
2018-08-01 22:56:01 +02:00
|
|
|
public abstract class AbstractFetchOperation extends AbstractHuamiOperation {
|
2017-09-08 23:56:05 +02:00
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(AbstractFetchOperation.class);
|
|
|
|
|
|
|
|
protected byte lastPacketCounter;
|
2019-05-20 16:36:06 +02:00
|
|
|
int fetchCount;
|
2017-09-08 23:56:05 +02:00
|
|
|
protected BluetoothGattCharacteristic characteristicActivityData;
|
|
|
|
protected BluetoothGattCharacteristic characteristicFetch;
|
2019-05-20 16:36:06 +02:00
|
|
|
Calendar startTimestamp;
|
2017-09-08 23:56:05 +02:00
|
|
|
|
2018-08-01 22:56:01 +02:00
|
|
|
public AbstractFetchOperation(HuamiSupport support) {
|
2017-09-08 23:56:05 +02:00
|
|
|
super(support);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void enableNeededNotifications(TransactionBuilder builder, boolean enable) {
|
|
|
|
if (!enable) {
|
|
|
|
// dynamically enabled, but always disabled on finish
|
2017-11-01 23:04:52 +01:00
|
|
|
builder.notify(characteristicFetch, enable);
|
|
|
|
builder.notify(characteristicActivityData, enable);
|
2017-09-08 23:56:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void doPerform() throws IOException {
|
|
|
|
startFetching();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void startFetching() throws IOException {
|
|
|
|
lastPacketCounter = -1;
|
|
|
|
|
2017-11-01 23:04:52 +01:00
|
|
|
TransactionBuilder builder = performInitialized(getName());
|
2017-09-08 23:56:05 +02:00
|
|
|
if (fetchCount == 0) {
|
|
|
|
builder.add(new SetDeviceBusyAction(getDevice(), getContext().getString(R.string.busy_task_fetch_activity_data), getContext()));
|
|
|
|
}
|
|
|
|
fetchCount++;
|
|
|
|
|
2018-11-01 22:05:16 +01:00
|
|
|
// TODO: this probably returns null when device is not connected/initialized yet!
|
2018-08-02 10:55:30 +02:00
|
|
|
characteristicActivityData = getCharacteristic(HuamiService.UUID_CHARACTERISTIC_5_ACTIVITY_DATA);
|
2017-09-08 23:56:05 +02:00
|
|
|
builder.notify(characteristicActivityData, false);
|
|
|
|
|
2018-08-02 10:55:30 +02:00
|
|
|
characteristicFetch = getCharacteristic(HuamiService.UUID_UNKNOWN_CHARACTERISTIC4);
|
2017-09-08 23:56:05 +02:00
|
|
|
builder.notify(characteristicFetch, true);
|
|
|
|
|
|
|
|
startFetching(builder);
|
|
|
|
builder.queue(getQueue());
|
|
|
|
}
|
|
|
|
|
|
|
|
protected abstract void startFetching(TransactionBuilder builder);
|
|
|
|
|
|
|
|
protected abstract String getLastSyncTimeKey();
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onCharacteristicChanged(BluetoothGatt gatt,
|
|
|
|
BluetoothGattCharacteristic characteristic) {
|
|
|
|
UUID characteristicUUID = characteristic.getUuid();
|
2018-08-02 10:55:30 +02:00
|
|
|
if (HuamiService.UUID_CHARACTERISTIC_5_ACTIVITY_DATA.equals(characteristicUUID)) {
|
2017-09-08 23:56:05 +02:00
|
|
|
handleActivityNotif(characteristic.getValue());
|
|
|
|
return true;
|
2018-08-02 10:55:30 +02:00
|
|
|
} else if (HuamiService.UUID_UNKNOWN_CHARACTERISTIC4.equals(characteristicUUID)) {
|
2017-09-08 23:56:05 +02:00
|
|
|
handleActivityMetadata(characteristic.getValue());
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return super.onCharacteristicChanged(gatt, characteristic);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@CallSuper
|
2017-10-19 21:52:38 +02:00
|
|
|
protected void handleActivityFetchFinish(boolean success) {
|
2019-05-20 16:36:06 +02:00
|
|
|
GB.updateTransferNotification(null, "", false, 100, getContext());
|
2017-09-08 23:56:05 +02:00
|
|
|
operationFinished();
|
|
|
|
unsetBusy();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method to handle the incoming activity data.
|
|
|
|
* There are two kind of messages we currently know:
|
|
|
|
* - the first one is 11 bytes long and contains metadata (how many bytes to expect, when the data starts, etc.)
|
|
|
|
* - the second one is 20 bytes long and contains the actual activity data
|
|
|
|
* <p/>
|
|
|
|
* The first message type is parsed by this method, for every other length of the value param, bufferActivityData is called.
|
|
|
|
*
|
|
|
|
* @param value
|
|
|
|
*/
|
|
|
|
protected abstract void handleActivityNotif(byte[] value);
|
|
|
|
|
|
|
|
protected abstract void bufferActivityData(byte[] value);
|
|
|
|
|
2018-09-15 22:38:20 +02:00
|
|
|
protected void startFetching(TransactionBuilder builder, byte fetchType, GregorianCalendar sinceWhen) {
|
|
|
|
final String taskName = StringUtils.ensureNotNull(builder.getTaskName());
|
|
|
|
byte[] fetchBytes = BLETypeConversions.join(new byte[]{
|
|
|
|
HuamiService.COMMAND_ACTIVITY_DATA_START_DATE,
|
|
|
|
fetchType},
|
|
|
|
getSupport().getTimeBytes(sinceWhen, TimeUnit.MINUTES));
|
|
|
|
builder.add(new AbstractGattListenerWriteAction(getQueue(), characteristicFetch, fetchBytes) {
|
|
|
|
@Override
|
|
|
|
protected boolean onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
|
|
|
|
UUID characteristicUUID = characteristic.getUuid();
|
|
|
|
if (HuamiService.UUID_UNKNOWN_CHARACTERISTIC4.equals(characteristicUUID)) {
|
|
|
|
byte[] value = characteristic.getValue();
|
|
|
|
|
|
|
|
if (ArrayUtils.equals(value, HuamiService.RESPONSE_ACTIVITY_DATA_START_DATE_SUCCESS, 0)) {
|
|
|
|
handleActivityMetadata(value);
|
|
|
|
TransactionBuilder newBuilder = createTransactionBuilder(taskName + " Step 2");
|
|
|
|
newBuilder.notify(characteristicActivityData, true);
|
|
|
|
newBuilder.write(characteristicFetch, new byte[]{HuamiService.COMMAND_FETCH_DATA});
|
|
|
|
try {
|
|
|
|
performImmediately(newBuilder);
|
|
|
|
} catch (IOException ex) {
|
|
|
|
GB.toast(getContext(), "Error fetching debug logs: " + ex.getMessage(), Toast.LENGTH_LONG, GB.ERROR, ex);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
handleActivityMetadata(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-20 16:36:06 +02:00
|
|
|
private void handleActivityMetadata(byte[] value) {
|
2022-08-18 23:03:28 +02:00
|
|
|
// TODO it's 16 on the MB7
|
2017-09-08 23:56:05 +02:00
|
|
|
if (value.length == 15) {
|
|
|
|
// first two bytes are whether our request was accepted
|
2018-08-02 10:55:30 +02:00
|
|
|
if (ArrayUtils.equals(value, HuamiService.RESPONSE_ACTIVITY_DATA_START_DATE_SUCCESS, 0)) {
|
2017-09-08 23:56:05 +02:00
|
|
|
// the third byte (0x01 on success) = ?
|
2017-11-01 23:04:52 +01:00
|
|
|
// the 4th - 7th bytes epresent the number of bytes/packets to expect, excluding the counter bytes
|
2019-05-20 16:36:06 +02:00
|
|
|
//int expectedDataLength = BLETypeConversions.toUint32(Arrays.copyOfRange(value, 3, 7));
|
2017-09-08 23:56:05 +02:00
|
|
|
|
|
|
|
// last 8 bytes are the start date
|
2017-09-18 23:24:11 +02:00
|
|
|
Calendar startTimestamp = getSupport().fromTimeBytes(Arrays.copyOfRange(value, 7, value.length));
|
2017-09-08 23:56:05 +02:00
|
|
|
setStartTimestamp(startTimestamp);
|
|
|
|
|
2018-06-19 22:03:49 +02:00
|
|
|
GB.updateTransferNotification(getContext().getString(R.string.busy_task_fetch_activity_data),
|
|
|
|
getContext().getString(R.string.FetchActivityOperation_about_to_transfer_since,
|
2019-05-20 16:36:06 +02:00
|
|
|
DateFormat.getDateTimeInstance().format(startTimestamp.getTime())), true, 0, getContext());
|
2017-09-08 23:56:05 +02:00
|
|
|
} else {
|
|
|
|
LOG.warn("Unexpected activity metadata: " + Logging.formatBytes(value));
|
2017-10-19 21:52:38 +02:00
|
|
|
handleActivityFetchFinish(false);
|
2017-09-08 23:56:05 +02:00
|
|
|
}
|
|
|
|
} else if (value.length == 3) {
|
2018-08-02 10:55:30 +02:00
|
|
|
if (Arrays.equals(HuamiService.RESPONSE_FINISH_SUCCESS, value)) {
|
2017-10-19 21:52:38 +02:00
|
|
|
handleActivityFetchFinish(true);
|
2017-09-08 23:56:05 +02:00
|
|
|
} else {
|
|
|
|
LOG.warn("Unexpected activity metadata: " + Logging.formatBytes(value));
|
2017-10-19 21:52:38 +02:00
|
|
|
handleActivityFetchFinish(false);
|
2017-09-08 23:56:05 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LOG.warn("Unexpected activity metadata: " + Logging.formatBytes(value));
|
2017-10-19 21:52:38 +02:00
|
|
|
handleActivityFetchFinish(false);
|
2017-09-08 23:56:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-20 16:36:06 +02:00
|
|
|
private void setStartTimestamp(Calendar startTimestamp) {
|
2017-09-08 23:56:05 +02:00
|
|
|
this.startTimestamp = startTimestamp;
|
|
|
|
}
|
|
|
|
|
2019-05-20 16:36:06 +02:00
|
|
|
Calendar getLastStartTimestamp() {
|
2018-04-02 00:22:55 +02:00
|
|
|
return startTimestamp;
|
|
|
|
}
|
|
|
|
|
2019-05-20 16:36:06 +02:00
|
|
|
void saveLastSyncTimestamp(@NonNull GregorianCalendar timestamp) {
|
|
|
|
SharedPreferences.Editor editor = GBApplication.getDeviceSpecificSharedPrefs(getDevice().getAddress()).edit();
|
2017-09-08 23:56:05 +02:00
|
|
|
editor.putLong(getLastSyncTimeKey(), timestamp.getTimeInMillis());
|
|
|
|
editor.apply();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected GregorianCalendar getLastSuccessfulSyncTime() {
|
2019-05-20 16:36:06 +02:00
|
|
|
long timeStampMillis = GBApplication.getDeviceSpecificSharedPrefs(getDevice().getAddress()).getLong(getLastSyncTimeKey(), 0);
|
2017-09-08 23:56:05 +02:00
|
|
|
if (timeStampMillis != 0) {
|
|
|
|
GregorianCalendar calendar = BLETypeConversions.createCalendar();
|
|
|
|
calendar.setTimeInMillis(timeStampMillis);
|
|
|
|
return calendar;
|
|
|
|
}
|
|
|
|
GregorianCalendar calendar = BLETypeConversions.createCalendar();
|
2019-05-20 16:36:06 +02:00
|
|
|
calendar.add(Calendar.DAY_OF_MONTH, -100);
|
2017-09-08 23:56:05 +02:00
|
|
|
return calendar;
|
|
|
|
}
|
|
|
|
}
|