diff --git a/app/build.gradle b/app/build.gradle index 68ad697..cf47c01 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -18,8 +18,8 @@ applicationId "com.casic.app.safetreecontroller" minSdkVersion 23 targetSdkVersion 33 - versionCode 1004 - versionName "1.0.0.4" + versionCode 1005 + versionName "1.0.0.5" ndk { abiFilters "arm64-v8a", "armeabi-v7a", "x86_64", "x86" } diff --git a/app/build.gradle b/app/build.gradle index 68ad697..cf47c01 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -18,8 +18,8 @@ applicationId "com.casic.app.safetreecontroller" minSdkVersion 23 targetSdkVersion 33 - versionCode 1004 - versionName "1.0.0.4" + versionCode 1005 + versionName "1.0.0.5" ndk { abiFilters "arm64-v8a", "armeabi-v7a", "x86_64", "x86" } diff --git a/app/src/main/java/com/casic/app/safetreecontroller/fragments/BaseSettingsFragment.kt b/app/src/main/java/com/casic/app/safetreecontroller/fragments/BaseSettingsFragment.kt index 164ced0..30056ca 100644 --- a/app/src/main/java/com/casic/app/safetreecontroller/fragments/BaseSettingsFragment.kt +++ b/app/src/main/java/com/casic/app/safetreecontroller/fragments/BaseSettingsFragment.kt @@ -1,15 +1,8 @@ package com.casic.app.safetreecontroller.fragments -import android.content.BroadcastReceiver -import android.content.Context -import android.content.Intent -import android.net.NetworkInfo -import android.net.wifi.WifiManager -import android.os.Build import android.os.Bundle import android.os.Handler import android.os.Message -import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -18,15 +11,12 @@ 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.extensions.getSystemService import com.pengxh.kt.lite.extensions.show -import com.pengxh.kt.lite.utils.BroadcastManager import com.pengxh.kt.lite.utils.WeakReferenceHandler import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay import kotlinx.coroutines.launch - class BaseSettingsFragment : KotlinBaseFragment(), Handler.Callback { @@ -35,51 +25,14 @@ } private val kTag = "BaseSettingsFragment" - private val broadcastManager by lazy { BroadcastManager(requireContext()) } - private val wifi by lazy { requireContext().getSystemService()!! } private var isGetBoardConfig = false + private var isGetWiFiConfig = false private var isGetAlgorithmConfig = false private var isUseAlgorithm = false private var isGetServerConfig = false override fun initOnCreate(savedInstanceState: Bundle?) { weakReferenceHandler = WeakReferenceHandler(this) - broadcastManager.addAction(object : BroadcastReceiver() { - override fun onReceive(context: Context?, intent: Intent?) { - intent?.apply { - if (action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) { - val wifiState = intent.getIntExtra( - WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN - ) - if (wifiState == WifiManager.WIFI_STATE_ENABLED) { - //WiFi已关闭 - binding.wifiNameView.text = "WiFi已关闭" - } - } else if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) { - val net = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - intent.getParcelableExtra( - WifiManager.EXTRA_NETWORK_INFO, NetworkInfo::class.java - ) - } else { - intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO) - } - - if (net == null) { - "NetworkInfo is null".show(requireContext()) - return - } - if (net.isConnected) { - binding.wifiNameView.text = wifi.connectionInfo.ssid - .removePrefix("\"") - .removeSuffix("\"") - } else { - //WiFi断开 - binding.wifiNameView.text = "WiFi已断开" - } - } - } - } - }, WifiManager.WIFI_STATE_CHANGED_ACTION, WifiManager.NETWORK_STATE_CHANGED_ACTION) } override fun initViewBinding( @@ -108,6 +61,14 @@ delay(500) + while (!isGetWiFiConfig) { + //查询安全树连接的WiFi配置 + SocketConnectionService.weakReferenceHandler?.sendEmptyMessage(LocaleConstant.QUERY_WIFI_CONFIG_CODE) + delay(1000) + } + + delay(500) + while (!isGetAlgorithmConfig) { //查询是否配合一体机使用 SocketConnectionService.weakReferenceHandler?.sendEmptyMessage(LocaleConstant.QUERY_IS_USE_AI_CODE) @@ -126,7 +87,7 @@ } override fun initEvent() { - binding.configurationButton.setOnClickListener { + binding.updateWiFiConfigButton.setOnClickListener { val password = binding.passwordView.text.toString() if (password.isBlank()) { "操作失败,WiFi密码为空!".show(requireContext()) @@ -136,28 +97,38 @@ val weakReferenceHandler = SocketConnectionService.weakReferenceHandler ?: return@setOnClickListener lifecycleScope.launch(Dispatchers.IO) { - if (!isUseAlgorithm) { - val serverHost = binding.serverHostView.text.toString() - val serverPort = binding.serverPortView.text.toString() - if (serverHost.isNotBlank() && serverPort.isNotBlank()) { - //需要设置远程服务器IP、端口号、WiFi - val message = weakReferenceHandler.obtainMessage() - message.what = LocaleConstant.SET_BOARD_SERVER_CONFIG_CODE - message.obj = arrayOf(serverHost, serverPort) - weakReferenceHandler.sendMessage(message) - delay(1000) - } else { - Log.d(kTag, "initEvent: 服务器配置参数异常,跳过此项配置") - } - } - - //不管什么模式,WiFi都可以设置 val message = weakReferenceHandler.obtainMessage() message.what = LocaleConstant.SET_BOARD_WIFI_CONFIG_CODE message.obj = arrayOf(binding.wifiNameView.text.toString(), password) weakReferenceHandler.sendMessage(message) } } + + binding.updateServerConfigButton.setOnClickListener { + val serverHost = binding.serverHostView.text.toString() + if (serverHost.isBlank()) { + "操作失败,服务器IP为空!".show(requireContext()) + return@setOnClickListener + } + + val serverPort = binding.serverPortView.text.toString() + if (serverPort.isBlank()) { + "操作失败,服务器端口为空!".show(requireContext()) + return@setOnClickListener + } + + val weakReferenceHandler = SocketConnectionService.weakReferenceHandler + ?: return@setOnClickListener + lifecycleScope.launch(Dispatchers.IO) { + if (!isUseAlgorithm) { + //需要设置远程服务器IP、端口号 + val message = weakReferenceHandler.obtainMessage() + message.what = LocaleConstant.SET_BOARD_SERVER_CONFIG_CODE + message.obj = arrayOf(serverHost, serverPort) + weakReferenceHandler.sendMessage(message) + } + } + } } override fun handleMessage(msg: Message): Boolean { @@ -166,6 +137,16 @@ binding.deviceCodeView.text = msg.obj as String } + LocaleConstant.WIFI_CONFIG_RESPONSE_CODE -> { + //AQS_20010002,abcd1234 + val data = msg.obj as String + isGetWiFiConfig = true + //分割数据 + val list = data.split(",") + binding.wifiNameView.text = list[0] + binding.passwordView.setText(list[1]) + } + LocaleConstant.QUERY_IS_USE_AI_RESPONSE_CODE -> { //配合一体机使用-0,单独使用-1 val state = msg.obj as String @@ -208,20 +189,4 @@ } return true } - - override fun onResume() { - super.onResume() - binding.wifiNameView.text = wifi.connectionInfo.ssid.removePrefix("\"").removeSuffix("\"") - } - - /** - * onDestroyView -> onDestroy -> onDetach - * */ - override fun onDestroy() { - super.onDestroy() - broadcastManager.destroy( - WifiManager.WIFI_STATE_CHANGED_ACTION, - WifiManager.NETWORK_STATE_CHANGED_ACTION - ) - } } \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index 68ad697..cf47c01 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -18,8 +18,8 @@ applicationId "com.casic.app.safetreecontroller" minSdkVersion 23 targetSdkVersion 33 - versionCode 1004 - versionName "1.0.0.4" + versionCode 1005 + versionName "1.0.0.5" ndk { abiFilters "arm64-v8a", "armeabi-v7a", "x86_64", "x86" } diff --git a/app/src/main/java/com/casic/app/safetreecontroller/fragments/BaseSettingsFragment.kt b/app/src/main/java/com/casic/app/safetreecontroller/fragments/BaseSettingsFragment.kt index 164ced0..30056ca 100644 --- a/app/src/main/java/com/casic/app/safetreecontroller/fragments/BaseSettingsFragment.kt +++ b/app/src/main/java/com/casic/app/safetreecontroller/fragments/BaseSettingsFragment.kt @@ -1,15 +1,8 @@ package com.casic.app.safetreecontroller.fragments -import android.content.BroadcastReceiver -import android.content.Context -import android.content.Intent -import android.net.NetworkInfo -import android.net.wifi.WifiManager -import android.os.Build import android.os.Bundle import android.os.Handler import android.os.Message -import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -18,15 +11,12 @@ 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.extensions.getSystemService import com.pengxh.kt.lite.extensions.show -import com.pengxh.kt.lite.utils.BroadcastManager import com.pengxh.kt.lite.utils.WeakReferenceHandler import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay import kotlinx.coroutines.launch - class BaseSettingsFragment : KotlinBaseFragment(), Handler.Callback { @@ -35,51 +25,14 @@ } private val kTag = "BaseSettingsFragment" - private val broadcastManager by lazy { BroadcastManager(requireContext()) } - private val wifi by lazy { requireContext().getSystemService()!! } private var isGetBoardConfig = false + private var isGetWiFiConfig = false private var isGetAlgorithmConfig = false private var isUseAlgorithm = false private var isGetServerConfig = false override fun initOnCreate(savedInstanceState: Bundle?) { weakReferenceHandler = WeakReferenceHandler(this) - broadcastManager.addAction(object : BroadcastReceiver() { - override fun onReceive(context: Context?, intent: Intent?) { - intent?.apply { - if (action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) { - val wifiState = intent.getIntExtra( - WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN - ) - if (wifiState == WifiManager.WIFI_STATE_ENABLED) { - //WiFi已关闭 - binding.wifiNameView.text = "WiFi已关闭" - } - } else if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) { - val net = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - intent.getParcelableExtra( - WifiManager.EXTRA_NETWORK_INFO, NetworkInfo::class.java - ) - } else { - intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO) - } - - if (net == null) { - "NetworkInfo is null".show(requireContext()) - return - } - if (net.isConnected) { - binding.wifiNameView.text = wifi.connectionInfo.ssid - .removePrefix("\"") - .removeSuffix("\"") - } else { - //WiFi断开 - binding.wifiNameView.text = "WiFi已断开" - } - } - } - } - }, WifiManager.WIFI_STATE_CHANGED_ACTION, WifiManager.NETWORK_STATE_CHANGED_ACTION) } override fun initViewBinding( @@ -108,6 +61,14 @@ delay(500) + while (!isGetWiFiConfig) { + //查询安全树连接的WiFi配置 + SocketConnectionService.weakReferenceHandler?.sendEmptyMessage(LocaleConstant.QUERY_WIFI_CONFIG_CODE) + delay(1000) + } + + delay(500) + while (!isGetAlgorithmConfig) { //查询是否配合一体机使用 SocketConnectionService.weakReferenceHandler?.sendEmptyMessage(LocaleConstant.QUERY_IS_USE_AI_CODE) @@ -126,7 +87,7 @@ } override fun initEvent() { - binding.configurationButton.setOnClickListener { + binding.updateWiFiConfigButton.setOnClickListener { val password = binding.passwordView.text.toString() if (password.isBlank()) { "操作失败,WiFi密码为空!".show(requireContext()) @@ -136,28 +97,38 @@ val weakReferenceHandler = SocketConnectionService.weakReferenceHandler ?: return@setOnClickListener lifecycleScope.launch(Dispatchers.IO) { - if (!isUseAlgorithm) { - val serverHost = binding.serverHostView.text.toString() - val serverPort = binding.serverPortView.text.toString() - if (serverHost.isNotBlank() && serverPort.isNotBlank()) { - //需要设置远程服务器IP、端口号、WiFi - val message = weakReferenceHandler.obtainMessage() - message.what = LocaleConstant.SET_BOARD_SERVER_CONFIG_CODE - message.obj = arrayOf(serverHost, serverPort) - weakReferenceHandler.sendMessage(message) - delay(1000) - } else { - Log.d(kTag, "initEvent: 服务器配置参数异常,跳过此项配置") - } - } - - //不管什么模式,WiFi都可以设置 val message = weakReferenceHandler.obtainMessage() message.what = LocaleConstant.SET_BOARD_WIFI_CONFIG_CODE message.obj = arrayOf(binding.wifiNameView.text.toString(), password) weakReferenceHandler.sendMessage(message) } } + + binding.updateServerConfigButton.setOnClickListener { + val serverHost = binding.serverHostView.text.toString() + if (serverHost.isBlank()) { + "操作失败,服务器IP为空!".show(requireContext()) + return@setOnClickListener + } + + val serverPort = binding.serverPortView.text.toString() + if (serverPort.isBlank()) { + "操作失败,服务器端口为空!".show(requireContext()) + return@setOnClickListener + } + + val weakReferenceHandler = SocketConnectionService.weakReferenceHandler + ?: return@setOnClickListener + lifecycleScope.launch(Dispatchers.IO) { + if (!isUseAlgorithm) { + //需要设置远程服务器IP、端口号 + val message = weakReferenceHandler.obtainMessage() + message.what = LocaleConstant.SET_BOARD_SERVER_CONFIG_CODE + message.obj = arrayOf(serverHost, serverPort) + weakReferenceHandler.sendMessage(message) + } + } + } } override fun handleMessage(msg: Message): Boolean { @@ -166,6 +137,16 @@ binding.deviceCodeView.text = msg.obj as String } + LocaleConstant.WIFI_CONFIG_RESPONSE_CODE -> { + //AQS_20010002,abcd1234 + val data = msg.obj as String + isGetWiFiConfig = true + //分割数据 + val list = data.split(",") + binding.wifiNameView.text = list[0] + binding.passwordView.setText(list[1]) + } + LocaleConstant.QUERY_IS_USE_AI_RESPONSE_CODE -> { //配合一体机使用-0,单独使用-1 val state = msg.obj as String @@ -208,20 +189,4 @@ } return true } - - override fun onResume() { - super.onResume() - binding.wifiNameView.text = wifi.connectionInfo.ssid.removePrefix("\"").removeSuffix("\"") - } - - /** - * onDestroyView -> onDestroy -> onDetach - * */ - override fun onDestroy() { - super.onDestroy() - broadcastManager.destroy( - WifiManager.WIFI_STATE_CHANGED_ACTION, - WifiManager.NETWORK_STATE_CHANGED_ACTION - ) - } } \ No newline at end of file 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 42736a1..20d1d20 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 @@ -109,6 +109,10 @@ tcpClient.sendMessage(CommandCreator.queryControlBoardConfig()) } + LocaleConstant.QUERY_WIFI_CONFIG_CODE -> { + tcpClient.sendMessage(CommandCreator.queryWiFiConfig()) + } + LocaleConstant.QUERY_IS_USE_AI_CODE -> { tcpClient.sendMessage(CommandCreator.queryIsUseAI()) } @@ -195,6 +199,13 @@ weakReferenceHandler.sendMessage(message) } + '2' -> { + val message = weakReferenceHandler.obtainMessage() + message.what = LocaleConstant.WIFI_CONFIG_RESPONSE_CODE + message.obj = data + weakReferenceHandler.sendMessage(message) + } + '4' -> { val message = weakReferenceHandler.obtainMessage() message.what = LocaleConstant.QUERY_IS_USE_AI_RESPONSE_CODE diff --git a/app/build.gradle b/app/build.gradle index 68ad697..cf47c01 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -18,8 +18,8 @@ applicationId "com.casic.app.safetreecontroller" minSdkVersion 23 targetSdkVersion 33 - versionCode 1004 - versionName "1.0.0.4" + versionCode 1005 + versionName "1.0.0.5" ndk { abiFilters "arm64-v8a", "armeabi-v7a", "x86_64", "x86" } diff --git a/app/src/main/java/com/casic/app/safetreecontroller/fragments/BaseSettingsFragment.kt b/app/src/main/java/com/casic/app/safetreecontroller/fragments/BaseSettingsFragment.kt index 164ced0..30056ca 100644 --- a/app/src/main/java/com/casic/app/safetreecontroller/fragments/BaseSettingsFragment.kt +++ b/app/src/main/java/com/casic/app/safetreecontroller/fragments/BaseSettingsFragment.kt @@ -1,15 +1,8 @@ package com.casic.app.safetreecontroller.fragments -import android.content.BroadcastReceiver -import android.content.Context -import android.content.Intent -import android.net.NetworkInfo -import android.net.wifi.WifiManager -import android.os.Build import android.os.Bundle import android.os.Handler import android.os.Message -import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -18,15 +11,12 @@ 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.extensions.getSystemService import com.pengxh.kt.lite.extensions.show -import com.pengxh.kt.lite.utils.BroadcastManager import com.pengxh.kt.lite.utils.WeakReferenceHandler import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay import kotlinx.coroutines.launch - class BaseSettingsFragment : KotlinBaseFragment(), Handler.Callback { @@ -35,51 +25,14 @@ } private val kTag = "BaseSettingsFragment" - private val broadcastManager by lazy { BroadcastManager(requireContext()) } - private val wifi by lazy { requireContext().getSystemService()!! } private var isGetBoardConfig = false + private var isGetWiFiConfig = false private var isGetAlgorithmConfig = false private var isUseAlgorithm = false private var isGetServerConfig = false override fun initOnCreate(savedInstanceState: Bundle?) { weakReferenceHandler = WeakReferenceHandler(this) - broadcastManager.addAction(object : BroadcastReceiver() { - override fun onReceive(context: Context?, intent: Intent?) { - intent?.apply { - if (action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) { - val wifiState = intent.getIntExtra( - WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN - ) - if (wifiState == WifiManager.WIFI_STATE_ENABLED) { - //WiFi已关闭 - binding.wifiNameView.text = "WiFi已关闭" - } - } else if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) { - val net = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - intent.getParcelableExtra( - WifiManager.EXTRA_NETWORK_INFO, NetworkInfo::class.java - ) - } else { - intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO) - } - - if (net == null) { - "NetworkInfo is null".show(requireContext()) - return - } - if (net.isConnected) { - binding.wifiNameView.text = wifi.connectionInfo.ssid - .removePrefix("\"") - .removeSuffix("\"") - } else { - //WiFi断开 - binding.wifiNameView.text = "WiFi已断开" - } - } - } - } - }, WifiManager.WIFI_STATE_CHANGED_ACTION, WifiManager.NETWORK_STATE_CHANGED_ACTION) } override fun initViewBinding( @@ -108,6 +61,14 @@ delay(500) + while (!isGetWiFiConfig) { + //查询安全树连接的WiFi配置 + SocketConnectionService.weakReferenceHandler?.sendEmptyMessage(LocaleConstant.QUERY_WIFI_CONFIG_CODE) + delay(1000) + } + + delay(500) + while (!isGetAlgorithmConfig) { //查询是否配合一体机使用 SocketConnectionService.weakReferenceHandler?.sendEmptyMessage(LocaleConstant.QUERY_IS_USE_AI_CODE) @@ -126,7 +87,7 @@ } override fun initEvent() { - binding.configurationButton.setOnClickListener { + binding.updateWiFiConfigButton.setOnClickListener { val password = binding.passwordView.text.toString() if (password.isBlank()) { "操作失败,WiFi密码为空!".show(requireContext()) @@ -136,28 +97,38 @@ val weakReferenceHandler = SocketConnectionService.weakReferenceHandler ?: return@setOnClickListener lifecycleScope.launch(Dispatchers.IO) { - if (!isUseAlgorithm) { - val serverHost = binding.serverHostView.text.toString() - val serverPort = binding.serverPortView.text.toString() - if (serverHost.isNotBlank() && serverPort.isNotBlank()) { - //需要设置远程服务器IP、端口号、WiFi - val message = weakReferenceHandler.obtainMessage() - message.what = LocaleConstant.SET_BOARD_SERVER_CONFIG_CODE - message.obj = arrayOf(serverHost, serverPort) - weakReferenceHandler.sendMessage(message) - delay(1000) - } else { - Log.d(kTag, "initEvent: 服务器配置参数异常,跳过此项配置") - } - } - - //不管什么模式,WiFi都可以设置 val message = weakReferenceHandler.obtainMessage() message.what = LocaleConstant.SET_BOARD_WIFI_CONFIG_CODE message.obj = arrayOf(binding.wifiNameView.text.toString(), password) weakReferenceHandler.sendMessage(message) } } + + binding.updateServerConfigButton.setOnClickListener { + val serverHost = binding.serverHostView.text.toString() + if (serverHost.isBlank()) { + "操作失败,服务器IP为空!".show(requireContext()) + return@setOnClickListener + } + + val serverPort = binding.serverPortView.text.toString() + if (serverPort.isBlank()) { + "操作失败,服务器端口为空!".show(requireContext()) + return@setOnClickListener + } + + val weakReferenceHandler = SocketConnectionService.weakReferenceHandler + ?: return@setOnClickListener + lifecycleScope.launch(Dispatchers.IO) { + if (!isUseAlgorithm) { + //需要设置远程服务器IP、端口号 + val message = weakReferenceHandler.obtainMessage() + message.what = LocaleConstant.SET_BOARD_SERVER_CONFIG_CODE + message.obj = arrayOf(serverHost, serverPort) + weakReferenceHandler.sendMessage(message) + } + } + } } override fun handleMessage(msg: Message): Boolean { @@ -166,6 +137,16 @@ binding.deviceCodeView.text = msg.obj as String } + LocaleConstant.WIFI_CONFIG_RESPONSE_CODE -> { + //AQS_20010002,abcd1234 + val data = msg.obj as String + isGetWiFiConfig = true + //分割数据 + val list = data.split(",") + binding.wifiNameView.text = list[0] + binding.passwordView.setText(list[1]) + } + LocaleConstant.QUERY_IS_USE_AI_RESPONSE_CODE -> { //配合一体机使用-0,单独使用-1 val state = msg.obj as String @@ -208,20 +189,4 @@ } return true } - - override fun onResume() { - super.onResume() - binding.wifiNameView.text = wifi.connectionInfo.ssid.removePrefix("\"").removeSuffix("\"") - } - - /** - * onDestroyView -> onDestroy -> onDetach - * */ - override fun onDestroy() { - super.onDestroy() - broadcastManager.destroy( - WifiManager.WIFI_STATE_CHANGED_ACTION, - WifiManager.NETWORK_STATE_CHANGED_ACTION - ) - } } \ No newline at end of file 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 42736a1..20d1d20 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 @@ -109,6 +109,10 @@ tcpClient.sendMessage(CommandCreator.queryControlBoardConfig()) } + LocaleConstant.QUERY_WIFI_CONFIG_CODE -> { + tcpClient.sendMessage(CommandCreator.queryWiFiConfig()) + } + LocaleConstant.QUERY_IS_USE_AI_CODE -> { tcpClient.sendMessage(CommandCreator.queryIsUseAI()) } @@ -195,6 +199,13 @@ weakReferenceHandler.sendMessage(message) } + '2' -> { + val message = weakReferenceHandler.obtainMessage() + message.what = LocaleConstant.WIFI_CONFIG_RESPONSE_CODE + message.obj = data + weakReferenceHandler.sendMessage(message) + } + '4' -> { val message = weakReferenceHandler.obtainMessage() message.what = LocaleConstant.QUERY_IS_USE_AI_RESPONSE_CODE 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 8a7f688..0bc8a4b 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 @@ -111,13 +111,20 @@ } /** - * 设置主控板WiFi指令 + * 设置主控板WiFi配置指令 * */ fun setWiFiConfig(wifiName: String, password: String): ByteArray { return "SetData:2,${wifiName},${password}\r\n".toByteArray() } /** + * 查询主控板WiFi配置指令 + * */ + fun queryWiFiConfig(): ByteArray { + return "GetData:2\r\n".toByteArray() + } + + /** * 设置主控板服务地址指令 * */ fun setServerConfig(host: String, port: String): ByteArray { diff --git a/app/build.gradle b/app/build.gradle index 68ad697..cf47c01 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -18,8 +18,8 @@ applicationId "com.casic.app.safetreecontroller" minSdkVersion 23 targetSdkVersion 33 - versionCode 1004 - versionName "1.0.0.4" + versionCode 1005 + versionName "1.0.0.5" ndk { abiFilters "arm64-v8a", "armeabi-v7a", "x86_64", "x86" } diff --git a/app/src/main/java/com/casic/app/safetreecontroller/fragments/BaseSettingsFragment.kt b/app/src/main/java/com/casic/app/safetreecontroller/fragments/BaseSettingsFragment.kt index 164ced0..30056ca 100644 --- a/app/src/main/java/com/casic/app/safetreecontroller/fragments/BaseSettingsFragment.kt +++ b/app/src/main/java/com/casic/app/safetreecontroller/fragments/BaseSettingsFragment.kt @@ -1,15 +1,8 @@ package com.casic.app.safetreecontroller.fragments -import android.content.BroadcastReceiver -import android.content.Context -import android.content.Intent -import android.net.NetworkInfo -import android.net.wifi.WifiManager -import android.os.Build import android.os.Bundle import android.os.Handler import android.os.Message -import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -18,15 +11,12 @@ 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.extensions.getSystemService import com.pengxh.kt.lite.extensions.show -import com.pengxh.kt.lite.utils.BroadcastManager import com.pengxh.kt.lite.utils.WeakReferenceHandler import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay import kotlinx.coroutines.launch - class BaseSettingsFragment : KotlinBaseFragment(), Handler.Callback { @@ -35,51 +25,14 @@ } private val kTag = "BaseSettingsFragment" - private val broadcastManager by lazy { BroadcastManager(requireContext()) } - private val wifi by lazy { requireContext().getSystemService()!! } private var isGetBoardConfig = false + private var isGetWiFiConfig = false private var isGetAlgorithmConfig = false private var isUseAlgorithm = false private var isGetServerConfig = false override fun initOnCreate(savedInstanceState: Bundle?) { weakReferenceHandler = WeakReferenceHandler(this) - broadcastManager.addAction(object : BroadcastReceiver() { - override fun onReceive(context: Context?, intent: Intent?) { - intent?.apply { - if (action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) { - val wifiState = intent.getIntExtra( - WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN - ) - if (wifiState == WifiManager.WIFI_STATE_ENABLED) { - //WiFi已关闭 - binding.wifiNameView.text = "WiFi已关闭" - } - } else if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) { - val net = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - intent.getParcelableExtra( - WifiManager.EXTRA_NETWORK_INFO, NetworkInfo::class.java - ) - } else { - intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO) - } - - if (net == null) { - "NetworkInfo is null".show(requireContext()) - return - } - if (net.isConnected) { - binding.wifiNameView.text = wifi.connectionInfo.ssid - .removePrefix("\"") - .removeSuffix("\"") - } else { - //WiFi断开 - binding.wifiNameView.text = "WiFi已断开" - } - } - } - } - }, WifiManager.WIFI_STATE_CHANGED_ACTION, WifiManager.NETWORK_STATE_CHANGED_ACTION) } override fun initViewBinding( @@ -108,6 +61,14 @@ delay(500) + while (!isGetWiFiConfig) { + //查询安全树连接的WiFi配置 + SocketConnectionService.weakReferenceHandler?.sendEmptyMessage(LocaleConstant.QUERY_WIFI_CONFIG_CODE) + delay(1000) + } + + delay(500) + while (!isGetAlgorithmConfig) { //查询是否配合一体机使用 SocketConnectionService.weakReferenceHandler?.sendEmptyMessage(LocaleConstant.QUERY_IS_USE_AI_CODE) @@ -126,7 +87,7 @@ } override fun initEvent() { - binding.configurationButton.setOnClickListener { + binding.updateWiFiConfigButton.setOnClickListener { val password = binding.passwordView.text.toString() if (password.isBlank()) { "操作失败,WiFi密码为空!".show(requireContext()) @@ -136,28 +97,38 @@ val weakReferenceHandler = SocketConnectionService.weakReferenceHandler ?: return@setOnClickListener lifecycleScope.launch(Dispatchers.IO) { - if (!isUseAlgorithm) { - val serverHost = binding.serverHostView.text.toString() - val serverPort = binding.serverPortView.text.toString() - if (serverHost.isNotBlank() && serverPort.isNotBlank()) { - //需要设置远程服务器IP、端口号、WiFi - val message = weakReferenceHandler.obtainMessage() - message.what = LocaleConstant.SET_BOARD_SERVER_CONFIG_CODE - message.obj = arrayOf(serverHost, serverPort) - weakReferenceHandler.sendMessage(message) - delay(1000) - } else { - Log.d(kTag, "initEvent: 服务器配置参数异常,跳过此项配置") - } - } - - //不管什么模式,WiFi都可以设置 val message = weakReferenceHandler.obtainMessage() message.what = LocaleConstant.SET_BOARD_WIFI_CONFIG_CODE message.obj = arrayOf(binding.wifiNameView.text.toString(), password) weakReferenceHandler.sendMessage(message) } } + + binding.updateServerConfigButton.setOnClickListener { + val serverHost = binding.serverHostView.text.toString() + if (serverHost.isBlank()) { + "操作失败,服务器IP为空!".show(requireContext()) + return@setOnClickListener + } + + val serverPort = binding.serverPortView.text.toString() + if (serverPort.isBlank()) { + "操作失败,服务器端口为空!".show(requireContext()) + return@setOnClickListener + } + + val weakReferenceHandler = SocketConnectionService.weakReferenceHandler + ?: return@setOnClickListener + lifecycleScope.launch(Dispatchers.IO) { + if (!isUseAlgorithm) { + //需要设置远程服务器IP、端口号 + val message = weakReferenceHandler.obtainMessage() + message.what = LocaleConstant.SET_BOARD_SERVER_CONFIG_CODE + message.obj = arrayOf(serverHost, serverPort) + weakReferenceHandler.sendMessage(message) + } + } + } } override fun handleMessage(msg: Message): Boolean { @@ -166,6 +137,16 @@ binding.deviceCodeView.text = msg.obj as String } + LocaleConstant.WIFI_CONFIG_RESPONSE_CODE -> { + //AQS_20010002,abcd1234 + val data = msg.obj as String + isGetWiFiConfig = true + //分割数据 + val list = data.split(",") + binding.wifiNameView.text = list[0] + binding.passwordView.setText(list[1]) + } + LocaleConstant.QUERY_IS_USE_AI_RESPONSE_CODE -> { //配合一体机使用-0,单独使用-1 val state = msg.obj as String @@ -208,20 +189,4 @@ } return true } - - override fun onResume() { - super.onResume() - binding.wifiNameView.text = wifi.connectionInfo.ssid.removePrefix("\"").removeSuffix("\"") - } - - /** - * onDestroyView -> onDestroy -> onDetach - * */ - override fun onDestroy() { - super.onDestroy() - broadcastManager.destroy( - WifiManager.WIFI_STATE_CHANGED_ACTION, - WifiManager.NETWORK_STATE_CHANGED_ACTION - ) - } } \ No newline at end of file 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 42736a1..20d1d20 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 @@ -109,6 +109,10 @@ tcpClient.sendMessage(CommandCreator.queryControlBoardConfig()) } + LocaleConstant.QUERY_WIFI_CONFIG_CODE -> { + tcpClient.sendMessage(CommandCreator.queryWiFiConfig()) + } + LocaleConstant.QUERY_IS_USE_AI_CODE -> { tcpClient.sendMessage(CommandCreator.queryIsUseAI()) } @@ -195,6 +199,13 @@ weakReferenceHandler.sendMessage(message) } + '2' -> { + val message = weakReferenceHandler.obtainMessage() + message.what = LocaleConstant.WIFI_CONFIG_RESPONSE_CODE + message.obj = data + weakReferenceHandler.sendMessage(message) + } + '4' -> { val message = weakReferenceHandler.obtainMessage() message.what = LocaleConstant.QUERY_IS_USE_AI_RESPONSE_CODE 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 8a7f688..0bc8a4b 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 @@ -111,13 +111,20 @@ } /** - * 设置主控板WiFi指令 + * 设置主控板WiFi配置指令 * */ fun setWiFiConfig(wifiName: String, password: String): ByteArray { return "SetData:2,${wifiName},${password}\r\n".toByteArray() } /** + * 查询主控板WiFi配置指令 + * */ + fun queryWiFiConfig(): ByteArray { + return "GetData:2\r\n".toByteArray() + } + + /** * 设置主控板服务地址指令 * */ fun setServerConfig(host: String, port: String): ByteArray { 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 d8423a1..88474dc 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 @@ -33,10 +33,11 @@ const val OPEN_DEVICE_STATE_TIPS_CODE = 20240017 const val CLOSE_DEVICE_STATE_TIPS_CODE = 20240018 const val QUERY_BOARD_CONFIG_CODE = 20240019 - const val QUERY_IS_USE_AI_CODE = 20240020 - const val QUERY_SERVER_CONFIG_CODE = 20240021 - const val SET_BOARD_WIFI_CONFIG_CODE = 20240022 - const val SET_BOARD_SERVER_CONFIG_CODE = 20240023 + const val QUERY_WIFI_CONFIG_CODE = 20240020 + const val QUERY_IS_USE_AI_CODE = 20240021 + const val QUERY_SERVER_CONFIG_CODE = 20240022 + const val SET_BOARD_WIFI_CONFIG_CODE = 20240023 + const val SET_BOARD_SERVER_CONFIG_CODE = 20240024 /** * Handler Response Code @@ -51,10 +52,11 @@ const val QUERY_DEVICE_STATE_VOICE_RESPONSE_CODE = 20241008 const val DEVICE_RESPONSE_CODE = 20241009 const val BOARD_CONFIG_RESPONSE_CODE = 20241010 - const val QUERY_IS_USE_AI_RESPONSE_CODE = 20241011 - const val SERVER_CONFIG_RESPONSE_CODE = 20241012 - const val SET_BOARD_WIFI_CONFIG_RESPONSE_CODE = 20241013 - const val SET_BOARD_SERVER_CONFIG_RESPONSE_CODE = 20241014 + const val WIFI_CONFIG_RESPONSE_CODE = 20241011 + const val QUERY_IS_USE_AI_RESPONSE_CODE = 20241012 + const val SERVER_CONFIG_RESPONSE_CODE = 20241013 + const val SET_BOARD_WIFI_CONFIG_RESPONSE_CODE = 20241014 + const val SET_BOARD_SERVER_CONFIG_RESPONSE_CODE = 20241015 /*** * SP Key diff --git a/app/build.gradle b/app/build.gradle index 68ad697..cf47c01 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -18,8 +18,8 @@ applicationId "com.casic.app.safetreecontroller" minSdkVersion 23 targetSdkVersion 33 - versionCode 1004 - versionName "1.0.0.4" + versionCode 1005 + versionName "1.0.0.5" ndk { abiFilters "arm64-v8a", "armeabi-v7a", "x86_64", "x86" } diff --git a/app/src/main/java/com/casic/app/safetreecontroller/fragments/BaseSettingsFragment.kt b/app/src/main/java/com/casic/app/safetreecontroller/fragments/BaseSettingsFragment.kt index 164ced0..30056ca 100644 --- a/app/src/main/java/com/casic/app/safetreecontroller/fragments/BaseSettingsFragment.kt +++ b/app/src/main/java/com/casic/app/safetreecontroller/fragments/BaseSettingsFragment.kt @@ -1,15 +1,8 @@ package com.casic.app.safetreecontroller.fragments -import android.content.BroadcastReceiver -import android.content.Context -import android.content.Intent -import android.net.NetworkInfo -import android.net.wifi.WifiManager -import android.os.Build import android.os.Bundle import android.os.Handler import android.os.Message -import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -18,15 +11,12 @@ 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.extensions.getSystemService import com.pengxh.kt.lite.extensions.show -import com.pengxh.kt.lite.utils.BroadcastManager import com.pengxh.kt.lite.utils.WeakReferenceHandler import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay import kotlinx.coroutines.launch - class BaseSettingsFragment : KotlinBaseFragment(), Handler.Callback { @@ -35,51 +25,14 @@ } private val kTag = "BaseSettingsFragment" - private val broadcastManager by lazy { BroadcastManager(requireContext()) } - private val wifi by lazy { requireContext().getSystemService()!! } private var isGetBoardConfig = false + private var isGetWiFiConfig = false private var isGetAlgorithmConfig = false private var isUseAlgorithm = false private var isGetServerConfig = false override fun initOnCreate(savedInstanceState: Bundle?) { weakReferenceHandler = WeakReferenceHandler(this) - broadcastManager.addAction(object : BroadcastReceiver() { - override fun onReceive(context: Context?, intent: Intent?) { - intent?.apply { - if (action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) { - val wifiState = intent.getIntExtra( - WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN - ) - if (wifiState == WifiManager.WIFI_STATE_ENABLED) { - //WiFi已关闭 - binding.wifiNameView.text = "WiFi已关闭" - } - } else if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) { - val net = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - intent.getParcelableExtra( - WifiManager.EXTRA_NETWORK_INFO, NetworkInfo::class.java - ) - } else { - intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO) - } - - if (net == null) { - "NetworkInfo is null".show(requireContext()) - return - } - if (net.isConnected) { - binding.wifiNameView.text = wifi.connectionInfo.ssid - .removePrefix("\"") - .removeSuffix("\"") - } else { - //WiFi断开 - binding.wifiNameView.text = "WiFi已断开" - } - } - } - } - }, WifiManager.WIFI_STATE_CHANGED_ACTION, WifiManager.NETWORK_STATE_CHANGED_ACTION) } override fun initViewBinding( @@ -108,6 +61,14 @@ delay(500) + while (!isGetWiFiConfig) { + //查询安全树连接的WiFi配置 + SocketConnectionService.weakReferenceHandler?.sendEmptyMessage(LocaleConstant.QUERY_WIFI_CONFIG_CODE) + delay(1000) + } + + delay(500) + while (!isGetAlgorithmConfig) { //查询是否配合一体机使用 SocketConnectionService.weakReferenceHandler?.sendEmptyMessage(LocaleConstant.QUERY_IS_USE_AI_CODE) @@ -126,7 +87,7 @@ } override fun initEvent() { - binding.configurationButton.setOnClickListener { + binding.updateWiFiConfigButton.setOnClickListener { val password = binding.passwordView.text.toString() if (password.isBlank()) { "操作失败,WiFi密码为空!".show(requireContext()) @@ -136,28 +97,38 @@ val weakReferenceHandler = SocketConnectionService.weakReferenceHandler ?: return@setOnClickListener lifecycleScope.launch(Dispatchers.IO) { - if (!isUseAlgorithm) { - val serverHost = binding.serverHostView.text.toString() - val serverPort = binding.serverPortView.text.toString() - if (serverHost.isNotBlank() && serverPort.isNotBlank()) { - //需要设置远程服务器IP、端口号、WiFi - val message = weakReferenceHandler.obtainMessage() - message.what = LocaleConstant.SET_BOARD_SERVER_CONFIG_CODE - message.obj = arrayOf(serverHost, serverPort) - weakReferenceHandler.sendMessage(message) - delay(1000) - } else { - Log.d(kTag, "initEvent: 服务器配置参数异常,跳过此项配置") - } - } - - //不管什么模式,WiFi都可以设置 val message = weakReferenceHandler.obtainMessage() message.what = LocaleConstant.SET_BOARD_WIFI_CONFIG_CODE message.obj = arrayOf(binding.wifiNameView.text.toString(), password) weakReferenceHandler.sendMessage(message) } } + + binding.updateServerConfigButton.setOnClickListener { + val serverHost = binding.serverHostView.text.toString() + if (serverHost.isBlank()) { + "操作失败,服务器IP为空!".show(requireContext()) + return@setOnClickListener + } + + val serverPort = binding.serverPortView.text.toString() + if (serverPort.isBlank()) { + "操作失败,服务器端口为空!".show(requireContext()) + return@setOnClickListener + } + + val weakReferenceHandler = SocketConnectionService.weakReferenceHandler + ?: return@setOnClickListener + lifecycleScope.launch(Dispatchers.IO) { + if (!isUseAlgorithm) { + //需要设置远程服务器IP、端口号 + val message = weakReferenceHandler.obtainMessage() + message.what = LocaleConstant.SET_BOARD_SERVER_CONFIG_CODE + message.obj = arrayOf(serverHost, serverPort) + weakReferenceHandler.sendMessage(message) + } + } + } } override fun handleMessage(msg: Message): Boolean { @@ -166,6 +137,16 @@ binding.deviceCodeView.text = msg.obj as String } + LocaleConstant.WIFI_CONFIG_RESPONSE_CODE -> { + //AQS_20010002,abcd1234 + val data = msg.obj as String + isGetWiFiConfig = true + //分割数据 + val list = data.split(",") + binding.wifiNameView.text = list[0] + binding.passwordView.setText(list[1]) + } + LocaleConstant.QUERY_IS_USE_AI_RESPONSE_CODE -> { //配合一体机使用-0,单独使用-1 val state = msg.obj as String @@ -208,20 +189,4 @@ } return true } - - override fun onResume() { - super.onResume() - binding.wifiNameView.text = wifi.connectionInfo.ssid.removePrefix("\"").removeSuffix("\"") - } - - /** - * onDestroyView -> onDestroy -> onDetach - * */ - override fun onDestroy() { - super.onDestroy() - broadcastManager.destroy( - WifiManager.WIFI_STATE_CHANGED_ACTION, - WifiManager.NETWORK_STATE_CHANGED_ACTION - ) - } } \ No newline at end of file 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 42736a1..20d1d20 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 @@ -109,6 +109,10 @@ tcpClient.sendMessage(CommandCreator.queryControlBoardConfig()) } + LocaleConstant.QUERY_WIFI_CONFIG_CODE -> { + tcpClient.sendMessage(CommandCreator.queryWiFiConfig()) + } + LocaleConstant.QUERY_IS_USE_AI_CODE -> { tcpClient.sendMessage(CommandCreator.queryIsUseAI()) } @@ -195,6 +199,13 @@ weakReferenceHandler.sendMessage(message) } + '2' -> { + val message = weakReferenceHandler.obtainMessage() + message.what = LocaleConstant.WIFI_CONFIG_RESPONSE_CODE + message.obj = data + weakReferenceHandler.sendMessage(message) + } + '4' -> { val message = weakReferenceHandler.obtainMessage() message.what = LocaleConstant.QUERY_IS_USE_AI_RESPONSE_CODE 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 8a7f688..0bc8a4b 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 @@ -111,13 +111,20 @@ } /** - * 设置主控板WiFi指令 + * 设置主控板WiFi配置指令 * */ fun setWiFiConfig(wifiName: String, password: String): ByteArray { return "SetData:2,${wifiName},${password}\r\n".toByteArray() } /** + * 查询主控板WiFi配置指令 + * */ + fun queryWiFiConfig(): ByteArray { + return "GetData:2\r\n".toByteArray() + } + + /** * 设置主控板服务地址指令 * */ fun setServerConfig(host: String, port: String): ByteArray { 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 d8423a1..88474dc 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 @@ -33,10 +33,11 @@ const val OPEN_DEVICE_STATE_TIPS_CODE = 20240017 const val CLOSE_DEVICE_STATE_TIPS_CODE = 20240018 const val QUERY_BOARD_CONFIG_CODE = 20240019 - const val QUERY_IS_USE_AI_CODE = 20240020 - const val QUERY_SERVER_CONFIG_CODE = 20240021 - const val SET_BOARD_WIFI_CONFIG_CODE = 20240022 - const val SET_BOARD_SERVER_CONFIG_CODE = 20240023 + const val QUERY_WIFI_CONFIG_CODE = 20240020 + const val QUERY_IS_USE_AI_CODE = 20240021 + const val QUERY_SERVER_CONFIG_CODE = 20240022 + const val SET_BOARD_WIFI_CONFIG_CODE = 20240023 + const val SET_BOARD_SERVER_CONFIG_CODE = 20240024 /** * Handler Response Code @@ -51,10 +52,11 @@ const val QUERY_DEVICE_STATE_VOICE_RESPONSE_CODE = 20241008 const val DEVICE_RESPONSE_CODE = 20241009 const val BOARD_CONFIG_RESPONSE_CODE = 20241010 - const val QUERY_IS_USE_AI_RESPONSE_CODE = 20241011 - const val SERVER_CONFIG_RESPONSE_CODE = 20241012 - const val SET_BOARD_WIFI_CONFIG_RESPONSE_CODE = 20241013 - const val SET_BOARD_SERVER_CONFIG_RESPONSE_CODE = 20241014 + const val WIFI_CONFIG_RESPONSE_CODE = 20241011 + const val QUERY_IS_USE_AI_RESPONSE_CODE = 20241012 + const val SERVER_CONFIG_RESPONSE_CODE = 20241013 + const val SET_BOARD_WIFI_CONFIG_RESPONSE_CODE = 20241014 + const val SET_BOARD_SERVER_CONFIG_RESPONSE_CODE = 20241015 /*** * SP Key diff --git a/app/src/main/res/drawable/bg_pwd_visible_toggle.xml b/app/src/main/res/drawable/bg_pwd_visible_toggle.xml new file mode 100644 index 0000000..2e8fb67 --- /dev/null +++ b/app/src/main/res/drawable/bg_pwd_visible_toggle.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/app/build.gradle b/app/build.gradle index 68ad697..cf47c01 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -18,8 +18,8 @@ applicationId "com.casic.app.safetreecontroller" minSdkVersion 23 targetSdkVersion 33 - versionCode 1004 - versionName "1.0.0.4" + versionCode 1005 + versionName "1.0.0.5" ndk { abiFilters "arm64-v8a", "armeabi-v7a", "x86_64", "x86" } diff --git a/app/src/main/java/com/casic/app/safetreecontroller/fragments/BaseSettingsFragment.kt b/app/src/main/java/com/casic/app/safetreecontroller/fragments/BaseSettingsFragment.kt index 164ced0..30056ca 100644 --- a/app/src/main/java/com/casic/app/safetreecontroller/fragments/BaseSettingsFragment.kt +++ b/app/src/main/java/com/casic/app/safetreecontroller/fragments/BaseSettingsFragment.kt @@ -1,15 +1,8 @@ package com.casic.app.safetreecontroller.fragments -import android.content.BroadcastReceiver -import android.content.Context -import android.content.Intent -import android.net.NetworkInfo -import android.net.wifi.WifiManager -import android.os.Build import android.os.Bundle import android.os.Handler import android.os.Message -import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -18,15 +11,12 @@ 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.extensions.getSystemService import com.pengxh.kt.lite.extensions.show -import com.pengxh.kt.lite.utils.BroadcastManager import com.pengxh.kt.lite.utils.WeakReferenceHandler import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay import kotlinx.coroutines.launch - class BaseSettingsFragment : KotlinBaseFragment(), Handler.Callback { @@ -35,51 +25,14 @@ } private val kTag = "BaseSettingsFragment" - private val broadcastManager by lazy { BroadcastManager(requireContext()) } - private val wifi by lazy { requireContext().getSystemService()!! } private var isGetBoardConfig = false + private var isGetWiFiConfig = false private var isGetAlgorithmConfig = false private var isUseAlgorithm = false private var isGetServerConfig = false override fun initOnCreate(savedInstanceState: Bundle?) { weakReferenceHandler = WeakReferenceHandler(this) - broadcastManager.addAction(object : BroadcastReceiver() { - override fun onReceive(context: Context?, intent: Intent?) { - intent?.apply { - if (action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) { - val wifiState = intent.getIntExtra( - WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN - ) - if (wifiState == WifiManager.WIFI_STATE_ENABLED) { - //WiFi已关闭 - binding.wifiNameView.text = "WiFi已关闭" - } - } else if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) { - val net = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - intent.getParcelableExtra( - WifiManager.EXTRA_NETWORK_INFO, NetworkInfo::class.java - ) - } else { - intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO) - } - - if (net == null) { - "NetworkInfo is null".show(requireContext()) - return - } - if (net.isConnected) { - binding.wifiNameView.text = wifi.connectionInfo.ssid - .removePrefix("\"") - .removeSuffix("\"") - } else { - //WiFi断开 - binding.wifiNameView.text = "WiFi已断开" - } - } - } - } - }, WifiManager.WIFI_STATE_CHANGED_ACTION, WifiManager.NETWORK_STATE_CHANGED_ACTION) } override fun initViewBinding( @@ -108,6 +61,14 @@ delay(500) + while (!isGetWiFiConfig) { + //查询安全树连接的WiFi配置 + SocketConnectionService.weakReferenceHandler?.sendEmptyMessage(LocaleConstant.QUERY_WIFI_CONFIG_CODE) + delay(1000) + } + + delay(500) + while (!isGetAlgorithmConfig) { //查询是否配合一体机使用 SocketConnectionService.weakReferenceHandler?.sendEmptyMessage(LocaleConstant.QUERY_IS_USE_AI_CODE) @@ -126,7 +87,7 @@ } override fun initEvent() { - binding.configurationButton.setOnClickListener { + binding.updateWiFiConfigButton.setOnClickListener { val password = binding.passwordView.text.toString() if (password.isBlank()) { "操作失败,WiFi密码为空!".show(requireContext()) @@ -136,28 +97,38 @@ val weakReferenceHandler = SocketConnectionService.weakReferenceHandler ?: return@setOnClickListener lifecycleScope.launch(Dispatchers.IO) { - if (!isUseAlgorithm) { - val serverHost = binding.serverHostView.text.toString() - val serverPort = binding.serverPortView.text.toString() - if (serverHost.isNotBlank() && serverPort.isNotBlank()) { - //需要设置远程服务器IP、端口号、WiFi - val message = weakReferenceHandler.obtainMessage() - message.what = LocaleConstant.SET_BOARD_SERVER_CONFIG_CODE - message.obj = arrayOf(serverHost, serverPort) - weakReferenceHandler.sendMessage(message) - delay(1000) - } else { - Log.d(kTag, "initEvent: 服务器配置参数异常,跳过此项配置") - } - } - - //不管什么模式,WiFi都可以设置 val message = weakReferenceHandler.obtainMessage() message.what = LocaleConstant.SET_BOARD_WIFI_CONFIG_CODE message.obj = arrayOf(binding.wifiNameView.text.toString(), password) weakReferenceHandler.sendMessage(message) } } + + binding.updateServerConfigButton.setOnClickListener { + val serverHost = binding.serverHostView.text.toString() + if (serverHost.isBlank()) { + "操作失败,服务器IP为空!".show(requireContext()) + return@setOnClickListener + } + + val serverPort = binding.serverPortView.text.toString() + if (serverPort.isBlank()) { + "操作失败,服务器端口为空!".show(requireContext()) + return@setOnClickListener + } + + val weakReferenceHandler = SocketConnectionService.weakReferenceHandler + ?: return@setOnClickListener + lifecycleScope.launch(Dispatchers.IO) { + if (!isUseAlgorithm) { + //需要设置远程服务器IP、端口号 + val message = weakReferenceHandler.obtainMessage() + message.what = LocaleConstant.SET_BOARD_SERVER_CONFIG_CODE + message.obj = arrayOf(serverHost, serverPort) + weakReferenceHandler.sendMessage(message) + } + } + } } override fun handleMessage(msg: Message): Boolean { @@ -166,6 +137,16 @@ binding.deviceCodeView.text = msg.obj as String } + LocaleConstant.WIFI_CONFIG_RESPONSE_CODE -> { + //AQS_20010002,abcd1234 + val data = msg.obj as String + isGetWiFiConfig = true + //分割数据 + val list = data.split(",") + binding.wifiNameView.text = list[0] + binding.passwordView.setText(list[1]) + } + LocaleConstant.QUERY_IS_USE_AI_RESPONSE_CODE -> { //配合一体机使用-0,单独使用-1 val state = msg.obj as String @@ -208,20 +189,4 @@ } return true } - - override fun onResume() { - super.onResume() - binding.wifiNameView.text = wifi.connectionInfo.ssid.removePrefix("\"").removeSuffix("\"") - } - - /** - * onDestroyView -> onDestroy -> onDetach - * */ - override fun onDestroy() { - super.onDestroy() - broadcastManager.destroy( - WifiManager.WIFI_STATE_CHANGED_ACTION, - WifiManager.NETWORK_STATE_CHANGED_ACTION - ) - } } \ No newline at end of file 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 42736a1..20d1d20 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 @@ -109,6 +109,10 @@ tcpClient.sendMessage(CommandCreator.queryControlBoardConfig()) } + LocaleConstant.QUERY_WIFI_CONFIG_CODE -> { + tcpClient.sendMessage(CommandCreator.queryWiFiConfig()) + } + LocaleConstant.QUERY_IS_USE_AI_CODE -> { tcpClient.sendMessage(CommandCreator.queryIsUseAI()) } @@ -195,6 +199,13 @@ weakReferenceHandler.sendMessage(message) } + '2' -> { + val message = weakReferenceHandler.obtainMessage() + message.what = LocaleConstant.WIFI_CONFIG_RESPONSE_CODE + message.obj = data + weakReferenceHandler.sendMessage(message) + } + '4' -> { val message = weakReferenceHandler.obtainMessage() message.what = LocaleConstant.QUERY_IS_USE_AI_RESPONSE_CODE 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 8a7f688..0bc8a4b 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 @@ -111,13 +111,20 @@ } /** - * 设置主控板WiFi指令 + * 设置主控板WiFi配置指令 * */ fun setWiFiConfig(wifiName: String, password: String): ByteArray { return "SetData:2,${wifiName},${password}\r\n".toByteArray() } /** + * 查询主控板WiFi配置指令 + * */ + fun queryWiFiConfig(): ByteArray { + return "GetData:2\r\n".toByteArray() + } + + /** * 设置主控板服务地址指令 * */ fun setServerConfig(host: String, port: String): ByteArray { 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 d8423a1..88474dc 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 @@ -33,10 +33,11 @@ const val OPEN_DEVICE_STATE_TIPS_CODE = 20240017 const val CLOSE_DEVICE_STATE_TIPS_CODE = 20240018 const val QUERY_BOARD_CONFIG_CODE = 20240019 - const val QUERY_IS_USE_AI_CODE = 20240020 - const val QUERY_SERVER_CONFIG_CODE = 20240021 - const val SET_BOARD_WIFI_CONFIG_CODE = 20240022 - const val SET_BOARD_SERVER_CONFIG_CODE = 20240023 + const val QUERY_WIFI_CONFIG_CODE = 20240020 + const val QUERY_IS_USE_AI_CODE = 20240021 + const val QUERY_SERVER_CONFIG_CODE = 20240022 + const val SET_BOARD_WIFI_CONFIG_CODE = 20240023 + const val SET_BOARD_SERVER_CONFIG_CODE = 20240024 /** * Handler Response Code @@ -51,10 +52,11 @@ const val QUERY_DEVICE_STATE_VOICE_RESPONSE_CODE = 20241008 const val DEVICE_RESPONSE_CODE = 20241009 const val BOARD_CONFIG_RESPONSE_CODE = 20241010 - const val QUERY_IS_USE_AI_RESPONSE_CODE = 20241011 - const val SERVER_CONFIG_RESPONSE_CODE = 20241012 - const val SET_BOARD_WIFI_CONFIG_RESPONSE_CODE = 20241013 - const val SET_BOARD_SERVER_CONFIG_RESPONSE_CODE = 20241014 + const val WIFI_CONFIG_RESPONSE_CODE = 20241011 + const val QUERY_IS_USE_AI_RESPONSE_CODE = 20241012 + const val SERVER_CONFIG_RESPONSE_CODE = 20241013 + const val SET_BOARD_WIFI_CONFIG_RESPONSE_CODE = 20241014 + const val SET_BOARD_SERVER_CONFIG_RESPONSE_CODE = 20241015 /*** * SP Key diff --git a/app/src/main/res/drawable/bg_pwd_visible_toggle.xml b/app/src/main/res/drawable/bg_pwd_visible_toggle.xml new file mode 100644 index 0000000..2e8fb67 --- /dev/null +++ b/app/src/main/res/drawable/bg_pwd_visible_toggle.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/app/src/main/res/drawable/bg_solid_layout_white_radius_5.xml b/app/src/main/res/drawable/bg_solid_layout_white_radius_5.xml index 59462ee..2cbbdba 100644 --- a/app/src/main/res/drawable/bg_solid_layout_white_radius_5.xml +++ b/app/src/main/res/drawable/bg_solid_layout_white_radius_5.xml @@ -7,5 +7,5 @@ - + \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index 68ad697..cf47c01 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -18,8 +18,8 @@ applicationId "com.casic.app.safetreecontroller" minSdkVersion 23 targetSdkVersion 33 - versionCode 1004 - versionName "1.0.0.4" + versionCode 1005 + versionName "1.0.0.5" ndk { abiFilters "arm64-v8a", "armeabi-v7a", "x86_64", "x86" } diff --git a/app/src/main/java/com/casic/app/safetreecontroller/fragments/BaseSettingsFragment.kt b/app/src/main/java/com/casic/app/safetreecontroller/fragments/BaseSettingsFragment.kt index 164ced0..30056ca 100644 --- a/app/src/main/java/com/casic/app/safetreecontroller/fragments/BaseSettingsFragment.kt +++ b/app/src/main/java/com/casic/app/safetreecontroller/fragments/BaseSettingsFragment.kt @@ -1,15 +1,8 @@ package com.casic.app.safetreecontroller.fragments -import android.content.BroadcastReceiver -import android.content.Context -import android.content.Intent -import android.net.NetworkInfo -import android.net.wifi.WifiManager -import android.os.Build import android.os.Bundle import android.os.Handler import android.os.Message -import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -18,15 +11,12 @@ 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.extensions.getSystemService import com.pengxh.kt.lite.extensions.show -import com.pengxh.kt.lite.utils.BroadcastManager import com.pengxh.kt.lite.utils.WeakReferenceHandler import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay import kotlinx.coroutines.launch - class BaseSettingsFragment : KotlinBaseFragment(), Handler.Callback { @@ -35,51 +25,14 @@ } private val kTag = "BaseSettingsFragment" - private val broadcastManager by lazy { BroadcastManager(requireContext()) } - private val wifi by lazy { requireContext().getSystemService()!! } private var isGetBoardConfig = false + private var isGetWiFiConfig = false private var isGetAlgorithmConfig = false private var isUseAlgorithm = false private var isGetServerConfig = false override fun initOnCreate(savedInstanceState: Bundle?) { weakReferenceHandler = WeakReferenceHandler(this) - broadcastManager.addAction(object : BroadcastReceiver() { - override fun onReceive(context: Context?, intent: Intent?) { - intent?.apply { - if (action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) { - val wifiState = intent.getIntExtra( - WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN - ) - if (wifiState == WifiManager.WIFI_STATE_ENABLED) { - //WiFi已关闭 - binding.wifiNameView.text = "WiFi已关闭" - } - } else if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) { - val net = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - intent.getParcelableExtra( - WifiManager.EXTRA_NETWORK_INFO, NetworkInfo::class.java - ) - } else { - intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO) - } - - if (net == null) { - "NetworkInfo is null".show(requireContext()) - return - } - if (net.isConnected) { - binding.wifiNameView.text = wifi.connectionInfo.ssid - .removePrefix("\"") - .removeSuffix("\"") - } else { - //WiFi断开 - binding.wifiNameView.text = "WiFi已断开" - } - } - } - } - }, WifiManager.WIFI_STATE_CHANGED_ACTION, WifiManager.NETWORK_STATE_CHANGED_ACTION) } override fun initViewBinding( @@ -108,6 +61,14 @@ delay(500) + while (!isGetWiFiConfig) { + //查询安全树连接的WiFi配置 + SocketConnectionService.weakReferenceHandler?.sendEmptyMessage(LocaleConstant.QUERY_WIFI_CONFIG_CODE) + delay(1000) + } + + delay(500) + while (!isGetAlgorithmConfig) { //查询是否配合一体机使用 SocketConnectionService.weakReferenceHandler?.sendEmptyMessage(LocaleConstant.QUERY_IS_USE_AI_CODE) @@ -126,7 +87,7 @@ } override fun initEvent() { - binding.configurationButton.setOnClickListener { + binding.updateWiFiConfigButton.setOnClickListener { val password = binding.passwordView.text.toString() if (password.isBlank()) { "操作失败,WiFi密码为空!".show(requireContext()) @@ -136,28 +97,38 @@ val weakReferenceHandler = SocketConnectionService.weakReferenceHandler ?: return@setOnClickListener lifecycleScope.launch(Dispatchers.IO) { - if (!isUseAlgorithm) { - val serverHost = binding.serverHostView.text.toString() - val serverPort = binding.serverPortView.text.toString() - if (serverHost.isNotBlank() && serverPort.isNotBlank()) { - //需要设置远程服务器IP、端口号、WiFi - val message = weakReferenceHandler.obtainMessage() - message.what = LocaleConstant.SET_BOARD_SERVER_CONFIG_CODE - message.obj = arrayOf(serverHost, serverPort) - weakReferenceHandler.sendMessage(message) - delay(1000) - } else { - Log.d(kTag, "initEvent: 服务器配置参数异常,跳过此项配置") - } - } - - //不管什么模式,WiFi都可以设置 val message = weakReferenceHandler.obtainMessage() message.what = LocaleConstant.SET_BOARD_WIFI_CONFIG_CODE message.obj = arrayOf(binding.wifiNameView.text.toString(), password) weakReferenceHandler.sendMessage(message) } } + + binding.updateServerConfigButton.setOnClickListener { + val serverHost = binding.serverHostView.text.toString() + if (serverHost.isBlank()) { + "操作失败,服务器IP为空!".show(requireContext()) + return@setOnClickListener + } + + val serverPort = binding.serverPortView.text.toString() + if (serverPort.isBlank()) { + "操作失败,服务器端口为空!".show(requireContext()) + return@setOnClickListener + } + + val weakReferenceHandler = SocketConnectionService.weakReferenceHandler + ?: return@setOnClickListener + lifecycleScope.launch(Dispatchers.IO) { + if (!isUseAlgorithm) { + //需要设置远程服务器IP、端口号 + val message = weakReferenceHandler.obtainMessage() + message.what = LocaleConstant.SET_BOARD_SERVER_CONFIG_CODE + message.obj = arrayOf(serverHost, serverPort) + weakReferenceHandler.sendMessage(message) + } + } + } } override fun handleMessage(msg: Message): Boolean { @@ -166,6 +137,16 @@ binding.deviceCodeView.text = msg.obj as String } + LocaleConstant.WIFI_CONFIG_RESPONSE_CODE -> { + //AQS_20010002,abcd1234 + val data = msg.obj as String + isGetWiFiConfig = true + //分割数据 + val list = data.split(",") + binding.wifiNameView.text = list[0] + binding.passwordView.setText(list[1]) + } + LocaleConstant.QUERY_IS_USE_AI_RESPONSE_CODE -> { //配合一体机使用-0,单独使用-1 val state = msg.obj as String @@ -208,20 +189,4 @@ } return true } - - override fun onResume() { - super.onResume() - binding.wifiNameView.text = wifi.connectionInfo.ssid.removePrefix("\"").removeSuffix("\"") - } - - /** - * onDestroyView -> onDestroy -> onDetach - * */ - override fun onDestroy() { - super.onDestroy() - broadcastManager.destroy( - WifiManager.WIFI_STATE_CHANGED_ACTION, - WifiManager.NETWORK_STATE_CHANGED_ACTION - ) - } } \ No newline at end of file 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 42736a1..20d1d20 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 @@ -109,6 +109,10 @@ tcpClient.sendMessage(CommandCreator.queryControlBoardConfig()) } + LocaleConstant.QUERY_WIFI_CONFIG_CODE -> { + tcpClient.sendMessage(CommandCreator.queryWiFiConfig()) + } + LocaleConstant.QUERY_IS_USE_AI_CODE -> { tcpClient.sendMessage(CommandCreator.queryIsUseAI()) } @@ -195,6 +199,13 @@ weakReferenceHandler.sendMessage(message) } + '2' -> { + val message = weakReferenceHandler.obtainMessage() + message.what = LocaleConstant.WIFI_CONFIG_RESPONSE_CODE + message.obj = data + weakReferenceHandler.sendMessage(message) + } + '4' -> { val message = weakReferenceHandler.obtainMessage() message.what = LocaleConstant.QUERY_IS_USE_AI_RESPONSE_CODE 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 8a7f688..0bc8a4b 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 @@ -111,13 +111,20 @@ } /** - * 设置主控板WiFi指令 + * 设置主控板WiFi配置指令 * */ fun setWiFiConfig(wifiName: String, password: String): ByteArray { return "SetData:2,${wifiName},${password}\r\n".toByteArray() } /** + * 查询主控板WiFi配置指令 + * */ + fun queryWiFiConfig(): ByteArray { + return "GetData:2\r\n".toByteArray() + } + + /** * 设置主控板服务地址指令 * */ fun setServerConfig(host: String, port: String): ByteArray { 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 d8423a1..88474dc 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 @@ -33,10 +33,11 @@ const val OPEN_DEVICE_STATE_TIPS_CODE = 20240017 const val CLOSE_DEVICE_STATE_TIPS_CODE = 20240018 const val QUERY_BOARD_CONFIG_CODE = 20240019 - const val QUERY_IS_USE_AI_CODE = 20240020 - const val QUERY_SERVER_CONFIG_CODE = 20240021 - const val SET_BOARD_WIFI_CONFIG_CODE = 20240022 - const val SET_BOARD_SERVER_CONFIG_CODE = 20240023 + const val QUERY_WIFI_CONFIG_CODE = 20240020 + const val QUERY_IS_USE_AI_CODE = 20240021 + const val QUERY_SERVER_CONFIG_CODE = 20240022 + const val SET_BOARD_WIFI_CONFIG_CODE = 20240023 + const val SET_BOARD_SERVER_CONFIG_CODE = 20240024 /** * Handler Response Code @@ -51,10 +52,11 @@ const val QUERY_DEVICE_STATE_VOICE_RESPONSE_CODE = 20241008 const val DEVICE_RESPONSE_CODE = 20241009 const val BOARD_CONFIG_RESPONSE_CODE = 20241010 - const val QUERY_IS_USE_AI_RESPONSE_CODE = 20241011 - const val SERVER_CONFIG_RESPONSE_CODE = 20241012 - const val SET_BOARD_WIFI_CONFIG_RESPONSE_CODE = 20241013 - const val SET_BOARD_SERVER_CONFIG_RESPONSE_CODE = 20241014 + const val WIFI_CONFIG_RESPONSE_CODE = 20241011 + const val QUERY_IS_USE_AI_RESPONSE_CODE = 20241012 + const val SERVER_CONFIG_RESPONSE_CODE = 20241013 + const val SET_BOARD_WIFI_CONFIG_RESPONSE_CODE = 20241014 + const val SET_BOARD_SERVER_CONFIG_RESPONSE_CODE = 20241015 /*** * SP Key diff --git a/app/src/main/res/drawable/bg_pwd_visible_toggle.xml b/app/src/main/res/drawable/bg_pwd_visible_toggle.xml new file mode 100644 index 0000000..2e8fb67 --- /dev/null +++ b/app/src/main/res/drawable/bg_pwd_visible_toggle.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/app/src/main/res/drawable/bg_solid_layout_white_radius_5.xml b/app/src/main/res/drawable/bg_solid_layout_white_radius_5.xml index 59462ee..2cbbdba 100644 --- a/app/src/main/res/drawable/bg_solid_layout_white_radius_5.xml +++ b/app/src/main/res/drawable/bg_solid_layout_white_radius_5.xml @@ -7,5 +7,5 @@ - + \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_eye.xml b/app/src/main/res/drawable/ic_eye.xml new file mode 100644 index 0000000..66aaaad --- /dev/null +++ b/app/src/main/res/drawable/ic_eye.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/build.gradle b/app/build.gradle index 68ad697..cf47c01 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -18,8 +18,8 @@ applicationId "com.casic.app.safetreecontroller" minSdkVersion 23 targetSdkVersion 33 - versionCode 1004 - versionName "1.0.0.4" + versionCode 1005 + versionName "1.0.0.5" ndk { abiFilters "arm64-v8a", "armeabi-v7a", "x86_64", "x86" } diff --git a/app/src/main/java/com/casic/app/safetreecontroller/fragments/BaseSettingsFragment.kt b/app/src/main/java/com/casic/app/safetreecontroller/fragments/BaseSettingsFragment.kt index 164ced0..30056ca 100644 --- a/app/src/main/java/com/casic/app/safetreecontroller/fragments/BaseSettingsFragment.kt +++ b/app/src/main/java/com/casic/app/safetreecontroller/fragments/BaseSettingsFragment.kt @@ -1,15 +1,8 @@ package com.casic.app.safetreecontroller.fragments -import android.content.BroadcastReceiver -import android.content.Context -import android.content.Intent -import android.net.NetworkInfo -import android.net.wifi.WifiManager -import android.os.Build import android.os.Bundle import android.os.Handler import android.os.Message -import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -18,15 +11,12 @@ 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.extensions.getSystemService import com.pengxh.kt.lite.extensions.show -import com.pengxh.kt.lite.utils.BroadcastManager import com.pengxh.kt.lite.utils.WeakReferenceHandler import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay import kotlinx.coroutines.launch - class BaseSettingsFragment : KotlinBaseFragment(), Handler.Callback { @@ -35,51 +25,14 @@ } private val kTag = "BaseSettingsFragment" - private val broadcastManager by lazy { BroadcastManager(requireContext()) } - private val wifi by lazy { requireContext().getSystemService()!! } private var isGetBoardConfig = false + private var isGetWiFiConfig = false private var isGetAlgorithmConfig = false private var isUseAlgorithm = false private var isGetServerConfig = false override fun initOnCreate(savedInstanceState: Bundle?) { weakReferenceHandler = WeakReferenceHandler(this) - broadcastManager.addAction(object : BroadcastReceiver() { - override fun onReceive(context: Context?, intent: Intent?) { - intent?.apply { - if (action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) { - val wifiState = intent.getIntExtra( - WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN - ) - if (wifiState == WifiManager.WIFI_STATE_ENABLED) { - //WiFi已关闭 - binding.wifiNameView.text = "WiFi已关闭" - } - } else if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) { - val net = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - intent.getParcelableExtra( - WifiManager.EXTRA_NETWORK_INFO, NetworkInfo::class.java - ) - } else { - intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO) - } - - if (net == null) { - "NetworkInfo is null".show(requireContext()) - return - } - if (net.isConnected) { - binding.wifiNameView.text = wifi.connectionInfo.ssid - .removePrefix("\"") - .removeSuffix("\"") - } else { - //WiFi断开 - binding.wifiNameView.text = "WiFi已断开" - } - } - } - } - }, WifiManager.WIFI_STATE_CHANGED_ACTION, WifiManager.NETWORK_STATE_CHANGED_ACTION) } override fun initViewBinding( @@ -108,6 +61,14 @@ delay(500) + while (!isGetWiFiConfig) { + //查询安全树连接的WiFi配置 + SocketConnectionService.weakReferenceHandler?.sendEmptyMessage(LocaleConstant.QUERY_WIFI_CONFIG_CODE) + delay(1000) + } + + delay(500) + while (!isGetAlgorithmConfig) { //查询是否配合一体机使用 SocketConnectionService.weakReferenceHandler?.sendEmptyMessage(LocaleConstant.QUERY_IS_USE_AI_CODE) @@ -126,7 +87,7 @@ } override fun initEvent() { - binding.configurationButton.setOnClickListener { + binding.updateWiFiConfigButton.setOnClickListener { val password = binding.passwordView.text.toString() if (password.isBlank()) { "操作失败,WiFi密码为空!".show(requireContext()) @@ -136,28 +97,38 @@ val weakReferenceHandler = SocketConnectionService.weakReferenceHandler ?: return@setOnClickListener lifecycleScope.launch(Dispatchers.IO) { - if (!isUseAlgorithm) { - val serverHost = binding.serverHostView.text.toString() - val serverPort = binding.serverPortView.text.toString() - if (serverHost.isNotBlank() && serverPort.isNotBlank()) { - //需要设置远程服务器IP、端口号、WiFi - val message = weakReferenceHandler.obtainMessage() - message.what = LocaleConstant.SET_BOARD_SERVER_CONFIG_CODE - message.obj = arrayOf(serverHost, serverPort) - weakReferenceHandler.sendMessage(message) - delay(1000) - } else { - Log.d(kTag, "initEvent: 服务器配置参数异常,跳过此项配置") - } - } - - //不管什么模式,WiFi都可以设置 val message = weakReferenceHandler.obtainMessage() message.what = LocaleConstant.SET_BOARD_WIFI_CONFIG_CODE message.obj = arrayOf(binding.wifiNameView.text.toString(), password) weakReferenceHandler.sendMessage(message) } } + + binding.updateServerConfigButton.setOnClickListener { + val serverHost = binding.serverHostView.text.toString() + if (serverHost.isBlank()) { + "操作失败,服务器IP为空!".show(requireContext()) + return@setOnClickListener + } + + val serverPort = binding.serverPortView.text.toString() + if (serverPort.isBlank()) { + "操作失败,服务器端口为空!".show(requireContext()) + return@setOnClickListener + } + + val weakReferenceHandler = SocketConnectionService.weakReferenceHandler + ?: return@setOnClickListener + lifecycleScope.launch(Dispatchers.IO) { + if (!isUseAlgorithm) { + //需要设置远程服务器IP、端口号 + val message = weakReferenceHandler.obtainMessage() + message.what = LocaleConstant.SET_BOARD_SERVER_CONFIG_CODE + message.obj = arrayOf(serverHost, serverPort) + weakReferenceHandler.sendMessage(message) + } + } + } } override fun handleMessage(msg: Message): Boolean { @@ -166,6 +137,16 @@ binding.deviceCodeView.text = msg.obj as String } + LocaleConstant.WIFI_CONFIG_RESPONSE_CODE -> { + //AQS_20010002,abcd1234 + val data = msg.obj as String + isGetWiFiConfig = true + //分割数据 + val list = data.split(",") + binding.wifiNameView.text = list[0] + binding.passwordView.setText(list[1]) + } + LocaleConstant.QUERY_IS_USE_AI_RESPONSE_CODE -> { //配合一体机使用-0,单独使用-1 val state = msg.obj as String @@ -208,20 +189,4 @@ } return true } - - override fun onResume() { - super.onResume() - binding.wifiNameView.text = wifi.connectionInfo.ssid.removePrefix("\"").removeSuffix("\"") - } - - /** - * onDestroyView -> onDestroy -> onDetach - * */ - override fun onDestroy() { - super.onDestroy() - broadcastManager.destroy( - WifiManager.WIFI_STATE_CHANGED_ACTION, - WifiManager.NETWORK_STATE_CHANGED_ACTION - ) - } } \ No newline at end of file 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 42736a1..20d1d20 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 @@ -109,6 +109,10 @@ tcpClient.sendMessage(CommandCreator.queryControlBoardConfig()) } + LocaleConstant.QUERY_WIFI_CONFIG_CODE -> { + tcpClient.sendMessage(CommandCreator.queryWiFiConfig()) + } + LocaleConstant.QUERY_IS_USE_AI_CODE -> { tcpClient.sendMessage(CommandCreator.queryIsUseAI()) } @@ -195,6 +199,13 @@ weakReferenceHandler.sendMessage(message) } + '2' -> { + val message = weakReferenceHandler.obtainMessage() + message.what = LocaleConstant.WIFI_CONFIG_RESPONSE_CODE + message.obj = data + weakReferenceHandler.sendMessage(message) + } + '4' -> { val message = weakReferenceHandler.obtainMessage() message.what = LocaleConstant.QUERY_IS_USE_AI_RESPONSE_CODE 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 8a7f688..0bc8a4b 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 @@ -111,13 +111,20 @@ } /** - * 设置主控板WiFi指令 + * 设置主控板WiFi配置指令 * */ fun setWiFiConfig(wifiName: String, password: String): ByteArray { return "SetData:2,${wifiName},${password}\r\n".toByteArray() } /** + * 查询主控板WiFi配置指令 + * */ + fun queryWiFiConfig(): ByteArray { + return "GetData:2\r\n".toByteArray() + } + + /** * 设置主控板服务地址指令 * */ fun setServerConfig(host: String, port: String): ByteArray { 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 d8423a1..88474dc 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 @@ -33,10 +33,11 @@ const val OPEN_DEVICE_STATE_TIPS_CODE = 20240017 const val CLOSE_DEVICE_STATE_TIPS_CODE = 20240018 const val QUERY_BOARD_CONFIG_CODE = 20240019 - const val QUERY_IS_USE_AI_CODE = 20240020 - const val QUERY_SERVER_CONFIG_CODE = 20240021 - const val SET_BOARD_WIFI_CONFIG_CODE = 20240022 - const val SET_BOARD_SERVER_CONFIG_CODE = 20240023 + const val QUERY_WIFI_CONFIG_CODE = 20240020 + const val QUERY_IS_USE_AI_CODE = 20240021 + const val QUERY_SERVER_CONFIG_CODE = 20240022 + const val SET_BOARD_WIFI_CONFIG_CODE = 20240023 + const val SET_BOARD_SERVER_CONFIG_CODE = 20240024 /** * Handler Response Code @@ -51,10 +52,11 @@ const val QUERY_DEVICE_STATE_VOICE_RESPONSE_CODE = 20241008 const val DEVICE_RESPONSE_CODE = 20241009 const val BOARD_CONFIG_RESPONSE_CODE = 20241010 - const val QUERY_IS_USE_AI_RESPONSE_CODE = 20241011 - const val SERVER_CONFIG_RESPONSE_CODE = 20241012 - const val SET_BOARD_WIFI_CONFIG_RESPONSE_CODE = 20241013 - const val SET_BOARD_SERVER_CONFIG_RESPONSE_CODE = 20241014 + const val WIFI_CONFIG_RESPONSE_CODE = 20241011 + const val QUERY_IS_USE_AI_RESPONSE_CODE = 20241012 + const val SERVER_CONFIG_RESPONSE_CODE = 20241013 + const val SET_BOARD_WIFI_CONFIG_RESPONSE_CODE = 20241014 + const val SET_BOARD_SERVER_CONFIG_RESPONSE_CODE = 20241015 /*** * SP Key diff --git a/app/src/main/res/drawable/bg_pwd_visible_toggle.xml b/app/src/main/res/drawable/bg_pwd_visible_toggle.xml new file mode 100644 index 0000000..2e8fb67 --- /dev/null +++ b/app/src/main/res/drawable/bg_pwd_visible_toggle.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/app/src/main/res/drawable/bg_solid_layout_white_radius_5.xml b/app/src/main/res/drawable/bg_solid_layout_white_radius_5.xml index 59462ee..2cbbdba 100644 --- a/app/src/main/res/drawable/bg_solid_layout_white_radius_5.xml +++ b/app/src/main/res/drawable/bg_solid_layout_white_radius_5.xml @@ -7,5 +7,5 @@ - + \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_eye.xml b/app/src/main/res/drawable/ic_eye.xml new file mode 100644 index 0000000..66aaaad --- /dev/null +++ b/app/src/main/res/drawable/ic_eye.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable/ic_eye_off.xml b/app/src/main/res/drawable/ic_eye_off.xml new file mode 100644 index 0000000..533b076 --- /dev/null +++ b/app/src/main/res/drawable/ic_eye_off.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/build.gradle b/app/build.gradle index 68ad697..cf47c01 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -18,8 +18,8 @@ applicationId "com.casic.app.safetreecontroller" minSdkVersion 23 targetSdkVersion 33 - versionCode 1004 - versionName "1.0.0.4" + versionCode 1005 + versionName "1.0.0.5" ndk { abiFilters "arm64-v8a", "armeabi-v7a", "x86_64", "x86" } diff --git a/app/src/main/java/com/casic/app/safetreecontroller/fragments/BaseSettingsFragment.kt b/app/src/main/java/com/casic/app/safetreecontroller/fragments/BaseSettingsFragment.kt index 164ced0..30056ca 100644 --- a/app/src/main/java/com/casic/app/safetreecontroller/fragments/BaseSettingsFragment.kt +++ b/app/src/main/java/com/casic/app/safetreecontroller/fragments/BaseSettingsFragment.kt @@ -1,15 +1,8 @@ package com.casic.app.safetreecontroller.fragments -import android.content.BroadcastReceiver -import android.content.Context -import android.content.Intent -import android.net.NetworkInfo -import android.net.wifi.WifiManager -import android.os.Build import android.os.Bundle import android.os.Handler import android.os.Message -import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -18,15 +11,12 @@ 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.extensions.getSystemService import com.pengxh.kt.lite.extensions.show -import com.pengxh.kt.lite.utils.BroadcastManager import com.pengxh.kt.lite.utils.WeakReferenceHandler import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay import kotlinx.coroutines.launch - class BaseSettingsFragment : KotlinBaseFragment(), Handler.Callback { @@ -35,51 +25,14 @@ } private val kTag = "BaseSettingsFragment" - private val broadcastManager by lazy { BroadcastManager(requireContext()) } - private val wifi by lazy { requireContext().getSystemService()!! } private var isGetBoardConfig = false + private var isGetWiFiConfig = false private var isGetAlgorithmConfig = false private var isUseAlgorithm = false private var isGetServerConfig = false override fun initOnCreate(savedInstanceState: Bundle?) { weakReferenceHandler = WeakReferenceHandler(this) - broadcastManager.addAction(object : BroadcastReceiver() { - override fun onReceive(context: Context?, intent: Intent?) { - intent?.apply { - if (action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) { - val wifiState = intent.getIntExtra( - WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN - ) - if (wifiState == WifiManager.WIFI_STATE_ENABLED) { - //WiFi已关闭 - binding.wifiNameView.text = "WiFi已关闭" - } - } else if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) { - val net = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - intent.getParcelableExtra( - WifiManager.EXTRA_NETWORK_INFO, NetworkInfo::class.java - ) - } else { - intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO) - } - - if (net == null) { - "NetworkInfo is null".show(requireContext()) - return - } - if (net.isConnected) { - binding.wifiNameView.text = wifi.connectionInfo.ssid - .removePrefix("\"") - .removeSuffix("\"") - } else { - //WiFi断开 - binding.wifiNameView.text = "WiFi已断开" - } - } - } - } - }, WifiManager.WIFI_STATE_CHANGED_ACTION, WifiManager.NETWORK_STATE_CHANGED_ACTION) } override fun initViewBinding( @@ -108,6 +61,14 @@ delay(500) + while (!isGetWiFiConfig) { + //查询安全树连接的WiFi配置 + SocketConnectionService.weakReferenceHandler?.sendEmptyMessage(LocaleConstant.QUERY_WIFI_CONFIG_CODE) + delay(1000) + } + + delay(500) + while (!isGetAlgorithmConfig) { //查询是否配合一体机使用 SocketConnectionService.weakReferenceHandler?.sendEmptyMessage(LocaleConstant.QUERY_IS_USE_AI_CODE) @@ -126,7 +87,7 @@ } override fun initEvent() { - binding.configurationButton.setOnClickListener { + binding.updateWiFiConfigButton.setOnClickListener { val password = binding.passwordView.text.toString() if (password.isBlank()) { "操作失败,WiFi密码为空!".show(requireContext()) @@ -136,28 +97,38 @@ val weakReferenceHandler = SocketConnectionService.weakReferenceHandler ?: return@setOnClickListener lifecycleScope.launch(Dispatchers.IO) { - if (!isUseAlgorithm) { - val serverHost = binding.serverHostView.text.toString() - val serverPort = binding.serverPortView.text.toString() - if (serverHost.isNotBlank() && serverPort.isNotBlank()) { - //需要设置远程服务器IP、端口号、WiFi - val message = weakReferenceHandler.obtainMessage() - message.what = LocaleConstant.SET_BOARD_SERVER_CONFIG_CODE - message.obj = arrayOf(serverHost, serverPort) - weakReferenceHandler.sendMessage(message) - delay(1000) - } else { - Log.d(kTag, "initEvent: 服务器配置参数异常,跳过此项配置") - } - } - - //不管什么模式,WiFi都可以设置 val message = weakReferenceHandler.obtainMessage() message.what = LocaleConstant.SET_BOARD_WIFI_CONFIG_CODE message.obj = arrayOf(binding.wifiNameView.text.toString(), password) weakReferenceHandler.sendMessage(message) } } + + binding.updateServerConfigButton.setOnClickListener { + val serverHost = binding.serverHostView.text.toString() + if (serverHost.isBlank()) { + "操作失败,服务器IP为空!".show(requireContext()) + return@setOnClickListener + } + + val serverPort = binding.serverPortView.text.toString() + if (serverPort.isBlank()) { + "操作失败,服务器端口为空!".show(requireContext()) + return@setOnClickListener + } + + val weakReferenceHandler = SocketConnectionService.weakReferenceHandler + ?: return@setOnClickListener + lifecycleScope.launch(Dispatchers.IO) { + if (!isUseAlgorithm) { + //需要设置远程服务器IP、端口号 + val message = weakReferenceHandler.obtainMessage() + message.what = LocaleConstant.SET_BOARD_SERVER_CONFIG_CODE + message.obj = arrayOf(serverHost, serverPort) + weakReferenceHandler.sendMessage(message) + } + } + } } override fun handleMessage(msg: Message): Boolean { @@ -166,6 +137,16 @@ binding.deviceCodeView.text = msg.obj as String } + LocaleConstant.WIFI_CONFIG_RESPONSE_CODE -> { + //AQS_20010002,abcd1234 + val data = msg.obj as String + isGetWiFiConfig = true + //分割数据 + val list = data.split(",") + binding.wifiNameView.text = list[0] + binding.passwordView.setText(list[1]) + } + LocaleConstant.QUERY_IS_USE_AI_RESPONSE_CODE -> { //配合一体机使用-0,单独使用-1 val state = msg.obj as String @@ -208,20 +189,4 @@ } return true } - - override fun onResume() { - super.onResume() - binding.wifiNameView.text = wifi.connectionInfo.ssid.removePrefix("\"").removeSuffix("\"") - } - - /** - * onDestroyView -> onDestroy -> onDetach - * */ - override fun onDestroy() { - super.onDestroy() - broadcastManager.destroy( - WifiManager.WIFI_STATE_CHANGED_ACTION, - WifiManager.NETWORK_STATE_CHANGED_ACTION - ) - } } \ No newline at end of file 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 42736a1..20d1d20 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 @@ -109,6 +109,10 @@ tcpClient.sendMessage(CommandCreator.queryControlBoardConfig()) } + LocaleConstant.QUERY_WIFI_CONFIG_CODE -> { + tcpClient.sendMessage(CommandCreator.queryWiFiConfig()) + } + LocaleConstant.QUERY_IS_USE_AI_CODE -> { tcpClient.sendMessage(CommandCreator.queryIsUseAI()) } @@ -195,6 +199,13 @@ weakReferenceHandler.sendMessage(message) } + '2' -> { + val message = weakReferenceHandler.obtainMessage() + message.what = LocaleConstant.WIFI_CONFIG_RESPONSE_CODE + message.obj = data + weakReferenceHandler.sendMessage(message) + } + '4' -> { val message = weakReferenceHandler.obtainMessage() message.what = LocaleConstant.QUERY_IS_USE_AI_RESPONSE_CODE 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 8a7f688..0bc8a4b 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 @@ -111,13 +111,20 @@ } /** - * 设置主控板WiFi指令 + * 设置主控板WiFi配置指令 * */ fun setWiFiConfig(wifiName: String, password: String): ByteArray { return "SetData:2,${wifiName},${password}\r\n".toByteArray() } /** + * 查询主控板WiFi配置指令 + * */ + fun queryWiFiConfig(): ByteArray { + return "GetData:2\r\n".toByteArray() + } + + /** * 设置主控板服务地址指令 * */ fun setServerConfig(host: String, port: String): ByteArray { 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 d8423a1..88474dc 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 @@ -33,10 +33,11 @@ const val OPEN_DEVICE_STATE_TIPS_CODE = 20240017 const val CLOSE_DEVICE_STATE_TIPS_CODE = 20240018 const val QUERY_BOARD_CONFIG_CODE = 20240019 - const val QUERY_IS_USE_AI_CODE = 20240020 - const val QUERY_SERVER_CONFIG_CODE = 20240021 - const val SET_BOARD_WIFI_CONFIG_CODE = 20240022 - const val SET_BOARD_SERVER_CONFIG_CODE = 20240023 + const val QUERY_WIFI_CONFIG_CODE = 20240020 + const val QUERY_IS_USE_AI_CODE = 20240021 + const val QUERY_SERVER_CONFIG_CODE = 20240022 + const val SET_BOARD_WIFI_CONFIG_CODE = 20240023 + const val SET_BOARD_SERVER_CONFIG_CODE = 20240024 /** * Handler Response Code @@ -51,10 +52,11 @@ const val QUERY_DEVICE_STATE_VOICE_RESPONSE_CODE = 20241008 const val DEVICE_RESPONSE_CODE = 20241009 const val BOARD_CONFIG_RESPONSE_CODE = 20241010 - const val QUERY_IS_USE_AI_RESPONSE_CODE = 20241011 - const val SERVER_CONFIG_RESPONSE_CODE = 20241012 - const val SET_BOARD_WIFI_CONFIG_RESPONSE_CODE = 20241013 - const val SET_BOARD_SERVER_CONFIG_RESPONSE_CODE = 20241014 + const val WIFI_CONFIG_RESPONSE_CODE = 20241011 + const val QUERY_IS_USE_AI_RESPONSE_CODE = 20241012 + const val SERVER_CONFIG_RESPONSE_CODE = 20241013 + const val SET_BOARD_WIFI_CONFIG_RESPONSE_CODE = 20241014 + const val SET_BOARD_SERVER_CONFIG_RESPONSE_CODE = 20241015 /*** * SP Key diff --git a/app/src/main/res/drawable/bg_pwd_visible_toggle.xml b/app/src/main/res/drawable/bg_pwd_visible_toggle.xml new file mode 100644 index 0000000..2e8fb67 --- /dev/null +++ b/app/src/main/res/drawable/bg_pwd_visible_toggle.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/app/src/main/res/drawable/bg_solid_layout_white_radius_5.xml b/app/src/main/res/drawable/bg_solid_layout_white_radius_5.xml index 59462ee..2cbbdba 100644 --- a/app/src/main/res/drawable/bg_solid_layout_white_radius_5.xml +++ b/app/src/main/res/drawable/bg_solid_layout_white_radius_5.xml @@ -7,5 +7,5 @@ - + \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_eye.xml b/app/src/main/res/drawable/ic_eye.xml new file mode 100644 index 0000000..66aaaad --- /dev/null +++ b/app/src/main/res/drawable/ic_eye.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable/ic_eye_off.xml b/app/src/main/res/drawable/ic_eye_off.xml new file mode 100644 index 0000000..533b076 --- /dev/null +++ b/app/src/main/res/drawable/ic_eye_off.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/layout/fragment_base_settings.xml b/app/src/main/res/layout/fragment_base_settings.xml index 2026d9c..a61dd66 100644 --- a/app/src/main/res/layout/fragment_base_settings.xml +++ b/app/src/main/res/layout/fragment_base_settings.xml @@ -1,5 +1,6 @@ @@ -167,33 +168,58 @@ android:textColor="@color/black" android:textSize="@dimen/sp_16" /> - + android:gravity="center_vertical" + android:orientation="horizontal"> + + + + + + + + - - + android:text="更新服务器配置" /> \ No newline at end of file