Browse Source

Optimization

androidx-act-recog
sipp11 5 years ago
parent
commit
b6c31854c8
  1. 2
      .idea/misc.xml
  2. 31
      app/src/main/java/co/zzyzx/sensorlogger/EndlessService.kt
  3. 12
      app/src/main/java/co/zzyzx/sensorlogger/db/Database.kt
  4. 7
      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" /> <configuration PROFILE_NAME="Debug" CONFIG_NAME="Debug" />
</configurations> </configurations>
</component> </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" /> <output url="file://$PROJECT_DIR$/build/classes" />
</component> </component>
<component name="ProjectType"> <component name="ProjectType">

31
app/src/main/java/co/zzyzx/sensorlogger/EndlessService.kt

@ -18,14 +18,13 @@ import android.os.IBinder
import android.os.PowerManager import android.os.PowerManager
import android.provider.Settings import android.provider.Settings
import android.widget.Toast import android.widget.Toast
import co.zzyzx.sensorlogger.db.Record
import co.zzyzx.sensorlogger.db.RecordRepository import co.zzyzx.sensorlogger.db.RecordRepository
import com.github.kittinunf.fuel.Fuel 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.text.SimpleDateFormat
import java.time.Instant
import java.util.* import java.util.*
import kotlin.collections.ArrayList
fun Double.format(digits: Int) = java.lang.String.format("%.${digits}f", this) 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 var mLocationManager: LocationManager? = null // need to do this because of permission
private lateinit var recRepo: RecordRepository 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? { override fun onBind(intent: Intent): IBinder? {
log("Some component want to bind with the service") 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 // we're starting a loop in a coroutine
GlobalScope.launch(Dispatchers.IO) { /* GlobalScope.launch(Dispatchers.IO) {
while (isServiceStarted) { while (isServiceStarted) {
launch(Dispatchers.IO) { launch(Dispatchers.IO) {
pingFakeServer() pingFakeServer()
@ -141,7 +151,7 @@ class EndlessService : Service(), SensorEventListener, LocationListener {
delay(1 * 180 * 1000) delay(1 * 180 * 1000)
} }
log("End of the loop for the service") log("End of the loop for the service")
} } */
} }
private fun stopService() { private fun stopService() {
@ -176,9 +186,9 @@ class EndlessService : Service(), SensorEventListener, LocationListener {
result.accuracy, result.accuracy,
result.provider, result.provider,
result.isFromMockProvider result.isFromMockProvider
).joinToString(",") ).joinToString(",")
recRepo.addNewRecord("location", txt) addNewRecord("location", txt)
val notification = createNotification(notiText) val notification = createNotification(notiText)
nm.notify(notificationId, notification) nm.notify(notificationId, notification)
} }
@ -219,7 +229,7 @@ class EndlessService : Service(), SensorEventListener, LocationListener {
accelLin[1].format(3), accelLin[1].format(3),
accelLin[2].format(3) accelLin[2].format(3)
).joinToString(",") ).joinToString(",")
recRepo.addNewRecord("accelerometer", txt) addNewRecord("accelerometer", txt)
} }
Sensor.TYPE_GYROSCOPE -> { Sensor.TYPE_GYROSCOPE -> {
val txt = arrayOf( val txt = arrayOf(
@ -227,7 +237,8 @@ class EndlessService : Service(), SensorEventListener, LocationListener {
evt.values[1].format(3), evt.values[1].format(3),
evt.values[2].format(3) evt.values[2].format(3)
).joinToString(",") ).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.Room
import android.arch.persistence.room.RoomDatabase import android.arch.persistence.room.RoomDatabase
import android.content.Context import android.content.Context
import co.zzyzx.sensorlogger.log
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import java.time.Instant 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 class AppDatabase : RoomDatabase() {
abstract fun recordDao(): RecordDao abstract fun recordDao(): RecordDao
} }
@ -31,8 +30,13 @@ class RecordRepository {
fun addNewRecord(sensor: String, data: String) { fun addNewRecord(sensor: String, data: String) {
val now = Instant.now().toEpochMilli() val now = Instant.now().toEpochMilli()
GlobalScope.launch { GlobalScope.launch {
log("add [${now}] ${sensor} - ${data}") dao.insert(Record(timestamp = now, sensor = sensor, data = data))
dao.insertAll(Record(timestamp = now, sensor = sensor, data = data)) }
}
fun addBulkRecord(recs: List<Record>) {
GlobalScope.launch {
dao.insert(*recs.toTypedArray())
} }
} }

7
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.lifecycle.LiveData
import android.arch.persistence.room.* import android.arch.persistence.room.*
@Dao @Dao
interface RecordDao { interface RecordDao {
@Query("SELECT COUNT(*) FROM record WHERE sensor = :sensor") @Query("SELECT COUNT(*) FROM record WHERE sensor = :sensor")
@ -12,18 +13,16 @@ interface RecordDao {
fun getAll(): List<Record> fun getAll(): List<Record>
@Query("SELECT * FROM record WHERE sensor LIKE :sensor ORDER BY timestamp DESC LIMIT 1") @Query("SELECT * FROM record WHERE sensor LIKE :sensor ORDER BY timestamp DESC LIMIT 1")
fun getLatestLiveData(sensor : String): LiveData<List<Record>> fun getLatestLiveData(sensor: String): LiveData<List<Record>>
@Query("SELECT * FROM record WHERE sensor LIKE :name ORDER BY timestamp DESC LIMIT 1") @Query("SELECT * FROM record WHERE sensor LIKE :name ORDER BY timestamp DESC LIMIT 1")
fun findBySensor(name: String): Record fun findBySensor(name: String): Record
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertAll(vararg records: Record)
@Insert
fun insert(vararg records: Record) fun insert(vararg records: Record)
@Delete @Delete
fun delete(record: Record) fun delete(record: Record)

Loading…
Cancel
Save