aboutsummaryrefslogtreecommitdiff
path: root/app/src
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2015-01-28 23:45:38 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2015-01-28 23:45:38 +0100
commit3e619a735e63a1222e71060d9e65b354a156b158 (patch)
tree2f31cdc65d0d773874800c114530eb620bcca536 /app/src
parentc4685214d8db34166213ffa373a16af1a99401a5 (diff)
downloadio.trygvis.soilmoisture-android-3e619a735e63a1222e71060d9e65b354a156b158.tar.gz
io.trygvis.soilmoisture-android-3e619a735e63a1222e71060d9e65b354a156b158.tar.bz2
io.trygvis.soilmoisture-android-3e619a735e63a1222e71060d9e65b354a156b158.tar.xz
io.trygvis.soilmoisture-android-3e619a735e63a1222e71060d9e65b354a156b158.zip
o Major refactoring on the BtPromise, mainly internal. Renaming BtPromise to BtSequence and BtSequencer.
Diffstat (limited to 'app/src')
-rw-r--r--app/src/main/java/io/trygvis/android/Consumer.java5
-rw-r--r--app/src/main/java/io/trygvis/android/F2.java5
-rw-r--r--app/src/main/java/io/trygvis/android/F3.java5
-rw-r--r--app/src/main/java/io/trygvis/android/F4.java5
-rw-r--r--app/src/main/java/io/trygvis/android/Function.java5
-rw-r--r--app/src/main/java/io/trygvis/android/Optional.java45
-rw-r--r--app/src/main/java/io/trygvis/android/Supplier.java5
-rw-r--r--app/src/main/java/io/trygvis/android/bt/BtCallback.java73
-rw-r--r--app/src/main/java/io/trygvis/android/bt/BtDevice.java29
-rw-r--r--app/src/main/java/io/trygvis/android/bt/BtPromise.java519
-rw-r--r--app/src/main/java/io/trygvis/android/bt/NotOverriddenException.java4
-rw-r--r--app/src/main/java/io/trygvis/soilmoisture/DefaultSoilMoistureService.java28
-rw-r--r--app/src/main/java/io/trygvis/soilmoisture/SensorActivity.java1
13 files changed, 27 insertions, 702 deletions
diff --git a/app/src/main/java/io/trygvis/android/Consumer.java b/app/src/main/java/io/trygvis/android/Consumer.java
deleted file mode 100644
index bf8c5e5..0000000
--- a/app/src/main/java/io/trygvis/android/Consumer.java
+++ /dev/null
@@ -1,5 +0,0 @@
-package io.trygvis.android;
-
-public interface Consumer<T> {
- void accept(T t);
-}
diff --git a/app/src/main/java/io/trygvis/android/F2.java b/app/src/main/java/io/trygvis/android/F2.java
deleted file mode 100644
index 1f362cc..0000000
--- a/app/src/main/java/io/trygvis/android/F2.java
+++ /dev/null
@@ -1,5 +0,0 @@
-package io.trygvis.android;
-
-public interface F2<A, B, C> {
- C apply(A a, B b);
-}
diff --git a/app/src/main/java/io/trygvis/android/F3.java b/app/src/main/java/io/trygvis/android/F3.java
deleted file mode 100644
index 30d3adf..0000000
--- a/app/src/main/java/io/trygvis/android/F3.java
+++ /dev/null
@@ -1,5 +0,0 @@
-package io.trygvis.android;
-
-public interface F3<A, B, C, D> {
- D apply(A a, B b, C c);
-}
diff --git a/app/src/main/java/io/trygvis/android/F4.java b/app/src/main/java/io/trygvis/android/F4.java
deleted file mode 100644
index 5ff50f5..0000000
--- a/app/src/main/java/io/trygvis/android/F4.java
+++ /dev/null
@@ -1,5 +0,0 @@
-package io.trygvis.android;
-
-public interface F4<A, B, C, D, E> {
- E apply(A a, B b, C c, D d);
-}
diff --git a/app/src/main/java/io/trygvis/android/Function.java b/app/src/main/java/io/trygvis/android/Function.java
deleted file mode 100644
index 5e3bb96..0000000
--- a/app/src/main/java/io/trygvis/android/Function.java
+++ /dev/null
@@ -1,5 +0,0 @@
-package io.trygvis.android;
-
-public interface Function<A, B> {
- B apply(A a);
-}
diff --git a/app/src/main/java/io/trygvis/android/Optional.java b/app/src/main/java/io/trygvis/android/Optional.java
deleted file mode 100644
index 616f9a7..0000000
--- a/app/src/main/java/io/trygvis/android/Optional.java
+++ /dev/null
@@ -1,45 +0,0 @@
-package io.trygvis.android;
-
-public final class Optional<T> {
- private final T value;
-
- public Optional(T value) {
- this.value = value;
- }
-
- public T get() {
- if (value == null) {
- throw new IllegalStateException("get() on empty");
- }
-
- return value;
- }
-
- public boolean isPresent() {
- return value != null;
- }
-
- public void ifPresent(Consumer<T> consumer) {
- if (value == null) {
- return;
- }
-
- consumer.accept(value);
- }
-
- public static <T> Optional<T> of(T t) {
- if (t == null) {
- throw new IllegalArgumentException("t can't be null");
- }
-
- return new Optional<>(t);
- }
-
- public static <T> Optional<T> empty() {
- return new Optional<>(null);
- }
-
- public static <T> Optional<T> ofNullable(T t) {
- return t != null ? of(t) : empty();
- }
-}
diff --git a/app/src/main/java/io/trygvis/android/Supplier.java b/app/src/main/java/io/trygvis/android/Supplier.java
deleted file mode 100644
index 222dff2..0000000
--- a/app/src/main/java/io/trygvis/android/Supplier.java
+++ /dev/null
@@ -1,5 +0,0 @@
-package io.trygvis.android;
-
-public interface Supplier<A> {
- A get();
-}
diff --git a/app/src/main/java/io/trygvis/android/bt/BtCallback.java b/app/src/main/java/io/trygvis/android/bt/BtCallback.java
deleted file mode 100644
index 0f7287f..0000000
--- a/app/src/main/java/io/trygvis/android/bt/BtCallback.java
+++ /dev/null
@@ -1,73 +0,0 @@
-package io.trygvis.android.bt;
-
-import android.bluetooth.BluetoothGatt;
-import android.bluetooth.BluetoothGattCharacteristic;
-import android.bluetooth.BluetoothGattDescriptor;
-
-import static io.trygvis.android.bt.BtPromise.PromiseResult;
-
-public class BtCallback {
- public final boolean stopOnFailure;
- public final String name;
-
- public BtCallback(boolean stopOnFailure, String name) {
- this.stopOnFailure = stopOnFailure;
- this.name = name;
- }
-
- public PromiseResult onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
- throw new NotOverriddenException();
- }
-
- public PromiseResult onServicesDiscovered(BluetoothGatt gatt) {
- throw new NotOverriddenException();
- }
-
- public PromiseResult onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
- throw new NotOverriddenException();
- }
-
- public PromiseResult onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
- throw new NotOverriddenException();
- }
-
- public PromiseResult onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
- throw new NotOverriddenException();
- }
-
- public PromiseResult onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor) {
- throw new NotOverriddenException();
- }
-
- public PromiseResult onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor) {
- throw new NotOverriddenException();
- }
-
- public PromiseResult onReliableWriteCompleted(BluetoothGatt gatt) {
- throw new NotOverriddenException();
- }
-
- public PromiseResult onReadRemoteRssi(BluetoothGatt gatt, int rssi) {
- throw new NotOverriddenException();
- }
-
- public PromiseResult onDirect(BluetoothGatt value) {
- throw new NotOverriddenException();
- }
-
- public void onFailure() {
- throw new NotOverriddenException();
- }
-
- public void onFinally(boolean success) {
- throw new NotOverriddenException();
- }
-
- @Override
- public String toString() {
- return "BtCallback{" +
- "name='" + name + '\'' +
- ", stopOnFailure=" + stopOnFailure +
- '}';
- }
-}
diff --git a/app/src/main/java/io/trygvis/android/bt/BtDevice.java b/app/src/main/java/io/trygvis/android/bt/BtDevice.java
index b90ac4f..07e4e52 100644
--- a/app/src/main/java/io/trygvis/android/bt/BtDevice.java
+++ b/app/src/main/java/io/trygvis/android/bt/BtDevice.java
@@ -10,10 +10,9 @@ import android.util.Log;
import java.util.Date;
-import static io.trygvis.android.bt.BtPromise.BtBluetoothGattCallback;
-import static io.trygvis.android.bt.BtPromise.PromiseResult.detour;
-import static io.trygvis.android.bt.BtPromise.PromiseResult.fail;
-import static io.trygvis.android.bt.BtPromise.PromiseResult.waitForNextEvent;
+import static io.trygvis.android.bt.BtSequence.SequenceResult.detour;
+import static io.trygvis.android.bt.BtSequence.SequenceResult.fail;
+import static io.trygvis.android.bt.BtSequence.SequenceResult.waitForNextEvent;
public class BtDevice<A> implements Comparable<BtDevice> {
private final static String TAG = BtDevice.class.getSimpleName();
@@ -103,16 +102,16 @@ public class BtDevice<A> implements Comparable<BtDevice> {
* <p>
* Services will be discovered.
*/
- public synchronized void withConnection(BtPromise promise) {
+ public synchronized void withConnection(BtSequence sequence) {
if (callback != null) {
throw new RuntimeException("The current callback is not done.");
}
Log.i(TAG, "withConnection(), address=" + address + ", connected: " + (gatt != null));
- BtPromise newPromise;
+ BtSequence newSequence;
if (gatt == null) {
- newPromise = new BtPromise().
+ newSequence = new BtSequence().
ignoreFailureForNext().
onConnectionStateChange((gatt, status, newState) -> {
Log.i(TAG, "defaultConnectCallback: status=" + status + ", newState=" + newState);
@@ -125,7 +124,7 @@ public class BtDevice<A> implements Comparable<BtDevice> {
} else {
Log.i(TAG, "Could not connect to " + address + ", trying again");
- return detour(new BtPromise().onConnectionStateChange((gatt2, status2, newState2) -> {
+ return detour(new BtSequence().onConnectionStateChange((gatt2, status2, newState2) -> {
if (status2 == BluetoothGatt.GATT_SUCCESS && newState2 == BluetoothGatt.STATE_CONNECTED) {
Log.i(TAG, "Connected to " + address + ", discovering services");
return gatt.discoverServices() ? waitForNextEvent() : fail();
@@ -139,25 +138,23 @@ public class BtDevice<A> implements Comparable<BtDevice> {
}).
onServicesDiscovered(gatt -> {
Log.i(TAG, "Services discovered, has " + gatt.getServices().size() + " services");
- return detour(promise);
+ return detour(sequence);
});
} else {
- newPromise = promise;
+ newSequence = sequence;
}
- callback = newPromise.
+ BtSequencer sequencer = new BtSequencer(bluetoothDevice.getAddress(), newSequence.
onFinally(success -> {
Log.i(TAG, "Promise done, device is available again: address=" + address + ", success=" + success);
callback = null;
- }).
- asCallback(bluetoothDevice.getAddress());
+ }));
+ callback = new BtBluetoothGattCallback(sequencer);
if (gatt == null) {
gatt = bluetoothDevice.connectGatt(btService, false, wrappingCallback);
} else {
- callback.onEvent(BtPromise.EventType.onDirect, "", gatt, null, null,
- BluetoothGatt.GATT_SUCCESS,
- BluetoothGatt.STATE_CONNECTED);
+ sequencer.onDirect();
}
}
diff --git a/app/src/main/java/io/trygvis/android/bt/BtPromise.java b/app/src/main/java/io/trygvis/android/bt/BtPromise.java
deleted file mode 100644
index bffdd19..0000000
--- a/app/src/main/java/io/trygvis/android/bt/BtPromise.java
+++ /dev/null
@@ -1,519 +0,0 @@
-package io.trygvis.android.bt;
-
-import android.bluetooth.BluetoothGatt;
-import android.bluetooth.BluetoothGattCallback;
-import android.bluetooth.BluetoothGattCharacteristic;
-import android.bluetooth.BluetoothGattDescriptor;
-import android.util.Log;
-
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
-
-import io.trygvis.android.Consumer;
-import io.trygvis.android.F2;
-import io.trygvis.android.F3;
-import io.trygvis.android.Function;
-
-import static io.trygvis.android.bt.BtPromise.EventType.onCharacteristicChanged;
-import static io.trygvis.android.bt.BtPromise.EventType.onCharacteristicRead;
-import static io.trygvis.android.bt.BtPromise.EventType.onCharacteristicWrite;
-import static io.trygvis.android.bt.BtPromise.EventType.onConnectionStateChange;
-import static io.trygvis.android.bt.BtPromise.EventType.onDescriptorRead;
-import static io.trygvis.android.bt.BtPromise.EventType.onDescriptorWrite;
-import static io.trygvis.android.bt.BtPromise.EventType.onDirect;
-import static io.trygvis.android.bt.BtPromise.EventType.onFinally;
-import static io.trygvis.android.bt.BtPromise.EventType.onReliableWriteCompleted;
-import static io.trygvis.android.bt.BtPromise.EventType.onServicesDiscovered;
-
-public class BtPromise {
- private final static String TAG = BtPromise.class.getSimpleName();
- private final List<BtCallback> actionQ = new ArrayList<>();
- // private final List<BtCallback> failureQ = new ArrayList<>();
- private final List<BtCallback> finallyQ = new ArrayList<>();
-
- private static final PromiseResult waitForNextEvent = new WaitForNextEvent();
- private static final PromiseResult stop = new Stop();
- private static final PromiseResult fail = new Fail();
- private static final PromiseResult continueDirectly = new ContinueDirectly();
-
- private Boolean stopOnFailure;
-
- public static class PromiseResult {
- private PromiseResult() {
- }
-
- public static PromiseResult waitForNextEvent() {
- return waitForNextEvent;
- }
-
- public static PromiseResult continueDirectly() {
- return continueDirectly;
- }
-
- public static PromiseResult stop() {
- return stop;
- }
-
- public static PromiseResult fail() {
- return fail;
- }
-
- public static PromiseResult detour(BtPromise promise) {
- return new Detour(promise);
- }
- }
-
- private static class WaitForNextEvent extends PromiseResult {
- }
-
- private static class ContinueDirectly extends PromiseResult {
- }
-
- private static class Stop extends PromiseResult {
- }
-
- private static class Fail extends PromiseResult {
- }
-
- private static class Detour extends PromiseResult {
- final BtPromise promise;
-
- private Detour(BtPromise promise) {
- this.promise = promise;
- }
- }
-
- private boolean stopOnFailure() {
- if (stopOnFailure != null) {
- boolean b = stopOnFailure;
- stopOnFailure = null;
- return b;
- }
- return false;
- }
-
- public BtPromise ignoreFailureForNext() {
- stopOnFailure = true;
- return this;
- }
-
- public synchronized BtPromise onConnectionStateChange(F3<BluetoothGatt, Integer, Integer, PromiseResult> callback) {
- actionQ.add(new BtCallback(stopOnFailure(), "onConnectionStateChange") {
- @Override
- public PromiseResult onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
- return callback.apply(gatt, status, newState);
- }
- });
- return this;
- }
-
- public synchronized BtPromise onServicesDiscovered(Function<BluetoothGatt, PromiseResult> callback) {
- actionQ.add(new BtCallback(stopOnFailure(), "onServicesDiscovered") {
- @Override
- public PromiseResult onServicesDiscovered(BluetoothGatt gatt) {
- return callback.apply(gatt);
- }
- });
- return this;
- }
-
- public synchronized BtPromise onCharacteristicRead(F2<BluetoothGatt, BluetoothGattCharacteristic, PromiseResult> callback) {
- actionQ.add(new BtCallback(stopOnFailure(), "onCharacteristicRead") {
- @Override
- public PromiseResult onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
- return callback.apply(gatt, characteristic);
- }
- });
- return this;
- }
-
- public synchronized BtPromise onCharacteristicWrite(F2<BluetoothGatt, BluetoothGattCharacteristic, PromiseResult> callback) {
- actionQ.add(new BtCallback(stopOnFailure(), "onCharacteristicWrite") {
- @Override
- public PromiseResult onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
- return callback.apply(gatt, characteristic);
- }
- });
- return this;
- }
-
- public synchronized BtPromise onCharacteristicChanged(F2<BluetoothGatt, BluetoothGattCharacteristic, PromiseResult> callback) {
- actionQ.add(new BtCallback(stopOnFailure(), "onCharacteristicChanged") {
- @Override
- public PromiseResult onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
- return callback.apply(gatt, characteristic);
- }
- });
- return this;
- }
-
- public synchronized BtPromise onDescriptorRead(F2<BluetoothGatt, BluetoothGattDescriptor, PromiseResult> callback) {
- actionQ.add(new BtCallback(stopOnFailure(), "onDescriptorRead") {
- @Override
- public PromiseResult onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor) {
- return callback.apply(gatt, descriptor);
- }
- });
- return this;
- }
-
- public synchronized BtPromise onDescriptorWrite(F2<BluetoothGatt, BluetoothGattDescriptor, PromiseResult> callback) {
- actionQ.add(new BtCallback(stopOnFailure(), "onDescriptorWrite") {
- @Override
- public PromiseResult onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor) {
- return callback.apply(gatt, descriptor);
- }
- });
- return this;
- }
-
- public synchronized BtPromise onReliableWriteCompleted(Function<BluetoothGatt, PromiseResult> callback) {
- actionQ.add(new BtCallback(stopOnFailure(), "onReliableWriteCompleted") {
- @Override
- public PromiseResult onReliableWriteCompleted(BluetoothGatt gatt) {
- return callback.apply(gatt);
- }
- });
- return this;
- }
-
- public synchronized BtPromise onReadRemoteRssi(F2<BluetoothGatt, Integer, PromiseResult> callback) {
- actionQ.add(new BtCallback(stopOnFailure(), "onReadRemoteRssi") {
- @Override
- public PromiseResult onReadRemoteRssi(BluetoothGatt gatt, int rssi) {
- return callback.apply(gatt, rssi);
- }
- });
- return this;
- }
-
- public synchronized BtPromise onDirect(Function<BluetoothGatt, PromiseResult> callback) {
- actionQ.add(new BtCallback(stopOnFailure(), "onDirect") {
- @Override
- public PromiseResult onDirect(BluetoothGatt value) {
- return callback.apply(value);
- }
- });
- return this;
- }
-
-// public synchronized BtPromise onFailure(Runnable callback) {
-// failureQ.add(new BtCallback(stopOnFailure(), "onFailure") {
-// @Override
-// public void onFailure() {
-// callback.run();
-// }
-// });
-// return this;
-// }
-
- public synchronized BtPromise onFinally(Consumer<Boolean> callback) {
- finallyQ.add(new BtCallback(stopOnFailure(), "finally") {
- @Override
- public void onFinally(boolean success) {
- callback.accept(success);
- }
- });
- return this;
- }
-
- public BtPromise andThen(BtPromise btPromise) {
- actionQ.addAll(btPromise.actionQ);
- finallyQ.addAll(btPromise.finallyQ);
- return this;
- }
-
- public String toString() {
- StringBuilder s = new StringBuilder("Queue: ");
-
- Iterator<BtCallback> it = actionQ.iterator();
-
- int i = 0;
- while (it.hasNext()) {
- BtCallback c = it.next();
- if (i > 0) {
- s.append(", ");
- }
- s.append(c.name);
- i++;
- }
-
- return s.toString();
- }
-
- BtBluetoothGattCallback asCallback(String address) {
- return new BtBluetoothGattCallback(address, actionQ, /*failureQ, */finallyQ);
- }
-
- enum EventType {
- onConnectionStateChange,
- onServicesDiscovered,
- onCharacteristicRead,
- onCharacteristicWrite,
- onCharacteristicChanged,
- onDescriptorRead,
- onDescriptorWrite,
- onReliableWriteCompleted,
-
- onReadRemoteRssi,
-
- onDirect,
- onFailure,
- onFinally,
- }
-
- private static PromiseResult callCallback(EventType key, BluetoothGatt gatt,
- BluetoothGattCharacteristic characteristic,
- BluetoothGattDescriptor descriptor, int status, int newState,
- Boolean success, BtCallback btCallback) {
- switch (key) {
- case onConnectionStateChange:
- return btCallback.onConnectionStateChange(gatt, status, newState);
- case onServicesDiscovered:
- return btCallback.onServicesDiscovered(gatt);
- case onCharacteristicRead:
- return btCallback.onCharacteristicRead(gatt, characteristic);
- case onCharacteristicWrite:
- return btCallback.onCharacteristicWrite(gatt, characteristic);
- case onCharacteristicChanged:
- return btCallback.onCharacteristicChanged(gatt, characteristic);
- case onDescriptorRead:
- return btCallback.onDescriptorRead(gatt, descriptor);
- case onDescriptorWrite:
- return btCallback.onDescriptorWrite(gatt, descriptor);
- case onReliableWriteCompleted:
- return btCallback.onReliableWriteCompleted(gatt);
-
- case onDirect:
- return btCallback.onDirect(gatt);
- case onFailure:
- btCallback.onFailure();
- return null;
- case onFinally:
- btCallback.onFinally(success);
- return null;
- default:
- Log.w(TAG, "Unknown callback: " + key);
- return null;
- }
- }
-
- static class BtBluetoothGattCallback extends BluetoothGattCallback {
-
- private final String address;
- private List<BtCallback> actionQ;
- private int currentAction = 0;
- // private List<BtCallback> failureQ;
- private List<BtCallback> finallyQ;
- private List<String> events = new ArrayList<>();
-
- private BtBluetoothGattCallback(String address, List<BtCallback> actionQ,
- /*List<BtCallback> failureQ, */List<BtCallback> finallyQ) {
- this.address = address;
- this.actionQ = new ArrayList<>(actionQ);
-// this.failureQ = new ArrayList<>(failureQ);
- this.finallyQ = new ArrayList<>(finallyQ);
- }
-
- void onEvent(EventType key, String values, BluetoothGatt gatt, BluetoothGattCharacteristic characteristic,
- BluetoothGattDescriptor descriptor, int status, int newState) {
- boolean success = status == BluetoothGatt.GATT_SUCCESS;
- events.add(key + "(" + values + "), success=" + success);
-
- Log.i(TAG, "event: " + key + "(" + values + "), success=" + success);
-
- BtCallback btCallback;
- synchronized (this) {
- if (!hasNext()) {
- Log.d(TAG, "All Bluetooth actions are done, no handler for last event.");
-
- doFinally(true);
- return;
- }
-
- btCallback = actionQ.get(currentAction++);
-
- if (!success) {
- if (btCallback.stopOnFailure) {
-// doFailure();
- doFinally(false);
- return;
- } else {
- Log.i(TAG, "Last status was a failure, but the callback still want it.");
- }
- }
-
-// Log.i(TAG, "Graceful sleep" + btCallback.name);
-// try {
-// Thread.sleep(1000);
-// } catch (InterruptedException e) {
-// // ignore
-// }
- }
-
- try {
- Log.i(TAG, "Executing bt action: " + btCallback.name);
- PromiseResult result = callCallback(key, gatt, characteristic, descriptor, status, newState, null,
- btCallback);
-
- if (result instanceof Stop) {
- Log.i(TAG, "The chain want to stop.");
- doFinally(true);
- return;
- }
-
- if (result instanceof Fail) {
- Log.i(TAG, "The chain returned fail().");
- doFinally(false);
- return;
- }
-
- if (result instanceof Detour) {
- BtPromise detour = ((Detour) result).promise;
-// Log.i(TAG, "Adding detour with " + detour.actionQ.size() + " actions.");
- events.add("detour, action size=" + detour.actionQ.size() + ", " +
-// "failure size=" + detour.failureQ.size() + ", " +
- "finally size=" + detour.finallyQ.size());
-
- // The new promise should probably be stacked on top, so that all of its
- // finally handlers are executed after the added set concludes and then the
- // current stack can continue.
-
- actionQ.addAll(currentAction, detour.actionQ);
-// failureQ.addAll(detour.failureQ);B
- finallyQ.addAll(detour.finallyQ);
-
- result = PromiseResult.continueDirectly();
- }
-
- if (result instanceof ContinueDirectly) {
- onEvent(onDirect, "direct", gatt, null, null, BluetoothGatt.GATT_SUCCESS, 0);
- return;
- }
-
- if (!hasNext()) {
- Log.i(TAG, "The queue is empty");
- showEvents();
- }
- } catch (NotOverriddenException e) {
- Log.w(TAG, "Unexpected callback by listener: " + key);
-// doFailure();
- doFinally(false);
- } catch (Exception e) {
- Log.w(TAG, "Exception in callback", e);
-// doFailure();
- doFinally(false);
- }
- }
-
- private boolean hasNext() {
- return currentAction < actionQ.size();
- }
-
-// private void doFailure() {
-// showEvents();
-//
-// Log.w(TAG, "Executing " + failureQ.size() + " failure handlers");
-//
-// for (BtCallback callback : failureQ) {
-// callCallback(onFailure, null, null, null, 0, 0, callback, null);
-// }
-//
-// doFinally();
-// }
-
- private void showEvents() {
- StringBuilder msg = new StringBuilder();
-
- msg.append("Address: ").append(address).append("\n");
-
- msg.append("Event handlers: \n");
- for (BtCallback cb : actionQ) {
- msg.append("- ").append(cb.name).append("\n");
- }
-
-// msg.append("Failure handlers: \n");
-// for (BtCallback cb : failureQ) {
-// msg.append("- ").append(cb.name).append("\n");
-// }
-
- msg.append("Finally handlers: \n");
- for (BtCallback cb : finallyQ) {
- msg.append("- ").append(cb.name).append("\n");
- }
-
- msg.append("Events received: \n");
- for (String event : events) {
- msg.append("- ").append(event).append("\n");
- }
-
- Log.w(TAG, msg.toString());
- }
-
- private void doFinally(boolean success) {
- showEvents();
-
- actionQ.clear();
-
- Log.w(TAG, "Executing " + finallyQ.size() + " finally handlers, success=" + success);
-
- for (BtCallback callback : finallyQ) {
- try {
- callCallback(onFinally, null, null, null, 0, 0, success, callback);
- } catch (NotOverriddenException e) {
- return;
- }
- }
- }
-
- // -----------------------------------------------------------------------
- //
- // -----------------------------------------------------------------------
-
- @Override
- public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
- onEvent(onConnectionStateChange, "status=" + status + ", newState=" + newState, gatt, null, null, status, newState);
- }
-
- @Override
- public void onServicesDiscovered(BluetoothGatt gatt, int status) {
- onEvent(onServicesDiscovered, "status=" + status, gatt, null, null, status, 9);
- }
-
- @Override
- public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
- onEvent(onCharacteristicRead, "status=" + status + ", characteristic=" + characteristic.getUuid(), gatt, characteristic, null, status, 0);
- }
-
- @Override
- public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
- onEvent(onCharacteristicWrite, "status=" + status + ", characteristic=" + characteristic.getUuid(), gatt, characteristic, null, status, 0);
- }
-
- @Override
- public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
- onEvent(onCharacteristicChanged, "characteristic=" + characteristic.getUuid(), gatt, characteristic, null, BluetoothGatt.GATT_SUCCESS, 0);
- }
-
- @Override
- public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
- onEvent(onDescriptorRead, "status=" + status + ", descriptor=" + descriptor.getUuid(), gatt, null, descriptor, status, 0);
- }
-
- @Override
- public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
- onEvent(onDescriptorWrite, "status=" + status + ", descriptor=" + descriptor.getUuid(), gatt, null, descriptor, status, 0);
- }
-
- @Override
- public void onReliableWriteCompleted(BluetoothGatt gatt, int status) {
- onEvent(onReliableWriteCompleted, "status=" + status, gatt, null, null, status, 0);
- }
-
- @Override
- public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
-// onEvent(onReadRemoteRssi, "status=" + status, gatt, null, null, status, 0);
- }
- }
-}
diff --git a/app/src/main/java/io/trygvis/android/bt/NotOverriddenException.java b/app/src/main/java/io/trygvis/android/bt/NotOverriddenException.java
deleted file mode 100644
index 0f2bb6a..0000000
--- a/app/src/main/java/io/trygvis/android/bt/NotOverriddenException.java
+++ /dev/null
@@ -1,4 +0,0 @@
-package io.trygvis.android.bt;
-
-class NotOverriddenException extends RuntimeException {
-}
diff --git a/app/src/main/java/io/trygvis/soilmoisture/DefaultSoilMoistureService.java b/app/src/main/java/io/trygvis/soilmoisture/DefaultSoilMoistureService.java
index 3e1c93b..2cf85d9 100644
--- a/app/src/main/java/io/trygvis/soilmoisture/DefaultSoilMoistureService.java
+++ b/app/src/main/java/io/trygvis/soilmoisture/DefaultSoilMoistureService.java
@@ -26,15 +26,16 @@ import java.util.TreeSet;
import io.trygvis.android.Function;
import io.trygvis.android.LocalBinder;
import io.trygvis.android.bt.BtDevice;
-import io.trygvis.android.bt.BtPromise;
+import io.trygvis.android.bt.BtSequence;
import io.trygvis.android.bt.BtService;
import io.trygvis.android.bt.DefaultBtService;
import io.trygvis.bluetooth.TrygvisIoUuids;
-import static io.trygvis.android.bt.BtPromise.PromiseResult.continueDirectly;
-import static io.trygvis.android.bt.BtPromise.PromiseResult.detour;
-import static io.trygvis.android.bt.BtPromise.PromiseResult.stop;
-import static io.trygvis.android.bt.BtPromise.PromiseResult.waitForNextEvent;
+import static io.trygvis.android.bt.BtSequence.SequenceResult;
+import static io.trygvis.android.bt.BtSequence.SequenceResult.continueDirectly;
+import static io.trygvis.android.bt.BtSequence.SequenceResult.detour;
+import static io.trygvis.android.bt.BtSequence.SequenceResult.stop;
+import static io.trygvis.android.bt.BtSequence.SequenceResult.waitForNextEvent;
import static io.trygvis.android.bt.BtService.BtServiceListenerBroadcastReceiver;
import static io.trygvis.bluetooth.TrygvisIoUuids.CLIENT_CHARACTERISTIC_CONFIG;
import static io.trygvis.soilmoisture.SmDevice.GetSensorCountRes;
@@ -139,8 +140,8 @@ public class DefaultSoilMoistureService extends Service implements SoilMoistureS
}
};
- private BtPromise readAttribute(String value, byte[] req, Function<byte[], BtPromise.PromiseResult> handler) {
- return new BtPromise().
+ private BtSequence readAttribute(String value, byte[] req, Function<byte[], SequenceResult> handler) {
+ return new BtSequence().
onDirect(gatt -> {
Log.i(TAG, "Getting attribute: " + value);
@@ -158,7 +159,7 @@ public class DefaultSoilMoistureService extends Service implements SoilMoistureS
});
}
- private BtPromise readSensorName(SmDevice device, int index) {
+ private BtSequence readSensorName(SmDevice device, int index) {
byte[] req = createGetSensorNameReq((byte) index);
return readAttribute("sensor name, index#" + index, req, bytes -> {
@@ -179,7 +180,7 @@ public class DefaultSoilMoistureService extends Service implements SoilMoistureS
SmDevice smDevice = btDevice.getTag();
Log.i(TAG, "Probing " + address + ", name=" + btDevice.getName());
- BtPromise promise = new BtPromise().
+ BtSequence sequence = new BtSequence().
onDirect(gatt -> {
BluetoothGattService service = gatt.getService(TrygvisIoUuids.Services.SOIL_MOISTURE_SERVICE);
@@ -210,7 +211,7 @@ public class DefaultSoilMoistureService extends Service implements SoilMoistureS
}
});
- BtPromise btPromise = readAttribute("sensor count", createGetSensorCountReq(), bytes -> {
+ BtSequence btSequence = readAttribute("sensor count", createGetSensorCountReq(), bytes -> {
GetSensorCountRes getSensorCountRes = parseResponse(bytes, GET_SENSOR_COUNT, GetSensorCountRes.class);
int count = getSensorCountRes.count;
@@ -221,9 +222,8 @@ public class DefaultSoilMoistureService extends Service implements SoilMoistureS
return detour(readSensorName(smDevice, 0));
});
- promise.andThen(btPromise);
- btDevice.withConnection(promise);
+ btDevice.withConnection(sequence.andThen(btSequence));
}
private void markDeviceAsNotUseful(SmDevice device) {
@@ -371,7 +371,7 @@ public class DefaultSoilMoistureService extends Service implements SoilMoistureS
}
void readCurrentValue(SmSensor sensor) {
- BtPromise promise = new BtPromise().
+ BtSequence sequence = new BtSequence().
onDirect(gatt -> {
BluetoothGattService service = gatt.getService(TrygvisIoUuids.Services.SOIL_MOISTURE_SERVICE);
BluetoothGattCharacteristic soilMoisture = service.getCharacteristic(TrygvisIoUuids.Characteristics.SOIL_MOISTURE);
@@ -402,7 +402,7 @@ public class DefaultSoilMoistureService extends Service implements SoilMoistureS
}
});
- sensor.getDevice().getBtDevice().withConnection(promise);
+ sensor.getDevice().getBtDevice().withConnection(sequence);
}
// -----------------------------------------------------------------------
diff --git a/app/src/main/java/io/trygvis/soilmoisture/SensorActivity.java b/app/src/main/java/io/trygvis/soilmoisture/SensorActivity.java
index 358382c..a5b68d8 100644
--- a/app/src/main/java/io/trygvis/soilmoisture/SensorActivity.java
+++ b/app/src/main/java/io/trygvis/soilmoisture/SensorActivity.java
@@ -128,7 +128,6 @@ public class SensorActivity extends Activity {
}
-
private class SensorSoilMoistureListener extends SoilMoistureListener {
}
}