From b6c31854c8f9e044e4740a9328628617839c585f Mon Sep 17 00:00:00 2001 From: sipp11 Date: Fri, 4 Oct 2019 00:56:45 +0900 Subject: [PATCH] Optimization --- .idea/misc.xml | 2 +- .../co/zzyzx/sensorlogger/EndlessService.kt | 31 +++++++++++++------ .../java/co/zzyzx/sensorlogger/db/Database.kt | 12 ++++--- .../main/java/co/zzyzx/sensorlogger/db/dao.kt | 7 ++--- 4 files changed, 33 insertions(+), 19 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index af0bbdd..703e5d4 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -5,7 +5,7 @@ - + diff --git a/app/src/main/java/co/zzyzx/sensorlogger/EndlessService.kt b/app/src/main/java/co/zzyzx/sensorlogger/EndlessService.kt index 4635b15..41f26ce 100644 --- a/app/src/main/java/co/zzyzx/sensorlogger/EndlessService.kt +++ b/app/src/main/java/co/zzyzx/sensorlogger/EndlessService.kt @@ -18,14 +18,13 @@ import android.os.IBinder import android.os.PowerManager import android.provider.Settings import android.widget.Toast +import co.zzyzx.sensorlogger.db.Record import co.zzyzx.sensorlogger.db.RecordRepository import com.github.kittinunf.fuel.Fuel -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.GlobalScope -import kotlinx.coroutines.delay -import kotlinx.coroutines.launch import java.text.SimpleDateFormat +import java.time.Instant import java.util.* +import kotlin.collections.ArrayList fun Double.format(digits: Int) = java.lang.String.format("%.${digits}f", this) @@ -46,6 +45,17 @@ class EndlessService : Service(), SensorEventListener, LocationListener { private var mLocationManager: LocationManager? = null // need to do this because of permission private lateinit var recRepo: RecordRepository + private var dataTemp = ArrayList(0) + + fun addNewRecord(sensor: String, text: String) { + if (dataTemp.size < 2000) { + dataTemp.add(Record(Instant.now().toEpochMilli(), sensor, text)) + } else { + val toSave = dataTemp.clone() as List + dataTemp.clear() + recRepo.addBulkRecord(toSave) + } + } override fun onBind(intent: Intent): IBinder? { log("Some component want to bind with the service") @@ -133,7 +143,7 @@ class EndlessService : Service(), SensorEventListener, LocationListener { } // we're starting a loop in a coroutine - GlobalScope.launch(Dispatchers.IO) { + /* GlobalScope.launch(Dispatchers.IO) { while (isServiceStarted) { launch(Dispatchers.IO) { pingFakeServer() @@ -141,7 +151,7 @@ class EndlessService : Service(), SensorEventListener, LocationListener { delay(1 * 180 * 1000) } log("End of the loop for the service") - } + } */ } private fun stopService() { @@ -176,9 +186,9 @@ class EndlessService : Service(), SensorEventListener, LocationListener { result.accuracy, result.provider, result.isFromMockProvider - ).joinToString(",") + ).joinToString(",") - recRepo.addNewRecord("location", txt) + addNewRecord("location", txt) val notification = createNotification(notiText) nm.notify(notificationId, notification) } @@ -219,7 +229,7 @@ class EndlessService : Service(), SensorEventListener, LocationListener { accelLin[1].format(3), accelLin[2].format(3) ).joinToString(",") - recRepo.addNewRecord("accelerometer", txt) + addNewRecord("accelerometer", txt) } Sensor.TYPE_GYROSCOPE -> { val txt = arrayOf( @@ -227,7 +237,8 @@ class EndlessService : Service(), SensorEventListener, LocationListener { evt.values[1].format(3), evt.values[2].format(3) ).joinToString(",") - recRepo.addNewRecord("gyroscope", txt) + + addNewRecord("gyroscope", txt) } } } diff --git a/app/src/main/java/co/zzyzx/sensorlogger/db/Database.kt b/app/src/main/java/co/zzyzx/sensorlogger/db/Database.kt index d5efdd9..289c242 100644 --- a/app/src/main/java/co/zzyzx/sensorlogger/db/Database.kt +++ b/app/src/main/java/co/zzyzx/sensorlogger/db/Database.kt @@ -4,13 +4,12 @@ import android.arch.persistence.room.Database import android.arch.persistence.room.Room import android.arch.persistence.room.RoomDatabase import android.content.Context -import co.zzyzx.sensorlogger.log import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch import java.time.Instant -@Database(entities = arrayOf(Record::class), version = 1) +@Database(entities = arrayOf(Record::class), version = 1, exportSchema = false) abstract class AppDatabase : RoomDatabase() { abstract fun recordDao(): RecordDao } @@ -31,8 +30,13 @@ class RecordRepository { fun addNewRecord(sensor: String, data: String) { val now = Instant.now().toEpochMilli() GlobalScope.launch { - log("add [${now}] ${sensor} - ${data}") - dao.insertAll(Record(timestamp = now, sensor = sensor, data = data)) + dao.insert(Record(timestamp = now, sensor = sensor, data = data)) + } + } + + fun addBulkRecord(recs: List) { + GlobalScope.launch { + dao.insert(*recs.toTypedArray()) } } diff --git a/app/src/main/java/co/zzyzx/sensorlogger/db/dao.kt b/app/src/main/java/co/zzyzx/sensorlogger/db/dao.kt index 7c33593..a270527 100644 --- a/app/src/main/java/co/zzyzx/sensorlogger/db/dao.kt +++ b/app/src/main/java/co/zzyzx/sensorlogger/db/dao.kt @@ -3,6 +3,7 @@ package co.zzyzx.sensorlogger.db import android.arch.lifecycle.LiveData import android.arch.persistence.room.* + @Dao interface RecordDao { @Query("SELECT COUNT(*) FROM record WHERE sensor = :sensor") @@ -12,18 +13,16 @@ interface RecordDao { fun getAll(): List @Query("SELECT * FROM record WHERE sensor LIKE :sensor ORDER BY timestamp DESC LIMIT 1") - fun getLatestLiveData(sensor : String): LiveData> + fun getLatestLiveData(sensor: String): LiveData> @Query("SELECT * FROM record WHERE sensor LIKE :name ORDER BY timestamp DESC LIMIT 1") fun findBySensor(name: String): Record @Insert(onConflict = OnConflictStrategy.REPLACE) - fun insertAll(vararg records: Record) - - @Insert fun insert(vararg records: Record) + @Delete fun delete(record: Record)