diff --git a/app/src/main/java/com/yinuo/safetywatcher/watcher/constant/GasConstants.kt b/app/src/main/java/com/yinuo/safetywatcher/watcher/constant/GasConstants.kt new file mode 100644 index 0000000..2269603 --- /dev/null +++ b/app/src/main/java/com/yinuo/safetywatcher/watcher/constant/GasConstants.kt @@ -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( + 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 -> "" + } +} \ No newline at end of file diff --git a/app/src/main/java/com/yinuo/safetywatcher/watcher/ui/SensorSettingActivity.kt b/app/src/main/java/com/yinuo/safetywatcher/watcher/ui/SensorSettingActivity.kt index 15ae1ca..9b40312 100644 --- a/app/src/main/java/com/yinuo/safetywatcher/watcher/ui/SensorSettingActivity.kt +++ b/app/src/main/java/com/yinuo/safetywatcher/watcher/ui/SensorSettingActivity.kt @@ -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("保存成功") } } } \ No newline at end of file diff --git a/app/src/main/java/com/yinuo/safetywatcher/watcher/ui/SensorThresholdSetActivity.kt b/app/src/main/java/com/yinuo/safetywatcher/watcher/ui/SensorThresholdSetActivity.kt index 1381343..b3eeaac 100644 --- a/app/src/main/java/com/yinuo/safetywatcher/watcher/ui/SensorThresholdSetActivity.kt +++ b/app/src/main/java/com/yinuo/safetywatcher/watcher/ui/SensorThresholdSetActivity.kt @@ -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("只允许输入数值") + } } } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_sensor_setting.xml b/app/src/main/res/layout/activity_sensor_setting.xml index 94245e8..1d0be17 100644 --- a/app/src/main/res/layout/activity_sensor_setting.xml +++ b/app/src/main/res/layout/activity_sensor_setting.xml @@ -79,7 +79,7 @@ - + android:background="@drawable/sensor_init_btn_bg"/>--> \ No newline at end of file diff --git a/app/src/main/res/layout/activity_sensor_threshold.xml b/app/src/main/res/layout/activity_sensor_threshold.xml index d829e29..2f38e22 100644 --- a/app/src/main/res/layout/activity_sensor_threshold.xml +++ b/app/src/main/res/layout/activity_sensor_threshold.xml @@ -1,7 +1,57 @@ + android:layout_height="match_parent" + android:orientation="vertical" + android:paddingStart="@dimen/_121dp" + android:paddingTop="@dimen/_61dp"> + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a30fb8d..bf60b4b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -64,4 +64,6 @@ 正在同步中,请勿重复点击 确定恢复出厂设置? + 低报警值(单位: + 高报警值(单位: