修改识别模式
This commit is contained in:
@@ -1,17 +1,38 @@
|
||||
package com.example.defenseapplication.liveVideo
|
||||
|
||||
import TimePickerFragment
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.widget.Toast
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import com.example.defenseapplication.R
|
||||
import com.example.defenseapplication.databinding.AutoModeActivityBinding
|
||||
import com.example.defenseapplication.utils.DeviceIdEvent
|
||||
import com.example.defenseapplication.utils.DeviceUpdateEvent
|
||||
import com.example.defenseapplication.utils.RetrofitNetwork
|
||||
import com.example.defenseapplication.view.AutoModeNoticeDialog
|
||||
import com.gyf.immersionbar.ImmersionBar
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.util.Calendar
|
||||
//自动模式
|
||||
class AutoModeActivity : AppCompatActivity() {
|
||||
|
||||
private lateinit var binding: AutoModeActivityBinding
|
||||
private lateinit var calendar: Calendar
|
||||
private var selectedMode = "0"
|
||||
private var selectedYear = 0
|
||||
private var selectedMonth = 0
|
||||
private var selectedDay = 0
|
||||
private var selectedHour = 0
|
||||
private var selectedMinute = 0
|
||||
private var selectedStartHour = 21
|
||||
private var selectedStartMinute = 30
|
||||
private var selectedEndHour = 8
|
||||
private var selectedEndMinute = 30
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
binding = AutoModeActivityBinding.inflate(layoutInflater)
|
||||
@@ -22,8 +43,15 @@ class AutoModeActivity : AppCompatActivity() {
|
||||
.titleBar(binding.topBar)
|
||||
.init()
|
||||
|
||||
calendar = Calendar.getInstance()
|
||||
selectedYear = calendar.get(Calendar.YEAR)
|
||||
selectedMonth = calendar.get(Calendar.MONTH)
|
||||
selectedDay = calendar.get(Calendar.DAY_OF_MONTH)
|
||||
|
||||
initListener()
|
||||
|
||||
loadCurrentSettings()
|
||||
|
||||
showAutoModeNoticeDialog()
|
||||
}
|
||||
|
||||
@@ -32,43 +60,195 @@ class AutoModeActivity : AppCompatActivity() {
|
||||
finish()
|
||||
}
|
||||
|
||||
binding.rbAutoMode.setOnClickListener {
|
||||
binding.rbSemiAutoMode.isChecked = false
|
||||
showAutoModeNoticeDialog()
|
||||
binding.rbAutoMode.setOnCheckedChangeListener { _, isChecked ->
|
||||
if (isChecked) {
|
||||
selectedMode = "0"
|
||||
showAutoModeNoticeDialog()
|
||||
binding.time.visibility = android.view.View.VISIBLE
|
||||
}
|
||||
}
|
||||
|
||||
binding.rbSemiAutoMode.setOnClickListener {
|
||||
binding.rbAutoMode.isChecked = false
|
||||
binding.rbSemiAutoMode.setOnCheckedChangeListener { _, isChecked ->
|
||||
if (isChecked) {
|
||||
selectedMode = "1"
|
||||
binding.time.visibility = android.view.View.GONE
|
||||
}
|
||||
}
|
||||
// 起始时间选择
|
||||
|
||||
binding.itemStartTime.setOnClickListener {
|
||||
showTimePicker { hour, minute ->
|
||||
val startTimeStr = String.format("%02d:%02d", hour, minute)
|
||||
showDateTimePicker { year, month, day, hour, minute ->
|
||||
selectedStartHour = hour
|
||||
selectedStartMinute = minute
|
||||
val startTimeStr = String.format("%04d-%02d-%02d %02d:%02d", year, month + 1, day, hour, minute)
|
||||
binding.tvStartTime.text = startTimeStr
|
||||
}
|
||||
}
|
||||
// 结束时间选择
|
||||
|
||||
binding.itemEndTime.setOnClickListener {
|
||||
showTimePicker { hour, minute ->
|
||||
val endTimeStr = String.format("%02d:%02d", hour, minute)
|
||||
showDateTimePicker { year, month, day, hour, minute ->
|
||||
selectedEndHour = hour
|
||||
selectedEndMinute = minute
|
||||
val endTimeStr = String.format("%04d-%02d-%02d %02d:%02d", year, month + 1, day, hour, minute)
|
||||
binding.tvEndTime.text = endTimeStr
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 显示时间选择器对话框
|
||||
* @param onTimeSelected 时间选择回调,参数为小时和分钟
|
||||
*/
|
||||
|
||||
private fun loadCurrentSettings() {
|
||||
val deviceId = DeviceIdEvent.getDeviceId()
|
||||
if (deviceId.isNullOrEmpty()) {
|
||||
return
|
||||
}
|
||||
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
try {
|
||||
val response = RetrofitNetwork.getNetworkService().getDeviceInfo(deviceId)
|
||||
withContext(Dispatchers.Main) {
|
||||
if (response.code == 200 && response.data != null) {
|
||||
response.data.workMode?.let { mode ->
|
||||
selectedMode = mode
|
||||
if (mode == "0") {
|
||||
binding.rbAutoMode.isChecked = true
|
||||
binding.rbSemiAutoMode.isChecked = false
|
||||
binding.time.visibility = android.view.View.VISIBLE
|
||||
} else {
|
||||
binding.rbAutoMode.isChecked = false
|
||||
binding.rbSemiAutoMode.isChecked = true
|
||||
binding.time.visibility = android.view.View.GONE
|
||||
}
|
||||
}
|
||||
response.data.startTime?.let { startTime ->
|
||||
binding.tvStartTime.text = if (startTime.length >= 5) {
|
||||
if (startTime.contains("-")) {
|
||||
startTime
|
||||
} else {
|
||||
String.format("%04d-%02d-%02d %s", selectedYear, selectedMonth + 1, selectedDay, startTime)
|
||||
}
|
||||
} else {
|
||||
String.format("%04d-%02d-%02d %02d:%02d", selectedYear, selectedMonth + 1, selectedDay, 21, 30)
|
||||
}
|
||||
parseDateTime(startTime, true)
|
||||
}
|
||||
response.data.endTime?.let { endTime ->
|
||||
binding.tvEndTime.text = if (endTime.length >= 5) {
|
||||
if (endTime.contains("-")) {
|
||||
endTime
|
||||
} else {
|
||||
String.format("%04d-%02d-%02d %s", selectedYear, selectedMonth + 1, selectedDay, endTime)
|
||||
}
|
||||
} else {
|
||||
String.format("%04d-%02d-%02d %02d:%02d", selectedYear, selectedMonth + 1, selectedDay, 8, 30)
|
||||
}
|
||||
parseDateTime(endTime, false)
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun showTimePicker(onTimeSelected: (hour: Int, minute: Int) -> Unit) {
|
||||
val timePickerFragment = TimePickerFragment { hour, minute ->
|
||||
onTimeSelected(hour, minute)
|
||||
}
|
||||
timePickerFragment.show(supportFragmentManager, "timePicker")
|
||||
}
|
||||
|
||||
private fun showDateTimePicker(onDateTimeSelected: (year: Int, month: Int, day: Int, hour: Int, minute: Int) -> Unit) {
|
||||
|
||||
}
|
||||
|
||||
private fun parseDateTime(dateTimeStr: String, isStartTime: Boolean) {
|
||||
if (dateTimeStr.contains("-") && dateTimeStr.contains(" ")) {
|
||||
val dateTimeParts = dateTimeStr.split(" ")
|
||||
if (dateTimeParts.size == 2) {
|
||||
val dateParts = dateTimeParts[0].split("-")
|
||||
val timeParts = dateTimeParts[1].split(":")
|
||||
if (dateParts.size == 3 && timeParts.size == 2) {
|
||||
if (isStartTime) {
|
||||
selectedStartHour = timeParts[0].toIntOrNull() ?: 21
|
||||
selectedStartMinute = timeParts[1].toIntOrNull() ?: 30
|
||||
} else {
|
||||
selectedEndHour = timeParts[0].toIntOrNull() ?: 8
|
||||
selectedEndMinute = timeParts[1].toIntOrNull() ?: 30
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
if (dateTimeStr.contains(":")) {
|
||||
val timeParts = dateTimeStr.split(":")
|
||||
if (timeParts.size == 2) {
|
||||
if (isStartTime) {
|
||||
selectedStartHour = timeParts[0].toIntOrNull() ?: 21
|
||||
selectedStartMinute = timeParts[1].toIntOrNull() ?: 30
|
||||
} else {
|
||||
selectedEndHour = timeParts[0].toIntOrNull() ?: 8
|
||||
selectedEndMinute = timeParts[1].toIntOrNull() ?: 30
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun showAutoModeNoticeDialog() {
|
||||
val dialog = AutoModeNoticeDialog(this)
|
||||
dialog.setOnConfirmListener {
|
||||
dialog.setOnConfirmListener { year, month, day, hour, minute ->
|
||||
selectedYear = year
|
||||
selectedMonth = month
|
||||
selectedDay = day
|
||||
selectedHour = hour
|
||||
selectedMinute = minute
|
||||
saveAutoModeSettings()
|
||||
}
|
||||
dialog.show()
|
||||
}
|
||||
|
||||
private fun saveAutoModeSettings() {
|
||||
val deviceId = DeviceIdEvent.getDeviceId()
|
||||
if (deviceId.isNullOrEmpty()) {
|
||||
Toast.makeText(this, "设备ID为空", Toast.LENGTH_SHORT).show()
|
||||
return
|
||||
}
|
||||
|
||||
val startTime = String.format("%04d-%02d-%02d %02d:%02d", selectedYear, selectedMonth + 1, selectedDay, selectedStartHour, selectedStartMinute)
|
||||
val endTime = String.format("%04d-%02d-%02d %02d:%02d", selectedYear, selectedMonth + 1, selectedDay, selectedEndHour, selectedEndMinute)
|
||||
val effectiveTime = String.format("%04d-%02d-%02d %02d:%02d", selectedYear, selectedMonth + 1, selectedDay, selectedHour, selectedMinute)
|
||||
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
try {
|
||||
val deviceBean = com.example.defenseapplication.bean.DeviceBean(
|
||||
id = deviceId,
|
||||
workMode = selectedMode,
|
||||
startTime = startTime,
|
||||
endTime = endTime
|
||||
)
|
||||
val response = RetrofitNetwork.getNetworkService().updateDeviceInfo(deviceBean)
|
||||
withContext(Dispatchers.Main) {
|
||||
if (response.code == 200) {
|
||||
val modeText = if (selectedMode == "0") {
|
||||
getString(R.string.full_auto_mode)
|
||||
} else {
|
||||
getString(R.string.semi_auto_mode)
|
||||
}
|
||||
val resultIntent = Intent()
|
||||
resultIntent.putExtra("auto_mode", modeText)
|
||||
resultIntent.putExtra("auto_mode_value", selectedMode)
|
||||
resultIntent.putExtra("effective_time", effectiveTime)
|
||||
setResult(RESULT_OK, resultIntent)
|
||||
Toast.makeText(this@AutoModeActivity, "修改成功,生效时间:$effectiveTime", Toast.LENGTH_SHORT).show()
|
||||
DeviceUpdateEvent.notifyDeviceUpdated(deviceId)
|
||||
} else {
|
||||
Toast.makeText(this@AutoModeActivity, response.msg, Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
withContext(Dispatchers.Main) {
|
||||
Toast.makeText(this@AutoModeActivity, "网络请求失败", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,8 +56,8 @@ class DeviceInfoActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
private fun updateDeviceInfo(device: com.example.defenseapplication.bean.DeviceBean) {
|
||||
binding.tvDeviceModelValue.text = device.deviceNo.toString() ?: "未知型号"
|
||||
binding.tvDeviceNoValue.text = device.startTime.toString() ?: "自动布防开始时间"
|
||||
binding.tvFwVersionValue.text = device.endTime.toString() ?: "自动布防结束时间"
|
||||
binding.tvDeviceModelValue.text = device.deviceNo.toString()
|
||||
binding.tvDeviceNoValue.text = device.deviceSn.toString()
|
||||
binding.tvFwVersionValue.text = device.fwVer.toString()
|
||||
}
|
||||
}
|
||||
@@ -1,19 +1,30 @@
|
||||
package com.example.defenseapplication.liveVideo
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.widget.Toast
|
||||
import com.example.defenseapplication.R
|
||||
import com.example.defenseapplication.base.BaseActivity
|
||||
import com.example.defenseapplication.databinding.SettingsActivityBinding
|
||||
import com.example.defenseapplication.utils.DeviceIdEvent
|
||||
import com.example.defenseapplication.utils.DeviceUpdateEvent
|
||||
import com.example.defenseapplication.utils.LanguageUtil
|
||||
import com.example.defenseapplication.utils.RetrofitNetwork
|
||||
import com.example.defenseapplication.view.CustomDialogFragmentText
|
||||
import com.example.defenseapplication.view.LanguageDialog
|
||||
import com.example.defenseapplication.view.RecognitionModeDialog
|
||||
import com.gyf.immersionbar.ImmersionBar
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
//设置页面
|
||||
class SettingsActivity : BaseActivity() {
|
||||
|
||||
private lateinit var binding: SettingsActivityBinding
|
||||
private val SP_NAME = "DeviceSettings"
|
||||
private val KEY_RECOGNITION_MODE = "recognition_mode"
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
@@ -35,7 +46,7 @@ class SettingsActivity : BaseActivity() {
|
||||
binding.itemRecognitionMode.setOnClickListener {
|
||||
val dialog = RecognitionModeDialog(this)
|
||||
dialog.setOnConfirmListener { mode ->
|
||||
binding.tvRecognitionMode.text = mode
|
||||
updateRecognitionMode(mode)
|
||||
}
|
||||
dialog.show()
|
||||
}
|
||||
@@ -45,7 +56,7 @@ class SettingsActivity : BaseActivity() {
|
||||
.setTitle(getString(R.string.device_name))
|
||||
.setInputText(binding.tvDeviceName.text.toString())
|
||||
dialog.setOnConfirmListener { name ->
|
||||
binding.tvDeviceName.text = name
|
||||
updateDeviceName(name)
|
||||
}
|
||||
dialog.show(supportFragmentManager, "CustomDialogFragment")
|
||||
}
|
||||
@@ -84,6 +95,136 @@ class SettingsActivity : BaseActivity() {
|
||||
val intent = Intent(this, WiFiManagementActivity::class.java)
|
||||
startActivity(intent)
|
||||
}
|
||||
fetchDeviceInfo()
|
||||
}
|
||||
|
||||
private fun fetchDeviceInfo() {
|
||||
val deviceId = DeviceIdEvent.getDeviceId()
|
||||
if (deviceId.isNullOrEmpty()) {
|
||||
Toast.makeText(this, "设备ID为空", Toast.LENGTH_SHORT).show()
|
||||
return
|
||||
}
|
||||
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
try {
|
||||
val response = RetrofitNetwork.getNetworkService().getDeviceInfo(deviceId)
|
||||
withContext(Dispatchers.Main) {
|
||||
if (response.code == 200 && response.data != null) {
|
||||
updateDeviceInfo(response.data)
|
||||
} else {
|
||||
Toast.makeText(this@SettingsActivity, response.msg, Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
withContext(Dispatchers.Main) {
|
||||
Toast.makeText(this@SettingsActivity, "网络请求失败", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateDeviceInfo(device: com.example.defenseapplication.bean.DeviceBean) {
|
||||
binding.tvDeviceName.text = device.deviceName ?: "未设置"
|
||||
binding.tvWifiName.text = device.wifiName ?: "未设置"
|
||||
|
||||
val serverMode = device.patternPerception
|
||||
val cacheMode = getCachedRecognitionMode()
|
||||
|
||||
binding.tvRecognitionMode.text = when {
|
||||
!serverMode.isNullOrEmpty() -> {
|
||||
val displayMode = when (serverMode) {
|
||||
"0" -> getString(R.string.bluetooth_recognition)
|
||||
"1" -> getString(R.string.bluetooth_face_recognition)
|
||||
else -> serverMode
|
||||
}
|
||||
saveRecognitionMode(displayMode)
|
||||
displayMode
|
||||
}
|
||||
!cacheMode.isNullOrEmpty() -> cacheMode
|
||||
else -> getString(R.string.bluetooth_recognition)
|
||||
}
|
||||
}
|
||||
|
||||
private fun getCachedRecognitionMode(): String? {
|
||||
val sp = getSharedPreferences("DeviceSettings", Context.MODE_PRIVATE)
|
||||
return sp.getString("recognition_mode", null)
|
||||
}
|
||||
|
||||
private fun saveRecognitionMode(mode: String) {
|
||||
val sp = getSharedPreferences("DeviceSettings", Context.MODE_PRIVATE)
|
||||
sp.edit().putString("recognition_mode", mode).apply()
|
||||
}
|
||||
|
||||
private fun updateDeviceName(name: String) {
|
||||
val deviceId = DeviceIdEvent.getDeviceId()
|
||||
if (deviceId.isNullOrEmpty()) {
|
||||
Toast.makeText(this, "设备ID为空", Toast.LENGTH_SHORT).show()
|
||||
return
|
||||
}
|
||||
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
try {
|
||||
val deviceBean = com.example.defenseapplication.bean.DeviceBean(
|
||||
id = deviceId,
|
||||
deviceName = name,
|
||||
)
|
||||
val response = RetrofitNetwork.getNetworkService().updateDeviceInfo(deviceBean)
|
||||
withContext(Dispatchers.Main) {
|
||||
if (response.code == 200) {
|
||||
binding.tvDeviceName.text = name
|
||||
Toast.makeText(this@SettingsActivity, "修改成功", Toast.LENGTH_SHORT).show()
|
||||
DeviceUpdateEvent.notifyDeviceUpdated(deviceId)
|
||||
} else {
|
||||
Toast.makeText(this@SettingsActivity, response.msg, Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
withContext(Dispatchers.Main) {
|
||||
Toast.makeText(this@SettingsActivity, "网络请求失败", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateRecognitionMode(mode: String) {
|
||||
val deviceId = DeviceIdEvent.getDeviceId()
|
||||
if (deviceId.isNullOrEmpty()) {
|
||||
Toast.makeText(this, "设备ID为空", Toast.LENGTH_SHORT).show()
|
||||
return
|
||||
}
|
||||
|
||||
val patternPerception = when (mode) {
|
||||
getString(R.string.bluetooth_recognition) -> "0"
|
||||
getString(R.string.bluetooth_face_recognition) -> "1"
|
||||
else -> "0"
|
||||
}
|
||||
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
try {
|
||||
val deviceBean = com.example.defenseapplication.bean.DeviceBean(
|
||||
id = deviceId,
|
||||
patternPerception = patternPerception,
|
||||
)
|
||||
val response = RetrofitNetwork.getNetworkService().updateDeviceInfo(deviceBean)
|
||||
withContext(Dispatchers.Main) {
|
||||
if (response.code == 200) {
|
||||
binding.tvRecognitionMode.text = mode
|
||||
saveRecognitionMode(mode)
|
||||
Toast.makeText(this@SettingsActivity, "修改成功", Toast.LENGTH_SHORT).show()
|
||||
DeviceUpdateEvent.notifyDeviceUpdated(deviceId)
|
||||
} else {
|
||||
Toast.makeText(this@SettingsActivity, response.msg, Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
withContext(Dispatchers.Main) {
|
||||
Toast.makeText(this@SettingsActivity, "网络请求失败", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user