diff --git a/app/src/main/java/com/casic/br/operationsite/service/ForegroundRunningService.kt b/app/src/main/java/com/casic/br/operationsite/service/ForegroundRunningService.kt index 9fd5e00..1a27231 100644 --- a/app/src/main/java/com/casic/br/operationsite/service/ForegroundRunningService.kt +++ b/app/src/main/java/com/casic/br/operationsite/service/ForegroundRunningService.kt @@ -4,11 +4,14 @@ import android.app.NotificationManager import android.app.Service import android.content.Intent +import android.media.AudioAttributes +import android.media.SoundPool import android.os.Handler import android.os.IBinder import android.os.Message import androidx.core.app.NotificationCompat import com.casic.br.operationsite.R +import com.casic.br.operationsite.utils.LocaleConstant import com.pengxh.kt.lite.utils.WeakReferenceHandler class ForegroundRunningService : Service(), Handler.Callback { @@ -17,10 +20,17 @@ var weakReferenceHandler: WeakReferenceHandler? = null } - private val kTag = "ForegroundRunningService" private val notificationId = 1 private val notificationManager by lazy { getSystemService(NOTIFICATION_SERVICE) as NotificationManager } private var notificationBuilder: NotificationCompat.Builder? = null + private val attr = AudioAttributes.Builder() + .setUsage(AudioAttributes.USAGE_ALARM) + .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) + .build() + private var soundPool = SoundPool.Builder().setMaxStreams(1).setAudioAttributes(attr).build() + private var soundResourceId = 0 + private var isSoundLoaded = false + private var isAlarmPlaying = false override fun onCreate() { super.onCreate() @@ -40,6 +50,13 @@ val notification = notificationBuilder?.build() startForeground(notificationId, notification) + + soundPool.setOnLoadCompleteListener { soundPool, sampleId, status -> + if (status == 0) { + isSoundLoaded = true + } + } + soundResourceId = soundPool.load(this, R.raw.ring, 1) } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { @@ -51,17 +68,45 @@ override fun handleMessage(msg: Message): Boolean { when (msg.what) { - 2024110501 -> notificationBuilder?.setContentTitle("通讯服务已连接") - 2024110502 -> notificationBuilder?.setContentTitle("通讯服务断开连接") - 2024110503 -> notificationBuilder?.setContentTitle("通讯服务连接出错,开始重连") + LocaleConstant.SOCKET_CONNECTED_CODE -> { + notificationBuilder?.setContentTitle("通讯服务已连接") + val notification = notificationBuilder?.build() + notificationManager.notify(notificationId, notification) + } + + LocaleConstant.SOCKET_DISCONNECTED_CODE -> { + notificationBuilder?.setContentTitle("通讯服务断开连接") + val notification = notificationBuilder?.build() + notificationManager.notify(notificationId, notification) + } + + LocaleConstant.SOCKET_CONNECT_ERROR_CODE -> { + notificationBuilder?.setContentTitle("通讯服务连接出错,开始重连") + val notification = notificationBuilder?.build() + notificationManager.notify(notificationId, notification) + } + + LocaleConstant.START_WARNING_CODE -> { + if (isSoundLoaded && !isAlarmPlaying) { + soundPool.play(soundResourceId, 1f, 1f, 1, -1, 1f) + isAlarmPlaying = true + } + } + + LocaleConstant.STOP_WARNING_CODE -> { + if (isAlarmPlaying) { + soundPool.autoPause() + isAlarmPlaying = false + } + } } - val notification = notificationBuilder?.build() - notificationManager.notify(notificationId, notification) return true } override fun onDestroy() { super.onDestroy() + soundPool.release() + soundPool = null stopForeground(STOP_FOREGROUND_REMOVE) } diff --git a/app/src/main/java/com/casic/br/operationsite/service/ForegroundRunningService.kt b/app/src/main/java/com/casic/br/operationsite/service/ForegroundRunningService.kt index 9fd5e00..1a27231 100644 --- a/app/src/main/java/com/casic/br/operationsite/service/ForegroundRunningService.kt +++ b/app/src/main/java/com/casic/br/operationsite/service/ForegroundRunningService.kt @@ -4,11 +4,14 @@ import android.app.NotificationManager import android.app.Service import android.content.Intent +import android.media.AudioAttributes +import android.media.SoundPool import android.os.Handler import android.os.IBinder import android.os.Message import androidx.core.app.NotificationCompat import com.casic.br.operationsite.R +import com.casic.br.operationsite.utils.LocaleConstant import com.pengxh.kt.lite.utils.WeakReferenceHandler class ForegroundRunningService : Service(), Handler.Callback { @@ -17,10 +20,17 @@ var weakReferenceHandler: WeakReferenceHandler? = null } - private val kTag = "ForegroundRunningService" private val notificationId = 1 private val notificationManager by lazy { getSystemService(NOTIFICATION_SERVICE) as NotificationManager } private var notificationBuilder: NotificationCompat.Builder? = null + private val attr = AudioAttributes.Builder() + .setUsage(AudioAttributes.USAGE_ALARM) + .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) + .build() + private var soundPool = SoundPool.Builder().setMaxStreams(1).setAudioAttributes(attr).build() + private var soundResourceId = 0 + private var isSoundLoaded = false + private var isAlarmPlaying = false override fun onCreate() { super.onCreate() @@ -40,6 +50,13 @@ val notification = notificationBuilder?.build() startForeground(notificationId, notification) + + soundPool.setOnLoadCompleteListener { soundPool, sampleId, status -> + if (status == 0) { + isSoundLoaded = true + } + } + soundResourceId = soundPool.load(this, R.raw.ring, 1) } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { @@ -51,17 +68,45 @@ override fun handleMessage(msg: Message): Boolean { when (msg.what) { - 2024110501 -> notificationBuilder?.setContentTitle("通讯服务已连接") - 2024110502 -> notificationBuilder?.setContentTitle("通讯服务断开连接") - 2024110503 -> notificationBuilder?.setContentTitle("通讯服务连接出错,开始重连") + LocaleConstant.SOCKET_CONNECTED_CODE -> { + notificationBuilder?.setContentTitle("通讯服务已连接") + val notification = notificationBuilder?.build() + notificationManager.notify(notificationId, notification) + } + + LocaleConstant.SOCKET_DISCONNECTED_CODE -> { + notificationBuilder?.setContentTitle("通讯服务断开连接") + val notification = notificationBuilder?.build() + notificationManager.notify(notificationId, notification) + } + + LocaleConstant.SOCKET_CONNECT_ERROR_CODE -> { + notificationBuilder?.setContentTitle("通讯服务连接出错,开始重连") + val notification = notificationBuilder?.build() + notificationManager.notify(notificationId, notification) + } + + LocaleConstant.START_WARNING_CODE -> { + if (isSoundLoaded && !isAlarmPlaying) { + soundPool.play(soundResourceId, 1f, 1f, 1, -1, 1f) + isAlarmPlaying = true + } + } + + LocaleConstant.STOP_WARNING_CODE -> { + if (isAlarmPlaying) { + soundPool.autoPause() + isAlarmPlaying = false + } + } } - val notification = notificationBuilder?.build() - notificationManager.notify(notificationId, notification) return true } override fun onDestroy() { super.onDestroy() + soundPool.release() + soundPool = null stopForeground(STOP_FOREGROUND_REMOVE) } diff --git a/app/src/main/java/com/casic/br/operationsite/service/SocketConnectionService.kt b/app/src/main/java/com/casic/br/operationsite/service/SocketConnectionService.kt index 5a294d9..fd94f61 100644 --- a/app/src/main/java/com/casic/br/operationsite/service/SocketConnectionService.kt +++ b/app/src/main/java/com/casic/br/operationsite/service/SocketConnectionService.kt @@ -164,18 +164,18 @@ } override fun onConnected() { - ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage(2024110501) + ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage(LocaleConstant.SOCKET_CONNECTED_CODE) //定时查询甲烷浓度 weakReferenceHandler?.post(methaneRunnable) } override fun onDisconnected() { - ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage(2024110502) + ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage(LocaleConstant.SOCKET_DISCONNECTED_CODE) weakReferenceHandler?.removeCallbacks(methaneRunnable) } override fun onConnectFailed() { - ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage(2024110503) + ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage(LocaleConstant.SOCKET_CONNECT_ERROR_CODE) weakReferenceHandler?.removeCallbacks(methaneRunnable) } @@ -260,6 +260,18 @@ //甲烷浓度 val dataArray = bytes.copyOfRange(7, bytes.size) val concentration = dataArray.handleGasConcentration() + val threshold = SaveKeyValues.getValue( + LocaleConstant.METHANE_DEFAULT_VALUE_KEY, 2.00f + ) as Float + if (concentration.toFloat() >= threshold) { + ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage( + LocaleConstant.START_WARNING_CODE + ) + } else { + ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage( + LocaleConstant.STOP_WARNING_CODE + ) + } val weakReferenceHandler = MethaneMonitorFragment.weakReferenceHandler ?: return val message = weakReferenceHandler.obtainMessage() message.what = LocaleConstant.QUERY_METHANE_RESPONSE_CODE diff --git a/app/src/main/java/com/casic/br/operationsite/service/ForegroundRunningService.kt b/app/src/main/java/com/casic/br/operationsite/service/ForegroundRunningService.kt index 9fd5e00..1a27231 100644 --- a/app/src/main/java/com/casic/br/operationsite/service/ForegroundRunningService.kt +++ b/app/src/main/java/com/casic/br/operationsite/service/ForegroundRunningService.kt @@ -4,11 +4,14 @@ import android.app.NotificationManager import android.app.Service import android.content.Intent +import android.media.AudioAttributes +import android.media.SoundPool import android.os.Handler import android.os.IBinder import android.os.Message import androidx.core.app.NotificationCompat import com.casic.br.operationsite.R +import com.casic.br.operationsite.utils.LocaleConstant import com.pengxh.kt.lite.utils.WeakReferenceHandler class ForegroundRunningService : Service(), Handler.Callback { @@ -17,10 +20,17 @@ var weakReferenceHandler: WeakReferenceHandler? = null } - private val kTag = "ForegroundRunningService" private val notificationId = 1 private val notificationManager by lazy { getSystemService(NOTIFICATION_SERVICE) as NotificationManager } private var notificationBuilder: NotificationCompat.Builder? = null + private val attr = AudioAttributes.Builder() + .setUsage(AudioAttributes.USAGE_ALARM) + .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) + .build() + private var soundPool = SoundPool.Builder().setMaxStreams(1).setAudioAttributes(attr).build() + private var soundResourceId = 0 + private var isSoundLoaded = false + private var isAlarmPlaying = false override fun onCreate() { super.onCreate() @@ -40,6 +50,13 @@ val notification = notificationBuilder?.build() startForeground(notificationId, notification) + + soundPool.setOnLoadCompleteListener { soundPool, sampleId, status -> + if (status == 0) { + isSoundLoaded = true + } + } + soundResourceId = soundPool.load(this, R.raw.ring, 1) } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { @@ -51,17 +68,45 @@ override fun handleMessage(msg: Message): Boolean { when (msg.what) { - 2024110501 -> notificationBuilder?.setContentTitle("通讯服务已连接") - 2024110502 -> notificationBuilder?.setContentTitle("通讯服务断开连接") - 2024110503 -> notificationBuilder?.setContentTitle("通讯服务连接出错,开始重连") + LocaleConstant.SOCKET_CONNECTED_CODE -> { + notificationBuilder?.setContentTitle("通讯服务已连接") + val notification = notificationBuilder?.build() + notificationManager.notify(notificationId, notification) + } + + LocaleConstant.SOCKET_DISCONNECTED_CODE -> { + notificationBuilder?.setContentTitle("通讯服务断开连接") + val notification = notificationBuilder?.build() + notificationManager.notify(notificationId, notification) + } + + LocaleConstant.SOCKET_CONNECT_ERROR_CODE -> { + notificationBuilder?.setContentTitle("通讯服务连接出错,开始重连") + val notification = notificationBuilder?.build() + notificationManager.notify(notificationId, notification) + } + + LocaleConstant.START_WARNING_CODE -> { + if (isSoundLoaded && !isAlarmPlaying) { + soundPool.play(soundResourceId, 1f, 1f, 1, -1, 1f) + isAlarmPlaying = true + } + } + + LocaleConstant.STOP_WARNING_CODE -> { + if (isAlarmPlaying) { + soundPool.autoPause() + isAlarmPlaying = false + } + } } - val notification = notificationBuilder?.build() - notificationManager.notify(notificationId, notification) return true } override fun onDestroy() { super.onDestroy() + soundPool.release() + soundPool = null stopForeground(STOP_FOREGROUND_REMOVE) } diff --git a/app/src/main/java/com/casic/br/operationsite/service/SocketConnectionService.kt b/app/src/main/java/com/casic/br/operationsite/service/SocketConnectionService.kt index 5a294d9..fd94f61 100644 --- a/app/src/main/java/com/casic/br/operationsite/service/SocketConnectionService.kt +++ b/app/src/main/java/com/casic/br/operationsite/service/SocketConnectionService.kt @@ -164,18 +164,18 @@ } override fun onConnected() { - ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage(2024110501) + ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage(LocaleConstant.SOCKET_CONNECTED_CODE) //定时查询甲烷浓度 weakReferenceHandler?.post(methaneRunnable) } override fun onDisconnected() { - ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage(2024110502) + ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage(LocaleConstant.SOCKET_DISCONNECTED_CODE) weakReferenceHandler?.removeCallbacks(methaneRunnable) } override fun onConnectFailed() { - ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage(2024110503) + ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage(LocaleConstant.SOCKET_CONNECT_ERROR_CODE) weakReferenceHandler?.removeCallbacks(methaneRunnable) } @@ -260,6 +260,18 @@ //甲烷浓度 val dataArray = bytes.copyOfRange(7, bytes.size) val concentration = dataArray.handleGasConcentration() + val threshold = SaveKeyValues.getValue( + LocaleConstant.METHANE_DEFAULT_VALUE_KEY, 2.00f + ) as Float + if (concentration.toFloat() >= threshold) { + ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage( + LocaleConstant.START_WARNING_CODE + ) + } else { + ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage( + LocaleConstant.STOP_WARNING_CODE + ) + } val weakReferenceHandler = MethaneMonitorFragment.weakReferenceHandler ?: return val message = weakReferenceHandler.obtainMessage() message.what = LocaleConstant.QUERY_METHANE_RESPONSE_CODE diff --git a/app/src/main/java/com/casic/br/operationsite/utils/LocaleConstant.kt b/app/src/main/java/com/casic/br/operationsite/utils/LocaleConstant.kt index 78e3ace..340529a 100644 --- a/app/src/main/java/com/casic/br/operationsite/utils/LocaleConstant.kt +++ b/app/src/main/java/com/casic/br/operationsite/utils/LocaleConstant.kt @@ -65,6 +65,10 @@ const val PLAY_RTSP_CODE = 20241001 + const val SOCKET_CONNECTED_CODE = 2024110501 + const val SOCKET_DISCONNECTED_CODE = 2024110502 + const val SOCKET_CONNECT_ERROR_CODE = 2024110503 + /** * Handler Request Code * */ @@ -112,6 +116,9 @@ const val SET_BOARD_WIFI_CONFIG_RESPONSE_CODE = 20241014 const val SET_BOARD_SERVER_CONFIG_RESPONSE_CODE = 20241015 + const val START_WARNING_CODE = 2025052801 + const val STOP_WARNING_CODE = 2025052802 + /*** * SP Key * */ diff --git a/app/src/main/java/com/casic/br/operationsite/service/ForegroundRunningService.kt b/app/src/main/java/com/casic/br/operationsite/service/ForegroundRunningService.kt index 9fd5e00..1a27231 100644 --- a/app/src/main/java/com/casic/br/operationsite/service/ForegroundRunningService.kt +++ b/app/src/main/java/com/casic/br/operationsite/service/ForegroundRunningService.kt @@ -4,11 +4,14 @@ import android.app.NotificationManager import android.app.Service import android.content.Intent +import android.media.AudioAttributes +import android.media.SoundPool import android.os.Handler import android.os.IBinder import android.os.Message import androidx.core.app.NotificationCompat import com.casic.br.operationsite.R +import com.casic.br.operationsite.utils.LocaleConstant import com.pengxh.kt.lite.utils.WeakReferenceHandler class ForegroundRunningService : Service(), Handler.Callback { @@ -17,10 +20,17 @@ var weakReferenceHandler: WeakReferenceHandler? = null } - private val kTag = "ForegroundRunningService" private val notificationId = 1 private val notificationManager by lazy { getSystemService(NOTIFICATION_SERVICE) as NotificationManager } private var notificationBuilder: NotificationCompat.Builder? = null + private val attr = AudioAttributes.Builder() + .setUsage(AudioAttributes.USAGE_ALARM) + .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) + .build() + private var soundPool = SoundPool.Builder().setMaxStreams(1).setAudioAttributes(attr).build() + private var soundResourceId = 0 + private var isSoundLoaded = false + private var isAlarmPlaying = false override fun onCreate() { super.onCreate() @@ -40,6 +50,13 @@ val notification = notificationBuilder?.build() startForeground(notificationId, notification) + + soundPool.setOnLoadCompleteListener { soundPool, sampleId, status -> + if (status == 0) { + isSoundLoaded = true + } + } + soundResourceId = soundPool.load(this, R.raw.ring, 1) } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { @@ -51,17 +68,45 @@ override fun handleMessage(msg: Message): Boolean { when (msg.what) { - 2024110501 -> notificationBuilder?.setContentTitle("通讯服务已连接") - 2024110502 -> notificationBuilder?.setContentTitle("通讯服务断开连接") - 2024110503 -> notificationBuilder?.setContentTitle("通讯服务连接出错,开始重连") + LocaleConstant.SOCKET_CONNECTED_CODE -> { + notificationBuilder?.setContentTitle("通讯服务已连接") + val notification = notificationBuilder?.build() + notificationManager.notify(notificationId, notification) + } + + LocaleConstant.SOCKET_DISCONNECTED_CODE -> { + notificationBuilder?.setContentTitle("通讯服务断开连接") + val notification = notificationBuilder?.build() + notificationManager.notify(notificationId, notification) + } + + LocaleConstant.SOCKET_CONNECT_ERROR_CODE -> { + notificationBuilder?.setContentTitle("通讯服务连接出错,开始重连") + val notification = notificationBuilder?.build() + notificationManager.notify(notificationId, notification) + } + + LocaleConstant.START_WARNING_CODE -> { + if (isSoundLoaded && !isAlarmPlaying) { + soundPool.play(soundResourceId, 1f, 1f, 1, -1, 1f) + isAlarmPlaying = true + } + } + + LocaleConstant.STOP_WARNING_CODE -> { + if (isAlarmPlaying) { + soundPool.autoPause() + isAlarmPlaying = false + } + } } - val notification = notificationBuilder?.build() - notificationManager.notify(notificationId, notification) return true } override fun onDestroy() { super.onDestroy() + soundPool.release() + soundPool = null stopForeground(STOP_FOREGROUND_REMOVE) } diff --git a/app/src/main/java/com/casic/br/operationsite/service/SocketConnectionService.kt b/app/src/main/java/com/casic/br/operationsite/service/SocketConnectionService.kt index 5a294d9..fd94f61 100644 --- a/app/src/main/java/com/casic/br/operationsite/service/SocketConnectionService.kt +++ b/app/src/main/java/com/casic/br/operationsite/service/SocketConnectionService.kt @@ -164,18 +164,18 @@ } override fun onConnected() { - ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage(2024110501) + ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage(LocaleConstant.SOCKET_CONNECTED_CODE) //定时查询甲烷浓度 weakReferenceHandler?.post(methaneRunnable) } override fun onDisconnected() { - ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage(2024110502) + ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage(LocaleConstant.SOCKET_DISCONNECTED_CODE) weakReferenceHandler?.removeCallbacks(methaneRunnable) } override fun onConnectFailed() { - ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage(2024110503) + ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage(LocaleConstant.SOCKET_CONNECT_ERROR_CODE) weakReferenceHandler?.removeCallbacks(methaneRunnable) } @@ -260,6 +260,18 @@ //甲烷浓度 val dataArray = bytes.copyOfRange(7, bytes.size) val concentration = dataArray.handleGasConcentration() + val threshold = SaveKeyValues.getValue( + LocaleConstant.METHANE_DEFAULT_VALUE_KEY, 2.00f + ) as Float + if (concentration.toFloat() >= threshold) { + ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage( + LocaleConstant.START_WARNING_CODE + ) + } else { + ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage( + LocaleConstant.STOP_WARNING_CODE + ) + } val weakReferenceHandler = MethaneMonitorFragment.weakReferenceHandler ?: return val message = weakReferenceHandler.obtainMessage() message.what = LocaleConstant.QUERY_METHANE_RESPONSE_CODE diff --git a/app/src/main/java/com/casic/br/operationsite/utils/LocaleConstant.kt b/app/src/main/java/com/casic/br/operationsite/utils/LocaleConstant.kt index 78e3ace..340529a 100644 --- a/app/src/main/java/com/casic/br/operationsite/utils/LocaleConstant.kt +++ b/app/src/main/java/com/casic/br/operationsite/utils/LocaleConstant.kt @@ -65,6 +65,10 @@ const val PLAY_RTSP_CODE = 20241001 + const val SOCKET_CONNECTED_CODE = 2024110501 + const val SOCKET_DISCONNECTED_CODE = 2024110502 + const val SOCKET_CONNECT_ERROR_CODE = 2024110503 + /** * Handler Request Code * */ @@ -112,6 +116,9 @@ const val SET_BOARD_WIFI_CONFIG_RESPONSE_CODE = 20241014 const val SET_BOARD_SERVER_CONFIG_RESPONSE_CODE = 20241015 + const val START_WARNING_CODE = 2025052801 + const val STOP_WARNING_CODE = 2025052802 + /*** * SP Key * */ diff --git a/app/src/main/java/com/casic/br/operationsite/view/DeviceControlActivity.kt b/app/src/main/java/com/casic/br/operationsite/view/DeviceControlActivity.kt index d7fd789..06eaebe 100644 --- a/app/src/main/java/com/casic/br/operationsite/view/DeviceControlActivity.kt +++ b/app/src/main/java/com/casic/br/operationsite/view/DeviceControlActivity.kt @@ -34,7 +34,6 @@ } private val fragmentPages by lazy { ArrayList() } - private val socketService by lazy { Intent(this, SocketConnectionService::class.java) } private val pageTitles = arrayOf("相机控制", "激光监测", "语音设置", "基础配置") private var isRtspPlaying = false @@ -68,8 +67,12 @@ override fun initOnCreate(savedInstanceState: Bundle?) { ActivityStackManager.addActivity(this) - startService(socketService) - startService(Intent(this, ForegroundRunningService::class.java)) + Intent(this, SocketConnectionService::class.java).apply { + startService(this) + } + Intent(this, ForegroundRunningService::class.java).apply { + startService(this) + } weakReferenceHandler = WeakReferenceHandler(this) binding.viewPager.adapter = TabPageViewAdapter( diff --git a/app/src/main/java/com/casic/br/operationsite/service/ForegroundRunningService.kt b/app/src/main/java/com/casic/br/operationsite/service/ForegroundRunningService.kt index 9fd5e00..1a27231 100644 --- a/app/src/main/java/com/casic/br/operationsite/service/ForegroundRunningService.kt +++ b/app/src/main/java/com/casic/br/operationsite/service/ForegroundRunningService.kt @@ -4,11 +4,14 @@ import android.app.NotificationManager import android.app.Service import android.content.Intent +import android.media.AudioAttributes +import android.media.SoundPool import android.os.Handler import android.os.IBinder import android.os.Message import androidx.core.app.NotificationCompat import com.casic.br.operationsite.R +import com.casic.br.operationsite.utils.LocaleConstant import com.pengxh.kt.lite.utils.WeakReferenceHandler class ForegroundRunningService : Service(), Handler.Callback { @@ -17,10 +20,17 @@ var weakReferenceHandler: WeakReferenceHandler? = null } - private val kTag = "ForegroundRunningService" private val notificationId = 1 private val notificationManager by lazy { getSystemService(NOTIFICATION_SERVICE) as NotificationManager } private var notificationBuilder: NotificationCompat.Builder? = null + private val attr = AudioAttributes.Builder() + .setUsage(AudioAttributes.USAGE_ALARM) + .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) + .build() + private var soundPool = SoundPool.Builder().setMaxStreams(1).setAudioAttributes(attr).build() + private var soundResourceId = 0 + private var isSoundLoaded = false + private var isAlarmPlaying = false override fun onCreate() { super.onCreate() @@ -40,6 +50,13 @@ val notification = notificationBuilder?.build() startForeground(notificationId, notification) + + soundPool.setOnLoadCompleteListener { soundPool, sampleId, status -> + if (status == 0) { + isSoundLoaded = true + } + } + soundResourceId = soundPool.load(this, R.raw.ring, 1) } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { @@ -51,17 +68,45 @@ override fun handleMessage(msg: Message): Boolean { when (msg.what) { - 2024110501 -> notificationBuilder?.setContentTitle("通讯服务已连接") - 2024110502 -> notificationBuilder?.setContentTitle("通讯服务断开连接") - 2024110503 -> notificationBuilder?.setContentTitle("通讯服务连接出错,开始重连") + LocaleConstant.SOCKET_CONNECTED_CODE -> { + notificationBuilder?.setContentTitle("通讯服务已连接") + val notification = notificationBuilder?.build() + notificationManager.notify(notificationId, notification) + } + + LocaleConstant.SOCKET_DISCONNECTED_CODE -> { + notificationBuilder?.setContentTitle("通讯服务断开连接") + val notification = notificationBuilder?.build() + notificationManager.notify(notificationId, notification) + } + + LocaleConstant.SOCKET_CONNECT_ERROR_CODE -> { + notificationBuilder?.setContentTitle("通讯服务连接出错,开始重连") + val notification = notificationBuilder?.build() + notificationManager.notify(notificationId, notification) + } + + LocaleConstant.START_WARNING_CODE -> { + if (isSoundLoaded && !isAlarmPlaying) { + soundPool.play(soundResourceId, 1f, 1f, 1, -1, 1f) + isAlarmPlaying = true + } + } + + LocaleConstant.STOP_WARNING_CODE -> { + if (isAlarmPlaying) { + soundPool.autoPause() + isAlarmPlaying = false + } + } } - val notification = notificationBuilder?.build() - notificationManager.notify(notificationId, notification) return true } override fun onDestroy() { super.onDestroy() + soundPool.release() + soundPool = null stopForeground(STOP_FOREGROUND_REMOVE) } diff --git a/app/src/main/java/com/casic/br/operationsite/service/SocketConnectionService.kt b/app/src/main/java/com/casic/br/operationsite/service/SocketConnectionService.kt index 5a294d9..fd94f61 100644 --- a/app/src/main/java/com/casic/br/operationsite/service/SocketConnectionService.kt +++ b/app/src/main/java/com/casic/br/operationsite/service/SocketConnectionService.kt @@ -164,18 +164,18 @@ } override fun onConnected() { - ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage(2024110501) + ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage(LocaleConstant.SOCKET_CONNECTED_CODE) //定时查询甲烷浓度 weakReferenceHandler?.post(methaneRunnable) } override fun onDisconnected() { - ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage(2024110502) + ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage(LocaleConstant.SOCKET_DISCONNECTED_CODE) weakReferenceHandler?.removeCallbacks(methaneRunnable) } override fun onConnectFailed() { - ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage(2024110503) + ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage(LocaleConstant.SOCKET_CONNECT_ERROR_CODE) weakReferenceHandler?.removeCallbacks(methaneRunnable) } @@ -260,6 +260,18 @@ //甲烷浓度 val dataArray = bytes.copyOfRange(7, bytes.size) val concentration = dataArray.handleGasConcentration() + val threshold = SaveKeyValues.getValue( + LocaleConstant.METHANE_DEFAULT_VALUE_KEY, 2.00f + ) as Float + if (concentration.toFloat() >= threshold) { + ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage( + LocaleConstant.START_WARNING_CODE + ) + } else { + ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage( + LocaleConstant.STOP_WARNING_CODE + ) + } val weakReferenceHandler = MethaneMonitorFragment.weakReferenceHandler ?: return val message = weakReferenceHandler.obtainMessage() message.what = LocaleConstant.QUERY_METHANE_RESPONSE_CODE diff --git a/app/src/main/java/com/casic/br/operationsite/utils/LocaleConstant.kt b/app/src/main/java/com/casic/br/operationsite/utils/LocaleConstant.kt index 78e3ace..340529a 100644 --- a/app/src/main/java/com/casic/br/operationsite/utils/LocaleConstant.kt +++ b/app/src/main/java/com/casic/br/operationsite/utils/LocaleConstant.kt @@ -65,6 +65,10 @@ const val PLAY_RTSP_CODE = 20241001 + const val SOCKET_CONNECTED_CODE = 2024110501 + const val SOCKET_DISCONNECTED_CODE = 2024110502 + const val SOCKET_CONNECT_ERROR_CODE = 2024110503 + /** * Handler Request Code * */ @@ -112,6 +116,9 @@ const val SET_BOARD_WIFI_CONFIG_RESPONSE_CODE = 20241014 const val SET_BOARD_SERVER_CONFIG_RESPONSE_CODE = 20241015 + const val START_WARNING_CODE = 2025052801 + const val STOP_WARNING_CODE = 2025052802 + /*** * SP Key * */ diff --git a/app/src/main/java/com/casic/br/operationsite/view/DeviceControlActivity.kt b/app/src/main/java/com/casic/br/operationsite/view/DeviceControlActivity.kt index d7fd789..06eaebe 100644 --- a/app/src/main/java/com/casic/br/operationsite/view/DeviceControlActivity.kt +++ b/app/src/main/java/com/casic/br/operationsite/view/DeviceControlActivity.kt @@ -34,7 +34,6 @@ } private val fragmentPages by lazy { ArrayList() } - private val socketService by lazy { Intent(this, SocketConnectionService::class.java) } private val pageTitles = arrayOf("相机控制", "激光监测", "语音设置", "基础配置") private var isRtspPlaying = false @@ -68,8 +67,12 @@ override fun initOnCreate(savedInstanceState: Bundle?) { ActivityStackManager.addActivity(this) - startService(socketService) - startService(Intent(this, ForegroundRunningService::class.java)) + Intent(this, SocketConnectionService::class.java).apply { + startService(this) + } + Intent(this, ForegroundRunningService::class.java).apply { + startService(this) + } weakReferenceHandler = WeakReferenceHandler(this) binding.viewPager.adapter = TabPageViewAdapter( diff --git a/app/src/main/res/raw/ring.wav b/app/src/main/res/raw/ring.wav new file mode 100644 index 0000000..3ecfb99 --- /dev/null +++ b/app/src/main/res/raw/ring.wav Binary files differ