Browse Source

Optimization

androidx-act-recog
sipp11 5 years ago
parent
commit
b6c31854c8
  1. 2
      .idea/misc.xml
  2. 29
      app/src/main/java/co/zzyzx/sensorlogger/EndlessService.kt
  3. 12
      app/src/main/java/co/zzyzx/sensorlogger/db/Database.kt
  4. 5
      app/src/main/java/co/zzyzx/sensorlogger/db/dao.kt

2
.idea/misc.xml

@ -5,7 +5,7 @@
<configuration PROFILE_NAME="Debug" CONFIG_NAME="Debug" />
</configurations>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">

29
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<Record>(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<Record>
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() {
@ -178,7 +188,7 @@ class EndlessService : Service(), SensorEventListener, LocationListener {
result.isFromMockProvider
).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)
}
}
}

12
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<Record>) {
GlobalScope.launch {
dao.insert(*recs.toTypedArray())
}
}

5
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")
@ -19,11 +20,9 @@ interface RecordDao {
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)

Loading…
Cancel
Save