diff options
-rw-r--r-- | app/src/main/java/io/trygvis/android/bt/BtService.java | 5 | ||||
-rw-r--r-- | app/src/main/java/io/trygvis/android/bt/DefaultBtService.java | 34 | ||||
-rw-r--r-- | app/src/main/java/io/trygvis/soilmoisture/DefaultSoilMoistureService.java | 18 | ||||
-rw-r--r-- | app/src/main/java/io/trygvis/soilmoisture/MainActivity.java | 12 | ||||
-rw-r--r-- | app/src/main/java/io/trygvis/soilmoisture/SmDevice.java | 6 | ||||
-rw-r--r-- | app/src/main/java/io/trygvis/soilmoisture/SmSensor.java (renamed from app/src/main/java/io/trygvis/soilmoisture/SoilMonitor.java) | 4 |
6 files changed, 55 insertions, 24 deletions
diff --git a/app/src/main/java/io/trygvis/android/bt/BtService.java b/app/src/main/java/io/trygvis/android/bt/BtService.java index ca8bcd9..fb11027 100644 --- a/app/src/main/java/io/trygvis/android/bt/BtService.java +++ b/app/src/main/java/io/trygvis/android/bt/BtService.java @@ -4,9 +4,12 @@ import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; +import android.database.sqlite.SQLiteDatabase; import java.util.Collection; +import io.trygvis.android.Function; + public interface BtService<A extends BtDevice.BtDeviceWrapper<A>> { boolean initialize(BtDbIntegration<A> btDbIntegration); @@ -27,6 +30,8 @@ public interface BtService<A extends BtDevice.BtDeviceWrapper<A>> { Collection<A> getTags(); + <T> T runTx(Function<SQLiteDatabase, T> action); + public static interface BtDbIntegration<A extends BtDevice.BtDeviceWrapper<A>> { A createTag(BtDevice<A> a); } diff --git a/app/src/main/java/io/trygvis/android/bt/DefaultBtService.java b/app/src/main/java/io/trygvis/android/bt/DefaultBtService.java index 3c14c1f..205421b 100644 --- a/app/src/main/java/io/trygvis/android/bt/DefaultBtService.java +++ b/app/src/main/java/io/trygvis/android/bt/DefaultBtService.java @@ -26,6 +26,7 @@ import java.util.Date; import java.util.HashSet; import java.util.Set; +import io.trygvis.android.Function; import io.trygvis.android.LocalBinder; import io.trygvis.soilmoisture.R; @@ -160,6 +161,23 @@ public class DefaultBtService<A extends BtDevice.BtDeviceWrapper<A>> extends Ser return tags; } + @Override + public <T> T runTx(Function<SQLiteDatabase, T> action) { + SQLiteDatabase db = openBtDevices(); + try { + db.beginTransaction(); + + T value = action.apply(db); + + db.setTransactionSuccessful(); + + return value; + } finally { + db.endTransaction(); + db.close(); + } + } + // ----------------------------------------------------------------------- // Scanning // ----------------------------------------------------------------------- @@ -244,10 +262,7 @@ public class DefaultBtService<A extends BtDevice.BtDeviceWrapper<A>> extends Ser long now = System.currentTimeMillis(); - SQLiteDatabase db = openBtDevices(); - try { - db.beginTransaction(); - + btDevice = runTx(db -> { Cursor cursor = db.query("bt_device", new String[]{"id", "first_seen"}, "address=?", new String[]{address}, null, null, null); @@ -273,15 +288,10 @@ public class DefaultBtService<A extends BtDevice.BtDeviceWrapper<A>> extends Ser } Log.i(TAG, "New device: " + address + ", seenBefore=" + seenBefore); - btDevice = new BtDevice<>(this, bluetoothDevice, btDbIntegration, id, rssi, scanResult, - seenBefore, firstSeen, lastSeen); - cursor.close(); - db.setTransactionSuccessful(); - } finally { - db.endTransaction(); - db.close(); - } + return new BtDevice<>(this, bluetoothDevice, btDbIntegration, id, rssi, scanResult, + seenBefore, firstSeen, lastSeen); + }); devices.add(btDevice); diff --git a/app/src/main/java/io/trygvis/soilmoisture/DefaultSoilMoistureService.java b/app/src/main/java/io/trygvis/soilmoisture/DefaultSoilMoistureService.java index 8a36476..0f08076 100644 --- a/app/src/main/java/io/trygvis/soilmoisture/DefaultSoilMoistureService.java +++ b/app/src/main/java/io/trygvis/soilmoisture/DefaultSoilMoistureService.java @@ -5,6 +5,7 @@ import android.bluetooth.BluetoothGatt; import android.bluetooth.BluetoothGattCharacteristic; import android.bluetooth.BluetoothGattService; import android.content.ComponentName; +import android.content.ContentValues; import android.content.Intent; import android.content.ServiceConnection; import android.os.IBinder; @@ -27,6 +28,7 @@ import static io.trygvis.android.bt.BtService.BtServiceListenerBroadcastReceiver public class DefaultSoilMoistureService extends Service implements SoilMoistureService { private final static String TAG = DefaultSoilMoistureService.class.getSimpleName(); + private final static int DEFAULT_WARNING_LEVEL = 750; private final IBinder binder = new LocalBinder<>(this); @@ -123,7 +125,21 @@ public class DefaultSoilMoistureService extends Service implements SoilMoistureS return false; }). - onFinally(() -> sendBroadcast(createNewDevice(address))); + onFinally(() -> { + btService.runTx(db -> { + if (!btDevice.isSeenBefore() && smDevice.isUseful()) { + for (SmSensor soilMonitor : smDevice.getSensors()) { + ContentValues values = new ContentValues(); + values.put("bt_device", btDevice.getId()); + values.put("warning_level", DEFAULT_WARNING_LEVEL); + db.insert("soil_monitor", null, values); + } + } + + return null; + }); + sendBroadcast(createNewDevice(address)); + }); btDevice.connect(executor); } else { diff --git a/app/src/main/java/io/trygvis/soilmoisture/MainActivity.java b/app/src/main/java/io/trygvis/soilmoisture/MainActivity.java index 64e8202..2e6df76 100644 --- a/app/src/main/java/io/trygvis/soilmoisture/MainActivity.java +++ b/app/src/main/java/io/trygvis/soilmoisture/MainActivity.java @@ -252,9 +252,9 @@ public class MainActivity extends ListActivity { for (SmDevice d : devices) { (d.isUseful() ? usefulDevices : unusefulDevices).add(d); } - List<SoilMonitor> monitors = new ArrayList<>(); + List<SmSensor> monitors = new ArrayList<>(); for (SmDevice d : devices) { - monitors.addAll(d.getMonitors()); + monitors.addAll(d.getSensors()); } if (groupByDevice) { @@ -333,7 +333,7 @@ public class MainActivity extends ListActivity { if (o instanceof SmDevice) { return 0; - } else if (o instanceof SoilMonitor) { + } else if (o instanceof SmSensor) { return 1; } @@ -371,8 +371,8 @@ public class MainActivity extends ListActivity { if (o instanceof SmDevice) { return getSmDeviceView((SmDevice) o, view); - } else if (o instanceof SoilMonitor) { - return getSoilMonitorView((SoilMonitor) o, view); + } else if (o instanceof SmSensor) { + return getSoilMonitorView((SmSensor) o, view); } throw new RuntimeException("Not implemented"); @@ -408,7 +408,7 @@ public class MainActivity extends ListActivity { return view; } - private View getSoilMonitorView(SoilMonitor soilMonitor, View view) { + private View getSoilMonitorView(SmSensor smSensor, View view) { throw new RuntimeException("Not implemented"); } } diff --git a/app/src/main/java/io/trygvis/soilmoisture/SmDevice.java b/app/src/main/java/io/trygvis/soilmoisture/SmDevice.java index 169f4b3..e24416c 100644 --- a/app/src/main/java/io/trygvis/soilmoisture/SmDevice.java +++ b/app/src/main/java/io/trygvis/soilmoisture/SmDevice.java @@ -16,7 +16,7 @@ class SmDevice implements BtDevice.BtDeviceWrapper<SmDevice> { private Boolean isUseful; - private List<SoilMonitor> monitors = new ArrayList<>(); + private List<SmSensor> sensors = new ArrayList<>(); public SmDevice(BtDevice<SmDevice> btDevice) { this.btDevice = btDevice; @@ -54,7 +54,7 @@ class SmDevice implements BtDevice.BtDeviceWrapper<SmDevice> { return name; } - public List<SoilMonitor> getMonitors() { - return monitors; + public List<SmSensor> getSensors() { + return sensors; } } diff --git a/app/src/main/java/io/trygvis/soilmoisture/SoilMonitor.java b/app/src/main/java/io/trygvis/soilmoisture/SmSensor.java index 564202d..f27c364 100644 --- a/app/src/main/java/io/trygvis/soilmoisture/SoilMonitor.java +++ b/app/src/main/java/io/trygvis/soilmoisture/SmSensor.java @@ -2,14 +2,14 @@ package io.trygvis.soilmoisture; import java.util.Date; -class SoilMonitor { +class SmSensor { private final SmDevice device; private Date timestamp; private int lastValue; - SoilMonitor(SmDevice device) { + SmSensor(SmDevice device) { this.device = device; } |