From e8a052fd2c03b399550b6c9be3199d35fdd47f10 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sat, 3 Jan 2015 22:49:58 +0100 Subject: o Adding a database of BT devices and soil devices, sensors and samples. --- .../io/trygvis/android/bt/DefaultBtService.java | 109 +++++++++++++++++++-- 1 file changed, 102 insertions(+), 7 deletions(-) (limited to 'app/src/main/java/io/trygvis/android/bt/DefaultBtService.java') 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 51d84af..5e13a1f 100644 --- a/app/src/main/java/io/trygvis/android/bt/DefaultBtService.java +++ b/app/src/main/java/io/trygvis/android/bt/DefaultBtService.java @@ -4,20 +4,28 @@ import android.app.Service; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothManager; +import android.content.ComponentName; +import android.content.ContentValues; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; +import android.os.Bundle; import android.os.Handler; import android.os.IBinder; import android.util.Log; import android.widget.Toast; +import org.flywaydb.core.Flyway; +import org.flywaydb.core.api.android.ContextHolder; + import java.util.ArrayList; import java.util.Collection; +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; @@ -34,7 +42,7 @@ public class DefaultBtService> extends Ser // State // ----------------------------------------------------------------------- - private Function, A> tagConstructor; + private BtDbIntegration btDbIntegration; private BluetoothManager bluetoothManager; @@ -49,13 +57,13 @@ public class DefaultBtService> extends Ser // ----------------------------------------------------------------------- @Override - public boolean initialize(Function, A> tagConstructor) { + public boolean initialize(BtDbIntegration btDbIntegration) { if (bluetoothManager != null) { Log.i(TAG, "Already initialized"); return false; } - this.tagConstructor = tagConstructor; + this.btDbIntegration = btDbIntegration; // Use this check to determine whether BLE is supported on the device. Then you can // selectively disable BLE-related features. @@ -171,19 +179,106 @@ public class DefaultBtService> extends Ser return binder; } + @Override + public void onCreate() { + Bundle data; + + try { + ComponentName myService = new ComponentName(this, this.getClass()); + data = getPackageManager().getServiceInfo(myService, PackageManager.GET_META_DATA).metaData; + } catch (PackageManager.NameNotFoundException e) { + throw new RuntimeException(e); + } + + SQLiteDatabase db = openBtDevices(); + String path; + try { + path = db.getPath(); + } finally { + db.close(); + } + + { + ContextHolder.setContext(this); + Flyway flyway = new Flyway(); + flyway.setDataSource("jdbc:sqlite:" + path, "", ""); + flyway.setTable("schema_version_bt"); + flyway.setLocations("db/migration/bt"); + + flyway.migrate(); + } + + String customMigrations = data.getString(getClass().getName() + ".migration", null); + + if (customMigrations != null) { + ContextHolder.setContext(this); + Flyway flyway = new Flyway(); + flyway.setDataSource("jdbc:sqlite:" + path, "", ""); + flyway.setTable("schema_version_custom"); + flyway.setLocations(customMigrations); + + flyway.setBaselineOnMigrate(true); + flyway.migrate(); + } + } + // ----------------------------------------------------------------------- // Stuff // ----------------------------------------------------------------------- + private SQLiteDatabase openBtDevices() { + return openOrCreateDatabase("bt-devices", MODE_ENABLE_WRITE_AHEAD_LOGGING, null); + } + private BtDevice register(BluetoothDevice bluetoothDevice, Integer rssi, BtScanResult scanResult) { - BtDevice btDevice = findDevice(bluetoothDevice.getAddress()); + String address = bluetoothDevice.getAddress(); + BtDevice btDevice = findDevice(address); if (btDevice != null) { return btDevice; } - Log.i(TAG, "New device: " + bluetoothDevice.getAddress()); - btDevice = new BtDevice<>(this, bluetoothDevice, tagConstructor, rssi, scanResult); + long now = System.currentTimeMillis(); + + SQLiteDatabase db = openBtDevices(); + try { + db.beginTransaction(); + + Cursor cursor = db.query("bt_device", new String[]{"id", "first_seen"}, "address=?", + new String[]{address}, null, null, null); + + long id; + Date firstSeen, lastSeen; + boolean seenBefore = cursor.moveToNext(); + + if (seenBefore) { + id = cursor.getLong(0); + firstSeen = new Date(cursor.getLong(1)); + lastSeen = new Date(now); + + ContentValues values = new ContentValues(); + values.put("last_seen", now); + db.update("bt_device", values, "address=?", new String[]{address}); + } else { + ContentValues values = new ContentValues(); + values.put("address", address); + values.put("first_seen", now); + values.put("last_seen", now); + id = db.insert("bt_device", null, values); + firstSeen = lastSeen = new Date(now); + } + + 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(); + } + devices.add(btDevice); sendBroadcast(createNewDevice(btDevice.getAddress())); -- cgit v1.2.3