desc:气体浓度达到高低报警值时需要和声光报警器联动报警;且高报和低报报警频率区分。

main
xiaowusky 1 year ago
parent a4b059762c
commit ebda6d1a5b

@ -231,7 +231,7 @@ abstract class BaseActivity : AppCompatActivity() {
var isLeftRightLongPress = false
override fun dispatchKeyEvent(event: KeyEvent): Boolean {
LogUtils.w("cyy dispatchKeyEvent keyCode = ${event?.keyCode} count = ${event?.repeatCount} acton = ${event?.action}")
// LogUtils.w("cyy dispatchKeyEvent keyCode = ${event?.keyCode} count = ${event?.repeatCount} acton = ${event?.action}")
val action = event.action
if (KeyEvent.ACTION_DOWN == action) {
return dealActionDown(event)

@ -249,6 +249,10 @@ fun getShowWarnTxt(warn: Warning?): String {
return ""
}
fun isOverHighThreshold(warnText:String):Boolean{
return warnText.contains("超过传感器量程") || warnText.contains("醉氧") || warnText.contains("超过高报值")
}
fun saveSensorNickName(gasType: String, name: String) {
if (name.isNotEmpty()) {
MMKVUtils.put("${gasType}_sensor_nickname", name)

@ -21,6 +21,7 @@ import com.yinuo.safetywatcher.watcher.constant.DELAY_TIME_OPEN_CAMERA
import com.yinuo.safetywatcher.watcher.port.GasPortUtils
import com.yinuo.safetywatcher.watcher.port.ParseHelper
import com.yinuo.safetywatcher.watcher.port.getShowWarnTxt
import com.yinuo.safetywatcher.watcher.port.isOverHighThreshold
import com.yinuo.safetywatcher.watcher.services.HeartbeatService
import com.yinuo.safetywatcher.watcher.ui.view.ConfirmDialog
import com.yinuo.safetywatcher.watcher.utils.BatteryHelper
@ -28,6 +29,7 @@ import com.yinuo.safetywatcher.watcher.utils.GPIOUtils
import com.yinuo.safetywatcher.watcher.utils.LztekUtil
import com.yinuo.safetywatcher.watcher.utils.RecordHelper
import com.yinuo.safetywatcher.watcher.utils.SimHelper
import com.yinuo.safetywatcher.watcher.utils.SoundUtils
import com.yinuo.safetywatcher.watcher.utils.SpeedUtils
import com.yinuo.safetywatcher.watcher.utils.WifiHelper
import kotlinx.coroutines.Dispatchers
@ -258,13 +260,16 @@ class HomeActivity : NoOptionsActivity() {
}
launch(Dispatchers.Main) {
lifecycleScope.launchWhenResumed {
if (builder.toString().isNotEmpty()) {
mBinding.tvWarn.text = builder.toString()
val warnText = builder.toString()
if (warnText.isNotEmpty()) {
mBinding.tvWarn.text = warnText
mBinding.tvWarn.visibility = View.VISIBLE
LztekUtil.openLinkI0()
SoundUtils.warnSound(isOverHighThreshold(warnText))
} else {
mBinding.tvWarn.visibility = View.GONE
LztekUtil.closeLinkIO()
SoundUtils.warnSoundClose()
}
}
}

@ -1,60 +1,94 @@
package com.yinuo.safetywatcher.watcher.utils
import android.content.Context
import android.media.AudioAttributes
import android.media.SoundPool
import android.os.Handler
import android.os.HandlerThread
import com.common.commonlib.utils.LogUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
object SoundUtils {
private val soundPool by lazy {
// 创建 SoundPool
val audioAttributes = AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build()
SoundPool.Builder()
.setAudioAttributes(audioAttributes)
.setMaxStreams(1) // 指定最大音效流的数量
.build()
}
private const val TYPE_SOS = 0
private var soundId = -1
private const val TYPE_WARN_HIGHT = 1
private const val TYPE_WARN_LOW = 2
private val soundMap = hashMapOf<Int, Boolean>(
TYPE_SOS to false,
TYPE_WARN_LOW to false,
TYPE_WARN_HIGHT to false
)
private var soundPlaying = false
private var currentPlayingDelay = 500
private var soundThread: HandlerThread? = null
private var soundHandler: Handler? = null
var gpioValue = 0
fun init(context: Context, resID: Int) {
soundId = soundPool.load(context, resID, 1); // 替换为你的音效资源文件
init {
GPIOUtils.setGpioDirection(140, "out")
soundThread = HandlerThread("sound")
soundThread!!.start()
soundHandler = Handler(soundThread!!.looper)
}
fun playSound() {
// 播放音效
soundPool.play(soundId, 1.0f, 1.0f, 1, 0, 1.0f); // 参数依次为音效ID、左声道音量、右声道音量、优先级、循环次数、播放速度
fun warnSound(isHigh: Boolean) {
playSoundIo(if (isHigh) TYPE_WARN_HIGHT else TYPE_WARN_LOW)
}
var soundPlaying = false
fun warnSoundClose() {
stopSoundIo(TYPE_WARN_HIGHT)
stopSoundIo(TYPE_WARN_LOW)
}
fun playSoundIo() {
if (soundPlaying) {
fun playSoundIo(type: Int = TYPE_SOS) {
if (soundMap[type] == true && soundPlaying) {
return
}
GPIOUtils.setGpioDirection(140, "out")
var gpioValue = 0
soundPlaying = true
GlobalScope.launch(Dispatchers.IO) {
while (soundPlaying) {
gpioValue = if (gpioValue > 0) 0 else 1
LogUtils.w("cyy gpioValue ${gpioValue}")
GPIOUtils.setGpioValue(140, gpioValue)
delay(500)
soundMap[type] = true
val delayTime = getDelayTime(soundMap)
// 不需要变化时,直接返回
if (soundPlaying && delayTime == currentPlayingDelay) {
return
}
if (!soundPlaying) {
currentPlayingDelay = delayTime
soundHandler?.post {
setVoiceIo()
}
}
}
fun stopSoundIo() {
soundPlaying = false
private fun getDelayTime(soundMap: HashMap<Int, Boolean>): Int {
if (soundMap[TYPE_SOS] == true || soundMap[TYPE_WARN_HIGHT] == true) {
return 500
} else if (soundMap[TYPE_WARN_LOW] == true) {
return 700
}
return 0
}
private fun setVoiceIo() {
// 在启动新的
soundPlaying = true
while (soundPlaying) {
gpioValue = if (gpioValue > 0) 0 else 1
LogUtils.w("cyy gpioValue ${gpioValue}, delay = $currentPlayingDelay")
GPIOUtils.setGpioValue(140, gpioValue)
Thread.sleep(currentPlayingDelay.toLong())
checkStatus()
}
GPIOUtils.setGpioValue(140, 0)
}
private fun checkStatus() {
val finalStatus =
(soundMap[TYPE_SOS] == true && soundMap[TYPE_WARN_LOW] == true && soundMap[TYPE_WARN_HIGHT] == true)
if (!finalStatus) {
soundPlaying = false
} else {
val delayTime = getDelayTime(soundMap)
currentPlayingDelay = delayTime
}
}
fun stopSoundIo(type: Int = TYPE_SOS) {
soundMap[type] = false
}
}

@ -74,21 +74,21 @@ object TxtOverlay {
while (true) {
val currentTimeMillis = System.currentTimeMillis()
overlayBuilder.clear()
Log.i("cyy", "gasMap size = ${gasMap.size}")
// Log.i("cyy", "gasMap size = ${gasMap.size}")
gasMap.forEach { item ->
val gas = item.value
// 3S内的数据我们认为有效
Log.i("cyy", "gasTime = ${dateFormat.format(gas.time)}")
// Log.i("cyy", "gasTime = ${dateFormat.format(gas.time)}")
if (currentTimeMillis - gas.time <= 3000) {
overlayBuilder.append("${gas.gasName.getGasShowName()}: ${gas.gasValue.forShowStr()} ${gas.unit}")
.append("@")
}
}
val tipStr = overlayBuilder.toString()
Log.i(
"cyy",
"time = ${dateFormat.format(currentTimeMillis)} mToDoShowTip = $tipStr"
)
// Log.i(
// "cyy",
// "time = ${dateFormat.format(currentTimeMillis)} mToDoShowTip = $tipStr"
// )
buildOverlayBitmap(currentTimeMillis, tipStr)
delay(1000)
}

Loading…
Cancel
Save