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

main
xiaowusky 1 year ago
parent a4b059762c
commit ebda6d1a5b

@ -231,7 +231,7 @@ abstract class BaseActivity : AppCompatActivity() {
var isLeftRightLongPress = false var isLeftRightLongPress = false
override fun dispatchKeyEvent(event: KeyEvent): Boolean { 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 val action = event.action
if (KeyEvent.ACTION_DOWN == action) { if (KeyEvent.ACTION_DOWN == action) {
return dealActionDown(event) return dealActionDown(event)

@ -249,6 +249,10 @@ fun getShowWarnTxt(warn: Warning?): String {
return "" return ""
} }
fun isOverHighThreshold(warnText:String):Boolean{
return warnText.contains("超过传感器量程") || warnText.contains("醉氧") || warnText.contains("超过高报值")
}
fun saveSensorNickName(gasType: String, name: String) { fun saveSensorNickName(gasType: String, name: String) {
if (name.isNotEmpty()) { if (name.isNotEmpty()) {
MMKVUtils.put("${gasType}_sensor_nickname", name) 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.GasPortUtils
import com.yinuo.safetywatcher.watcher.port.ParseHelper import com.yinuo.safetywatcher.watcher.port.ParseHelper
import com.yinuo.safetywatcher.watcher.port.getShowWarnTxt 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.services.HeartbeatService
import com.yinuo.safetywatcher.watcher.ui.view.ConfirmDialog import com.yinuo.safetywatcher.watcher.ui.view.ConfirmDialog
import com.yinuo.safetywatcher.watcher.utils.BatteryHelper 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.LztekUtil
import com.yinuo.safetywatcher.watcher.utils.RecordHelper import com.yinuo.safetywatcher.watcher.utils.RecordHelper
import com.yinuo.safetywatcher.watcher.utils.SimHelper 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.SpeedUtils
import com.yinuo.safetywatcher.watcher.utils.WifiHelper import com.yinuo.safetywatcher.watcher.utils.WifiHelper
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@ -258,13 +260,16 @@ class HomeActivity : NoOptionsActivity() {
} }
launch(Dispatchers.Main) { launch(Dispatchers.Main) {
lifecycleScope.launchWhenResumed { lifecycleScope.launchWhenResumed {
if (builder.toString().isNotEmpty()) { val warnText = builder.toString()
mBinding.tvWarn.text = builder.toString() if (warnText.isNotEmpty()) {
mBinding.tvWarn.text = warnText
mBinding.tvWarn.visibility = View.VISIBLE mBinding.tvWarn.visibility = View.VISIBLE
LztekUtil.openLinkI0() LztekUtil.openLinkI0()
SoundUtils.warnSound(isOverHighThreshold(warnText))
} else { } else {
mBinding.tvWarn.visibility = View.GONE mBinding.tvWarn.visibility = View.GONE
LztekUtil.closeLinkIO() LztekUtil.closeLinkIO()
SoundUtils.warnSoundClose()
} }
} }
} }

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

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

Loading…
Cancel
Save