|
| 1 | +package io.eigr.synapsys.extensions.android.sensors.actor |
| 2 | + |
| 3 | +import android.hardware.Sensor |
| 4 | +import android.hardware.SensorEvent |
| 5 | +import android.hardware.SensorEventListener |
| 6 | +import android.hardware.SensorManager |
| 7 | +import io.eigr.synapsys.core.actor.Actor |
| 8 | +import io.eigr.synapsys.core.actor.ActorPointer |
| 9 | +import io.eigr.synapsys.extensions.android.sensors.events.SensorData |
| 10 | +import io.eigr.synapsys.extensions.android.sensors.internals.ActorHandler |
| 11 | +import kotlinx.coroutines.runBlocking |
| 12 | +import android.content.Context as AndroidContext |
| 13 | +import io.eigr.synapsys.core.actor.Context as ActorContext |
| 14 | + |
| 15 | +open class SensorActor<S : Any, M : SensorData>( |
| 16 | + id: String, |
| 17 | + initialState: S?, |
| 18 | + private val androidContext: AndroidContext, |
| 19 | + private val sensorType: Int, |
| 20 | + private val samplingPeriod: Int = SensorManager.SENSOR_DELAY_NORMAL, |
| 21 | +) : Actor<S, M, Unit>( |
| 22 | + id = "sensor-actor-${id}-${sensorType}", |
| 23 | + initialState = initialState |
| 24 | +), |
| 25 | + SensorEventListener { |
| 26 | + |
| 27 | + private val sensorManager by lazy { |
| 28 | + androidContext.getSystemService(AndroidContext.SENSOR_SERVICE) as SensorManager |
| 29 | + } |
| 30 | + |
| 31 | + private lateinit var targetActor: ActorPointer<*> |
| 32 | + |
| 33 | + override fun onStart(ctx: ActorContext<S>): ActorContext<S> { |
| 34 | + targetActor = ctx.system.actorOf( |
| 35 | + id = "processor-$id", |
| 36 | + initialState = initialState!!) {id, state -> ActorHandler(id, state) } |
| 37 | + |
| 38 | + val sensor = sensorManager.getDefaultSensor(sensorType) |
| 39 | + sensor?.let { |
| 40 | + sensorManager.registerListener(this, it, samplingPeriod) |
| 41 | + } ?: { |
| 42 | + //ctx.system.log.error("Sensor $sensorType not available") |
| 43 | + } |
| 44 | + |
| 45 | + return ctx |
| 46 | + } |
| 47 | + |
| 48 | + override fun onStop() { |
| 49 | + sensorManager.unregisterListener(this) |
| 50 | + } |
| 51 | + |
| 52 | + override fun onReceive(message: M, ctx: ActorContext<S>): Pair<ActorContext<S>, Unit> { |
| 53 | + return ctx to Unit |
| 54 | + } |
| 55 | + |
| 56 | + @Suppress("UNCHECKED_CAST") |
| 57 | + override fun onSensorChanged(event: SensorEvent) { |
| 58 | + val message = when (event.sensor.type) { |
| 59 | + Sensor.TYPE_ACCELEROMETER -> SensorData.AccelerometerData( |
| 60 | + x = event.values[0], |
| 61 | + y = event.values[1], |
| 62 | + z = event.values[2], |
| 63 | + timestamp = event.timestamp |
| 64 | + ) |
| 65 | + |
| 66 | + Sensor.TYPE_GYROSCOPE -> SensorData.GyroscopeData( |
| 67 | + x = event.values[0], |
| 68 | + y = event.values[1], |
| 69 | + z = event.values[2], |
| 70 | + timestamp = event.timestamp |
| 71 | + ) |
| 72 | + |
| 73 | + Sensor.TYPE_HEART_RATE -> SensorData.HeartRateData( |
| 74 | + bpm = event.values[0], |
| 75 | + timestamp = event.timestamp |
| 76 | + ) |
| 77 | + |
| 78 | + else -> SensorData.RawSensorData( |
| 79 | + sensorType = event.sensor.type, |
| 80 | + values = event.values.copyOf(), |
| 81 | + timestamp = event.timestamp |
| 82 | + ) |
| 83 | + } |
| 84 | + |
| 85 | + runBlocking { targetActor.send(message as M) } |
| 86 | + } |
| 87 | + |
| 88 | + override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {} |
| 89 | +} |
0 commit comments