我正在 kotlin 中记录多个移动传感器的值并将它们存储在 DB Browser SQLite 数据库中。我面临的问题是,我在 2 分钟内将太多值存储在数据库中,大约有 3000 多个值。我想创建传感器值的数据集并存储正常使用期间移动传感器 1 周的值。我尝试过应用不同的逻辑,但它不起作用。
我尝试过应用计时器逻辑,但它仍然不起作用。我只需要 1 周内将大约 6000 到 8000 个值存储在数据库中,以便我可以训练我的 ML 模型。我想控制数据库中记录值的频率。下面是记录传感器的代码。
package com.example.sensorsapp
import android.content.ContentValues
import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import java.util.concurrent.Executors
import java.util.concurrent.ScheduledExecutorService
import java.util.concurrent.TimeUnit
class MainActivity : AppCompatActivity(), SensorEventListener {
private lateinit var sensorManager: SensorManager
private lateinit var accelerometer: Sensor
private lateinit var gyroscope: Sensor
private lateinit var gravity: Sensor
private lateinit var proximity: Sensor
private lateinit var orientation: Sensor
private lateinit var rotationVector: Sensor
private lateinit var linearAcceleration: Sensor
private lateinit var dbHelper: MyDatabaseHelper
private lateinit var db: SQLiteDatabase
private lateinit var readingTextView: TextView
private lateinit var gyroscopeTextView: TextView
private lateinit var gravityTextView: TextView
private lateinit var proximityTextView: TextView
private lateinit var orientationTextView: TextView
private lateinit var rotationVectorTextView: TextView
private lateinit var linearAccelerationTextView: TextView
private lateinit var startStopButton: Button
private var isRecording = false
private lateinit var executor: ScheduledExecutorService
private val recordInterval = 300000L // Record every 5 minutes
private var lastRecordTimestamp = 0L
private val recordedData = mutableMapOf<Int, Triple<Float, Float, Float>>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Initialize UI elements
readingTextView = findViewById(R.id.readingTextView)
startStopButton = findViewById(R.id.startStopButton)
gyroscopeTextView = findViewById(R.id.gyroscopeTextView)
gravityTextView = findViewById(R.id.gravityTextView)
proximityTextView = findViewById(R.id.proximityTextView)
orientationTextView = findViewById(R.id.orientationTextView)
rotationVectorTextView = findViewById(R.id.rotationVectorTextView)
linearAccelerationTextView = findViewById(R.id.linearAccelerationTextView)
startStopButton = findViewById(R.id.startStopButton)
// Initialize the database helper
dbHelper = MyDatabaseHelper(this)
db = dbHelper.writableDatabase
// Initialize the sensor manager and sensors
sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
gyroscope = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE)
gravity = sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY)
proximity = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY)
orientation = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION)
rotationVector = sensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR)
linearAcceleration = sensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION)
if (accelerometer != null) {
sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL)
}
if (gyroscope != null) {
sensorManager.registerListener(this, gyroscope, SensorManager.SENSOR_DELAY_NORMAL)
}
if (gravity != null) {
sensorManager.registerListener(this, gravity, SensorManager.SENSOR_DELAY_NORMAL)
}
if (proximity != null) {
sensorManager.registerListener(this, proximity, SensorManager.SENSOR_DELAY_NORMAL)
}
if (orientation != null) {
sensorManager.registerListener(this, orientation, SensorManager.SENSOR_DELAY_NORMAL)
}
if (rotationVector != null) {
sensorManager.registerListener(this, rotationVector, SensorManager.SENSOR_DELAY_NORMAL)
}
if (linearAcceleration != null) {
sensorManager.registerListener(this, linearAcceleration, SensorManager.SENSOR_DELAY_NORMAL)
}
startStopButton.setOnClickListener {
if (isRecording) {
stopRecording()
} else {
startRecording()
}
}
}
override fun onSensorChanged(event: SensorEvent?) {
if (event == null) return
val currentTime = System.currentTimeMillis()
when (event.sensor.type) {
Sensor.TYPE_ACCELEROMETER, Sensor.TYPE_GYROSCOPE, Sensor.TYPE_GRAVITY, Sensor.TYPE_LINEAR_ACCELERATION -> {
val xValue = event.values[0]
val yValue = event.values[1]
val zValue = event.values[2]
//val reading = "Accelerometer: X: $xValue, Y: $yValue, Z: $zValue"
//readingTextView.text = reading
if (isRecording) {
val sensorId = event.sensor.type
val previousValues = recordedData[sensorId]
if (previousValues == null ||
currentTime - lastRecordTimestamp >= recordInterval
) {
// Record the data if it's the first reading or the specified time interval has passed
recordSensorData(event.sensor, xValue, yValue, zValue)
lastRecordTimestamp = currentTime
} else {
// Compare the current values with the previous values and record if they differ significantly
val deltaThreshold = 1.0f // Adjust as needed
if (Math.abs(xValue - previousValues.first) >= deltaThreshold ||
Math.abs(yValue - previousValues.second) >= deltaThreshold ||
Math.abs(zValue - previousValues.third) >= deltaThreshold
) {
recordSensorData(event.sensor, xValue, yValue, zValue)
lastRecordTimestamp = currentTime
}
}
}
}
}
}
override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {
// Handle accuracy change if needed
}
private fun startRecording() {
isRecording = true
startStopButton.text = "Stop Recording"
}
private fun stopRecording() {
isRecording = false
startStopButton.text = "Start Recording"
}
private fun recordSensorData(sensor: Sensor, xValue: Float, yValue: Float, zValue: Float) {
val sensorName = sensor.getStringType()
val values = ContentValues().apply {
put(MyDatabaseHelper.TIMESTAMP, System.currentTimeMillis())
put(MyDatabaseHelper.SENSOR_NAME, sensorName)
put(MyDatabaseHelper.X_VALUE, xValue)
put(MyDatabaseHelper.Y_VALUE, yValue)
put(MyDatabaseHelper.Z_VALUE, zValue)
}
db.insert(MyDatabaseHelper.TABLE_NAME, null, values)
recordedData[sensor.type] = Triple(xValue, yValue, zValue)
}
override fun onDestroy() {
super.onDestroy()
// Unregister the sensor listener and close the database when the activity is destroyed
sensorManager.unregisterListener(this)
db.close()
}
}
如果我理解问题正确的话,你可以在recordSensorData()中检查最近7天的记录数,当超过8000条时停止插入记录。
但是,如果这些记录来得那么快(2 分钟内超过 3000 个值),您将在几分钟内获得数据,然后长达一周的休息时间。再次强调 - 您可以在 recordSensorData() 中添加一些逻辑来检查添加到表中的最后一条记录的时间戳,例如自上次插入后等待 5 分钟。