desc:设置报警阈值

main
xiaowusky 2 years ago
parent cb2805e8a0
commit 2ea7836a55

@ -0,0 +1,70 @@
package com.yinuo.safetywatcher.watcher.constant
import com.common.commonlib.utils.MMKVUtils
const val CO_MIN = 0f
const val CO_MAX = 0f
const val O2_MIN = 0f
const val O2_MAX = 0f
const val CH4_MIN = 0f
const val CH4_MAX = 0f
const val H2S_MIN = 0f
const val H2S_MAX = 0f
val default_map = hashMapOf<String, Float>(
Pair("CO_min", CO_MIN),
Pair("CO_max", CO_MAX),
Pair("O2_min", O2_MIN),
Pair("O2_max", O2_MAX),
Pair("CH4_min", CH4_MIN),
Pair("CH4_max", CH4_MAX),
Pair("H2S_min", H2S_MIN),
Pair("H2S_max", H2S_MAX)
)
fun getLowThresHoldKey(gasType: String): String {
return "${gasType.uppercase()}_min"
}
fun getHighThresHoldKey(gasType: String): String {
return "${gasType.uppercase()}_max"
}
fun saveLowThreshold(gasType: String, value: Float){
MMKVUtils.put(getLowThresHoldKey(gasType), value)
}
fun saveHighThreshold(gasType: String, value: Float){
MMKVUtils.put(getHighThresHoldKey(gasType), value)
}
fun getGasLowThreshold(gasType: String): Float {
val lowThresholdKey = getLowThresHoldKey(gasType)
val float = MMKVUtils.getFloat(lowThresholdKey)
if (float <= 0) {
return default_map[lowThresholdKey] ?: -1f
}
return float
}
fun getGasHighThreshold(gasType: String): Float {
val highThresholdKey = getHighThresHoldKey(gasType)
val float = MMKVUtils.getFloat(highThresholdKey)
if (float <= 0) {
return default_map[highThresholdKey] ?: -1f
}
return float
}
fun getGasUnit(gasType: String): String {
return when (gasType.uppercase()) {
"CO" -> "ppm"
"CH4" -> "%VOL"
"O2" -> "%LEL"
"H2S" -> "ppm"
else -> ""
}
}

@ -11,6 +11,7 @@ import com.yinuo.safetywatcher.databinding.ActivitySensorSettingBinding
import com.yinuo.safetywatcher.watcher.base.NoOptionsActivity
import com.yinuo.safetywatcher.watcher.utils.hideIme
import com.yinuo.safetywatcher.watcher.utils.showIme
import com.yinuo.safetywatcher.watcher.utils.showToast
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
@ -27,8 +28,10 @@ class SensorSettingActivity : NoOptionsActivity() {
return mBinding.root
}
var gasName = ""
override fun initView() {
val gasName = intent.getStringExtra("GasType")!!
gasName = intent.getStringExtra("GasType")!!
val range = MMKVUtils.getString("range_${gasName}")
lifecycleScope.launch {
val typeDao = DBUtils.gasTypeDao()
@ -40,12 +43,12 @@ class SensorSettingActivity : NoOptionsActivity() {
}
mBinding.tvWarnSetting.setOnClickListener {
startActivity(
Intent(
this@SensorSettingActivity,
SensorThresholdSetActivity::class.java
)
val intent = Intent(
this@SensorSettingActivity,
SensorThresholdSetActivity::class.java
)
intent.putExtra("GasType", gasName)
startActivity(intent)
}
mBinding.etName.setOnKeyListener { _, keyCode, event ->
@ -86,6 +89,7 @@ class SensorSettingActivity : NoOptionsActivity() {
}
}
}
showToast("保存成功")
}
}
}

