2019-02-13 20:43:30 +01:00
|
|
|
/* Copyright (C) 2015-2019 Andreas Shimokawa, Carsten Pfeiffer, Daniele
|
|
|
|
Gobbetti
|
2017-03-10 14:53:19 +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/>. */
|
2015-08-03 23:09:49 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.service.btle;
|
2015-04-19 02:37:29 +02:00
|
|
|
|
|
|
|
import android.bluetooth.BluetoothGattCharacteristic;
|
2015-05-12 06:28:11 +02:00
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2015-04-19 02:37:29 +02:00
|
|
|
|
2019-01-26 15:52:40 +01:00
|
|
|
import androidx.annotation.Nullable;
|
2015-08-03 23:09:49 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.NotifyAction;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.ReadAction;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.WaitAction;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.WriteAction;
|
|
|
|
|
2015-04-19 02:37:29 +02:00
|
|
|
public class TransactionBuilder {
|
2015-05-12 06:28:11 +02:00
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(TransactionBuilder.class);
|
2015-04-19 02:37:29 +02:00
|
|
|
|
2015-11-23 23:04:46 +01:00
|
|
|
private final Transaction mTransaction;
|
2015-10-18 22:08:52 +02:00
|
|
|
private boolean mQueued;
|
2015-04-19 11:28:03 +02:00
|
|
|
|
2015-04-19 02:37:29 +02:00
|
|
|
public TransactionBuilder(String taskName) {
|
|
|
|
mTransaction = new Transaction(taskName);
|
|
|
|
}
|
2015-04-19 11:28:03 +02:00
|
|
|
|
2015-04-19 02:37:29 +02:00
|
|
|
public TransactionBuilder read(BluetoothGattCharacteristic characteristic) {
|
|
|
|
if (characteristic == null) {
|
2015-05-12 06:28:11 +02:00
|
|
|
LOG.warn("Unable to read characteristic: null");
|
2015-04-19 02:37:29 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
ReadAction action = new ReadAction(characteristic);
|
|
|
|
return add(action);
|
|
|
|
}
|
2015-04-19 11:28:03 +02:00
|
|
|
|
2015-04-19 02:37:29 +02:00
|
|
|
public TransactionBuilder write(BluetoothGattCharacteristic characteristic, byte[] data) {
|
|
|
|
if (characteristic == null) {
|
2015-05-12 06:28:11 +02:00
|
|
|
LOG.warn("Unable to write characteristic: null");
|
2015-04-19 02:37:29 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
WriteAction action = new WriteAction(characteristic, data);
|
|
|
|
return add(action);
|
|
|
|
}
|
2015-04-19 11:28:03 +02:00
|
|
|
|
2015-05-24 00:11:14 +02:00
|
|
|
public TransactionBuilder notify(BluetoothGattCharacteristic characteristic, boolean enable) {
|
|
|
|
if (characteristic == null) {
|
|
|
|
LOG.warn("Unable to notify characteristic: null");
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
NotifyAction action = createNotifyAction(characteristic, enable);
|
|
|
|
return add(action);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected NotifyAction createNotifyAction(BluetoothGattCharacteristic characteristic, boolean enable) {
|
|
|
|
return new NotifyAction(characteristic, enable);
|
|
|
|
}
|
|
|
|
|
2018-09-15 22:43:45 +02:00
|
|
|
/**
|
|
|
|
* Causes the queue to sleep for the specified time.
|
|
|
|
* Note that this is usually a bad idea, since it will not be able to process messages
|
|
|
|
* during that time. It is also likely to cause race conditions.
|
|
|
|
* @param millis the number of milliseconds to sleep
|
|
|
|
*/
|
2015-04-19 02:37:29 +02:00
|
|
|
public TransactionBuilder wait(int millis) {
|
|
|
|
WaitAction action = new WaitAction(millis);
|
|
|
|
return add(action);
|
|
|
|
}
|
2015-04-19 11:28:03 +02:00
|
|
|
|
2015-04-19 02:37:29 +02:00
|
|
|
public TransactionBuilder add(BtLEAction action) {
|
|
|
|
mTransaction.add(action);
|
|
|
|
return this;
|
|
|
|
}
|
2015-04-19 11:28:03 +02:00
|
|
|
|
2015-08-05 23:24:58 +02:00
|
|
|
/**
|
|
|
|
* Sets a GattCallback instance that will be called when the transaction is executed,
|
|
|
|
* resulting in GattCallback events.
|
2015-09-24 14:45:21 +02:00
|
|
|
*
|
2015-08-05 23:24:58 +02:00
|
|
|
* @param callback the callback to set, may be null
|
|
|
|
*/
|
|
|
|
public void setGattCallback(@Nullable GattCallback callback) {
|
|
|
|
mTransaction.setGattCallback(callback);
|
|
|
|
}
|
|
|
|
|
2015-09-24 14:45:21 +02:00
|
|
|
public
|
|
|
|
@Nullable
|
|
|
|
GattCallback getGattCallback() {
|
2015-08-05 23:24:58 +02:00
|
|
|
return mTransaction.getGattCallback();
|
|
|
|
}
|
|
|
|
|
2015-04-19 02:37:29 +02:00
|
|
|
/**
|
|
|
|
* To be used as the final step to execute the transaction by the given queue.
|
2015-04-19 11:28:03 +02:00
|
|
|
*
|
2015-04-19 02:37:29 +02:00
|
|
|
* @param queue
|
|
|
|
*/
|
|
|
|
public void queue(BtLEQueue queue) {
|
2015-10-18 22:08:52 +02:00
|
|
|
if (mQueued) {
|
|
|
|
throw new IllegalStateException("This builder had already been queued. You must not reuse it.");
|
|
|
|
}
|
|
|
|
mQueued = true;
|
2015-04-19 02:37:29 +02:00
|
|
|
queue.add(mTransaction);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Transaction getTransaction() {
|
|
|
|
return mTransaction;
|
|
|
|
}
|
2018-09-14 20:11:27 +02:00
|
|
|
|
|
|
|
public String getTaskName() {
|
|
|
|
return mTransaction.getTaskName();
|
|
|
|
}
|
2015-04-19 02:37:29 +02:00
|
|
|
}
|