diff --git a/app/src/main/java/com/casic/app/safetreecontroller/fragments/MethaneMonitorFragment.kt b/app/src/main/java/com/casic/app/safetreecontroller/fragments/MethaneMonitorFragment.kt index 95d3749..78a350b 100644 --- a/app/src/main/java/com/casic/app/safetreecontroller/fragments/MethaneMonitorFragment.kt +++ b/app/src/main/java/com/casic/app/safetreecontroller/fragments/MethaneMonitorFragment.kt @@ -96,7 +96,7 @@ return@setOnClickListener } - SaveKeyValues.putValue(LocaleConstant.METHANE_DEFAULT_VALUE, threshold) + SaveKeyValues.putValue(LocaleConstant.METHANE_DEFAULT_VALUE_KEY, threshold) val weakReferenceHandler = SocketConnectionService.weakReferenceHandler ?: return@setOnClickListener val message = weakReferenceHandler.obtainMessage() @@ -147,7 +147,7 @@ override fun onResume() { super.onResume() - val checked = SaveKeyValues.getValue(LocaleConstant.OPEN_METHANE, true) as Boolean + val checked = SaveKeyValues.getValue(LocaleConstant.OPEN_METHANE_KEY, true) as Boolean if (checked) { binding.openRadioButton.isChecked = true } else { @@ -155,7 +155,7 @@ } //回显甲烷默认阈值 - val value = SaveKeyValues.getValue(LocaleConstant.METHANE_DEFAULT_VALUE, 1000) as Int + val value = SaveKeyValues.getValue(LocaleConstant.METHANE_DEFAULT_VALUE_KEY, 1000) as Int binding.thresholdView.setText(value.toString()) } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/safetreecontroller/fragments/MethaneMonitorFragment.kt b/app/src/main/java/com/casic/app/safetreecontroller/fragments/MethaneMonitorFragment.kt index 95d3749..78a350b 100644 --- a/app/src/main/java/com/casic/app/safetreecontroller/fragments/MethaneMonitorFragment.kt +++ b/app/src/main/java/com/casic/app/safetreecontroller/fragments/MethaneMonitorFragment.kt @@ -96,7 +96,7 @@ return@setOnClickListener } - SaveKeyValues.putValue(LocaleConstant.METHANE_DEFAULT_VALUE, threshold) + SaveKeyValues.putValue(LocaleConstant.METHANE_DEFAULT_VALUE_KEY, threshold) val weakReferenceHandler = SocketConnectionService.weakReferenceHandler ?: return@setOnClickListener val message = weakReferenceHandler.obtainMessage() @@ -147,7 +147,7 @@ override fun onResume() { super.onResume() - val checked = SaveKeyValues.getValue(LocaleConstant.OPEN_METHANE, true) as Boolean + val checked = SaveKeyValues.getValue(LocaleConstant.OPEN_METHANE_KEY, true) as Boolean if (checked) { binding.openRadioButton.isChecked = true } else { @@ -155,7 +155,7 @@ } //回显甲烷默认阈值 - val value = SaveKeyValues.getValue(LocaleConstant.METHANE_DEFAULT_VALUE, 1000) as Int + val value = SaveKeyValues.getValue(LocaleConstant.METHANE_DEFAULT_VALUE_KEY, 1000) as Int binding.thresholdView.setText(value.toString()) } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/safetreecontroller/fragments/VoiceSettingsFragment.kt b/app/src/main/java/com/casic/app/safetreecontroller/fragments/VoiceSettingsFragment.kt index c1ab44b..6314ed6 100644 --- a/app/src/main/java/com/casic/app/safetreecontroller/fragments/VoiceSettingsFragment.kt +++ b/app/src/main/java/com/casic/app/safetreecontroller/fragments/VoiceSettingsFragment.kt @@ -3,16 +3,30 @@ import android.os.Bundle import android.view.LayoutInflater import android.view.ViewGroup +import androidx.lifecycle.lifecycleScope import com.casic.app.safetreecontroller.R import com.casic.app.safetreecontroller.databinding.FragmentVoiceSettingsBinding +import com.casic.app.safetreecontroller.service.SocketConnectionService import com.casic.app.safetreecontroller.utils.LocaleConstant import com.pengxh.kt.lite.base.KotlinBaseFragment import com.pengxh.kt.lite.utils.SaveKeyValues +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.delay +import kotlinx.coroutines.launch class VoiceSettingsFragment : KotlinBaseFragment() { private val kTag = "VoiceSettingsFragment" + private val valueToChineseMap = mapOf( + 0.0f to "静音", + 1.0f to "一档", + 2.0f to "二档", + 3.0f to "三档", + 4.0f to "四档", + 5.0f to "五档" + ) private var isVisibleToUser = false + private var lastSliderValue = 0f override fun initOnCreate(savedInstanceState: Bundle?) { @@ -39,15 +53,54 @@ override fun initEvent() { binding.volumeSlider.addOnChangeListener { _, value, _ -> - binding.volumeValueView.text = value.toInt().toString() + //判断是增大还是减音量 + if (value > lastSliderValue) { + //还需要判断是一步一步的变大还是突然变大 + if (value - lastSliderValue == 1.0f) { + //一步一步的变大 + SocketConnectionService.weakReferenceHandler?.sendEmptyMessage(LocaleConstant.INCREASE_VOICE_CODE) + } else { + //突然变大。计算需要一步步变大几次 + val changeTimes = (value - lastSliderValue).toInt() + lifecycleScope.launch(Dispatchers.IO) { + for (i in 0 until changeTimes) { + SocketConnectionService.weakReferenceHandler?.sendEmptyMessage( + LocaleConstant.INCREASE_VOICE_CODE + ) + delay(100) + } + } + } + } else { + //还需要判断是一步一步的变小还是突然变小 + if (lastSliderValue - value == 1.0f) { + //一步一步的变小 + SocketConnectionService.weakReferenceHandler?.sendEmptyMessage(LocaleConstant.REDUCE_VOICE_CODE) + } else { + //突然变小。计算需要一步步变小几次 + val changeTimes = (lastSliderValue - value).toInt() + lifecycleScope.launch(Dispatchers.IO) { + for (i in 0 until changeTimes) { + SocketConnectionService.weakReferenceHandler?.sendEmptyMessage( + LocaleConstant.REDUCE_VOICE_CODE + ) + delay(100) + } + } + } + } + binding.volumeValueView.text = valueToChineseMap[value] + //更新值 + lastSliderValue = value + SaveKeyValues.putValue(LocaleConstant.CURRENT_VOICE_VALUE_KEY, value) } binding.modeRadioGroup.setOnCheckedChangeListener { _, checkedId -> if (isVisibleToUser) { if (checkedId == R.id.localeRadioButton) { - SaveKeyValues.putValue(LocaleConstant.LOCALE_MODE, true) + SaveKeyValues.putValue(LocaleConstant.LOCALE_MODE_KEY, true) } else if (checkedId == R.id.remoteRadioButton) { - SaveKeyValues.putValue(LocaleConstant.LOCALE_MODE, false) + SaveKeyValues.putValue(LocaleConstant.LOCALE_MODE_KEY, false) } } } @@ -55,9 +108,9 @@ binding.alarmRadioGroup.setOnCheckedChangeListener { _, checkedId -> if (isVisibleToUser) { if (checkedId == R.id.openAlarmRadioButton) { - SaveKeyValues.putValue(LocaleConstant.OPEN_ALARM, true) + SaveKeyValues.putValue(LocaleConstant.OPEN_ALARM_KEY, true) } else if (checkedId == R.id.closeAlarmRadioButton) { - SaveKeyValues.putValue(LocaleConstant.OPEN_ALARM, false) + SaveKeyValues.putValue(LocaleConstant.OPEN_ALARM_KEY, false) } } } @@ -65,9 +118,9 @@ binding.deviceRadioGroup.setOnCheckedChangeListener { _, checkedId -> if (isVisibleToUser) { if (checkedId == R.id.openVoiceRadioButton) { - SaveKeyValues.putValue(LocaleConstant.OPEN_VOICE, true) + SaveKeyValues.putValue(LocaleConstant.OPEN_VOICE_KEY, true) } else if (checkedId == R.id.closeVoiceRadioButton) { - SaveKeyValues.putValue(LocaleConstant.OPEN_VOICE, false) + SaveKeyValues.putValue(LocaleConstant.OPEN_VOICE_KEY, false) } } } @@ -75,23 +128,28 @@ override fun onResume() { super.onResume() - binding.volumeValueView.text = binding.volumeSlider.value.toInt().toString() + binding.volumeSlider.value = SaveKeyValues.getValue( + LocaleConstant.CURRENT_VOICE_VALUE_KEY, 5.0f + ) as Float + binding.volumeValueView.text = valueToChineseMap[binding.volumeSlider.value] + //记录Slider上一个值 + lastSliderValue = binding.volumeSlider.value - val isLocale = SaveKeyValues.getValue(LocaleConstant.LOCALE_MODE, true) as Boolean + val isLocale = SaveKeyValues.getValue(LocaleConstant.LOCALE_MODE_KEY, true) as Boolean if (isLocale) { binding.localeRadioButton.isChecked = true } else { binding.remoteRadioButton.isChecked = true } - val openAlarm = SaveKeyValues.getValue(LocaleConstant.OPEN_ALARM, true) as Boolean + val openAlarm = SaveKeyValues.getValue(LocaleConstant.OPEN_ALARM_KEY, true) as Boolean if (openAlarm) { binding.openAlarmRadioButton.isChecked = true } else { binding.closeAlarmRadioButton.isChecked = true } - val openVoice = SaveKeyValues.getValue(LocaleConstant.OPEN_VOICE, true) as Boolean + val openVoice = SaveKeyValues.getValue(LocaleConstant.OPEN_VOICE_KEY, true) as Boolean if (openVoice) { binding.openVoiceRadioButton.isChecked = true } else { diff --git a/app/src/main/java/com/casic/app/safetreecontroller/fragments/MethaneMonitorFragment.kt b/app/src/main/java/com/casic/app/safetreecontroller/fragments/MethaneMonitorFragment.kt index 95d3749..78a350b 100644 --- a/app/src/main/java/com/casic/app/safetreecontroller/fragments/MethaneMonitorFragment.kt +++ b/app/src/main/java/com/casic/app/safetreecontroller/fragments/MethaneMonitorFragment.kt @@ -96,7 +96,7 @@ return@setOnClickListener } - SaveKeyValues.putValue(LocaleConstant.METHANE_DEFAULT_VALUE, threshold) + SaveKeyValues.putValue(LocaleConstant.METHANE_DEFAULT_VALUE_KEY, threshold) val weakReferenceHandler = SocketConnectionService.weakReferenceHandler ?: return@setOnClickListener val message = weakReferenceHandler.obtainMessage() @@ -147,7 +147,7 @@ override fun onResume() { super.onResume() - val checked = SaveKeyValues.getValue(LocaleConstant.OPEN_METHANE, true) as Boolean + val checked = SaveKeyValues.getValue(LocaleConstant.OPEN_METHANE_KEY, true) as Boolean if (checked) { binding.openRadioButton.isChecked = true } else { @@ -155,7 +155,7 @@ } //回显甲烷默认阈值 - val value = SaveKeyValues.getValue(LocaleConstant.METHANE_DEFAULT_VALUE, 1000) as Int + val value = SaveKeyValues.getValue(LocaleConstant.METHANE_DEFAULT_VALUE_KEY, 1000) as Int binding.thresholdView.setText(value.toString()) } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/safetreecontroller/fragments/VoiceSettingsFragment.kt b/app/src/main/java/com/casic/app/safetreecontroller/fragments/VoiceSettingsFragment.kt index c1ab44b..6314ed6 100644 --- a/app/src/main/java/com/casic/app/safetreecontroller/fragments/VoiceSettingsFragment.kt +++ b/app/src/main/java/com/casic/app/safetreecontroller/fragments/VoiceSettingsFragment.kt @@ -3,16 +3,30 @@ import android.os.Bundle import android.view.LayoutInflater import android.view.ViewGroup +import androidx.lifecycle.lifecycleScope import com.casic.app.safetreecontroller.R import com.casic.app.safetreecontroller.databinding.FragmentVoiceSettingsBinding +import com.casic.app.safetreecontroller.service.SocketConnectionService import com.casic.app.safetreecontroller.utils.LocaleConstant import com.pengxh.kt.lite.base.KotlinBaseFragment import com.pengxh.kt.lite.utils.SaveKeyValues +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.delay +import kotlinx.coroutines.launch class VoiceSettingsFragment : KotlinBaseFragment() { private val kTag = "VoiceSettingsFragment" + private val valueToChineseMap = mapOf( + 0.0f to "静音", + 1.0f to "一档", + 2.0f to "二档", + 3.0f to "三档", + 4.0f to "四档", + 5.0f to "五档" + ) private var isVisibleToUser = false + private var lastSliderValue = 0f override fun initOnCreate(savedInstanceState: Bundle?) { @@ -39,15 +53,54 @@ override fun initEvent() { binding.volumeSlider.addOnChangeListener { _, value, _ -> - binding.volumeValueView.text = value.toInt().toString() + //判断是增大还是减音量 + if (value > lastSliderValue) { + //还需要判断是一步一步的变大还是突然变大 + if (value - lastSliderValue == 1.0f) { + //一步一步的变大 + SocketConnectionService.weakReferenceHandler?.sendEmptyMessage(LocaleConstant.INCREASE_VOICE_CODE) + } else { + //突然变大。计算需要一步步变大几次 + val changeTimes = (value - lastSliderValue).toInt() + lifecycleScope.launch(Dispatchers.IO) { + for (i in 0 until changeTimes) { + SocketConnectionService.weakReferenceHandler?.sendEmptyMessage( + LocaleConstant.INCREASE_VOICE_CODE + ) + delay(100) + } + } + } + } else { + //还需要判断是一步一步的变小还是突然变小 + if (lastSliderValue - value == 1.0f) { + //一步一步的变小 + SocketConnectionService.weakReferenceHandler?.sendEmptyMessage(LocaleConstant.REDUCE_VOICE_CODE) + } else { + //突然变小。计算需要一步步变小几次 + val changeTimes = (lastSliderValue - value).toInt() + lifecycleScope.launch(Dispatchers.IO) { + for (i in 0 until changeTimes) { + SocketConnectionService.weakReferenceHandler?.sendEmptyMessage( + LocaleConstant.REDUCE_VOICE_CODE + ) + delay(100) + } + } + } + } + binding.volumeValueView.text = valueToChineseMap[value] + //更新值 + lastSliderValue = value + SaveKeyValues.putValue(LocaleConstant.CURRENT_VOICE_VALUE_KEY, value) } binding.modeRadioGroup.setOnCheckedChangeListener { _, checkedId -> if (isVisibleToUser) { if (checkedId == R.id.localeRadioButton) { - SaveKeyValues.putValue(LocaleConstant.LOCALE_MODE, true) + SaveKeyValues.putValue(LocaleConstant.LOCALE_MODE_KEY, true) } else if (checkedId == R.id.remoteRadioButton) { - SaveKeyValues.putValue(LocaleConstant.LOCALE_MODE, false) + SaveKeyValues.putValue(LocaleConstant.LOCALE_MODE_KEY, false) } } } @@ -55,9 +108,9 @@ binding.alarmRadioGroup.setOnCheckedChangeListener { _, checkedId -> if (isVisibleToUser) { if (checkedId == R.id.openAlarmRadioButton) { - SaveKeyValues.putValue(LocaleConstant.OPEN_ALARM, true) + SaveKeyValues.putValue(LocaleConstant.OPEN_ALARM_KEY, true) } else if (checkedId == R.id.closeAlarmRadioButton) { - SaveKeyValues.putValue(LocaleConstant.OPEN_ALARM, false) + SaveKeyValues.putValue(LocaleConstant.OPEN_ALARM_KEY, false) } } } @@ -65,9 +118,9 @@ binding.deviceRadioGroup.setOnCheckedChangeListener { _, checkedId -> if (isVisibleToUser) { if (checkedId == R.id.openVoiceRadioButton) { - SaveKeyValues.putValue(LocaleConstant.OPEN_VOICE, true) + SaveKeyValues.putValue(LocaleConstant.OPEN_VOICE_KEY, true) } else if (checkedId == R.id.closeVoiceRadioButton) { - SaveKeyValues.putValue(LocaleConstant.OPEN_VOICE, false) + SaveKeyValues.putValue(LocaleConstant.OPEN_VOICE_KEY, false) } } } @@ -75,23 +128,28 @@ override fun onResume() { super.onResume() - binding.volumeValueView.text = binding.volumeSlider.value.toInt().toString() + binding.volumeSlider.value = SaveKeyValues.getValue( + LocaleConstant.CURRENT_VOICE_VALUE_KEY, 5.0f + ) as Float + binding.volumeValueView.text = valueToChineseMap[binding.volumeSlider.value] + //记录Slider上一个值 + lastSliderValue = binding.volumeSlider.value - val isLocale = SaveKeyValues.getValue(LocaleConstant.LOCALE_MODE, true) as Boolean + val isLocale = SaveKeyValues.getValue(LocaleConstant.LOCALE_MODE_KEY, true) as Boolean if (isLocale) { binding.localeRadioButton.isChecked = true } else { binding.remoteRadioButton.isChecked = true } - val openAlarm = SaveKeyValues.getValue(LocaleConstant.OPEN_ALARM, true) as Boolean + val openAlarm = SaveKeyValues.getValue(LocaleConstant.OPEN_ALARM_KEY, true) as Boolean if (openAlarm) { binding.openAlarmRadioButton.isChecked = true } else { binding.closeAlarmRadioButton.isChecked = true } - val openVoice = SaveKeyValues.getValue(LocaleConstant.OPEN_VOICE, true) as Boolean + val openVoice = SaveKeyValues.getValue(LocaleConstant.OPEN_VOICE_KEY, true) as Boolean if (openVoice) { binding.openVoiceRadioButton.isChecked = true } else { diff --git a/app/src/main/java/com/casic/app/safetreecontroller/retrofit/RetrofitServiceManager.kt b/app/src/main/java/com/casic/app/safetreecontroller/retrofit/RetrofitServiceManager.kt index fcded27..577f1d9 100644 --- a/app/src/main/java/com/casic/app/safetreecontroller/retrofit/RetrofitServiceManager.kt +++ b/app/src/main/java/com/casic/app/safetreecontroller/retrofit/RetrofitServiceManager.kt @@ -14,7 +14,7 @@ */ suspend fun executeDeviceCommand(action: String, speed: Int): String { val httpConfig = SaveKeyValues.getValue( - LocaleConstant.DEVICE_CONTROL_SERVER_CONFIG, LocaleConstant.CAMERA_IP + LocaleConstant.DEVICE_CONTROL_SERVER_CONFIG_KEY, LocaleConstant.CAMERA_IP ) as String if (httpConfig == "") { Log.d(kTag, "executeDeviceCommand: httpConfig is null") diff --git a/app/src/main/java/com/casic/app/safetreecontroller/fragments/MethaneMonitorFragment.kt b/app/src/main/java/com/casic/app/safetreecontroller/fragments/MethaneMonitorFragment.kt index 95d3749..78a350b 100644 --- a/app/src/main/java/com/casic/app/safetreecontroller/fragments/MethaneMonitorFragment.kt +++ b/app/src/main/java/com/casic/app/safetreecontroller/fragments/MethaneMonitorFragment.kt @@ -96,7 +96,7 @@ return@setOnClickListener } - SaveKeyValues.putValue(LocaleConstant.METHANE_DEFAULT_VALUE, threshold) + SaveKeyValues.putValue(LocaleConstant.METHANE_DEFAULT_VALUE_KEY, threshold) val weakReferenceHandler = SocketConnectionService.weakReferenceHandler ?: return@setOnClickListener val message = weakReferenceHandler.obtainMessage() @@ -147,7 +147,7 @@ override fun onResume() { super.onResume() - val checked = SaveKeyValues.getValue(LocaleConstant.OPEN_METHANE, true) as Boolean + val checked = SaveKeyValues.getValue(LocaleConstant.OPEN_METHANE_KEY, true) as Boolean if (checked) { binding.openRadioButton.isChecked = true } else { @@ -155,7 +155,7 @@ } //回显甲烷默认阈值 - val value = SaveKeyValues.getValue(LocaleConstant.METHANE_DEFAULT_VALUE, 1000) as Int + val value = SaveKeyValues.getValue(LocaleConstant.METHANE_DEFAULT_VALUE_KEY, 1000) as Int binding.thresholdView.setText(value.toString()) } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/safetreecontroller/fragments/VoiceSettingsFragment.kt b/app/src/main/java/com/casic/app/safetreecontroller/fragments/VoiceSettingsFragment.kt index c1ab44b..6314ed6 100644 --- a/app/src/main/java/com/casic/app/safetreecontroller/fragments/VoiceSettingsFragment.kt +++ b/app/src/main/java/com/casic/app/safetreecontroller/fragments/VoiceSettingsFragment.kt @@ -3,16 +3,30 @@ import android.os.Bundle import android.view.LayoutInflater import android.view.ViewGroup +import androidx.lifecycle.lifecycleScope import com.casic.app.safetreecontroller.R import com.casic.app.safetreecontroller.databinding.FragmentVoiceSettingsBinding +import com.casic.app.safetreecontroller.service.SocketConnectionService import com.casic.app.safetreecontroller.utils.LocaleConstant import com.pengxh.kt.lite.base.KotlinBaseFragment import com.pengxh.kt.lite.utils.SaveKeyValues +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.delay +import kotlinx.coroutines.launch class VoiceSettingsFragment : KotlinBaseFragment() { private val kTag = "VoiceSettingsFragment" + private val valueToChineseMap = mapOf( + 0.0f to "静音", + 1.0f to "一档", + 2.0f to "二档", + 3.0f to "三档", + 4.0f to "四档", + 5.0f to "五档" + ) private var isVisibleToUser = false + private var lastSliderValue = 0f override fun initOnCreate(savedInstanceState: Bundle?) { @@ -39,15 +53,54 @@ override fun initEvent() { binding.volumeSlider.addOnChangeListener { _, value, _ -> - binding.volumeValueView.text = value.toInt().toString() + //判断是增大还是减音量 + if (value > lastSliderValue) { + //还需要判断是一步一步的变大还是突然变大 + if (value - lastSliderValue == 1.0f) { + //一步一步的变大 + SocketConnectionService.weakReferenceHandler?.sendEmptyMessage(LocaleConstant.INCREASE_VOICE_CODE) + } else { + //突然变大。计算需要一步步变大几次 + val changeTimes = (value - lastSliderValue).toInt() + lifecycleScope.launch(Dispatchers.IO) { + for (i in 0 until changeTimes) { + SocketConnectionService.weakReferenceHandler?.sendEmptyMessage( + LocaleConstant.INCREASE_VOICE_CODE + ) + delay(100) + } + } + } + } else { + //还需要判断是一步一步的变小还是突然变小 + if (lastSliderValue - value == 1.0f) { + //一步一步的变小 + SocketConnectionService.weakReferenceHandler?.sendEmptyMessage(LocaleConstant.REDUCE_VOICE_CODE) + } else { + //突然变小。计算需要一步步变小几次 + val changeTimes = (lastSliderValue - value).toInt() + lifecycleScope.launch(Dispatchers.IO) { + for (i in 0 until changeTimes) { + SocketConnectionService.weakReferenceHandler?.sendEmptyMessage( + LocaleConstant.REDUCE_VOICE_CODE + ) + delay(100) + } + } + } + } + binding.volumeValueView.text = valueToChineseMap[value] + //更新值 + lastSliderValue = value + SaveKeyValues.putValue(LocaleConstant.CURRENT_VOICE_VALUE_KEY, value) } binding.modeRadioGroup.setOnCheckedChangeListener { _, checkedId -> if (isVisibleToUser) { if (checkedId == R.id.localeRadioButton) { - SaveKeyValues.putValue(LocaleConstant.LOCALE_MODE, true) + SaveKeyValues.putValue(LocaleConstant.LOCALE_MODE_KEY, true) } else if (checkedId == R.id.remoteRadioButton) { - SaveKeyValues.putValue(LocaleConstant.LOCALE_MODE, false) + SaveKeyValues.putValue(LocaleConstant.LOCALE_MODE_KEY, false) } } } @@ -55,9 +108,9 @@ binding.alarmRadioGroup.setOnCheckedChangeListener { _, checkedId -> if (isVisibleToUser) { if (checkedId == R.id.openAlarmRadioButton) { - SaveKeyValues.putValue(LocaleConstant.OPEN_ALARM, true) + SaveKeyValues.putValue(LocaleConstant.OPEN_ALARM_KEY, true) } else if (checkedId == R.id.closeAlarmRadioButton) { - SaveKeyValues.putValue(LocaleConstant.OPEN_ALARM, false) + SaveKeyValues.putValue(LocaleConstant.OPEN_ALARM_KEY, false) } } } @@ -65,9 +118,9 @@ binding.deviceRadioGroup.setOnCheckedChangeListener { _, checkedId -> if (isVisibleToUser) { if (checkedId == R.id.openVoiceRadioButton) { - SaveKeyValues.putValue(LocaleConstant.OPEN_VOICE, true) + SaveKeyValues.putValue(LocaleConstant.OPEN_VOICE_KEY, true) } else if (checkedId == R.id.closeVoiceRadioButton) { - SaveKeyValues.putValue(LocaleConstant.OPEN_VOICE, false) + SaveKeyValues.putValue(LocaleConstant.OPEN_VOICE_KEY, false) } } } @@ -75,23 +128,28 @@ override fun onResume() { super.onResume() - binding.volumeValueView.text = binding.volumeSlider.value.toInt().toString() + binding.volumeSlider.value = SaveKeyValues.getValue( + LocaleConstant.CURRENT_VOICE_VALUE_KEY, 5.0f + ) as Float + binding.volumeValueView.text = valueToChineseMap[binding.volumeSlider.value] + //记录Slider上一个值 + lastSliderValue = binding.volumeSlider.value - val isLocale = SaveKeyValues.getValue(LocaleConstant.LOCALE_MODE, true) as Boolean + val isLocale = SaveKeyValues.getValue(LocaleConstant.LOCALE_MODE_KEY, true) as Boolean if (isLocale) { binding.localeRadioButton.isChecked = true } else { binding.remoteRadioButton.isChecked = true } - val openAlarm = SaveKeyValues.getValue(LocaleConstant.OPEN_ALARM, true) as Boolean + val openAlarm = SaveKeyValues.getValue(LocaleConstant.OPEN_ALARM_KEY, true) as Boolean if (openAlarm) { binding.openAlarmRadioButton.isChecked = true } else { binding.closeAlarmRadioButton.isChecked = true } - val openVoice = SaveKeyValues.getValue(LocaleConstant.OPEN_VOICE, true) as Boolean + val openVoice = SaveKeyValues.getValue(LocaleConstant.OPEN_VOICE_KEY, true) as Boolean if (openVoice) { binding.openVoiceRadioButton.isChecked = true } else { diff --git a/app/src/main/java/com/casic/app/safetreecontroller/retrofit/RetrofitServiceManager.kt b/app/src/main/java/com/casic/app/safetreecontroller/retrofit/RetrofitServiceManager.kt index fcded27..577f1d9 100644 --- a/app/src/main/java/com/casic/app/safetreecontroller/retrofit/RetrofitServiceManager.kt +++ b/app/src/main/java/com/casic/app/safetreecontroller/retrofit/RetrofitServiceManager.kt @@ -14,7 +14,7 @@ */ suspend fun executeDeviceCommand(action: String, speed: Int): String { val httpConfig = SaveKeyValues.getValue( - LocaleConstant.DEVICE_CONTROL_SERVER_CONFIG, LocaleConstant.CAMERA_IP + LocaleConstant.DEVICE_CONTROL_SERVER_CONFIG_KEY, LocaleConstant.CAMERA_IP ) as String if (httpConfig == "") { Log.d(kTag, "executeDeviceCommand: httpConfig is null") diff --git a/app/src/main/java/com/casic/app/safetreecontroller/service/SocketConnectionService.kt b/app/src/main/java/com/casic/app/safetreecontroller/service/SocketConnectionService.kt index f12f7fa..03c1e53 100644 --- a/app/src/main/java/com/casic/app/safetreecontroller/service/SocketConnectionService.kt +++ b/app/src/main/java/com/casic/app/safetreecontroller/service/SocketConnectionService.kt @@ -45,6 +45,14 @@ LocaleConstant.QUERY_CPU_TEMPERATURE_CODE -> { tcpClient.sendMessage(CommandCreator.createCpuTemperatureCommand()) } + + LocaleConstant.INCREASE_VOICE_CODE -> { + tcpClient.sendMessage(CommandCreator.createIncreaseVoiceCommand()) + } + + LocaleConstant.REDUCE_VOICE_CODE -> { + tcpClient.sendMessage(CommandCreator.createReduceVoiceCommand()) + } } return true } @@ -96,6 +104,8 @@ * 激光关返回:[7, 32, 0, 1, 0, 1, -69, 1, 0, -111, 0, 0, -110] * 甲烷浓度返回:[7, 32, 0, 1, 0, 1, -86, 1, 0, 0, 0, 13, 14] * 激光温度返回:[7, 32, 0, 1, 0, 1, -52, 1, 0, -106, 1, 96, -8] + * 音量加返回:[7, 32, 0, 1, 0, 1, -69, 1, 0, -104, 1, 0, -102] + * 音量减返回:[7, 32, 0, 1, 0, 1, -69, 1, 0, -104, 0, 0, -103] * */ if (bytes.size < 6) { Log.d(kTag, "onMessageReceived: 数据异常,长度不够") @@ -130,12 +140,25 @@ } "BB" -> { - //激光状态 if (bytes.size == 13) { - if (bytes[10].toInt() == 1) { - SaveKeyValues.putValue(LocaleConstant.OPEN_METHANE, true) - } else { - SaveKeyValues.putValue(LocaleConstant.OPEN_METHANE, false) + when ((bytes[9].toInt() and 0xFF).toString(16).uppercase()) { + "91" -> { + //激光状态 + if (bytes[10].toInt() == 1) { + SaveKeyValues.putValue(LocaleConstant.OPEN_METHANE_KEY, true) + } else { + SaveKeyValues.putValue(LocaleConstant.OPEN_METHANE_KEY, false) + } + } + + "98" -> { + //音量调节 + if (bytes[10].toInt() == 1) { + Log.d(kTag, "onMessageReceived: 增大音量成功") + } else { + Log.d(kTag, "onMessageReceived: 减小音量成功") + } + } } } } diff --git a/app/src/main/java/com/casic/app/safetreecontroller/fragments/MethaneMonitorFragment.kt b/app/src/main/java/com/casic/app/safetreecontroller/fragments/MethaneMonitorFragment.kt index 95d3749..78a350b 100644 --- a/app/src/main/java/com/casic/app/safetreecontroller/fragments/MethaneMonitorFragment.kt +++ b/app/src/main/java/com/casic/app/safetreecontroller/fragments/MethaneMonitorFragment.kt @@ -96,7 +96,7 @@ return@setOnClickListener } - SaveKeyValues.putValue(LocaleConstant.METHANE_DEFAULT_VALUE, threshold) + SaveKeyValues.putValue(LocaleConstant.METHANE_DEFAULT_VALUE_KEY, threshold) val weakReferenceHandler = SocketConnectionService.weakReferenceHandler ?: return@setOnClickListener val message = weakReferenceHandler.obtainMessage() @@ -147,7 +147,7 @@ override fun onResume() { super.onResume() - val checked = SaveKeyValues.getValue(LocaleConstant.OPEN_METHANE, true) as Boolean + val checked = SaveKeyValues.getValue(LocaleConstant.OPEN_METHANE_KEY, true) as Boolean if (checked) { binding.openRadioButton.isChecked = true } else { @@ -155,7 +155,7 @@ } //回显甲烷默认阈值 - val value = SaveKeyValues.getValue(LocaleConstant.METHANE_DEFAULT_VALUE, 1000) as Int + val value = SaveKeyValues.getValue(LocaleConstant.METHANE_DEFAULT_VALUE_KEY, 1000) as Int binding.thresholdView.setText(value.toString()) } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/safetreecontroller/fragments/VoiceSettingsFragment.kt b/app/src/main/java/com/casic/app/safetreecontroller/fragments/VoiceSettingsFragment.kt index c1ab44b..6314ed6 100644 --- a/app/src/main/java/com/casic/app/safetreecontroller/fragments/VoiceSettingsFragment.kt +++ b/app/src/main/java/com/casic/app/safetreecontroller/fragments/VoiceSettingsFragment.kt @@ -3,16 +3,30 @@ import android.os.Bundle import android.view.LayoutInflater import android.view.ViewGroup +import androidx.lifecycle.lifecycleScope import com.casic.app.safetreecontroller.R import com.casic.app.safetreecontroller.databinding.FragmentVoiceSettingsBinding +import com.casic.app.safetreecontroller.service.SocketConnectionService import com.casic.app.safetreecontroller.utils.LocaleConstant import com.pengxh.kt.lite.base.KotlinBaseFragment import com.pengxh.kt.lite.utils.SaveKeyValues +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.delay +import kotlinx.coroutines.launch class VoiceSettingsFragment : KotlinBaseFragment() { private val kTag = "VoiceSettingsFragment" + private val valueToChineseMap = mapOf( + 0.0f to "静音", + 1.0f to "一档", + 2.0f to "二档", + 3.0f to "三档", + 4.0f to "四档", + 5.0f to "五档" + ) private var isVisibleToUser = false + private var lastSliderValue = 0f override fun initOnCreate(savedInstanceState: Bundle?) { @@ -39,15 +53,54 @@ override fun initEvent() { binding.volumeSlider.addOnChangeListener { _, value, _ -> - binding.volumeValueView.text = value.toInt().toString() + //判断是增大还是减音量 + if (value > lastSliderValue) { + //还需要判断是一步一步的变大还是突然变大 + if (value - lastSliderValue == 1.0f) { + //一步一步的变大 + SocketConnectionService.weakReferenceHandler?.sendEmptyMessage(LocaleConstant.INCREASE_VOICE_CODE) + } else { + //突然变大。计算需要一步步变大几次 + val changeTimes = (value - lastSliderValue).toInt() + lifecycleScope.launch(Dispatchers.IO) { + for (i in 0 until changeTimes) { + SocketConnectionService.weakReferenceHandler?.sendEmptyMessage( + LocaleConstant.INCREASE_VOICE_CODE + ) + delay(100) + } + } + } + } else { + //还需要判断是一步一步的变小还是突然变小 + if (lastSliderValue - value == 1.0f) { + //一步一步的变小 + SocketConnectionService.weakReferenceHandler?.sendEmptyMessage(LocaleConstant.REDUCE_VOICE_CODE) + } else { + //突然变小。计算需要一步步变小几次 + val changeTimes = (lastSliderValue - value).toInt() + lifecycleScope.launch(Dispatchers.IO) { + for (i in 0 until changeTimes) { + SocketConnectionService.weakReferenceHandler?.sendEmptyMessage( + LocaleConstant.REDUCE_VOICE_CODE + ) + delay(100) + } + } + } + } + binding.volumeValueView.text = valueToChineseMap[value] + //更新值 + lastSliderValue = value + SaveKeyValues.putValue(LocaleConstant.CURRENT_VOICE_VALUE_KEY, value) } binding.modeRadioGroup.setOnCheckedChangeListener { _, checkedId -> if (isVisibleToUser) { if (checkedId == R.id.localeRadioButton) { - SaveKeyValues.putValue(LocaleConstant.LOCALE_MODE, true) + SaveKeyValues.putValue(LocaleConstant.LOCALE_MODE_KEY, true) } else if (checkedId == R.id.remoteRadioButton) { - SaveKeyValues.putValue(LocaleConstant.LOCALE_MODE, false) + SaveKeyValues.putValue(LocaleConstant.LOCALE_MODE_KEY, false) } } } @@ -55,9 +108,9 @@ binding.alarmRadioGroup.setOnCheckedChangeListener { _, checkedId -> if (isVisibleToUser) { if (checkedId == R.id.openAlarmRadioButton) { - SaveKeyValues.putValue(LocaleConstant.OPEN_ALARM, true) + SaveKeyValues.putValue(LocaleConstant.OPEN_ALARM_KEY, true) } else if (checkedId == R.id.closeAlarmRadioButton) { - SaveKeyValues.putValue(LocaleConstant.OPEN_ALARM, false) + SaveKeyValues.putValue(LocaleConstant.OPEN_ALARM_KEY, false) } } } @@ -65,9 +118,9 @@ binding.deviceRadioGroup.setOnCheckedChangeListener { _, checkedId -> if (isVisibleToUser) { if (checkedId == R.id.openVoiceRadioButton) { - SaveKeyValues.putValue(LocaleConstant.OPEN_VOICE, true) + SaveKeyValues.putValue(LocaleConstant.OPEN_VOICE_KEY, true) } else if (checkedId == R.id.closeVoiceRadioButton) { - SaveKeyValues.putValue(LocaleConstant.OPEN_VOICE, false) + SaveKeyValues.putValue(LocaleConstant.OPEN_VOICE_KEY, false) } } } @@ -75,23 +128,28 @@ override fun onResume() { super.onResume() - binding.volumeValueView.text = binding.volumeSlider.value.toInt().toString() + binding.volumeSlider.value = SaveKeyValues.getValue( + LocaleConstant.CURRENT_VOICE_VALUE_KEY, 5.0f + ) as Float + binding.volumeValueView.text = valueToChineseMap[binding.volumeSlider.value] + //记录Slider上一个值 + lastSliderValue = binding.volumeSlider.value - val isLocale = SaveKeyValues.getValue(LocaleConstant.LOCALE_MODE, true) as Boolean + val isLocale = SaveKeyValues.getValue(LocaleConstant.LOCALE_MODE_KEY, true) as Boolean if (isLocale) { binding.localeRadioButton.isChecked = true } else { binding.remoteRadioButton.isChecked = true } - val openAlarm = SaveKeyValues.getValue(LocaleConstant.OPEN_ALARM, true) as Boolean + val openAlarm = SaveKeyValues.getValue(LocaleConstant.OPEN_ALARM_KEY, true) as Boolean if (openAlarm) { binding.openAlarmRadioButton.isChecked = true } else { binding.closeAlarmRadioButton.isChecked = true } - val openVoice = SaveKeyValues.getValue(LocaleConstant.OPEN_VOICE, true) as Boolean + val openVoice = SaveKeyValues.getValue(LocaleConstant.OPEN_VOICE_KEY, true) as Boolean if (openVoice) { binding.openVoiceRadioButton.isChecked = true } else { diff --git a/app/src/main/java/com/casic/app/safetreecontroller/retrofit/RetrofitServiceManager.kt b/app/src/main/java/com/casic/app/safetreecontroller/retrofit/RetrofitServiceManager.kt index fcded27..577f1d9 100644 --- a/app/src/main/java/com/casic/app/safetreecontroller/retrofit/RetrofitServiceManager.kt +++ b/app/src/main/java/com/casic/app/safetreecontroller/retrofit/RetrofitServiceManager.kt @@ -14,7 +14,7 @@ */ suspend fun executeDeviceCommand(action: String, speed: Int): String { val httpConfig = SaveKeyValues.getValue( - LocaleConstant.DEVICE_CONTROL_SERVER_CONFIG, LocaleConstant.CAMERA_IP + LocaleConstant.DEVICE_CONTROL_SERVER_CONFIG_KEY, LocaleConstant.CAMERA_IP ) as String if (httpConfig == "") { Log.d(kTag, "executeDeviceCommand: httpConfig is null") diff --git a/app/src/main/java/com/casic/app/safetreecontroller/service/SocketConnectionService.kt b/app/src/main/java/com/casic/app/safetreecontroller/service/SocketConnectionService.kt index f12f7fa..03c1e53 100644 --- a/app/src/main/java/com/casic/app/safetreecontroller/service/SocketConnectionService.kt +++ b/app/src/main/java/com/casic/app/safetreecontroller/service/SocketConnectionService.kt @@ -45,6 +45,14 @@ LocaleConstant.QUERY_CPU_TEMPERATURE_CODE -> { tcpClient.sendMessage(CommandCreator.createCpuTemperatureCommand()) } + + LocaleConstant.INCREASE_VOICE_CODE -> { + tcpClient.sendMessage(CommandCreator.createIncreaseVoiceCommand()) + } + + LocaleConstant.REDUCE_VOICE_CODE -> { + tcpClient.sendMessage(CommandCreator.createReduceVoiceCommand()) + } } return true } @@ -96,6 +104,8 @@ * 激光关返回:[7, 32, 0, 1, 0, 1, -69, 1, 0, -111, 0, 0, -110] * 甲烷浓度返回:[7, 32, 0, 1, 0, 1, -86, 1, 0, 0, 0, 13, 14] * 激光温度返回:[7, 32, 0, 1, 0, 1, -52, 1, 0, -106, 1, 96, -8] + * 音量加返回:[7, 32, 0, 1, 0, 1, -69, 1, 0, -104, 1, 0, -102] + * 音量减返回:[7, 32, 0, 1, 0, 1, -69, 1, 0, -104, 0, 0, -103] * */ if (bytes.size < 6) { Log.d(kTag, "onMessageReceived: 数据异常,长度不够") @@ -130,12 +140,25 @@ } "BB" -> { - //激光状态 if (bytes.size == 13) { - if (bytes[10].toInt() == 1) { - SaveKeyValues.putValue(LocaleConstant.OPEN_METHANE, true) - } else { - SaveKeyValues.putValue(LocaleConstant.OPEN_METHANE, false) + when ((bytes[9].toInt() and 0xFF).toString(16).uppercase()) { + "91" -> { + //激光状态 + if (bytes[10].toInt() == 1) { + SaveKeyValues.putValue(LocaleConstant.OPEN_METHANE_KEY, true) + } else { + SaveKeyValues.putValue(LocaleConstant.OPEN_METHANE_KEY, false) + } + } + + "98" -> { + //音量调节 + if (bytes[10].toInt() == 1) { + Log.d(kTag, "onMessageReceived: 增大音量成功") + } else { + Log.d(kTag, "onMessageReceived: 减小音量成功") + } + } } } } diff --git a/app/src/main/java/com/casic/app/safetreecontroller/utils/CommandCreator.kt b/app/src/main/java/com/casic/app/safetreecontroller/utils/CommandCreator.kt index 59eb314..d2b05c2 100644 --- a/app/src/main/java/com/casic/app/safetreecontroller/utils/CommandCreator.kt +++ b/app/src/main/java/com/casic/app/safetreecontroller/utils/CommandCreator.kt @@ -65,4 +65,18 @@ fun createCpuTemperatureCommand(): ByteArray { return byteArrayOf(0xAA.toByte(), 0x01, 0x00, 0x96.toByte(), 0x00, 0x00, 0x97.toByte()) } + + /** + * 音量增大指令 + * */ + fun createIncreaseVoiceCommand(): ByteArray { + return byteArrayOf(0xAA.toByte(), 0x01, 0x00, 0x98.toByte(), 0x01, 0x00, 0x9A.toByte()) + } + + /** + * 音量减小指令 + * */ + fun createReduceVoiceCommand(): ByteArray { + return byteArrayOf(0xAA.toByte(), 0x01, 0x00, 0x98.toByte(), 0x00, 0x00, 0x99.toByte()) + } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/safetreecontroller/fragments/MethaneMonitorFragment.kt b/app/src/main/java/com/casic/app/safetreecontroller/fragments/MethaneMonitorFragment.kt index 95d3749..78a350b 100644 --- a/app/src/main/java/com/casic/app/safetreecontroller/fragments/MethaneMonitorFragment.kt +++ b/app/src/main/java/com/casic/app/safetreecontroller/fragments/MethaneMonitorFragment.kt @@ -96,7 +96,7 @@ return@setOnClickListener } - SaveKeyValues.putValue(LocaleConstant.METHANE_DEFAULT_VALUE, threshold) + SaveKeyValues.putValue(LocaleConstant.METHANE_DEFAULT_VALUE_KEY, threshold) val weakReferenceHandler = SocketConnectionService.weakReferenceHandler ?: return@setOnClickListener val message = weakReferenceHandler.obtainMessage() @@ -147,7 +147,7 @@ override fun onResume() { super.onResume() - val checked = SaveKeyValues.getValue(LocaleConstant.OPEN_METHANE, true) as Boolean + val checked = SaveKeyValues.getValue(LocaleConstant.OPEN_METHANE_KEY, true) as Boolean if (checked) { binding.openRadioButton.isChecked = true } else { @@ -155,7 +155,7 @@ } //回显甲烷默认阈值 - val value = SaveKeyValues.getValue(LocaleConstant.METHANE_DEFAULT_VALUE, 1000) as Int + val value = SaveKeyValues.getValue(LocaleConstant.METHANE_DEFAULT_VALUE_KEY, 1000) as Int binding.thresholdView.setText(value.toString()) } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/safetreecontroller/fragments/VoiceSettingsFragment.kt b/app/src/main/java/com/casic/app/safetreecontroller/fragments/VoiceSettingsFragment.kt index c1ab44b..6314ed6 100644 --- a/app/src/main/java/com/casic/app/safetreecontroller/fragments/VoiceSettingsFragment.kt +++ b/app/src/main/java/com/casic/app/safetreecontroller/fragments/VoiceSettingsFragment.kt @@ -3,16 +3,30 @@ import android.os.Bundle import android.view.LayoutInflater import android.view.ViewGroup +import androidx.lifecycle.lifecycleScope import com.casic.app.safetreecontroller.R import com.casic.app.safetreecontroller.databinding.FragmentVoiceSettingsBinding +import com.casic.app.safetreecontroller.service.SocketConnectionService import com.casic.app.safetreecontroller.utils.LocaleConstant import com.pengxh.kt.lite.base.KotlinBaseFragment import com.pengxh.kt.lite.utils.SaveKeyValues +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.delay +import kotlinx.coroutines.launch class VoiceSettingsFragment : KotlinBaseFragment() { private val kTag = "VoiceSettingsFragment" + private val valueToChineseMap = mapOf( + 0.0f to "静音", + 1.0f to "一档", + 2.0f to "二档", + 3.0f to "三档", + 4.0f to "四档", + 5.0f to "五档" + ) private var isVisibleToUser = false + private var lastSliderValue = 0f override fun initOnCreate(savedInstanceState: Bundle?) { @@ -39,15 +53,54 @@ override fun initEvent() { binding.volumeSlider.addOnChangeListener { _, value, _ -> - binding.volumeValueView.text = value.toInt().toString() + //判断是增大还是减音量 + if (value > lastSliderValue) { + //还需要判断是一步一步的变大还是突然变大 + if (value - lastSliderValue == 1.0f) { + //一步一步的变大 + SocketConnectionService.weakReferenceHandler?.sendEmptyMessage(LocaleConstant.INCREASE_VOICE_CODE) + } else { + //突然变大。计算需要一步步变大几次 + val changeTimes = (value - lastSliderValue).toInt() + lifecycleScope.launch(Dispatchers.IO) { + for (i in 0 until changeTimes) { + SocketConnectionService.weakReferenceHandler?.sendEmptyMessage( + LocaleConstant.INCREASE_VOICE_CODE + ) + delay(100) + } + } + } + } else { + //还需要判断是一步一步的变小还是突然变小 + if (lastSliderValue - value == 1.0f) { + //一步一步的变小 + SocketConnectionService.weakReferenceHandler?.sendEmptyMessage(LocaleConstant.REDUCE_VOICE_CODE) + } else { + //突然变小。计算需要一步步变小几次 + val changeTimes = (lastSliderValue - value).toInt() + lifecycleScope.launch(Dispatchers.IO) { + for (i in 0 until changeTimes) { + SocketConnectionService.weakReferenceHandler?.sendEmptyMessage( + LocaleConstant.REDUCE_VOICE_CODE + ) + delay(100) + } + } + } + } + binding.volumeValueView.text = valueToChineseMap[value] + //更新值 + lastSliderValue = value + SaveKeyValues.putValue(LocaleConstant.CURRENT_VOICE_VALUE_KEY, value) } binding.modeRadioGroup.setOnCheckedChangeListener { _, checkedId -> if (isVisibleToUser) { if (checkedId == R.id.localeRadioButton) { - SaveKeyValues.putValue(LocaleConstant.LOCALE_MODE, true) + SaveKeyValues.putValue(LocaleConstant.LOCALE_MODE_KEY, true) } else if (checkedId == R.id.remoteRadioButton) { - SaveKeyValues.putValue(LocaleConstant.LOCALE_MODE, false) + SaveKeyValues.putValue(LocaleConstant.LOCALE_MODE_KEY, false) } } } @@ -55,9 +108,9 @@ binding.alarmRadioGroup.setOnCheckedChangeListener { _, checkedId -> if (isVisibleToUser) { if (checkedId == R.id.openAlarmRadioButton) { - SaveKeyValues.putValue(LocaleConstant.OPEN_ALARM, true) + SaveKeyValues.putValue(LocaleConstant.OPEN_ALARM_KEY, true) } else if (checkedId == R.id.closeAlarmRadioButton) { - SaveKeyValues.putValue(LocaleConstant.OPEN_ALARM, false) + SaveKeyValues.putValue(LocaleConstant.OPEN_ALARM_KEY, false) } } } @@ -65,9 +118,9 @@ binding.deviceRadioGroup.setOnCheckedChangeListener { _, checkedId -> if (isVisibleToUser) { if (checkedId == R.id.openVoiceRadioButton) { - SaveKeyValues.putValue(LocaleConstant.OPEN_VOICE, true) + SaveKeyValues.putValue(LocaleConstant.OPEN_VOICE_KEY, true) } else if (checkedId == R.id.closeVoiceRadioButton) { - SaveKeyValues.putValue(LocaleConstant.OPEN_VOICE, false) + SaveKeyValues.putValue(LocaleConstant.OPEN_VOICE_KEY, false) } } } @@ -75,23 +128,28 @@ override fun onResume() { super.onResume() - binding.volumeValueView.text = binding.volumeSlider.value.toInt().toString() + binding.volumeSlider.value = SaveKeyValues.getValue( + LocaleConstant.CURRENT_VOICE_VALUE_KEY, 5.0f + ) as Float + binding.volumeValueView.text = valueToChineseMap[binding.volumeSlider.value] + //记录Slider上一个值 + lastSliderValue = binding.volumeSlider.value - val isLocale = SaveKeyValues.getValue(LocaleConstant.LOCALE_MODE, true) as Boolean + val isLocale = SaveKeyValues.getValue(LocaleConstant.LOCALE_MODE_KEY, true) as Boolean if (isLocale) { binding.localeRadioButton.isChecked = true } else { binding.remoteRadioButton.isChecked = true } - val openAlarm = SaveKeyValues.getValue(LocaleConstant.OPEN_ALARM, true) as Boolean + val openAlarm = SaveKeyValues.getValue(LocaleConstant.OPEN_ALARM_KEY, true) as Boolean if (openAlarm) { binding.openAlarmRadioButton.isChecked = true } else { binding.closeAlarmRadioButton.isChecked = true } - val openVoice = SaveKeyValues.getValue(LocaleConstant.OPEN_VOICE, true) as Boolean + val openVoice = SaveKeyValues.getValue(LocaleConstant.OPEN_VOICE_KEY, true) as Boolean if (openVoice) { binding.openVoiceRadioButton.isChecked = true } else { diff --git a/app/src/main/java/com/casic/app/safetreecontroller/retrofit/RetrofitServiceManager.kt b/app/src/main/java/com/casic/app/safetreecontroller/retrofit/RetrofitServiceManager.kt index fcded27..577f1d9 100644 --- a/app/src/main/java/com/casic/app/safetreecontroller/retrofit/RetrofitServiceManager.kt +++ b/app/src/main/java/com/casic/app/safetreecontroller/retrofit/RetrofitServiceManager.kt @@ -14,7 +14,7 @@ */ suspend fun executeDeviceCommand(action: String, speed: Int): String { val httpConfig = SaveKeyValues.getValue( - LocaleConstant.DEVICE_CONTROL_SERVER_CONFIG, LocaleConstant.CAMERA_IP + LocaleConstant.DEVICE_CONTROL_SERVER_CONFIG_KEY, LocaleConstant.CAMERA_IP ) as String if (httpConfig == "") { Log.d(kTag, "executeDeviceCommand: httpConfig is null") diff --git a/app/src/main/java/com/casic/app/safetreecontroller/service/SocketConnectionService.kt b/app/src/main/java/com/casic/app/safetreecontroller/service/SocketConnectionService.kt index f12f7fa..03c1e53 100644 --- a/app/src/main/java/com/casic/app/safetreecontroller/service/SocketConnectionService.kt +++ b/app/src/main/java/com/casic/app/safetreecontroller/service/SocketConnectionService.kt @@ -45,6 +45,14 @@ LocaleConstant.QUERY_CPU_TEMPERATURE_CODE -> { tcpClient.sendMessage(CommandCreator.createCpuTemperatureCommand()) } + + LocaleConstant.INCREASE_VOICE_CODE -> { + tcpClient.sendMessage(CommandCreator.createIncreaseVoiceCommand()) + } + + LocaleConstant.REDUCE_VOICE_CODE -> { + tcpClient.sendMessage(CommandCreator.createReduceVoiceCommand()) + } } return true } @@ -96,6 +104,8 @@ * 激光关返回:[7, 32, 0, 1, 0, 1, -69, 1, 0, -111, 0, 0, -110] * 甲烷浓度返回:[7, 32, 0, 1, 0, 1, -86, 1, 0, 0, 0, 13, 14] * 激光温度返回:[7, 32, 0, 1, 0, 1, -52, 1, 0, -106, 1, 96, -8] + * 音量加返回:[7, 32, 0, 1, 0, 1, -69, 1, 0, -104, 1, 0, -102] + * 音量减返回:[7, 32, 0, 1, 0, 1, -69, 1, 0, -104, 0, 0, -103] * */ if (bytes.size < 6) { Log.d(kTag, "onMessageReceived: 数据异常,长度不够") @@ -130,12 +140,25 @@ } "BB" -> { - //激光状态 if (bytes.size == 13) { - if (bytes[10].toInt() == 1) { - SaveKeyValues.putValue(LocaleConstant.OPEN_METHANE, true) - } else { - SaveKeyValues.putValue(LocaleConstant.OPEN_METHANE, false) + when ((bytes[9].toInt() and 0xFF).toString(16).uppercase()) { + "91" -> { + //激光状态 + if (bytes[10].toInt() == 1) { + SaveKeyValues.putValue(LocaleConstant.OPEN_METHANE_KEY, true) + } else { + SaveKeyValues.putValue(LocaleConstant.OPEN_METHANE_KEY, false) + } + } + + "98" -> { + //音量调节 + if (bytes[10].toInt() == 1) { + Log.d(kTag, "onMessageReceived: 增大音量成功") + } else { + Log.d(kTag, "onMessageReceived: 减小音量成功") + } + } } } } diff --git a/app/src/main/java/com/casic/app/safetreecontroller/utils/CommandCreator.kt b/app/src/main/java/com/casic/app/safetreecontroller/utils/CommandCreator.kt index 59eb314..d2b05c2 100644 --- a/app/src/main/java/com/casic/app/safetreecontroller/utils/CommandCreator.kt +++ b/app/src/main/java/com/casic/app/safetreecontroller/utils/CommandCreator.kt @@ -65,4 +65,18 @@ fun createCpuTemperatureCommand(): ByteArray { return byteArrayOf(0xAA.toByte(), 0x01, 0x00, 0x96.toByte(), 0x00, 0x00, 0x97.toByte()) } + + /** + * 音量增大指令 + * */ + fun createIncreaseVoiceCommand(): ByteArray { + return byteArrayOf(0xAA.toByte(), 0x01, 0x00, 0x98.toByte(), 0x01, 0x00, 0x9A.toByte()) + } + + /** + * 音量减小指令 + * */ + fun createReduceVoiceCommand(): ByteArray { + return byteArrayOf(0xAA.toByte(), 0x01, 0x00, 0x98.toByte(), 0x00, 0x00, 0x99.toByte()) + } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/safetreecontroller/utils/LocaleConstant.kt b/app/src/main/java/com/casic/app/safetreecontroller/utils/LocaleConstant.kt index b7be093..b33f0d0 100644 --- a/app/src/main/java/com/casic/app/safetreecontroller/utils/LocaleConstant.kt +++ b/app/src/main/java/com/casic/app/safetreecontroller/utils/LocaleConstant.kt @@ -18,6 +18,8 @@ const val CLOSE_METHANE_CODE = 20240002 const val UPDATE_GAS_THRESHOLD_CODE = 20240003 const val QUERY_CPU_TEMPERATURE_CODE = 20240004 + const val INCREASE_VOICE_CODE = 20240005 + const val REDUCE_VOICE_CODE = 20240006 /** * Handler Response Code @@ -29,12 +31,13 @@ /*** * SP Key * */ - const val DEVICE_CONTROL_SERVER_CONFIG = "Key_1" - const val OPEN_METHANE = "Key_2" - const val LOCALE_MODE = "Key_4" - const val OPEN_ALARM = "Key_5" - const val OPEN_VOICE = "Key_6" - const val METHANE_DEFAULT_VALUE = "Key_7" + const val DEVICE_CONTROL_SERVER_CONFIG_KEY = "Key_1" + const val OPEN_METHANE_KEY = "Key_2" + const val LOCALE_MODE_KEY = "Key_4" + const val OPEN_ALARM_KEY = "Key_5" + const val OPEN_VOICE_KEY = "Key_6" + const val METHANE_DEFAULT_VALUE_KEY = "Key_7" + const val CURRENT_VOICE_VALUE_KEY = "Key_8" //相机IP const val CAMERA_IP = "192.168.10.137" diff --git a/app/src/main/java/com/casic/app/safetreecontroller/fragments/MethaneMonitorFragment.kt b/app/src/main/java/com/casic/app/safetreecontroller/fragments/MethaneMonitorFragment.kt index 95d3749..78a350b 100644 --- a/app/src/main/java/com/casic/app/safetreecontroller/fragments/MethaneMonitorFragment.kt +++ b/app/src/main/java/com/casic/app/safetreecontroller/fragments/MethaneMonitorFragment.kt @@ -96,7 +96,7 @@ return@setOnClickListener } - SaveKeyValues.putValue(LocaleConstant.METHANE_DEFAULT_VALUE, threshold) + SaveKeyValues.putValue(LocaleConstant.METHANE_DEFAULT_VALUE_KEY, threshold) val weakReferenceHandler = SocketConnectionService.weakReferenceHandler ?: return@setOnClickListener val message = weakReferenceHandler.obtainMessage() @@ -147,7 +147,7 @@ override fun onResume() { super.onResume() - val checked = SaveKeyValues.getValue(LocaleConstant.OPEN_METHANE, true) as Boolean + val checked = SaveKeyValues.getValue(LocaleConstant.OPEN_METHANE_KEY, true) as Boolean if (checked) { binding.openRadioButton.isChecked = true } else { @@ -155,7 +155,7 @@ } //回显甲烷默认阈值 - val value = SaveKeyValues.getValue(LocaleConstant.METHANE_DEFAULT_VALUE, 1000) as Int + val value = SaveKeyValues.getValue(LocaleConstant.METHANE_DEFAULT_VALUE_KEY, 1000) as Int binding.thresholdView.setText(value.toString()) } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/safetreecontroller/fragments/VoiceSettingsFragment.kt b/app/src/main/java/com/casic/app/safetreecontroller/fragments/VoiceSettingsFragment.kt index c1ab44b..6314ed6 100644 --- a/app/src/main/java/com/casic/app/safetreecontroller/fragments/VoiceSettingsFragment.kt +++ b/app/src/main/java/com/casic/app/safetreecontroller/fragments/VoiceSettingsFragment.kt @@ -3,16 +3,30 @@ import android.os.Bundle import android.view.LayoutInflater import android.view.ViewGroup +import androidx.lifecycle.lifecycleScope import com.casic.app.safetreecontroller.R import com.casic.app.safetreecontroller.databinding.FragmentVoiceSettingsBinding +import com.casic.app.safetreecontroller.service.SocketConnectionService import com.casic.app.safetreecontroller.utils.LocaleConstant import com.pengxh.kt.lite.base.KotlinBaseFragment import com.pengxh.kt.lite.utils.SaveKeyValues +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.delay +import kotlinx.coroutines.launch class VoiceSettingsFragment : KotlinBaseFragment() { private val kTag = "VoiceSettingsFragment" + private val valueToChineseMap = mapOf( + 0.0f to "静音", + 1.0f to "一档", + 2.0f to "二档", + 3.0f to "三档", + 4.0f to "四档", + 5.0f to "五档" + ) private var isVisibleToUser = false + private var lastSliderValue = 0f override fun initOnCreate(savedInstanceState: Bundle?) { @@ -39,15 +53,54 @@ override fun initEvent() { binding.volumeSlider.addOnChangeListener { _, value, _ -> - binding.volumeValueView.text = value.toInt().toString() + //判断是增大还是减音量 + if (value > lastSliderValue) { + //还需要判断是一步一步的变大还是突然变大 + if (value - lastSliderValue == 1.0f) { + //一步一步的变大 + SocketConnectionService.weakReferenceHandler?.sendEmptyMessage(LocaleConstant.INCREASE_VOICE_CODE) + } else { + //突然变大。计算需要一步步变大几次 + val changeTimes = (value - lastSliderValue).toInt() + lifecycleScope.launch(Dispatchers.IO) { + for (i in 0 until changeTimes) { + SocketConnectionService.weakReferenceHandler?.sendEmptyMessage( + LocaleConstant.INCREASE_VOICE_CODE + ) + delay(100) + } + } + } + } else { + //还需要判断是一步一步的变小还是突然变小 + if (lastSliderValue - value == 1.0f) { + //一步一步的变小 + SocketConnectionService.weakReferenceHandler?.sendEmptyMessage(LocaleConstant.REDUCE_VOICE_CODE) + } else { + //突然变小。计算需要一步步变小几次 + val changeTimes = (lastSliderValue - value).toInt() + lifecycleScope.launch(Dispatchers.IO) { + for (i in 0 until changeTimes) { + SocketConnectionService.weakReferenceHandler?.sendEmptyMessage( + LocaleConstant.REDUCE_VOICE_CODE + ) + delay(100) + } + } + } + } + binding.volumeValueView.text = valueToChineseMap[value] + //更新值 + lastSliderValue = value + SaveKeyValues.putValue(LocaleConstant.CURRENT_VOICE_VALUE_KEY, value) } binding.modeRadioGroup.setOnCheckedChangeListener { _, checkedId -> if (isVisibleToUser) { if (checkedId == R.id.localeRadioButton) { - SaveKeyValues.putValue(LocaleConstant.LOCALE_MODE, true) + SaveKeyValues.putValue(LocaleConstant.LOCALE_MODE_KEY, true) } else if (checkedId == R.id.remoteRadioButton) { - SaveKeyValues.putValue(LocaleConstant.LOCALE_MODE, false) + SaveKeyValues.putValue(LocaleConstant.LOCALE_MODE_KEY, false) } } } @@ -55,9 +108,9 @@ binding.alarmRadioGroup.setOnCheckedChangeListener { _, checkedId -> if (isVisibleToUser) { if (checkedId == R.id.openAlarmRadioButton) { - SaveKeyValues.putValue(LocaleConstant.OPEN_ALARM, true) + SaveKeyValues.putValue(LocaleConstant.OPEN_ALARM_KEY, true) } else if (checkedId == R.id.closeAlarmRadioButton) { - SaveKeyValues.putValue(LocaleConstant.OPEN_ALARM, false) + SaveKeyValues.putValue(LocaleConstant.OPEN_ALARM_KEY, false) } } } @@ -65,9 +118,9 @@ binding.deviceRadioGroup.setOnCheckedChangeListener { _, checkedId -> if (isVisibleToUser) { if (checkedId == R.id.openVoiceRadioButton) { - SaveKeyValues.putValue(LocaleConstant.OPEN_VOICE, true) + SaveKeyValues.putValue(LocaleConstant.OPEN_VOICE_KEY, true) } else if (checkedId == R.id.closeVoiceRadioButton) { - SaveKeyValues.putValue(LocaleConstant.OPEN_VOICE, false) + SaveKeyValues.putValue(LocaleConstant.OPEN_VOICE_KEY, false) } } } @@ -75,23 +128,28 @@ override fun onResume() { super.onResume() - binding.volumeValueView.text = binding.volumeSlider.value.toInt().toString() + binding.volumeSlider.value = SaveKeyValues.getValue( + LocaleConstant.CURRENT_VOICE_VALUE_KEY, 5.0f + ) as Float + binding.volumeValueView.text = valueToChineseMap[binding.volumeSlider.value] + //记录Slider上一个值 + lastSliderValue = binding.volumeSlider.value - val isLocale = SaveKeyValues.getValue(LocaleConstant.LOCALE_MODE, true) as Boolean + val isLocale = SaveKeyValues.getValue(LocaleConstant.LOCALE_MODE_KEY, true) as Boolean if (isLocale) { binding.localeRadioButton.isChecked = true } else { binding.remoteRadioButton.isChecked = true } - val openAlarm = SaveKeyValues.getValue(LocaleConstant.OPEN_ALARM, true) as Boolean + val openAlarm = SaveKeyValues.getValue(LocaleConstant.OPEN_ALARM_KEY, true) as Boolean if (openAlarm) { binding.openAlarmRadioButton.isChecked = true } else { binding.closeAlarmRadioButton.isChecked = true } - val openVoice = SaveKeyValues.getValue(LocaleConstant.OPEN_VOICE, true) as Boolean + val openVoice = SaveKeyValues.getValue(LocaleConstant.OPEN_VOICE_KEY, true) as Boolean if (openVoice) { binding.openVoiceRadioButton.isChecked = true } else { diff --git a/app/src/main/java/com/casic/app/safetreecontroller/retrofit/RetrofitServiceManager.kt b/app/src/main/java/com/casic/app/safetreecontroller/retrofit/RetrofitServiceManager.kt index fcded27..577f1d9 100644 --- a/app/src/main/java/com/casic/app/safetreecontroller/retrofit/RetrofitServiceManager.kt +++ b/app/src/main/java/com/casic/app/safetreecontroller/retrofit/RetrofitServiceManager.kt @@ -14,7 +14,7 @@ */ suspend fun executeDeviceCommand(action: String, speed: Int): String { val httpConfig = SaveKeyValues.getValue( - LocaleConstant.DEVICE_CONTROL_SERVER_CONFIG, LocaleConstant.CAMERA_IP + LocaleConstant.DEVICE_CONTROL_SERVER_CONFIG_KEY, LocaleConstant.CAMERA_IP ) as String if (httpConfig == "") { Log.d(kTag, "executeDeviceCommand: httpConfig is null") diff --git a/app/src/main/java/com/casic/app/safetreecontroller/service/SocketConnectionService.kt b/app/src/main/java/com/casic/app/safetreecontroller/service/SocketConnectionService.kt index f12f7fa..03c1e53 100644 --- a/app/src/main/java/com/casic/app/safetreecontroller/service/SocketConnectionService.kt +++ b/app/src/main/java/com/casic/app/safetreecontroller/service/SocketConnectionService.kt @@ -45,6 +45,14 @@ LocaleConstant.QUERY_CPU_TEMPERATURE_CODE -> { tcpClient.sendMessage(CommandCreator.createCpuTemperatureCommand()) } + + LocaleConstant.INCREASE_VOICE_CODE -> { + tcpClient.sendMessage(CommandCreator.createIncreaseVoiceCommand()) + } + + LocaleConstant.REDUCE_VOICE_CODE -> { + tcpClient.sendMessage(CommandCreator.createReduceVoiceCommand()) + } } return true } @@ -96,6 +104,8 @@ * 激光关返回:[7, 32, 0, 1, 0, 1, -69, 1, 0, -111, 0, 0, -110] * 甲烷浓度返回:[7, 32, 0, 1, 0, 1, -86, 1, 0, 0, 0, 13, 14] * 激光温度返回:[7, 32, 0, 1, 0, 1, -52, 1, 0, -106, 1, 96, -8] + * 音量加返回:[7, 32, 0, 1, 0, 1, -69, 1, 0, -104, 1, 0, -102] + * 音量减返回:[7, 32, 0, 1, 0, 1, -69, 1, 0, -104, 0, 0, -103] * */ if (bytes.size < 6) { Log.d(kTag, "onMessageReceived: 数据异常,长度不够") @@ -130,12 +140,25 @@ } "BB" -> { - //激光状态 if (bytes.size == 13) { - if (bytes[10].toInt() == 1) { - SaveKeyValues.putValue(LocaleConstant.OPEN_METHANE, true) - } else { - SaveKeyValues.putValue(LocaleConstant.OPEN_METHANE, false) + when ((bytes[9].toInt() and 0xFF).toString(16).uppercase()) { + "91" -> { + //激光状态 + if (bytes[10].toInt() == 1) { + SaveKeyValues.putValue(LocaleConstant.OPEN_METHANE_KEY, true) + } else { + SaveKeyValues.putValue(LocaleConstant.OPEN_METHANE_KEY, false) + } + } + + "98" -> { + //音量调节 + if (bytes[10].toInt() == 1) { + Log.d(kTag, "onMessageReceived: 增大音量成功") + } else { + Log.d(kTag, "onMessageReceived: 减小音量成功") + } + } } } } diff --git a/app/src/main/java/com/casic/app/safetreecontroller/utils/CommandCreator.kt b/app/src/main/java/com/casic/app/safetreecontroller/utils/CommandCreator.kt index 59eb314..d2b05c2 100644 --- a/app/src/main/java/com/casic/app/safetreecontroller/utils/CommandCreator.kt +++ b/app/src/main/java/com/casic/app/safetreecontroller/utils/CommandCreator.kt @@ -65,4 +65,18 @@ fun createCpuTemperatureCommand(): ByteArray { return byteArrayOf(0xAA.toByte(), 0x01, 0x00, 0x96.toByte(), 0x00, 0x00, 0x97.toByte()) } + + /** + * 音量增大指令 + * */ + fun createIncreaseVoiceCommand(): ByteArray { + return byteArrayOf(0xAA.toByte(), 0x01, 0x00, 0x98.toByte(), 0x01, 0x00, 0x9A.toByte()) + } + + /** + * 音量减小指令 + * */ + fun createReduceVoiceCommand(): ByteArray { + return byteArrayOf(0xAA.toByte(), 0x01, 0x00, 0x98.toByte(), 0x00, 0x00, 0x99.toByte()) + } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/safetreecontroller/utils/LocaleConstant.kt b/app/src/main/java/com/casic/app/safetreecontroller/utils/LocaleConstant.kt index b7be093..b33f0d0 100644 --- a/app/src/main/java/com/casic/app/safetreecontroller/utils/LocaleConstant.kt +++ b/app/src/main/java/com/casic/app/safetreecontroller/utils/LocaleConstant.kt @@ -18,6 +18,8 @@ const val CLOSE_METHANE_CODE = 20240002 const val UPDATE_GAS_THRESHOLD_CODE = 20240003 const val QUERY_CPU_TEMPERATURE_CODE = 20240004 + const val INCREASE_VOICE_CODE = 20240005 + const val REDUCE_VOICE_CODE = 20240006 /** * Handler Response Code @@ -29,12 +31,13 @@ /*** * SP Key * */ - const val DEVICE_CONTROL_SERVER_CONFIG = "Key_1" - const val OPEN_METHANE = "Key_2" - const val LOCALE_MODE = "Key_4" - const val OPEN_ALARM = "Key_5" - const val OPEN_VOICE = "Key_6" - const val METHANE_DEFAULT_VALUE = "Key_7" + const val DEVICE_CONTROL_SERVER_CONFIG_KEY = "Key_1" + const val OPEN_METHANE_KEY = "Key_2" + const val LOCALE_MODE_KEY = "Key_4" + const val OPEN_ALARM_KEY = "Key_5" + const val OPEN_VOICE_KEY = "Key_6" + const val METHANE_DEFAULT_VALUE_KEY = "Key_7" + const val CURRENT_VOICE_VALUE_KEY = "Key_8" //相机IP const val CAMERA_IP = "192.168.10.137" diff --git a/app/src/main/res/layout/fragment_voice_settings.xml b/app/src/main/res/layout/fragment_voice_settings.xml index d4aec40..cc0d086 100644 --- a/app/src/main/res/layout/fragment_voice_settings.xml +++ b/app/src/main/res/layout/fragment_voice_settings.xml @@ -46,9 +46,10 @@ android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" - android:value="20f" + android:stepSize="1" + android:value="5f" android:valueFrom="0f" - android:valueTo="100f" + android:valueTo="5f" app:labelBehavior="gone" />