@ -1,14 +1,22 @@
package com.yinuo.safetywatcher.watcher.ui
import android.view.View
import android.widget.Toast
import com.yinuo.safetywatcher.R
import com.yinuo.safetywatcher.databinding.ActivitySensorThresholdBinding
import com.yinuo.safetywatcher.watcher.base.NoOptionsActivity
import com.yinuo.safetywatcher.watcher.constant.getGasHighThreshold
import com.yinuo.safetywatcher.watcher.constant.getGasLowThreshold
import com.yinuo.safetywatcher.watcher.constant.getGasUnit
import com.yinuo.safetywatcher.watcher.constant.saveHighThreshold
import com.yinuo.safetywatcher.watcher.constant.saveLowThreshold
import com.yinuo.safetywatcher.watcher.utils.showToast
class SensorThresholdSetActivity : NoOptionsActivity() {
private val mBinding by lazy {
ActivitySensorThresholdBinding.inflate(layoutInflater)
}
var gasName = ""
override fun getTopBarTitle(): String? {
return getString(R.string.sensor_threshold_setting)
@ -19,5 +27,41 @@ class SensorThresholdSetActivity : NoOptionsActivity() {
}
override fun initView() {
gasName = intent.getStringExtra("GasType")!!
val gasUnit = getGasUnit(gasName)
mBinding.tvMin.append("$gasUnit)")
mBinding.tvMax.append("$gasUnit)")
val gasLowThreshold = getGasLowThreshold(gasName)
val gasHighThreshold = getGasHighThreshold(gasName)
if (gasLowThreshold > 0) {
mBinding.etMin.setText(gasLowThreshold.toString())
}
if (gasHighThreshold > 0) {
mBinding.etMax.setText(gasHighThreshold.toString())
}
mBinding.tvSave.setOnClickListener {
saveData()
showToast("保存成功")
}
}
private fun saveData() {
try {
val min = mBinding.etMin.text.toString()
if (min.isNotEmpty()) {
saveLowThreshold(gasName, min.toFloat())
}
val max = mBinding.etMax.text.toString()
if (max.isNotEmpty()) {
saveHighThreshold(gasName, max.toFloat())
}
} catch (e: Exception) {
showToast("只允许输入数值")
}
}
}

@ -79,7 +79,7 @@
<TextView
android:id="@+id/tv_save"
android:layout_width="@dimen/_160dp"
android:layout_width="@dimen/_380dp"
android:layout_height="@dimen/_80dp"
android:textColor="@color/white"
android:gravity="center"
@ -88,7 +88,7 @@
android:textSize="@dimen/_30dp"
android:background="@drawable/save_btn_bg"/>
<TextView
<!--<TextView
android:id="@+id/tv_sensor_init"
android:layout_width="@dimen/_260dp"
android:layout_height="@dimen/_80dp"
@ -98,6 +98,6 @@
android:textSize="@dimen/_30dp"
android:focusable="true"
android:text="@string/sensor_init_txt"
android:background="@drawable/sensor_init_btn_bg"/>
android:background="@drawable/sensor_init_btn_bg"/>-->
</LinearLayout>
</LinearLayout>

@ -1,7 +1,57 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingStart="@dimen/_121dp"
android:paddingTop="@dimen/_61dp">
<TextView
android:id="@+id/tv_min"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/low_threshold"
android:textColor="@color/white"
android:textSize="@dimen/_30dp" />
<EditText
android:id="@+id/et_min"
android:layout_width="@dimen/_600dp"
android:layout_height="@dimen/_100dp"
android:layout_marginTop="@dimen/_20dp"
android:background="@drawable/cloud_sync_btn_bg"
android:inputType="number"
android:textColor="@color/white"
android:textSize="@dimen/_30dp" />
<TextView
android:id="@+id/tv_max"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/_40dp"
android:text="@string/high_threshold"
android:textColor="@color/white"
android:textSize="@dimen/_30dp" />
<EditText
android:id="@+id/et_max"
android:layout_width="@dimen/_600dp"
android:layout_height="@dimen/_100dp"
android:layout_marginTop="@dimen/_20dp"
android:background="@drawable/cloud_sync_btn_bg"
android:inputType="number"
android:textColor="@color/white"
android:textSize="@dimen/_30dp" />
<TextView
android:id="@+id/tv_save"
android:layout_width="@dimen/_160dp"
android:layout_height="@dimen/_80dp"
android:layout_marginTop="@dimen/_40dp"
android:background="@drawable/save_btn_bg"
android:focusable="true"
android:gravity="center"
android:text="@string/save"
android:textColor="@color/white"
android:textSize="@dimen/_30dp" />
</LinearLayout>

@ -64,4 +64,6 @@
<string name="repeat_click_sync_tip">正在同步中,请勿重复点击</string>
<string name="confirm_clear_data_tip">确定恢复出厂设置?</string>
<string name="low_threshold">低报警值(单位:</string>
<string name="high_threshold">高报警值(单位:</string>
</resources>

Loading…
Cancel
Save