Skip to content

Commit

Permalink
fix sensor data collection interval error
Browse files Browse the repository at this point in the history
  • Loading branch information
sercant committed Apr 16, 2018
1 parent bea9cec commit 7381cd3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Accelerometer private constructor(
const val ACTION_AWARE_ACCELEROMETER_STOP = "com.aware.android.sensor.accelerometer.SENSOR_STOP"
const val ACTION_AWARE_ACCELEROMETER_LABEL = "com.aware.android.sensor.accelerometer.SET_LABEL"
const val ACTION_AWARE_ACCELEROMETER_SYNC = "com.aware.android.sensor.accelerometer.SYNC"
const val ACTION_AWARE_ACCELEROMETER_SYNC_SENT = "com.aware.android.sensor.accelerometer.SYNC_SENT"

const val EXTRA_AWARE_ACCELEROMETER_LABEL = "label"

Expand Down Expand Up @@ -124,6 +125,13 @@ class Accelerometer private constructor(
* @param sensorObserver callback for live data updates.
*/
fun setSensorObserver(sensorObserver: SensorObserver) = apply { config.sensorObserver = sensorObserver }
fun setSensorObserver(callback: (type: String, data: Any?, error: Any?) -> Unit) = apply {
config.sensorObserver = object : SensorObserver {
override fun onDataChanged(type: String, data: Any?, error: Any?) {
callback(type, data, error)
}
}
}

/**
* @param deviceId id of the device that will be associated with the events and the sensor. (default = "")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@ import com.awareframework.android.sensor.accelerometer.model.AccelerometerEvent
import java.util.TimeZone
import kotlin.collections.ArrayList

private fun logd(msg: String) { if (AccelerometerSensor.CONFIG.debug) Log.d(AccelerometerSensor.TAG, msg) }
private fun logw(msg: String) { Log.w(AccelerometerSensor.TAG, msg) }
private fun logd(msg: String) {
if (AccelerometerSensor.CONFIG.debug) Log.d(AccelerometerSensor.TAG, msg)
}

private fun logw(msg: String) {
Log.w(AccelerometerSensor.TAG, msg)
}

/**
* Implementation of Aware accelerometer in kotlin as a standalone service.
Expand Down Expand Up @@ -103,7 +108,7 @@ class AccelerometerSensor : AwareSensor(), SensorEventListener {
} else {
saveAccelerometerDevice(mAccelerometer)

val samplingPeriodUs = if(CONFIG.interval > 0) 1000000 / CONFIG.interval else 0
val samplingPeriodUs = if (CONFIG.interval > 0) 1000000 / CONFIG.interval else 0
mSensorManager.registerListener(this, mAccelerometer, samplingPeriodUs, sensorHandler)
lastSavedAt = System.currentTimeMillis()

Expand All @@ -129,23 +134,23 @@ class AccelerometerSensor : AwareSensor(), SensorEventListener {
}

override fun onSensorChanged(event: SensorEvent) {
if (event.timestamp - lastTimestamp < CONFIG.interval / 1000) {
val currentTime = System.currentTimeMillis()
if (currentTime - lastTimestamp < (900.0 / CONFIG.interval)) {
// skip this event
return
}
lastTimestamp = event.timestamp
lastTimestamp = currentTime

if (lastValues != null && CONFIG.threshold > 0
&& Math.abs(event.values[0] - lastValues!![0]) < CONFIG.threshold
&& Math.abs(event.values[1] - lastValues!![1]) < CONFIG.threshold
&& Math.abs(event.values[2] - lastValues!![2]) < CONFIG.threshold) {
if (CONFIG.threshold > 0
&& Math.abs(event.values[0] - lastValues[0]) < CONFIG.threshold
&& Math.abs(event.values[1] - lastValues[1]) < CONFIG.threshold
&& Math.abs(event.values[2] - lastValues[2]) < CONFIG.threshold) {
return
}
lastValues.forEachIndexed { index, fl ->
lastValues.forEachIndexed { index, _ ->
lastValues[index] = event.values[index]
}

val currentTime = System.currentTimeMillis()
val data = AccelerometerEvent().apply {
timestamp = currentTime
eventTimestamp = event.timestamp
Expand Down Expand Up @@ -187,6 +192,8 @@ class AccelerometerSensor : AwareSensor(), SensorEventListener {
override fun onSync(intent: Intent?) {
dbEngine?.startSync(AccelerometerEvent.TABLE_NAME)
dbEngine?.startSync(AccelerometerDevice.TABLE_NAME, DbSyncConfig(removeAfterSync = false))

sendBroadcast(Intent(Accelerometer.ACTION_AWARE_ACCELEROMETER_SYNC_SENT))
}

override fun onDestroy() {
Expand Down Expand Up @@ -256,7 +263,7 @@ class AccelerometerSensor : AwareSensor(), SensorEventListener {
}
}

fun getIntentFilter() : IntentFilter {
fun getIntentFilter(): IntentFilter {
val intentFilter = IntentFilter()
// intentFilter.addAction(Accelerometer.ACTION_AWARE_ACCELEROMETER_START)
// intentFilter.addAction(Accelerometer.ACTION_AWARE_ACCELEROMETER_STOP)
Expand Down

0 comments on commit 7381cd3

Please sign in to comment.