Implement adaptive emit by speed to reduce resources
This commit is contained in:
parent
0889130a3d
commit
6d5948ea15
@ -97,6 +97,7 @@ class DumonGeolocation : Plugin() {
|
|||||||
context,
|
context,
|
||||||
onImuUpdate = {
|
onImuUpdate = {
|
||||||
latestImu = it
|
latestImu = it
|
||||||
|
adjustIntervalAndSensorRate(it.speed)
|
||||||
emitPositionUpdate()
|
emitPositionUpdate()
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@ -220,14 +221,6 @@ class DumonGeolocation : Plugin() {
|
|||||||
call.resolve()
|
call.resolve()
|
||||||
}
|
}
|
||||||
|
|
||||||
@PluginMethod
|
|
||||||
fun setEmitInterval(call: PluginCall) {
|
|
||||||
val intervalMs = call.getInt("intervalMs") ?: 1000
|
|
||||||
emitIntervalMs = intervalMs.toLong().coerceIn(250L, 30000L) // batas antara 0.25s – 30s
|
|
||||||
Log.d("DUMON_GEOLOCATION", "Emit interval set to $emitIntervalMs ms")
|
|
||||||
call.resolve()
|
|
||||||
}
|
|
||||||
|
|
||||||
@PermissionCallback
|
@PermissionCallback
|
||||||
private fun onPermissionResult(call: PluginCall) {
|
private fun onPermissionResult(call: PluginCall) {
|
||||||
val locationStatus = PermissionUtils.getPermissionStatus(
|
val locationStatus = PermissionUtils.getPermissionStatus(
|
||||||
@ -281,6 +274,22 @@ class DumonGeolocation : Plugin() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun adjustIntervalAndSensorRate(speed: Float) {
|
||||||
|
val targetInterval = when {
|
||||||
|
speed > 5f -> 1000L
|
||||||
|
speed > 1.5f -> 5000L
|
||||||
|
speed > 0.3f -> 15000L
|
||||||
|
else -> 30000L
|
||||||
|
}
|
||||||
|
|
||||||
|
if (emitIntervalMs != targetInterval) {
|
||||||
|
emitIntervalMs = targetInterval
|
||||||
|
Log.d("DUMON_GEOLOCATION", "Auto-set emitIntervalMs = $emitIntervalMs ms")
|
||||||
|
}
|
||||||
|
|
||||||
|
imuManager?.setSensorDelayBySpeed(speed)
|
||||||
|
}
|
||||||
|
|
||||||
private fun degToRad(deg: Double): Double {
|
private fun degToRad(deg: Double): Double {
|
||||||
return deg * PI / 180.0
|
return deg * PI / 180.0
|
||||||
}
|
}
|
||||||
|
|||||||
@ -29,6 +29,8 @@ class ImuSensorManager(
|
|||||||
private var latestDirectionRad = 0f
|
private var latestDirectionRad = 0f
|
||||||
private var latestSpeed = 0f
|
private var latestSpeed = 0f
|
||||||
|
|
||||||
|
private var currentDelay: Int = SensorManager.SENSOR_DELAY_GAME
|
||||||
|
|
||||||
fun start() {
|
fun start() {
|
||||||
accelerometer?.let { sensorManager.registerListener(this, it, SensorManager.SENSOR_DELAY_GAME) }
|
accelerometer?.let { sensorManager.registerListener(this, it, SensorManager.SENSOR_DELAY_GAME) }
|
||||||
gyroscope?.let { sensorManager.registerListener(this, it, SensorManager.SENSOR_DELAY_GAME) }
|
gyroscope?.let { sensorManager.registerListener(this, it, SensorManager.SENSOR_DELAY_GAME) }
|
||||||
@ -41,6 +43,28 @@ class ImuSensorManager(
|
|||||||
Log.d("IMU_SENSOR", "IMU sensor tracking stopped")
|
Log.d("IMU_SENSOR", "IMU sensor tracking stopped")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun setSensorDelayBySpeed(speed: Float) {
|
||||||
|
val desiredDelay = when {
|
||||||
|
speed > 5f -> SensorManager.SENSOR_DELAY_GAME
|
||||||
|
speed > 1.5f -> SensorManager.SENSOR_DELAY_UI
|
||||||
|
speed > 0.3f -> SensorManager.SENSOR_DELAY_NORMAL
|
||||||
|
else -> SensorManager.SENSOR_DELAY_NORMAL
|
||||||
|
}
|
||||||
|
|
||||||
|
if (desiredDelay != currentDelay) {
|
||||||
|
currentDelay = desiredDelay
|
||||||
|
restartSensorsWithDelay(currentDelay)
|
||||||
|
Log.d("IMU_SENSOR", "Sensor delay changed to $currentDelay")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun restartSensorsWithDelay(delay: Int) {
|
||||||
|
sensorManager.unregisterListener(this)
|
||||||
|
accelerometer?.let { sensorManager.registerListener(this, it, delay) }
|
||||||
|
gyroscope?.let { sensorManager.registerListener(this, it, delay) }
|
||||||
|
rotationVector?.let { sensorManager.registerListener(this, it, delay) }
|
||||||
|
}
|
||||||
|
|
||||||
override fun onSensorChanged(event: SensorEvent?) {
|
override fun onSensorChanged(event: SensorEvent?) {
|
||||||
event?.let {
|
event?.let {
|
||||||
when (it.sensor.type) {
|
when (it.sensor.type) {
|
||||||
|
|||||||
16
dist/docs.json
vendored
16
dist/docs.json
vendored
@ -49,22 +49,6 @@
|
|||||||
],
|
],
|
||||||
"slug": "checkandrequestpermissions"
|
"slug": "checkandrequestpermissions"
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"name": "setEmitInterval",
|
|
||||||
"signature": "(options: { intervalMs: number; }) => Promise<void>",
|
|
||||||
"parameters": [
|
|
||||||
{
|
|
||||||
"name": "options",
|
|
||||||
"docs": "",
|
|
||||||
"type": "{ intervalMs: number; }"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"returns": "Promise<void>",
|
|
||||||
"tags": [],
|
|
||||||
"docs": "",
|
|
||||||
"complexTypes": [],
|
|
||||||
"slug": "setemitinterval"
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"name": "configureEdgeToEdge",
|
"name": "configureEdgeToEdge",
|
||||||
"signature": "(options: { bgColor: string; style: 'DARK' | 'LIGHT'; overlay?: boolean; }) => Promise<void>",
|
"signature": "(options: { bgColor: string; style: 'DARK' | 'LIGHT'; overlay?: boolean; }) => Promise<void>",
|
||||||
|
|||||||
3
dist/esm/definitions.d.ts
vendored
3
dist/esm/definitions.d.ts
vendored
@ -20,9 +20,6 @@ export interface DumonGeolocationPlugin {
|
|||||||
stopPositioning(): Promise<void>;
|
stopPositioning(): Promise<void>;
|
||||||
getLatestPosition(): Promise<PositioningData>;
|
getLatestPosition(): Promise<PositioningData>;
|
||||||
checkAndRequestPermissions(): Promise<PermissionStatus>;
|
checkAndRequestPermissions(): Promise<PermissionStatus>;
|
||||||
setEmitInterval(options: {
|
|
||||||
intervalMs: number;
|
|
||||||
}): Promise<void>;
|
|
||||||
configureEdgeToEdge(options: {
|
configureEdgeToEdge(options: {
|
||||||
bgColor: string;
|
bgColor: string;
|
||||||
style: 'DARK' | 'LIGHT';
|
style: 'DARK' | 'LIGHT';
|
||||||
|
|||||||
2
dist/esm/definitions.js.map
vendored
2
dist/esm/definitions.js.map
vendored
@ -1 +1 @@
|
|||||||
{"version":3,"file":"definitions.js","sourceRoot":"","sources":["../../src/definitions.ts"],"names":[],"mappings":"","sourcesContent":["import type { PluginListenerHandle } from '@capacitor/core';\n\n// export interface SatelliteStatus {\n// satellitesInView: number;\n// usedInFix: number;\n// constellationCounts: { [key: string]: number };\n// }\n\n// export interface WifiAp {\n// ssid: string;\n// bssid: string;\n// rssi: number;\n// distance?: number;\n// }\n\n// export interface WifiScanResult {\n// apCount: number;\n// aps: WifiAp[];\n// }\n\n// export interface ImuData {\n// accelX: number;\n// accelY: number;\n// accelZ: number;\n// gyroX: number;\n// gyroY: number;\n// gyroZ: number;\n// speed?: number;\n// acceleration?: number;\n// directionRad?: number;\n// }\n\n// export interface GpsData {\n// latitude: number;\n// longitude: number;\n// accuracy: number;\n// satellitesInView?: number;\n// usedInFix?: number;\n// constellationCounts?: { [key: string]: number };\n// }\n\n// export interface PositioningData {\n// source: 'GNSS' | 'WIFI' | 'FUSED' | 'MOCK';\n// timestamp: number;\n// latitude: number;\n// longitude: number;\n// accuracy: number;\n\n// gnssData?: SatelliteStatus;\n// wifiData?: WifiAp[];\n// imuData?: ImuData;\n// }\n\nexport interface PositioningData {\n source: 'GNSS' | 'WIFI' | 'FUSED' | 'MOCK';\n timestamp: number;\n latitude: number;\n longitude: number;\n accuracy: number;\n speed: number;\n acceleration: number;\n directionRad: number;\n isMocked: boolean;\n predicted?: boolean;\n}\n\nexport interface PermissionStatus {\n location: 'granted' | 'denied';\n wifi: 'granted' | 'denied';\n}\n\nexport interface DumonGeolocationPlugin {\n startPositioning(): Promise<void>;\n stopPositioning(): Promise<void>;\n getLatestPosition(): Promise<PositioningData>;\n checkAndRequestPermissions(): Promise<PermissionStatus>;\n setEmitInterval(options: { intervalMs: number }): Promise<void>;\n\n configureEdgeToEdge(options: {\n bgColor: string;\n style: 'DARK' | 'LIGHT';\n overlay?: boolean;\n }): Promise<void>;\n\n addListener(\n eventName: 'onPositionUpdate',\n listenerFunc: (data: PositioningData) => void\n ): PluginListenerHandle;\n}"]}
|
{"version":3,"file":"definitions.js","sourceRoot":"","sources":["../../src/definitions.ts"],"names":[],"mappings":"","sourcesContent":["import type { PluginListenerHandle } from '@capacitor/core';\n\n// export interface SatelliteStatus {\n// satellitesInView: number;\n// usedInFix: number;\n// constellationCounts: { [key: string]: number };\n// }\n\n// export interface WifiAp {\n// ssid: string;\n// bssid: string;\n// rssi: number;\n// distance?: number;\n// }\n\n// export interface WifiScanResult {\n// apCount: number;\n// aps: WifiAp[];\n// }\n\n// export interface ImuData {\n// accelX: number;\n// accelY: number;\n// accelZ: number;\n// gyroX: number;\n// gyroY: number;\n// gyroZ: number;\n// speed?: number;\n// acceleration?: number;\n// directionRad?: number;\n// }\n\n// export interface GpsData {\n// latitude: number;\n// longitude: number;\n// accuracy: number;\n// satellitesInView?: number;\n// usedInFix?: number;\n// constellationCounts?: { [key: string]: number };\n// }\n\n// export interface PositioningData {\n// source: 'GNSS' | 'WIFI' | 'FUSED' | 'MOCK';\n// timestamp: number;\n// latitude: number;\n// longitude: number;\n// accuracy: number;\n\n// gnssData?: SatelliteStatus;\n// wifiData?: WifiAp[];\n// imuData?: ImuData;\n// }\n\nexport interface PositioningData {\n source: 'GNSS' | 'WIFI' | 'FUSED' | 'MOCK';\n timestamp: number;\n latitude: number;\n longitude: number;\n accuracy: number;\n speed: number;\n acceleration: number;\n directionRad: number;\n isMocked: boolean;\n predicted?: boolean;\n}\n\nexport interface PermissionStatus {\n location: 'granted' | 'denied';\n wifi: 'granted' | 'denied';\n}\n\nexport interface DumonGeolocationPlugin {\n startPositioning(): Promise<void>;\n stopPositioning(): Promise<void>;\n getLatestPosition(): Promise<PositioningData>;\n checkAndRequestPermissions(): Promise<PermissionStatus>;\n\n configureEdgeToEdge(options: {\n bgColor: string;\n style: 'DARK' | 'LIGHT';\n overlay?: boolean;\n }): Promise<void>;\n\n addListener(\n eventName: 'onPositionUpdate',\n listenerFunc: (data: PositioningData) => void\n ): PluginListenerHandle;\n}"]}
|
||||||
@ -74,7 +74,6 @@ export interface DumonGeolocationPlugin {
|
|||||||
stopPositioning(): Promise<void>;
|
stopPositioning(): Promise<void>;
|
||||||
getLatestPosition(): Promise<PositioningData>;
|
getLatestPosition(): Promise<PositioningData>;
|
||||||
checkAndRequestPermissions(): Promise<PermissionStatus>;
|
checkAndRequestPermissions(): Promise<PermissionStatus>;
|
||||||
setEmitInterval(options: { intervalMs: number }): Promise<void>;
|
|
||||||
|
|
||||||
configureEdgeToEdge(options: {
|
configureEdgeToEdge(options: {
|
||||||
bgColor: string;
|
bgColor: string;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user