diff --git a/app/src/main/java/com/casic/br/operationsite/adapter/ManagePointAdapter.kt b/app/src/main/java/com/casic/br/operationsite/adapter/ManagePointAdapter.kt index 536c6cb..74eaf29 100644 --- a/app/src/main/java/com/casic/br/operationsite/adapter/ManagePointAdapter.kt +++ b/app/src/main/java/com/casic/br/operationsite/adapter/ManagePointAdapter.kt @@ -46,8 +46,8 @@ textView.setTextColor(R.color.greenColor.convertColor(context)) textView.setOnClickListener { //添加巡航点 - val find = points.find { it.thirdType == 0 } - if (find == null) { + val result = points.find { it.thirdType == 0 } + if (result == null) { val pointBean = PointBean().apply { firstType = 92 secondType = 1 @@ -63,8 +63,8 @@ handler.sendMessage(message) } } else { - find.thirdType = 1 - pointBeanDao.updatePoint(find) + result.thirdType = 1 + pointBeanDao.updatePoint(result) notifyDataSetChanged() CameraInspectionService.weakReferenceHandler?.let { handler -> val message = handler.obtainMessage() diff --git a/app/src/main/java/com/casic/br/operationsite/adapter/ManagePointAdapter.kt b/app/src/main/java/com/casic/br/operationsite/adapter/ManagePointAdapter.kt index 536c6cb..74eaf29 100644 --- a/app/src/main/java/com/casic/br/operationsite/adapter/ManagePointAdapter.kt +++ b/app/src/main/java/com/casic/br/operationsite/adapter/ManagePointAdapter.kt @@ -46,8 +46,8 @@ textView.setTextColor(R.color.greenColor.convertColor(context)) textView.setOnClickListener { //添加巡航点 - val find = points.find { it.thirdType == 0 } - if (find == null) { + val result = points.find { it.thirdType == 0 } + if (result == null) { val pointBean = PointBean().apply { firstType = 92 secondType = 1 @@ -63,8 +63,8 @@ handler.sendMessage(message) } } else { - find.thirdType = 1 - pointBeanDao.updatePoint(find) + result.thirdType = 1 + pointBeanDao.updatePoint(result) notifyDataSetChanged() CameraInspectionService.weakReferenceHandler?.let { handler -> val message = handler.obtainMessage() diff --git a/app/src/main/java/com/casic/br/operationsite/fragments/DeviceControllerFragment.kt b/app/src/main/java/com/casic/br/operationsite/fragments/DeviceControllerFragment.kt index 005e44a..862a00c 100644 --- a/app/src/main/java/com/casic/br/operationsite/fragments/DeviceControllerFragment.kt +++ b/app/src/main/java/com/casic/br/operationsite/fragments/DeviceControllerFragment.kt @@ -14,10 +14,10 @@ import com.casic.br.operationsite.view.DeviceControlActivity import com.casic.br.operationsite.vm.DeviceViewModel import com.casic.br.operationsite.widgets.ManagePointDialog -import com.casic.br.operationsite.widgets.ShowPointDialog import com.pengxh.kt.lite.base.KotlinBaseFragment import com.pengxh.kt.lite.extensions.show import com.pengxh.kt.lite.widget.SteeringWheelView +import com.pengxh.kt.lite.widget.dialog.AlertControlDialog import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay import kotlinx.coroutines.launch @@ -100,55 +100,71 @@ binding.currentSpeedView.text = "速度:${speed}" } - binding.managePointButton.setOnClickListener { + binding.startInspectionButton.setOnClickListener { + val beans = pointBeanDao.firstLinePoints + //至少得2个点才能巡航 + if (beans.size < 2) { + "请至少添加两个点".show(requireContext()) + return@setOnClickListener + } lifecycleScope.launch(Dispatchers.IO) { CameraInspectionService.weakReferenceHandler?.let { - it.sendEmptyMessage(LocaleConstant.START_ADD_POINT_CODE) - delay(300) + it.sendEmptyMessage(LocaleConstant.START_INSPECTION_CODE) + delay(500) it.sendEmptyMessage(LocaleConstant.SELECT_LINE_CODE) } } + } - //弹框添加预置点 + binding.managePointButton.setOnClickListener { ManagePointDialog.Builder() .setContext(requireContext()) .setOnDialogButtonClickListener(object : ManagePointDialog.OnDialogButtonClickListener { - override fun onCancelClick() { - - } - override fun onConfirmClick() { - CameraInspectionService.weakReferenceHandler?.sendEmptyMessage( - LocaleConstant.SAVE_POINT_CODE - ) - } - }).build().show() - } - - binding.queryPointButton.setOnClickListener { - val beans = pointBeanDao.firstLinePoints - if (beans.isEmpty()) { - "请先添加巡航点".show(requireContext()) - return@setOnClickListener - } - - ShowPointDialog.Builder() - .setContext(requireContext()) - .setOnDialogButtonClickListener(object : - ShowPointDialog.OnDialogButtonClickListener { - override fun onCancelClick() { - - } - - override fun onConfirmClick() { - //至少得2个点才能巡航 + val beans = pointBeanDao.firstLinePoints if (beans.size < 2) { "请至少添加两个点".show(requireContext()) return } + + lifecycleScope.launch(Dispatchers.IO) { + CameraInspectionService.weakReferenceHandler?.let { + it.sendEmptyMessage(LocaleConstant.START_ADD_POINT_CONFIG_CODE) + delay(500) + it.sendEmptyMessage(LocaleConstant.SELECT_LINE_CODE) + //循环设置巡航点 + for (i in 1..beans.size) { + val message = it.obtainMessage() + message.what = LocaleConstant.SETUP_POINT_CODE + message.obj = i + it.sendMessageDelayed(message, 500) + } + it.sendEmptyMessage(LocaleConstant.START_ADD_POINT_CONFIG_CODE) + delay(500) + it.sendEmptyMessage(LocaleConstant.END_ADD_POINT_CONFIG_CODE) + } + } + } + }).build().show() + } + + binding.stopInspectionButton.setOnClickListener { + AlertControlDialog.Builder() + .setContext(requireContext()) + .setTitle("提示") + .setMessage("是否停止巡航?") + .setNegativeButton("取消") + .setPositiveButton("确定") + .setOnDialogButtonClickListener(object : + AlertControlDialog.OnDialogButtonClickListener { + override fun onCancelClick() { + + } + + override fun onConfirmClick() { CameraInspectionService.weakReferenceHandler?.sendEmptyMessage( - LocaleConstant.START_INSPECTION_CODE + LocaleConstant.STOP_INSPECTION_CODE ) } }).build().show() diff --git a/app/src/main/java/com/casic/br/operationsite/adapter/ManagePointAdapter.kt b/app/src/main/java/com/casic/br/operationsite/adapter/ManagePointAdapter.kt index 536c6cb..74eaf29 100644 --- a/app/src/main/java/com/casic/br/operationsite/adapter/ManagePointAdapter.kt +++ b/app/src/main/java/com/casic/br/operationsite/adapter/ManagePointAdapter.kt @@ -46,8 +46,8 @@ textView.setTextColor(R.color.greenColor.convertColor(context)) textView.setOnClickListener { //添加巡航点 - val find = points.find { it.thirdType == 0 } - if (find == null) { + val result = points.find { it.thirdType == 0 } + if (result == null) { val pointBean = PointBean().apply { firstType = 92 secondType = 1 @@ -63,8 +63,8 @@ handler.sendMessage(message) } } else { - find.thirdType = 1 - pointBeanDao.updatePoint(find) + result.thirdType = 1 + pointBeanDao.updatePoint(result) notifyDataSetChanged() CameraInspectionService.weakReferenceHandler?.let { handler -> val message = handler.obtainMessage() diff --git a/app/src/main/java/com/casic/br/operationsite/fragments/DeviceControllerFragment.kt b/app/src/main/java/com/casic/br/operationsite/fragments/DeviceControllerFragment.kt index 005e44a..862a00c 100644 --- a/app/src/main/java/com/casic/br/operationsite/fragments/DeviceControllerFragment.kt +++ b/app/src/main/java/com/casic/br/operationsite/fragments/DeviceControllerFragment.kt @@ -14,10 +14,10 @@ import com.casic.br.operationsite.view.DeviceControlActivity import com.casic.br.operationsite.vm.DeviceViewModel import com.casic.br.operationsite.widgets.ManagePointDialog -import com.casic.br.operationsite.widgets.ShowPointDialog import com.pengxh.kt.lite.base.KotlinBaseFragment import com.pengxh.kt.lite.extensions.show import com.pengxh.kt.lite.widget.SteeringWheelView +import com.pengxh.kt.lite.widget.dialog.AlertControlDialog import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay import kotlinx.coroutines.launch @@ -100,55 +100,71 @@ binding.currentSpeedView.text = "速度:${speed}" } - binding.managePointButton.setOnClickListener { + binding.startInspectionButton.setOnClickListener { + val beans = pointBeanDao.firstLinePoints + //至少得2个点才能巡航 + if (beans.size < 2) { + "请至少添加两个点".show(requireContext()) + return@setOnClickListener + } lifecycleScope.launch(Dispatchers.IO) { CameraInspectionService.weakReferenceHandler?.let { - it.sendEmptyMessage(LocaleConstant.START_ADD_POINT_CODE) - delay(300) + it.sendEmptyMessage(LocaleConstant.START_INSPECTION_CODE) + delay(500) it.sendEmptyMessage(LocaleConstant.SELECT_LINE_CODE) } } + } - //弹框添加预置点 + binding.managePointButton.setOnClickListener { ManagePointDialog.Builder() .setContext(requireContext()) .setOnDialogButtonClickListener(object : ManagePointDialog.OnDialogButtonClickListener { - override fun onCancelClick() { - - } - override fun onConfirmClick() { - CameraInspectionService.weakReferenceHandler?.sendEmptyMessage( - LocaleConstant.SAVE_POINT_CODE - ) - } - }).build().show() - } - - binding.queryPointButton.setOnClickListener { - val beans = pointBeanDao.firstLinePoints - if (beans.isEmpty()) { - "请先添加巡航点".show(requireContext()) - return@setOnClickListener - } - - ShowPointDialog.Builder() - .setContext(requireContext()) - .setOnDialogButtonClickListener(object : - ShowPointDialog.OnDialogButtonClickListener { - override fun onCancelClick() { - - } - - override fun onConfirmClick() { - //至少得2个点才能巡航 + val beans = pointBeanDao.firstLinePoints if (beans.size < 2) { "请至少添加两个点".show(requireContext()) return } + + lifecycleScope.launch(Dispatchers.IO) { + CameraInspectionService.weakReferenceHandler?.let { + it.sendEmptyMessage(LocaleConstant.START_ADD_POINT_CONFIG_CODE) + delay(500) + it.sendEmptyMessage(LocaleConstant.SELECT_LINE_CODE) + //循环设置巡航点 + for (i in 1..beans.size) { + val message = it.obtainMessage() + message.what = LocaleConstant.SETUP_POINT_CODE + message.obj = i + it.sendMessageDelayed(message, 500) + } + it.sendEmptyMessage(LocaleConstant.START_ADD_POINT_CONFIG_CODE) + delay(500) + it.sendEmptyMessage(LocaleConstant.END_ADD_POINT_CONFIG_CODE) + } + } + } + }).build().show() + } + + binding.stopInspectionButton.setOnClickListener { + AlertControlDialog.Builder() + .setContext(requireContext()) + .setTitle("提示") + .setMessage("是否停止巡航?") + .setNegativeButton("取消") + .setPositiveButton("确定") + .setOnDialogButtonClickListener(object : + AlertControlDialog.OnDialogButtonClickListener { + override fun onCancelClick() { + + } + + override fun onConfirmClick() { CameraInspectionService.weakReferenceHandler?.sendEmptyMessage( - LocaleConstant.START_INSPECTION_CODE + LocaleConstant.STOP_INSPECTION_CODE ) } }).build().show() diff --git a/app/src/main/java/com/casic/br/operationsite/service/CameraInspectionService.kt b/app/src/main/java/com/casic/br/operationsite/service/CameraInspectionService.kt index 3b8dea5..c2cfbb4 100644 --- a/app/src/main/java/com/casic/br/operationsite/service/CameraInspectionService.kt +++ b/app/src/main/java/com/casic/br/operationsite/service/CameraInspectionService.kt @@ -16,11 +16,6 @@ import com.casic.br.operationsite.utils.TcpClient import com.pengxh.kt.lite.utils.SaveKeyValues import com.pengxh.kt.lite.utils.WeakReferenceHandler -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.SupervisorJob -import kotlinx.coroutines.delay -import kotlinx.coroutines.launch class CameraInspectionService : Service(), OnTcpConnectStateListener, Handler.Callback { @@ -32,7 +27,6 @@ private val notificationId = 2 private val tcpClient by lazy { TcpClient(this) } private val notificationManager by lazy { getSystemService(NOTIFICATION_SERVICE) as NotificationManager } - private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob()) private var notificationBuilder: NotificationCompat.Builder? = null override fun handleMessage(msg: Message): Boolean { @@ -41,26 +35,26 @@ tcpClient.start() } - LocaleConstant.START_ADD_POINT_CODE -> { - tcpClient.sendMessage(CommandCreator.invokeCommand(92)) - } - - LocaleConstant.ADD_LINE_CODE -> { - val line = msg.obj as Int - tcpClient.sendMessage(CommandCreator.invokeCommand(line)) - } - LocaleConstant.ADD_POINT_CODE -> { val point = msg.obj as Int - tcpClient.sendMessage(CommandCreator.setConfig(point)) + tcpClient.sendMessage(CommandCreator.setupConfig(point)) } - LocaleConstant.SAVE_POINT_CODE -> { - scope.launch(Dispatchers.IO) { - tcpClient.sendMessage(CommandCreator.invokeCommand(92)) - delay(500) - tcpClient.sendMessage(CommandCreator.invokeCommand(9)) - } + LocaleConstant.START_ADD_POINT_CONFIG_CODE -> { + tcpClient.sendMessage(CommandCreator.setupConfig(92)) + } + + LocaleConstant.SELECT_LINE_CODE -> { + tcpClient.sendMessage(CommandCreator.invokeCommand(1)) + } + + LocaleConstant.SETUP_POINT_CODE -> { + val point = msg.obj as Int + tcpClient.sendMessage(CommandCreator.invokeCommand(point)) + } + + LocaleConstant.END_ADD_POINT_CONFIG_CODE -> { + tcpClient.sendMessage(CommandCreator.invokeCommand(9)) } LocaleConstant.DELETE_POINT_CODE -> { @@ -69,12 +63,11 @@ } LocaleConstant.START_INSPECTION_CODE -> { - val line = msg.obj as Int - scope.launch(Dispatchers.IO) { - tcpClient.sendMessage(CommandCreator.invokeCommand(92)) - delay(500) - tcpClient.sendMessage(CommandCreator.invokeCommand(line)) - } + tcpClient.sendMessage(CommandCreator.invokeCommand(92)) + } + + LocaleConstant.STOP_INSPECTION_CODE -> { + tcpClient.sendMessage(CommandCreator.stopInspection()) } } return true diff --git a/app/src/main/java/com/casic/br/operationsite/adapter/ManagePointAdapter.kt b/app/src/main/java/com/casic/br/operationsite/adapter/ManagePointAdapter.kt index 536c6cb..74eaf29 100644 --- a/app/src/main/java/com/casic/br/operationsite/adapter/ManagePointAdapter.kt +++ b/app/src/main/java/com/casic/br/operationsite/adapter/ManagePointAdapter.kt @@ -46,8 +46,8 @@ textView.setTextColor(R.color.greenColor.convertColor(context)) textView.setOnClickListener { //添加巡航点 - val find = points.find { it.thirdType == 0 } - if (find == null) { + val result = points.find { it.thirdType == 0 } + if (result == null) { val pointBean = PointBean().apply { firstType = 92 secondType = 1 @@ -63,8 +63,8 @@ handler.sendMessage(message) } } else { - find.thirdType = 1 - pointBeanDao.updatePoint(find) + result.thirdType = 1 + pointBeanDao.updatePoint(result) notifyDataSetChanged() CameraInspectionService.weakReferenceHandler?.let { handler -> val message = handler.obtainMessage() diff --git a/app/src/main/java/com/casic/br/operationsite/fragments/DeviceControllerFragment.kt b/app/src/main/java/com/casic/br/operationsite/fragments/DeviceControllerFragment.kt index 005e44a..862a00c 100644 --- a/app/src/main/java/com/casic/br/operationsite/fragments/DeviceControllerFragment.kt +++ b/app/src/main/java/com/casic/br/operationsite/fragments/DeviceControllerFragment.kt @@ -14,10 +14,10 @@ import com.casic.br.operationsite.view.DeviceControlActivity import com.casic.br.operationsite.vm.DeviceViewModel import com.casic.br.operationsite.widgets.ManagePointDialog -import com.casic.br.operationsite.widgets.ShowPointDialog import com.pengxh.kt.lite.base.KotlinBaseFragment import com.pengxh.kt.lite.extensions.show import com.pengxh.kt.lite.widget.SteeringWheelView +import com.pengxh.kt.lite.widget.dialog.AlertControlDialog import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay import kotlinx.coroutines.launch @@ -100,55 +100,71 @@ binding.currentSpeedView.text = "速度:${speed}" } - binding.managePointButton.setOnClickListener { + binding.startInspectionButton.setOnClickListener { + val beans = pointBeanDao.firstLinePoints + //至少得2个点才能巡航 + if (beans.size < 2) { + "请至少添加两个点".show(requireContext()) + return@setOnClickListener + } lifecycleScope.launch(Dispatchers.IO) { CameraInspectionService.weakReferenceHandler?.let { - it.sendEmptyMessage(LocaleConstant.START_ADD_POINT_CODE) - delay(300) + it.sendEmptyMessage(LocaleConstant.START_INSPECTION_CODE) + delay(500) it.sendEmptyMessage(LocaleConstant.SELECT_LINE_CODE) } } + } - //弹框添加预置点 + binding.managePointButton.setOnClickListener { ManagePointDialog.Builder() .setContext(requireContext()) .setOnDialogButtonClickListener(object : ManagePointDialog.OnDialogButtonClickListener { - override fun onCancelClick() { - - } - override fun onConfirmClick() { - CameraInspectionService.weakReferenceHandler?.sendEmptyMessage( - LocaleConstant.SAVE_POINT_CODE - ) - } - }).build().show() - } - - binding.queryPointButton.setOnClickListener { - val beans = pointBeanDao.firstLinePoints - if (beans.isEmpty()) { - "请先添加巡航点".show(requireContext()) - return@setOnClickListener - } - - ShowPointDialog.Builder() - .setContext(requireContext()) - .setOnDialogButtonClickListener(object : - ShowPointDialog.OnDialogButtonClickListener { - override fun onCancelClick() { - - } - - override fun onConfirmClick() { - //至少得2个点才能巡航 + val beans = pointBeanDao.firstLinePoints if (beans.size < 2) { "请至少添加两个点".show(requireContext()) return } + + lifecycleScope.launch(Dispatchers.IO) { + CameraInspectionService.weakReferenceHandler?.let { + it.sendEmptyMessage(LocaleConstant.START_ADD_POINT_CONFIG_CODE) + delay(500) + it.sendEmptyMessage(LocaleConstant.SELECT_LINE_CODE) + //循环设置巡航点 + for (i in 1..beans.size) { + val message = it.obtainMessage() + message.what = LocaleConstant.SETUP_POINT_CODE + message.obj = i + it.sendMessageDelayed(message, 500) + } + it.sendEmptyMessage(LocaleConstant.START_ADD_POINT_CONFIG_CODE) + delay(500) + it.sendEmptyMessage(LocaleConstant.END_ADD_POINT_CONFIG_CODE) + } + } + } + }).build().show() + } + + binding.stopInspectionButton.setOnClickListener { + AlertControlDialog.Builder() + .setContext(requireContext()) + .setTitle("提示") + .setMessage("是否停止巡航?") + .setNegativeButton("取消") + .setPositiveButton("确定") + .setOnDialogButtonClickListener(object : + AlertControlDialog.OnDialogButtonClickListener { + override fun onCancelClick() { + + } + + override fun onConfirmClick() { CameraInspectionService.weakReferenceHandler?.sendEmptyMessage( - LocaleConstant.START_INSPECTION_CODE + LocaleConstant.STOP_INSPECTION_CODE ) } }).build().show() diff --git a/app/src/main/java/com/casic/br/operationsite/service/CameraInspectionService.kt b/app/src/main/java/com/casic/br/operationsite/service/CameraInspectionService.kt index 3b8dea5..c2cfbb4 100644 --- a/app/src/main/java/com/casic/br/operationsite/service/CameraInspectionService.kt +++ b/app/src/main/java/com/casic/br/operationsite/service/CameraInspectionService.kt @@ -16,11 +16,6 @@ import com.casic.br.operationsite.utils.TcpClient import com.pengxh.kt.lite.utils.SaveKeyValues import com.pengxh.kt.lite.utils.WeakReferenceHandler -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.SupervisorJob -import kotlinx.coroutines.delay -import kotlinx.coroutines.launch class CameraInspectionService : Service(), OnTcpConnectStateListener, Handler.Callback { @@ -32,7 +27,6 @@ private val notificationId = 2 private val tcpClient by lazy { TcpClient(this) } private val notificationManager by lazy { getSystemService(NOTIFICATION_SERVICE) as NotificationManager } - private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob()) private var notificationBuilder: NotificationCompat.Builder? = null override fun handleMessage(msg: Message): Boolean { @@ -41,26 +35,26 @@ tcpClient.start() } - LocaleConstant.START_ADD_POINT_CODE -> { - tcpClient.sendMessage(CommandCreator.invokeCommand(92)) - } - - LocaleConstant.ADD_LINE_CODE -> { - val line = msg.obj as Int - tcpClient.sendMessage(CommandCreator.invokeCommand(line)) - } - LocaleConstant.ADD_POINT_CODE -> { val point = msg.obj as Int - tcpClient.sendMessage(CommandCreator.setConfig(point)) + tcpClient.sendMessage(CommandCreator.setupConfig(point)) } - LocaleConstant.SAVE_POINT_CODE -> { - scope.launch(Dispatchers.IO) { - tcpClient.sendMessage(CommandCreator.invokeCommand(92)) - delay(500) - tcpClient.sendMessage(CommandCreator.invokeCommand(9)) - } + LocaleConstant.START_ADD_POINT_CONFIG_CODE -> { + tcpClient.sendMessage(CommandCreator.setupConfig(92)) + } + + LocaleConstant.SELECT_LINE_CODE -> { + tcpClient.sendMessage(CommandCreator.invokeCommand(1)) + } + + LocaleConstant.SETUP_POINT_CODE -> { + val point = msg.obj as Int + tcpClient.sendMessage(CommandCreator.invokeCommand(point)) + } + + LocaleConstant.END_ADD_POINT_CONFIG_CODE -> { + tcpClient.sendMessage(CommandCreator.invokeCommand(9)) } LocaleConstant.DELETE_POINT_CODE -> { @@ -69,12 +63,11 @@ } LocaleConstant.START_INSPECTION_CODE -> { - val line = msg.obj as Int - scope.launch(Dispatchers.IO) { - tcpClient.sendMessage(CommandCreator.invokeCommand(92)) - delay(500) - tcpClient.sendMessage(CommandCreator.invokeCommand(line)) - } + tcpClient.sendMessage(CommandCreator.invokeCommand(92)) + } + + LocaleConstant.STOP_INSPECTION_CODE -> { + tcpClient.sendMessage(CommandCreator.stopInspection()) } } return true diff --git a/app/src/main/java/com/casic/br/operationsite/utils/CommandCreator.kt b/app/src/main/java/com/casic/br/operationsite/utils/CommandCreator.kt index 60be355..5050636 100644 --- a/app/src/main/java/com/casic/br/operationsite/utils/CommandCreator.kt +++ b/app/src/main/java/com/casic/br/operationsite/utils/CommandCreator.kt @@ -1,5 +1,7 @@ package com.casic.br.operationsite.utils +import android.util.Log + object CommandCreator { /** * 查询激光状态 @@ -217,7 +219,7 @@ /** * 设置指令 - * + * 前提:先设置1/2/3....点 * * 设置巡航线路: * 1.设置92,调用1(1~8巡航线路) @@ -225,27 +227,8 @@ * 3.设置92,再调用9 (保存巡航线路) * */ fun setupConfig(index: Int): ByteArray { - val bytes = byteArrayOf( - 0xFF.toByte(), - 0x01, - 0x00, - 0x03, - 0x00, - 0x00, - 0x00 - ) - - // 将 index 转换为 2 字节的高位和低位 - bytes[4] = (index ushr 8).toByte() // 高位字节 - bytes[5] = (index and 0xFF).toByte() // 低位字节 - - //计算校验位。 - var sum = 0 - for (l in 1 until bytes.size - 1) { - sum += bytes[l].toInt() and 0xFF - } - bytes[6] = sum.toByte() - return bytes + val bytes = byteArrayOf(0xFF.toByte(), 0x01, 0x00, 0x03, 0x00, 0x00, 0x00) + return bytes.checksum(index) } /** @@ -255,54 +238,32 @@ * 1.调用92,调用1(开始跑巡航线路) * */ fun invokeCommand(index: Int): ByteArray { - val bytes = byteArrayOf( - 0xFF.toByte(), - 0x01, - 0x00, - 0x07, - 0x00, - 0x00, - 0x00 - ) - - // 将 index 转换为 2 字节的高位和低位 - bytes[4] = (index ushr 8).toByte() // 高位字节 - bytes[5] = (index and 0xFF).toByte() // 低位字节 - - //校验码SUM = (字节2+字节3+字节4+字节5+字节6)%256 - var sum = 0 - for (l in 1 until bytes.size - 1) { - sum += bytes[l].toInt() and 0xFF - } - bytes[6] = sum.toByte() - return bytes + val bytes = byteArrayOf(0xFF.toByte(), 0x01, 0x00, 0x07, 0x00, 0x00, 0x00) + return bytes.checksum(index) } fun deletePoint(index: Int): ByteArray { - val bytes = byteArrayOf( - 0xFF.toByte(), - 0x01, - 0x00, - 0x05, - 0x00, - 0x00, - 0x00 - ) - - // 将 index 转换为 2 字节的高位和低位 - bytes[4] = (index ushr 8).toByte() // 高位字节 - bytes[5] = (index and 0xFF).toByte() // 低位字节 - - //计算校验位。 - var sum = 0 - for (l in 1 until bytes.size - 1) { - sum += bytes[l].toInt() and 0xFF - } - bytes[6] = sum.toByte() - return bytes + val bytes = byteArrayOf(0xFF.toByte(), 0x01, 0x00, 0x05, 0x00, 0x00, 0x00) + return bytes.checksum(index) } fun stopInspection(): ByteArray { return byteArrayOf(0xFF.toByte(), 0x01, 0x00, 0x00, 0x00, 0x00, 0x01) } + + private fun ByteArray.checksum(value: Int): ByteArray { + // 将 index 转换为 2 字节的高位和低位 + this[4] = (value ushr 8).toByte() // 高位字节 + this[5] = (value and 0xFF).toByte() // 低位字节 + + //计算校验位。 + var sum = 0 + for (l in 1 until this.size - 1) { + sum += this[l].toInt() and 0xFF + } + this[6] = sum.toByte() + val hex = this.joinToString(" ") { "%02X".format(it) } + Log.d("CommandCreator", "指令16进制: $hex") + return this + } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/br/operationsite/adapter/ManagePointAdapter.kt b/app/src/main/java/com/casic/br/operationsite/adapter/ManagePointAdapter.kt index 536c6cb..74eaf29 100644 --- a/app/src/main/java/com/casic/br/operationsite/adapter/ManagePointAdapter.kt +++ b/app/src/main/java/com/casic/br/operationsite/adapter/ManagePointAdapter.kt @@ -46,8 +46,8 @@ textView.setTextColor(R.color.greenColor.convertColor(context)) textView.setOnClickListener { //添加巡航点 - val find = points.find { it.thirdType == 0 } - if (find == null) { + val result = points.find { it.thirdType == 0 } + if (result == null) { val pointBean = PointBean().apply { firstType = 92 secondType = 1 @@ -63,8 +63,8 @@ handler.sendMessage(message) } } else { - find.thirdType = 1 - pointBeanDao.updatePoint(find) + result.thirdType = 1 + pointBeanDao.updatePoint(result) notifyDataSetChanged() CameraInspectionService.weakReferenceHandler?.let { handler -> val message = handler.obtainMessage() diff --git a/app/src/main/java/com/casic/br/operationsite/fragments/DeviceControllerFragment.kt b/app/src/main/java/com/casic/br/operationsite/fragments/DeviceControllerFragment.kt index 005e44a..862a00c 100644 --- a/app/src/main/java/com/casic/br/operationsite/fragments/DeviceControllerFragment.kt +++ b/app/src/main/java/com/casic/br/operationsite/fragments/DeviceControllerFragment.kt @@ -14,10 +14,10 @@ import com.casic.br.operationsite.view.DeviceControlActivity import com.casic.br.operationsite.vm.DeviceViewModel import com.casic.br.operationsite.widgets.ManagePointDialog -import com.casic.br.operationsite.widgets.ShowPointDialog import com.pengxh.kt.lite.base.KotlinBaseFragment import com.pengxh.kt.lite.extensions.show import com.pengxh.kt.lite.widget.SteeringWheelView +import com.pengxh.kt.lite.widget.dialog.AlertControlDialog import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay import kotlinx.coroutines.launch @@ -100,55 +100,71 @@ binding.currentSpeedView.text = "速度:${speed}" } - binding.managePointButton.setOnClickListener { + binding.startInspectionButton.setOnClickListener { + val beans = pointBeanDao.firstLinePoints + //至少得2个点才能巡航 + if (beans.size < 2) { + "请至少添加两个点".show(requireContext()) + return@setOnClickListener + } lifecycleScope.launch(Dispatchers.IO) { CameraInspectionService.weakReferenceHandler?.let { - it.sendEmptyMessage(LocaleConstant.START_ADD_POINT_CODE) - delay(300) + it.sendEmptyMessage(LocaleConstant.START_INSPECTION_CODE) + delay(500) it.sendEmptyMessage(LocaleConstant.SELECT_LINE_CODE) } } + } - //弹框添加预置点 + binding.managePointButton.setOnClickListener { ManagePointDialog.Builder() .setContext(requireContext()) .setOnDialogButtonClickListener(object : ManagePointDialog.OnDialogButtonClickListener { - override fun onCancelClick() { - - } - override fun onConfirmClick() { - CameraInspectionService.weakReferenceHandler?.sendEmptyMessage( - LocaleConstant.SAVE_POINT_CODE - ) - } - }).build().show() - } - - binding.queryPointButton.setOnClickListener { - val beans = pointBeanDao.firstLinePoints - if (beans.isEmpty()) { - "请先添加巡航点".show(requireContext()) - return@setOnClickListener - } - - ShowPointDialog.Builder() - .setContext(requireContext()) - .setOnDialogButtonClickListener(object : - ShowPointDialog.OnDialogButtonClickListener { - override fun onCancelClick() { - - } - - override fun onConfirmClick() { - //至少得2个点才能巡航 + val beans = pointBeanDao.firstLinePoints if (beans.size < 2) { "请至少添加两个点".show(requireContext()) return } + + lifecycleScope.launch(Dispatchers.IO) { + CameraInspectionService.weakReferenceHandler?.let { + it.sendEmptyMessage(LocaleConstant.START_ADD_POINT_CONFIG_CODE) + delay(500) + it.sendEmptyMessage(LocaleConstant.SELECT_LINE_CODE) + //循环设置巡航点 + for (i in 1..beans.size) { + val message = it.obtainMessage() + message.what = LocaleConstant.SETUP_POINT_CODE + message.obj = i + it.sendMessageDelayed(message, 500) + } + it.sendEmptyMessage(LocaleConstant.START_ADD_POINT_CONFIG_CODE) + delay(500) + it.sendEmptyMessage(LocaleConstant.END_ADD_POINT_CONFIG_CODE) + } + } + } + }).build().show() + } + + binding.stopInspectionButton.setOnClickListener { + AlertControlDialog.Builder() + .setContext(requireContext()) + .setTitle("提示") + .setMessage("是否停止巡航?") + .setNegativeButton("取消") + .setPositiveButton("确定") + .setOnDialogButtonClickListener(object : + AlertControlDialog.OnDialogButtonClickListener { + override fun onCancelClick() { + + } + + override fun onConfirmClick() { CameraInspectionService.weakReferenceHandler?.sendEmptyMessage( - LocaleConstant.START_INSPECTION_CODE + LocaleConstant.STOP_INSPECTION_CODE ) } }).build().show() diff --git a/app/src/main/java/com/casic/br/operationsite/service/CameraInspectionService.kt b/app/src/main/java/com/casic/br/operationsite/service/CameraInspectionService.kt index 3b8dea5..c2cfbb4 100644 --- a/app/src/main/java/com/casic/br/operationsite/service/CameraInspectionService.kt +++ b/app/src/main/java/com/casic/br/operationsite/service/CameraInspectionService.kt @@ -16,11 +16,6 @@ import com.casic.br.operationsite.utils.TcpClient import com.pengxh.kt.lite.utils.SaveKeyValues import com.pengxh.kt.lite.utils.WeakReferenceHandler -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.SupervisorJob -import kotlinx.coroutines.delay -import kotlinx.coroutines.launch class CameraInspectionService : Service(), OnTcpConnectStateListener, Handler.Callback { @@ -32,7 +27,6 @@ private val notificationId = 2 private val tcpClient by lazy { TcpClient(this) } private val notificationManager by lazy { getSystemService(NOTIFICATION_SERVICE) as NotificationManager } - private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob()) private var notificationBuilder: NotificationCompat.Builder? = null override fun handleMessage(msg: Message): Boolean { @@ -41,26 +35,26 @@ tcpClient.start() } - LocaleConstant.START_ADD_POINT_CODE -> { - tcpClient.sendMessage(CommandCreator.invokeCommand(92)) - } - - LocaleConstant.ADD_LINE_CODE -> { - val line = msg.obj as Int - tcpClient.sendMessage(CommandCreator.invokeCommand(line)) - } - LocaleConstant.ADD_POINT_CODE -> { val point = msg.obj as Int - tcpClient.sendMessage(CommandCreator.setConfig(point)) + tcpClient.sendMessage(CommandCreator.setupConfig(point)) } - LocaleConstant.SAVE_POINT_CODE -> { - scope.launch(Dispatchers.IO) { - tcpClient.sendMessage(CommandCreator.invokeCommand(92)) - delay(500) - tcpClient.sendMessage(CommandCreator.invokeCommand(9)) - } + LocaleConstant.START_ADD_POINT_CONFIG_CODE -> { + tcpClient.sendMessage(CommandCreator.setupConfig(92)) + } + + LocaleConstant.SELECT_LINE_CODE -> { + tcpClient.sendMessage(CommandCreator.invokeCommand(1)) + } + + LocaleConstant.SETUP_POINT_CODE -> { + val point = msg.obj as Int + tcpClient.sendMessage(CommandCreator.invokeCommand(point)) + } + + LocaleConstant.END_ADD_POINT_CONFIG_CODE -> { + tcpClient.sendMessage(CommandCreator.invokeCommand(9)) } LocaleConstant.DELETE_POINT_CODE -> { @@ -69,12 +63,11 @@ } LocaleConstant.START_INSPECTION_CODE -> { - val line = msg.obj as Int - scope.launch(Dispatchers.IO) { - tcpClient.sendMessage(CommandCreator.invokeCommand(92)) - delay(500) - tcpClient.sendMessage(CommandCreator.invokeCommand(line)) - } + tcpClient.sendMessage(CommandCreator.invokeCommand(92)) + } + + LocaleConstant.STOP_INSPECTION_CODE -> { + tcpClient.sendMessage(CommandCreator.stopInspection()) } } return true diff --git a/app/src/main/java/com/casic/br/operationsite/utils/CommandCreator.kt b/app/src/main/java/com/casic/br/operationsite/utils/CommandCreator.kt index 60be355..5050636 100644 --- a/app/src/main/java/com/casic/br/operationsite/utils/CommandCreator.kt +++ b/app/src/main/java/com/casic/br/operationsite/utils/CommandCreator.kt @@ -1,5 +1,7 @@ package com.casic.br.operationsite.utils +import android.util.Log + object CommandCreator { /** * 查询激光状态 @@ -217,7 +219,7 @@ /** * 设置指令 - * + * 前提:先设置1/2/3....点 * * 设置巡航线路: * 1.设置92,调用1(1~8巡航线路) @@ -225,27 +227,8 @@ * 3.设置92,再调用9 (保存巡航线路) * */ fun setupConfig(index: Int): ByteArray { - val bytes = byteArrayOf( - 0xFF.toByte(), - 0x01, - 0x00, - 0x03, - 0x00, - 0x00, - 0x00 - ) - - // 将 index 转换为 2 字节的高位和低位 - bytes[4] = (index ushr 8).toByte() // 高位字节 - bytes[5] = (index and 0xFF).toByte() // 低位字节 - - //计算校验位。 - var sum = 0 - for (l in 1 until bytes.size - 1) { - sum += bytes[l].toInt() and 0xFF - } - bytes[6] = sum.toByte() - return bytes + val bytes = byteArrayOf(0xFF.toByte(), 0x01, 0x00, 0x03, 0x00, 0x00, 0x00) + return bytes.checksum(index) } /** @@ -255,54 +238,32 @@ * 1.调用92,调用1(开始跑巡航线路) * */ fun invokeCommand(index: Int): ByteArray { - val bytes = byteArrayOf( - 0xFF.toByte(), - 0x01, - 0x00, - 0x07, - 0x00, - 0x00, - 0x00 - ) - - // 将 index 转换为 2 字节的高位和低位 - bytes[4] = (index ushr 8).toByte() // 高位字节 - bytes[5] = (index and 0xFF).toByte() // 低位字节 - - //校验码SUM = (字节2+字节3+字节4+字节5+字节6)%256 - var sum = 0 - for (l in 1 until bytes.size - 1) { - sum += bytes[l].toInt() and 0xFF - } - bytes[6] = sum.toByte() - return bytes + val bytes = byteArrayOf(0xFF.toByte(), 0x01, 0x00, 0x07, 0x00, 0x00, 0x00) + return bytes.checksum(index) } fun deletePoint(index: Int): ByteArray { - val bytes = byteArrayOf( - 0xFF.toByte(), - 0x01, - 0x00, - 0x05, - 0x00, - 0x00, - 0x00 - ) - - // 将 index 转换为 2 字节的高位和低位 - bytes[4] = (index ushr 8).toByte() // 高位字节 - bytes[5] = (index and 0xFF).toByte() // 低位字节 - - //计算校验位。 - var sum = 0 - for (l in 1 until bytes.size - 1) { - sum += bytes[l].toInt() and 0xFF - } - bytes[6] = sum.toByte() - return bytes + val bytes = byteArrayOf(0xFF.toByte(), 0x01, 0x00, 0x05, 0x00, 0x00, 0x00) + return bytes.checksum(index) } fun stopInspection(): ByteArray { return byteArrayOf(0xFF.toByte(), 0x01, 0x00, 0x00, 0x00, 0x00, 0x01) } + + private fun ByteArray.checksum(value: Int): ByteArray { + // 将 index 转换为 2 字节的高位和低位 + this[4] = (value ushr 8).toByte() // 高位字节 + this[5] = (value and 0xFF).toByte() // 低位字节 + + //计算校验位。 + var sum = 0 + for (l in 1 until this.size - 1) { + sum += this[l].toInt() and 0xFF + } + this[6] = sum.toByte() + val hex = this.joinToString(" ") { "%02X".format(it) } + Log.d("CommandCreator", "指令16进制: $hex") + return this + } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/br/operationsite/utils/LocaleConstant.kt b/app/src/main/java/com/casic/br/operationsite/utils/LocaleConstant.kt index 6b0718e..cb17e58 100644 --- a/app/src/main/java/com/casic/br/operationsite/utils/LocaleConstant.kt +++ b/app/src/main/java/com/casic/br/operationsite/utils/LocaleConstant.kt @@ -115,13 +115,14 @@ const val CONNECT_TCP_CODE = 20251001 const val CONNECT_CAMERA_TCP_CODE = 20251002 - const val START_ADD_POINT_CODE = 20251003 - const val SELECT_LINE_CODE = 20251004 - const val ADD_POINT_CODE = 20251005 - const val SAVE_POINT_CODE = 20251006 - const val DELETE_POINT_CODE = 20251007 - const val START_INSPECTION_CODE = 20251008 - const val STOP_INSPECTION_CODE = 20251009 + const val ADD_POINT_CODE = 20251003 + const val START_ADD_POINT_CONFIG_CODE = 20251004 + const val SELECT_LINE_CODE = 20251005 + const val SETUP_POINT_CODE = 20251006 + const val END_ADD_POINT_CONFIG_CODE = 20251007 + const val DELETE_POINT_CODE = 20251008 + const val START_INSPECTION_CODE = 20251009 + const val STOP_INSPECTION_CODE = 20251010 /*** * SP Key diff --git a/app/src/main/java/com/casic/br/operationsite/adapter/ManagePointAdapter.kt b/app/src/main/java/com/casic/br/operationsite/adapter/ManagePointAdapter.kt index 536c6cb..74eaf29 100644 --- a/app/src/main/java/com/casic/br/operationsite/adapter/ManagePointAdapter.kt +++ b/app/src/main/java/com/casic/br/operationsite/adapter/ManagePointAdapter.kt @@ -46,8 +46,8 @@ textView.setTextColor(R.color.greenColor.convertColor(context)) textView.setOnClickListener { //添加巡航点 - val find = points.find { it.thirdType == 0 } - if (find == null) { + val result = points.find { it.thirdType == 0 } + if (result == null) { val pointBean = PointBean().apply { firstType = 92 secondType = 1 @@ -63,8 +63,8 @@ handler.sendMessage(message) } } else { - find.thirdType = 1 - pointBeanDao.updatePoint(find) + result.thirdType = 1 + pointBeanDao.updatePoint(result) notifyDataSetChanged() CameraInspectionService.weakReferenceHandler?.let { handler -> val message = handler.obtainMessage() diff --git a/app/src/main/java/com/casic/br/operationsite/fragments/DeviceControllerFragment.kt b/app/src/main/java/com/casic/br/operationsite/fragments/DeviceControllerFragment.kt index 005e44a..862a00c 100644 --- a/app/src/main/java/com/casic/br/operationsite/fragments/DeviceControllerFragment.kt +++ b/app/src/main/java/com/casic/br/operationsite/fragments/DeviceControllerFragment.kt @@ -14,10 +14,10 @@ import com.casic.br.operationsite.view.DeviceControlActivity import com.casic.br.operationsite.vm.DeviceViewModel import com.casic.br.operationsite.widgets.ManagePointDialog -import com.casic.br.operationsite.widgets.ShowPointDialog import com.pengxh.kt.lite.base.KotlinBaseFragment import com.pengxh.kt.lite.extensions.show import com.pengxh.kt.lite.widget.SteeringWheelView +import com.pengxh.kt.lite.widget.dialog.AlertControlDialog import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay import kotlinx.coroutines.launch @@ -100,55 +100,71 @@ binding.currentSpeedView.text = "速度:${speed}" } - binding.managePointButton.setOnClickListener { + binding.startInspectionButton.setOnClickListener { + val beans = pointBeanDao.firstLinePoints + //至少得2个点才能巡航 + if (beans.size < 2) { + "请至少添加两个点".show(requireContext()) + return@setOnClickListener + } lifecycleScope.launch(Dispatchers.IO) { CameraInspectionService.weakReferenceHandler?.let { - it.sendEmptyMessage(LocaleConstant.START_ADD_POINT_CODE) - delay(300) + it.sendEmptyMessage(LocaleConstant.START_INSPECTION_CODE) + delay(500) it.sendEmptyMessage(LocaleConstant.SELECT_LINE_CODE) } } + } - //弹框添加预置点 + binding.managePointButton.setOnClickListener { ManagePointDialog.Builder() .setContext(requireContext()) .setOnDialogButtonClickListener(object : ManagePointDialog.OnDialogButtonClickListener { - override fun onCancelClick() { - - } - override fun onConfirmClick() { - CameraInspectionService.weakReferenceHandler?.sendEmptyMessage( - LocaleConstant.SAVE_POINT_CODE - ) - } - }).build().show() - } - - binding.queryPointButton.setOnClickListener { - val beans = pointBeanDao.firstLinePoints - if (beans.isEmpty()) { - "请先添加巡航点".show(requireContext()) - return@setOnClickListener - } - - ShowPointDialog.Builder() - .setContext(requireContext()) - .setOnDialogButtonClickListener(object : - ShowPointDialog.OnDialogButtonClickListener { - override fun onCancelClick() { - - } - - override fun onConfirmClick() { - //至少得2个点才能巡航 + val beans = pointBeanDao.firstLinePoints if (beans.size < 2) { "请至少添加两个点".show(requireContext()) return } + + lifecycleScope.launch(Dispatchers.IO) { + CameraInspectionService.weakReferenceHandler?.let { + it.sendEmptyMessage(LocaleConstant.START_ADD_POINT_CONFIG_CODE) + delay(500) + it.sendEmptyMessage(LocaleConstant.SELECT_LINE_CODE) + //循环设置巡航点 + for (i in 1..beans.size) { + val message = it.obtainMessage() + message.what = LocaleConstant.SETUP_POINT_CODE + message.obj = i + it.sendMessageDelayed(message, 500) + } + it.sendEmptyMessage(LocaleConstant.START_ADD_POINT_CONFIG_CODE) + delay(500) + it.sendEmptyMessage(LocaleConstant.END_ADD_POINT_CONFIG_CODE) + } + } + } + }).build().show() + } + + binding.stopInspectionButton.setOnClickListener { + AlertControlDialog.Builder() + .setContext(requireContext()) + .setTitle("提示") + .setMessage("是否停止巡航?") + .setNegativeButton("取消") + .setPositiveButton("确定") + .setOnDialogButtonClickListener(object : + AlertControlDialog.OnDialogButtonClickListener { + override fun onCancelClick() { + + } + + override fun onConfirmClick() { CameraInspectionService.weakReferenceHandler?.sendEmptyMessage( - LocaleConstant.START_INSPECTION_CODE + LocaleConstant.STOP_INSPECTION_CODE ) } }).build().show() diff --git a/app/src/main/java/com/casic/br/operationsite/service/CameraInspectionService.kt b/app/src/main/java/com/casic/br/operationsite/service/CameraInspectionService.kt index 3b8dea5..c2cfbb4 100644 --- a/app/src/main/java/com/casic/br/operationsite/service/CameraInspectionService.kt +++ b/app/src/main/java/com/casic/br/operationsite/service/CameraInspectionService.kt @@ -16,11 +16,6 @@ import com.casic.br.operationsite.utils.TcpClient import com.pengxh.kt.lite.utils.SaveKeyValues import com.pengxh.kt.lite.utils.WeakReferenceHandler -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.SupervisorJob -import kotlinx.coroutines.delay -import kotlinx.coroutines.launch class CameraInspectionService : Service(), OnTcpConnectStateListener, Handler.Callback { @@ -32,7 +27,6 @@ private val notificationId = 2 private val tcpClient by lazy { TcpClient(this) } private val notificationManager by lazy { getSystemService(NOTIFICATION_SERVICE) as NotificationManager } - private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob()) private var notificationBuilder: NotificationCompat.Builder? = null override fun handleMessage(msg: Message): Boolean { @@ -41,26 +35,26 @@ tcpClient.start() } - LocaleConstant.START_ADD_POINT_CODE -> { - tcpClient.sendMessage(CommandCreator.invokeCommand(92)) - } - - LocaleConstant.ADD_LINE_CODE -> { - val line = msg.obj as Int - tcpClient.sendMessage(CommandCreator.invokeCommand(line)) - } - LocaleConstant.ADD_POINT_CODE -> { val point = msg.obj as Int - tcpClient.sendMessage(CommandCreator.setConfig(point)) + tcpClient.sendMessage(CommandCreator.setupConfig(point)) } - LocaleConstant.SAVE_POINT_CODE -> { - scope.launch(Dispatchers.IO) { - tcpClient.sendMessage(CommandCreator.invokeCommand(92)) - delay(500) - tcpClient.sendMessage(CommandCreator.invokeCommand(9)) - } + LocaleConstant.START_ADD_POINT_CONFIG_CODE -> { + tcpClient.sendMessage(CommandCreator.setupConfig(92)) + } + + LocaleConstant.SELECT_LINE_CODE -> { + tcpClient.sendMessage(CommandCreator.invokeCommand(1)) + } + + LocaleConstant.SETUP_POINT_CODE -> { + val point = msg.obj as Int + tcpClient.sendMessage(CommandCreator.invokeCommand(point)) + } + + LocaleConstant.END_ADD_POINT_CONFIG_CODE -> { + tcpClient.sendMessage(CommandCreator.invokeCommand(9)) } LocaleConstant.DELETE_POINT_CODE -> { @@ -69,12 +63,11 @@ } LocaleConstant.START_INSPECTION_CODE -> { - val line = msg.obj as Int - scope.launch(Dispatchers.IO) { - tcpClient.sendMessage(CommandCreator.invokeCommand(92)) - delay(500) - tcpClient.sendMessage(CommandCreator.invokeCommand(line)) - } + tcpClient.sendMessage(CommandCreator.invokeCommand(92)) + } + + LocaleConstant.STOP_INSPECTION_CODE -> { + tcpClient.sendMessage(CommandCreator.stopInspection()) } } return true diff --git a/app/src/main/java/com/casic/br/operationsite/utils/CommandCreator.kt b/app/src/main/java/com/casic/br/operationsite/utils/CommandCreator.kt index 60be355..5050636 100644 --- a/app/src/main/java/com/casic/br/operationsite/utils/CommandCreator.kt +++ b/app/src/main/java/com/casic/br/operationsite/utils/CommandCreator.kt @@ -1,5 +1,7 @@ package com.casic.br.operationsite.utils +import android.util.Log + object CommandCreator { /** * 查询激光状态 @@ -217,7 +219,7 @@ /** * 设置指令 - * + * 前提:先设置1/2/3....点 * * 设置巡航线路: * 1.设置92,调用1(1~8巡航线路) @@ -225,27 +227,8 @@ * 3.设置92,再调用9 (保存巡航线路) * */ fun setupConfig(index: Int): ByteArray { - val bytes = byteArrayOf( - 0xFF.toByte(), - 0x01, - 0x00, - 0x03, - 0x00, - 0x00, - 0x00 - ) - - // 将 index 转换为 2 字节的高位和低位 - bytes[4] = (index ushr 8).toByte() // 高位字节 - bytes[5] = (index and 0xFF).toByte() // 低位字节 - - //计算校验位。 - var sum = 0 - for (l in 1 until bytes.size - 1) { - sum += bytes[l].toInt() and 0xFF - } - bytes[6] = sum.toByte() - return bytes + val bytes = byteArrayOf(0xFF.toByte(), 0x01, 0x00, 0x03, 0x00, 0x00, 0x00) + return bytes.checksum(index) } /** @@ -255,54 +238,32 @@ * 1.调用92,调用1(开始跑巡航线路) * */ fun invokeCommand(index: Int): ByteArray { - val bytes = byteArrayOf( - 0xFF.toByte(), - 0x01, - 0x00, - 0x07, - 0x00, - 0x00, - 0x00 - ) - - // 将 index 转换为 2 字节的高位和低位 - bytes[4] = (index ushr 8).toByte() // 高位字节 - bytes[5] = (index and 0xFF).toByte() // 低位字节 - - //校验码SUM = (字节2+字节3+字节4+字节5+字节6)%256 - var sum = 0 - for (l in 1 until bytes.size - 1) { - sum += bytes[l].toInt() and 0xFF - } - bytes[6] = sum.toByte() - return bytes + val bytes = byteArrayOf(0xFF.toByte(), 0x01, 0x00, 0x07, 0x00, 0x00, 0x00) + return bytes.checksum(index) } fun deletePoint(index: Int): ByteArray { - val bytes = byteArrayOf( - 0xFF.toByte(), - 0x01, - 0x00, - 0x05, - 0x00, - 0x00, - 0x00 - ) - - // 将 index 转换为 2 字节的高位和低位 - bytes[4] = (index ushr 8).toByte() // 高位字节 - bytes[5] = (index and 0xFF).toByte() // 低位字节 - - //计算校验位。 - var sum = 0 - for (l in 1 until bytes.size - 1) { - sum += bytes[l].toInt() and 0xFF - } - bytes[6] = sum.toByte() - return bytes + val bytes = byteArrayOf(0xFF.toByte(), 0x01, 0x00, 0x05, 0x00, 0x00, 0x00) + return bytes.checksum(index) } fun stopInspection(): ByteArray { return byteArrayOf(0xFF.toByte(), 0x01, 0x00, 0x00, 0x00, 0x00, 0x01) } + + private fun ByteArray.checksum(value: Int): ByteArray { + // 将 index 转换为 2 字节的高位和低位 + this[4] = (value ushr 8).toByte() // 高位字节 + this[5] = (value and 0xFF).toByte() // 低位字节 + + //计算校验位。 + var sum = 0 + for (l in 1 until this.size - 1) { + sum += this[l].toInt() and 0xFF + } + this[6] = sum.toByte() + val hex = this.joinToString(" ") { "%02X".format(it) } + Log.d("CommandCreator", "指令16进制: $hex") + return this + } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/br/operationsite/utils/LocaleConstant.kt b/app/src/main/java/com/casic/br/operationsite/utils/LocaleConstant.kt index 6b0718e..cb17e58 100644 --- a/app/src/main/java/com/casic/br/operationsite/utils/LocaleConstant.kt +++ b/app/src/main/java/com/casic/br/operationsite/utils/LocaleConstant.kt @@ -115,13 +115,14 @@ const val CONNECT_TCP_CODE = 20251001 const val CONNECT_CAMERA_TCP_CODE = 20251002 - const val START_ADD_POINT_CODE = 20251003 - const val SELECT_LINE_CODE = 20251004 - const val ADD_POINT_CODE = 20251005 - const val SAVE_POINT_CODE = 20251006 - const val DELETE_POINT_CODE = 20251007 - const val START_INSPECTION_CODE = 20251008 - const val STOP_INSPECTION_CODE = 20251009 + const val ADD_POINT_CODE = 20251003 + const val START_ADD_POINT_CONFIG_CODE = 20251004 + const val SELECT_LINE_CODE = 20251005 + const val SETUP_POINT_CODE = 20251006 + const val END_ADD_POINT_CONFIG_CODE = 20251007 + const val DELETE_POINT_CODE = 20251008 + const val START_INSPECTION_CODE = 20251009 + const val STOP_INSPECTION_CODE = 20251010 /*** * SP Key diff --git a/app/src/main/java/com/casic/br/operationsite/widgets/ManagePointDialog.kt b/app/src/main/java/com/casic/br/operationsite/widgets/ManagePointDialog.kt index fc9fa2d..9b92e5e 100644 --- a/app/src/main/java/com/casic/br/operationsite/widgets/ManagePointDialog.kt +++ b/app/src/main/java/com/casic/br/operationsite/widgets/ManagePointDialog.kt @@ -41,20 +41,16 @@ override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - initDialogLayoutParams(0.8f, 0.6f) + initDialogLayoutParams(0.75f, 0.55f) setCanceledOnTouchOutside(false) setCancelable(false) - val pointAdapter = ManagePointAdapter(context, pointBeanDao.firstLinePoints, 8) - binding.recyclerView.adapter = pointAdapter + binding.recyclerView.adapter = ManagePointAdapter(context, pointBeanDao.firstLinePoints, 8) binding.recyclerView.addItemDecoration(RecyclerViewItemDivider(0f, 0f, Color.LTGRAY)) - binding.textView.isSelected = true + binding.textView.isSelected = true - binding.dialogCancelButton.setOnClickListener { - listener.onCancelClick() - dismiss() - } + binding.dialogCancelButton.setOnClickListener { dismiss() } binding.dialogConfirmButton.setOnClickListener { listener.onConfirmClick() @@ -64,7 +60,5 @@ interface OnDialogButtonClickListener { fun onConfirmClick() - - fun onCancelClick() } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/br/operationsite/adapter/ManagePointAdapter.kt b/app/src/main/java/com/casic/br/operationsite/adapter/ManagePointAdapter.kt index 536c6cb..74eaf29 100644 --- a/app/src/main/java/com/casic/br/operationsite/adapter/ManagePointAdapter.kt +++ b/app/src/main/java/com/casic/br/operationsite/adapter/ManagePointAdapter.kt @@ -46,8 +46,8 @@ textView.setTextColor(R.color.greenColor.convertColor(context)) textView.setOnClickListener { //添加巡航点 - val find = points.find { it.thirdType == 0 } - if (find == null) { + val result = points.find { it.thirdType == 0 } + if (result == null) { val pointBean = PointBean().apply { firstType = 92 secondType = 1 @@ -63,8 +63,8 @@ handler.sendMessage(message) } } else { - find.thirdType = 1 - pointBeanDao.updatePoint(find) + result.thirdType = 1 + pointBeanDao.updatePoint(result) notifyDataSetChanged() CameraInspectionService.weakReferenceHandler?.let { handler -> val message = handler.obtainMessage() diff --git a/app/src/main/java/com/casic/br/operationsite/fragments/DeviceControllerFragment.kt b/app/src/main/java/com/casic/br/operationsite/fragments/DeviceControllerFragment.kt index 005e44a..862a00c 100644 --- a/app/src/main/java/com/casic/br/operationsite/fragments/DeviceControllerFragment.kt +++ b/app/src/main/java/com/casic/br/operationsite/fragments/DeviceControllerFragment.kt @@ -14,10 +14,10 @@ import com.casic.br.operationsite.view.DeviceControlActivity import com.casic.br.operationsite.vm.DeviceViewModel import com.casic.br.operationsite.widgets.ManagePointDialog -import com.casic.br.operationsite.widgets.ShowPointDialog import com.pengxh.kt.lite.base.KotlinBaseFragment import com.pengxh.kt.lite.extensions.show import com.pengxh.kt.lite.widget.SteeringWheelView +import com.pengxh.kt.lite.widget.dialog.AlertControlDialog import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay import kotlinx.coroutines.launch @@ -100,55 +100,71 @@ binding.currentSpeedView.text = "速度:${speed}" } - binding.managePointButton.setOnClickListener { + binding.startInspectionButton.setOnClickListener { + val beans = pointBeanDao.firstLinePoints + //至少得2个点才能巡航 + if (beans.size < 2) { + "请至少添加两个点".show(requireContext()) + return@setOnClickListener + } lifecycleScope.launch(Dispatchers.IO) { CameraInspectionService.weakReferenceHandler?.let { - it.sendEmptyMessage(LocaleConstant.START_ADD_POINT_CODE) - delay(300) + it.sendEmptyMessage(LocaleConstant.START_INSPECTION_CODE) + delay(500) it.sendEmptyMessage(LocaleConstant.SELECT_LINE_CODE) } } + } - //弹框添加预置点 + binding.managePointButton.setOnClickListener { ManagePointDialog.Builder() .setContext(requireContext()) .setOnDialogButtonClickListener(object : ManagePointDialog.OnDialogButtonClickListener { - override fun onCancelClick() { - - } - override fun onConfirmClick() { - CameraInspectionService.weakReferenceHandler?.sendEmptyMessage( - LocaleConstant.SAVE_POINT_CODE - ) - } - }).build().show() - } - - binding.queryPointButton.setOnClickListener { - val beans = pointBeanDao.firstLinePoints - if (beans.isEmpty()) { - "请先添加巡航点".show(requireContext()) - return@setOnClickListener - } - - ShowPointDialog.Builder() - .setContext(requireContext()) - .setOnDialogButtonClickListener(object : - ShowPointDialog.OnDialogButtonClickListener { - override fun onCancelClick() { - - } - - override fun onConfirmClick() { - //至少得2个点才能巡航 + val beans = pointBeanDao.firstLinePoints if (beans.size < 2) { "请至少添加两个点".show(requireContext()) return } + + lifecycleScope.launch(Dispatchers.IO) { + CameraInspectionService.weakReferenceHandler?.let { + it.sendEmptyMessage(LocaleConstant.START_ADD_POINT_CONFIG_CODE) + delay(500) + it.sendEmptyMessage(LocaleConstant.SELECT_LINE_CODE) + //循环设置巡航点 + for (i in 1..beans.size) { + val message = it.obtainMessage() + message.what = LocaleConstant.SETUP_POINT_CODE + message.obj = i + it.sendMessageDelayed(message, 500) + } + it.sendEmptyMessage(LocaleConstant.START_ADD_POINT_CONFIG_CODE) + delay(500) + it.sendEmptyMessage(LocaleConstant.END_ADD_POINT_CONFIG_CODE) + } + } + } + }).build().show() + } + + binding.stopInspectionButton.setOnClickListener { + AlertControlDialog.Builder() + .setContext(requireContext()) + .setTitle("提示") + .setMessage("是否停止巡航?") + .setNegativeButton("取消") + .setPositiveButton("确定") + .setOnDialogButtonClickListener(object : + AlertControlDialog.OnDialogButtonClickListener { + override fun onCancelClick() { + + } + + override fun onConfirmClick() { CameraInspectionService.weakReferenceHandler?.sendEmptyMessage( - LocaleConstant.START_INSPECTION_CODE + LocaleConstant.STOP_INSPECTION_CODE ) } }).build().show() diff --git a/app/src/main/java/com/casic/br/operationsite/service/CameraInspectionService.kt b/app/src/main/java/com/casic/br/operationsite/service/CameraInspectionService.kt index 3b8dea5..c2cfbb4 100644 --- a/app/src/main/java/com/casic/br/operationsite/service/CameraInspectionService.kt +++ b/app/src/main/java/com/casic/br/operationsite/service/CameraInspectionService.kt @@ -16,11 +16,6 @@ import com.casic.br.operationsite.utils.TcpClient import com.pengxh.kt.lite.utils.SaveKeyValues import com.pengxh.kt.lite.utils.WeakReferenceHandler -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.SupervisorJob -import kotlinx.coroutines.delay -import kotlinx.coroutines.launch class CameraInspectionService : Service(), OnTcpConnectStateListener, Handler.Callback { @@ -32,7 +27,6 @@ private val notificationId = 2 private val tcpClient by lazy { TcpClient(this) } private val notificationManager by lazy { getSystemService(NOTIFICATION_SERVICE) as NotificationManager } - private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob()) private var notificationBuilder: NotificationCompat.Builder? = null override fun handleMessage(msg: Message): Boolean { @@ -41,26 +35,26 @@ tcpClient.start() } - LocaleConstant.START_ADD_POINT_CODE -> { - tcpClient.sendMessage(CommandCreator.invokeCommand(92)) - } - - LocaleConstant.ADD_LINE_CODE -> { - val line = msg.obj as Int - tcpClient.sendMessage(CommandCreator.invokeCommand(line)) - } - LocaleConstant.ADD_POINT_CODE -> { val point = msg.obj as Int - tcpClient.sendMessage(CommandCreator.setConfig(point)) + tcpClient.sendMessage(CommandCreator.setupConfig(point)) } - LocaleConstant.SAVE_POINT_CODE -> { - scope.launch(Dispatchers.IO) { - tcpClient.sendMessage(CommandCreator.invokeCommand(92)) - delay(500) - tcpClient.sendMessage(CommandCreator.invokeCommand(9)) - } + LocaleConstant.START_ADD_POINT_CONFIG_CODE -> { + tcpClient.sendMessage(CommandCreator.setupConfig(92)) + } + + LocaleConstant.SELECT_LINE_CODE -> { + tcpClient.sendMessage(CommandCreator.invokeCommand(1)) + } + + LocaleConstant.SETUP_POINT_CODE -> { + val point = msg.obj as Int + tcpClient.sendMessage(CommandCreator.invokeCommand(point)) + } + + LocaleConstant.END_ADD_POINT_CONFIG_CODE -> { + tcpClient.sendMessage(CommandCreator.invokeCommand(9)) } LocaleConstant.DELETE_POINT_CODE -> { @@ -69,12 +63,11 @@ } LocaleConstant.START_INSPECTION_CODE -> { - val line = msg.obj as Int - scope.launch(Dispatchers.IO) { - tcpClient.sendMessage(CommandCreator.invokeCommand(92)) - delay(500) - tcpClient.sendMessage(CommandCreator.invokeCommand(line)) - } + tcpClient.sendMessage(CommandCreator.invokeCommand(92)) + } + + LocaleConstant.STOP_INSPECTION_CODE -> { + tcpClient.sendMessage(CommandCreator.stopInspection()) } } return true diff --git a/app/src/main/java/com/casic/br/operationsite/utils/CommandCreator.kt b/app/src/main/java/com/casic/br/operationsite/utils/CommandCreator.kt index 60be355..5050636 100644 --- a/app/src/main/java/com/casic/br/operationsite/utils/CommandCreator.kt +++ b/app/src/main/java/com/casic/br/operationsite/utils/CommandCreator.kt @@ -1,5 +1,7 @@ package com.casic.br.operationsite.utils +import android.util.Log + object CommandCreator { /** * 查询激光状态 @@ -217,7 +219,7 @@ /** * 设置指令 - * + * 前提:先设置1/2/3....点 * * 设置巡航线路: * 1.设置92,调用1(1~8巡航线路) @@ -225,27 +227,8 @@ * 3.设置92,再调用9 (保存巡航线路) * */ fun setupConfig(index: Int): ByteArray { - val bytes = byteArrayOf( - 0xFF.toByte(), - 0x01, - 0x00, - 0x03, - 0x00, - 0x00, - 0x00 - ) - - // 将 index 转换为 2 字节的高位和低位 - bytes[4] = (index ushr 8).toByte() // 高位字节 - bytes[5] = (index and 0xFF).toByte() // 低位字节 - - //计算校验位。 - var sum = 0 - for (l in 1 until bytes.size - 1) { - sum += bytes[l].toInt() and 0xFF - } - bytes[6] = sum.toByte() - return bytes + val bytes = byteArrayOf(0xFF.toByte(), 0x01, 0x00, 0x03, 0x00, 0x00, 0x00) + return bytes.checksum(index) } /** @@ -255,54 +238,32 @@ * 1.调用92,调用1(开始跑巡航线路) * */ fun invokeCommand(index: Int): ByteArray { - val bytes = byteArrayOf( - 0xFF.toByte(), - 0x01, - 0x00, - 0x07, - 0x00, - 0x00, - 0x00 - ) - - // 将 index 转换为 2 字节的高位和低位 - bytes[4] = (index ushr 8).toByte() // 高位字节 - bytes[5] = (index and 0xFF).toByte() // 低位字节 - - //校验码SUM = (字节2+字节3+字节4+字节5+字节6)%256 - var sum = 0 - for (l in 1 until bytes.size - 1) { - sum += bytes[l].toInt() and 0xFF - } - bytes[6] = sum.toByte() - return bytes + val bytes = byteArrayOf(0xFF.toByte(), 0x01, 0x00, 0x07, 0x00, 0x00, 0x00) + return bytes.checksum(index) } fun deletePoint(index: Int): ByteArray { - val bytes = byteArrayOf( - 0xFF.toByte(), - 0x01, - 0x00, - 0x05, - 0x00, - 0x00, - 0x00 - ) - - // 将 index 转换为 2 字节的高位和低位 - bytes[4] = (index ushr 8).toByte() // 高位字节 - bytes[5] = (index and 0xFF).toByte() // 低位字节 - - //计算校验位。 - var sum = 0 - for (l in 1 until bytes.size - 1) { - sum += bytes[l].toInt() and 0xFF - } - bytes[6] = sum.toByte() - return bytes + val bytes = byteArrayOf(0xFF.toByte(), 0x01, 0x00, 0x05, 0x00, 0x00, 0x00) + return bytes.checksum(index) } fun stopInspection(): ByteArray { return byteArrayOf(0xFF.toByte(), 0x01, 0x00, 0x00, 0x00, 0x00, 0x01) } + + private fun ByteArray.checksum(value: Int): ByteArray { + // 将 index 转换为 2 字节的高位和低位 + this[4] = (value ushr 8).toByte() // 高位字节 + this[5] = (value and 0xFF).toByte() // 低位字节 + + //计算校验位。 + var sum = 0 + for (l in 1 until this.size - 1) { + sum += this[l].toInt() and 0xFF + } + this[6] = sum.toByte() + val hex = this.joinToString(" ") { "%02X".format(it) } + Log.d("CommandCreator", "指令16进制: $hex") + return this + } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/br/operationsite/utils/LocaleConstant.kt b/app/src/main/java/com/casic/br/operationsite/utils/LocaleConstant.kt index 6b0718e..cb17e58 100644 --- a/app/src/main/java/com/casic/br/operationsite/utils/LocaleConstant.kt +++ b/app/src/main/java/com/casic/br/operationsite/utils/LocaleConstant.kt @@ -115,13 +115,14 @@ const val CONNECT_TCP_CODE = 20251001 const val CONNECT_CAMERA_TCP_CODE = 20251002 - const val START_ADD_POINT_CODE = 20251003 - const val SELECT_LINE_CODE = 20251004 - const val ADD_POINT_CODE = 20251005 - const val SAVE_POINT_CODE = 20251006 - const val DELETE_POINT_CODE = 20251007 - const val START_INSPECTION_CODE = 20251008 - const val STOP_INSPECTION_CODE = 20251009 + const val ADD_POINT_CODE = 20251003 + const val START_ADD_POINT_CONFIG_CODE = 20251004 + const val SELECT_LINE_CODE = 20251005 + const val SETUP_POINT_CODE = 20251006 + const val END_ADD_POINT_CONFIG_CODE = 20251007 + const val DELETE_POINT_CODE = 20251008 + const val START_INSPECTION_CODE = 20251009 + const val STOP_INSPECTION_CODE = 20251010 /*** * SP Key diff --git a/app/src/main/java/com/casic/br/operationsite/widgets/ManagePointDialog.kt b/app/src/main/java/com/casic/br/operationsite/widgets/ManagePointDialog.kt index fc9fa2d..9b92e5e 100644 --- a/app/src/main/java/com/casic/br/operationsite/widgets/ManagePointDialog.kt +++ b/app/src/main/java/com/casic/br/operationsite/widgets/ManagePointDialog.kt @@ -41,20 +41,16 @@ override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - initDialogLayoutParams(0.8f, 0.6f) + initDialogLayoutParams(0.75f, 0.55f) setCanceledOnTouchOutside(false) setCancelable(false) - val pointAdapter = ManagePointAdapter(context, pointBeanDao.firstLinePoints, 8) - binding.recyclerView.adapter = pointAdapter + binding.recyclerView.adapter = ManagePointAdapter(context, pointBeanDao.firstLinePoints, 8) binding.recyclerView.addItemDecoration(RecyclerViewItemDivider(0f, 0f, Color.LTGRAY)) - binding.textView.isSelected = true + binding.textView.isSelected = true - binding.dialogCancelButton.setOnClickListener { - listener.onCancelClick() - dismiss() - } + binding.dialogCancelButton.setOnClickListener { dismiss() } binding.dialogConfirmButton.setOnClickListener { listener.onConfirmClick() @@ -64,7 +60,5 @@ interface OnDialogButtonClickListener { fun onConfirmClick() - - fun onCancelClick() } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/br/operationsite/widgets/ShowPointDialog.kt b/app/src/main/java/com/casic/br/operationsite/widgets/ShowPointDialog.kt deleted file mode 100644 index 65ac096..0000000 --- a/app/src/main/java/com/casic/br/operationsite/widgets/ShowPointDialog.kt +++ /dev/null @@ -1,82 +0,0 @@ -package com.casic.br.operationsite.widgets - -import android.app.Dialog -import android.content.Context -import android.graphics.Color -import android.os.Bundle -import com.casic.br.operationsite.R -import com.casic.br.operationsite.base.BaseApplication -import com.casic.br.operationsite.bean.PointBean -import com.casic.br.operationsite.databinding.DialogShowPointBinding -import com.casic.br.operationsite.extensions.initDialogLayoutParams -import com.pengxh.kt.lite.adapter.NormalRecyclerAdapter -import com.pengxh.kt.lite.adapter.ViewHolder -import com.pengxh.kt.lite.divider.RecyclerViewItemDivider -import com.pengxh.kt.lite.extensions.binding - -class ShowPointDialog(builder: Builder) : Dialog(builder.context) { - - private val kTag = "ShowPointDialog" - private val pointBeanDao by lazy { BaseApplication.get().dataBase.pointBeanDao() } - private var pointBeans: MutableList = ArrayList() - private val listener = builder.listener - - class Builder { - lateinit var context: Context - lateinit var listener: OnDialogButtonClickListener - - fun setContext(context: Context): Builder { - this.context = context - return this - } - - fun setOnDialogButtonClickListener(listener: OnDialogButtonClickListener): Builder { - this.listener = listener - return this - } - - fun build(): ShowPointDialog { - return ShowPointDialog(this) - } - } - - private val binding: DialogShowPointBinding by binding() - - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - initDialogLayoutParams(0.8f, 0.6f) - setCanceledOnTouchOutside(false) - setCancelable(false) - - val beans = pointBeanDao.firstLinePoints - if (beans.isEmpty()) return - - val pointAdapter = object : NormalRecyclerAdapter( - R.layout.item_inspection_point_rv_l, beans - ) { - override fun convertView( - viewHolder: ViewHolder, position: Int, item: PointBean - ) { - viewHolder.setText(R.id.textView, "预置点位 ${item.thirdType}") - } - } - binding.recyclerView.adapter = pointAdapter - binding.recyclerView.addItemDecoration(RecyclerViewItemDivider(0f, 0f, Color.LTGRAY)) - - binding.dialogCancelButton.setOnClickListener { - listener.onCancelClick() - dismiss() - } - - binding.dialogConfirmButton.setOnClickListener { - listener.onConfirmClick() - dismiss() - } - } - - interface OnDialogButtonClickListener { - fun onConfirmClick() - - fun onCancelClick() - } -} diff --git a/app/src/main/java/com/casic/br/operationsite/adapter/ManagePointAdapter.kt b/app/src/main/java/com/casic/br/operationsite/adapter/ManagePointAdapter.kt index 536c6cb..74eaf29 100644 --- a/app/src/main/java/com/casic/br/operationsite/adapter/ManagePointAdapter.kt +++ b/app/src/main/java/com/casic/br/operationsite/adapter/ManagePointAdapter.kt @@ -46,8 +46,8 @@ textView.setTextColor(R.color.greenColor.convertColor(context)) textView.setOnClickListener { //添加巡航点 - val find = points.find { it.thirdType == 0 } - if (find == null) { + val result = points.find { it.thirdType == 0 } + if (result == null) { val pointBean = PointBean().apply { firstType = 92 secondType = 1 @@ -63,8 +63,8 @@ handler.sendMessage(message) } } else { - find.thirdType = 1 - pointBeanDao.updatePoint(find) + result.thirdType = 1 + pointBeanDao.updatePoint(result) notifyDataSetChanged() CameraInspectionService.weakReferenceHandler?.let { handler -> val message = handler.obtainMessage() diff --git a/app/src/main/java/com/casic/br/operationsite/fragments/DeviceControllerFragment.kt b/app/src/main/java/com/casic/br/operationsite/fragments/DeviceControllerFragment.kt index 005e44a..862a00c 100644 --- a/app/src/main/java/com/casic/br/operationsite/fragments/DeviceControllerFragment.kt +++ b/app/src/main/java/com/casic/br/operationsite/fragments/DeviceControllerFragment.kt @@ -14,10 +14,10 @@ import com.casic.br.operationsite.view.DeviceControlActivity import com.casic.br.operationsite.vm.DeviceViewModel import com.casic.br.operationsite.widgets.ManagePointDialog -import com.casic.br.operationsite.widgets.ShowPointDialog import com.pengxh.kt.lite.base.KotlinBaseFragment import com.pengxh.kt.lite.extensions.show import com.pengxh.kt.lite.widget.SteeringWheelView +import com.pengxh.kt.lite.widget.dialog.AlertControlDialog import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay import kotlinx.coroutines.launch @@ -100,55 +100,71 @@ binding.currentSpeedView.text = "速度:${speed}" } - binding.managePointButton.setOnClickListener { + binding.startInspectionButton.setOnClickListener { + val beans = pointBeanDao.firstLinePoints + //至少得2个点才能巡航 + if (beans.size < 2) { + "请至少添加两个点".show(requireContext()) + return@setOnClickListener + } lifecycleScope.launch(Dispatchers.IO) { CameraInspectionService.weakReferenceHandler?.let { - it.sendEmptyMessage(LocaleConstant.START_ADD_POINT_CODE) - delay(300) + it.sendEmptyMessage(LocaleConstant.START_INSPECTION_CODE) + delay(500) it.sendEmptyMessage(LocaleConstant.SELECT_LINE_CODE) } } + } - //弹框添加预置点 + binding.managePointButton.setOnClickListener { ManagePointDialog.Builder() .setContext(requireContext()) .setOnDialogButtonClickListener(object : ManagePointDialog.OnDialogButtonClickListener { - override fun onCancelClick() { - - } - override fun onConfirmClick() { - CameraInspectionService.weakReferenceHandler?.sendEmptyMessage( - LocaleConstant.SAVE_POINT_CODE - ) - } - }).build().show() - } - - binding.queryPointButton.setOnClickListener { - val beans = pointBeanDao.firstLinePoints - if (beans.isEmpty()) { - "请先添加巡航点".show(requireContext()) - return@setOnClickListener - } - - ShowPointDialog.Builder() - .setContext(requireContext()) - .setOnDialogButtonClickListener(object : - ShowPointDialog.OnDialogButtonClickListener { - override fun onCancelClick() { - - } - - override fun onConfirmClick() { - //至少得2个点才能巡航 + val beans = pointBeanDao.firstLinePoints if (beans.size < 2) { "请至少添加两个点".show(requireContext()) return } + + lifecycleScope.launch(Dispatchers.IO) { + CameraInspectionService.weakReferenceHandler?.let { + it.sendEmptyMessage(LocaleConstant.START_ADD_POINT_CONFIG_CODE) + delay(500) + it.sendEmptyMessage(LocaleConstant.SELECT_LINE_CODE) + //循环设置巡航点 + for (i in 1..beans.size) { + val message = it.obtainMessage() + message.what = LocaleConstant.SETUP_POINT_CODE + message.obj = i + it.sendMessageDelayed(message, 500) + } + it.sendEmptyMessage(LocaleConstant.START_ADD_POINT_CONFIG_CODE) + delay(500) + it.sendEmptyMessage(LocaleConstant.END_ADD_POINT_CONFIG_CODE) + } + } + } + }).build().show() + } + + binding.stopInspectionButton.setOnClickListener { + AlertControlDialog.Builder() + .setContext(requireContext()) + .setTitle("提示") + .setMessage("是否停止巡航?") + .setNegativeButton("取消") + .setPositiveButton("确定") + .setOnDialogButtonClickListener(object : + AlertControlDialog.OnDialogButtonClickListener { + override fun onCancelClick() { + + } + + override fun onConfirmClick() { CameraInspectionService.weakReferenceHandler?.sendEmptyMessage( - LocaleConstant.START_INSPECTION_CODE + LocaleConstant.STOP_INSPECTION_CODE ) } }).build().show() diff --git a/app/src/main/java/com/casic/br/operationsite/service/CameraInspectionService.kt b/app/src/main/java/com/casic/br/operationsite/service/CameraInspectionService.kt index 3b8dea5..c2cfbb4 100644 --- a/app/src/main/java/com/casic/br/operationsite/service/CameraInspectionService.kt +++ b/app/src/main/java/com/casic/br/operationsite/service/CameraInspectionService.kt @@ -16,11 +16,6 @@ import com.casic.br.operationsite.utils.TcpClient import com.pengxh.kt.lite.utils.SaveKeyValues import com.pengxh.kt.lite.utils.WeakReferenceHandler -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.SupervisorJob -import kotlinx.coroutines.delay -import kotlinx.coroutines.launch class CameraInspectionService : Service(), OnTcpConnectStateListener, Handler.Callback { @@ -32,7 +27,6 @@ private val notificationId = 2 private val tcpClient by lazy { TcpClient(this) } private val notificationManager by lazy { getSystemService(NOTIFICATION_SERVICE) as NotificationManager } - private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob()) private var notificationBuilder: NotificationCompat.Builder? = null override fun handleMessage(msg: Message): Boolean { @@ -41,26 +35,26 @@ tcpClient.start() } - LocaleConstant.START_ADD_POINT_CODE -> { - tcpClient.sendMessage(CommandCreator.invokeCommand(92)) - } - - LocaleConstant.ADD_LINE_CODE -> { - val line = msg.obj as Int - tcpClient.sendMessage(CommandCreator.invokeCommand(line)) - } - LocaleConstant.ADD_POINT_CODE -> { val point = msg.obj as Int - tcpClient.sendMessage(CommandCreator.setConfig(point)) + tcpClient.sendMessage(CommandCreator.setupConfig(point)) } - LocaleConstant.SAVE_POINT_CODE -> { - scope.launch(Dispatchers.IO) { - tcpClient.sendMessage(CommandCreator.invokeCommand(92)) - delay(500) - tcpClient.sendMessage(CommandCreator.invokeCommand(9)) - } + LocaleConstant.START_ADD_POINT_CONFIG_CODE -> { + tcpClient.sendMessage(CommandCreator.setupConfig(92)) + } + + LocaleConstant.SELECT_LINE_CODE -> { + tcpClient.sendMessage(CommandCreator.invokeCommand(1)) + } + + LocaleConstant.SETUP_POINT_CODE -> { + val point = msg.obj as Int + tcpClient.sendMessage(CommandCreator.invokeCommand(point)) + } + + LocaleConstant.END_ADD_POINT_CONFIG_CODE -> { + tcpClient.sendMessage(CommandCreator.invokeCommand(9)) } LocaleConstant.DELETE_POINT_CODE -> { @@ -69,12 +63,11 @@ } LocaleConstant.START_INSPECTION_CODE -> { - val line = msg.obj as Int - scope.launch(Dispatchers.IO) { - tcpClient.sendMessage(CommandCreator.invokeCommand(92)) - delay(500) - tcpClient.sendMessage(CommandCreator.invokeCommand(line)) - } + tcpClient.sendMessage(CommandCreator.invokeCommand(92)) + } + + LocaleConstant.STOP_INSPECTION_CODE -> { + tcpClient.sendMessage(CommandCreator.stopInspection()) } } return true diff --git a/app/src/main/java/com/casic/br/operationsite/utils/CommandCreator.kt b/app/src/main/java/com/casic/br/operationsite/utils/CommandCreator.kt index 60be355..5050636 100644 --- a/app/src/main/java/com/casic/br/operationsite/utils/CommandCreator.kt +++ b/app/src/main/java/com/casic/br/operationsite/utils/CommandCreator.kt @@ -1,5 +1,7 @@ package com.casic.br.operationsite.utils +import android.util.Log + object CommandCreator { /** * 查询激光状态 @@ -217,7 +219,7 @@ /** * 设置指令 - * + * 前提:先设置1/2/3....点 * * 设置巡航线路: * 1.设置92,调用1(1~8巡航线路) @@ -225,27 +227,8 @@ * 3.设置92,再调用9 (保存巡航线路) * */ fun setupConfig(index: Int): ByteArray { - val bytes = byteArrayOf( - 0xFF.toByte(), - 0x01, - 0x00, - 0x03, - 0x00, - 0x00, - 0x00 - ) - - // 将 index 转换为 2 字节的高位和低位 - bytes[4] = (index ushr 8).toByte() // 高位字节 - bytes[5] = (index and 0xFF).toByte() // 低位字节 - - //计算校验位。 - var sum = 0 - for (l in 1 until bytes.size - 1) { - sum += bytes[l].toInt() and 0xFF - } - bytes[6] = sum.toByte() - return bytes + val bytes = byteArrayOf(0xFF.toByte(), 0x01, 0x00, 0x03, 0x00, 0x00, 0x00) + return bytes.checksum(index) } /** @@ -255,54 +238,32 @@ * 1.调用92,调用1(开始跑巡航线路) * */ fun invokeCommand(index: Int): ByteArray { - val bytes = byteArrayOf( - 0xFF.toByte(), - 0x01, - 0x00, - 0x07, - 0x00, - 0x00, - 0x00 - ) - - // 将 index 转换为 2 字节的高位和低位 - bytes[4] = (index ushr 8).toByte() // 高位字节 - bytes[5] = (index and 0xFF).toByte() // 低位字节 - - //校验码SUM = (字节2+字节3+字节4+字节5+字节6)%256 - var sum = 0 - for (l in 1 until bytes.size - 1) { - sum += bytes[l].toInt() and 0xFF - } - bytes[6] = sum.toByte() - return bytes + val bytes = byteArrayOf(0xFF.toByte(), 0x01, 0x00, 0x07, 0x00, 0x00, 0x00) + return bytes.checksum(index) } fun deletePoint(index: Int): ByteArray { - val bytes = byteArrayOf( - 0xFF.toByte(), - 0x01, - 0x00, - 0x05, - 0x00, - 0x00, - 0x00 - ) - - // 将 index 转换为 2 字节的高位和低位 - bytes[4] = (index ushr 8).toByte() // 高位字节 - bytes[5] = (index and 0xFF).toByte() // 低位字节 - - //计算校验位。 - var sum = 0 - for (l in 1 until bytes.size - 1) { - sum += bytes[l].toInt() and 0xFF - } - bytes[6] = sum.toByte() - return bytes + val bytes = byteArrayOf(0xFF.toByte(), 0x01, 0x00, 0x05, 0x00, 0x00, 0x00) + return bytes.checksum(index) } fun stopInspection(): ByteArray { return byteArrayOf(0xFF.toByte(), 0x01, 0x00, 0x00, 0x00, 0x00, 0x01) } + + private fun ByteArray.checksum(value: Int): ByteArray { + // 将 index 转换为 2 字节的高位和低位 + this[4] = (value ushr 8).toByte() // 高位字节 + this[5] = (value and 0xFF).toByte() // 低位字节 + + //计算校验位。 + var sum = 0 + for (l in 1 until this.size - 1) { + sum += this[l].toInt() and 0xFF + } + this[6] = sum.toByte() + val hex = this.joinToString(" ") { "%02X".format(it) } + Log.d("CommandCreator", "指令16进制: $hex") + return this + } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/br/operationsite/utils/LocaleConstant.kt b/app/src/main/java/com/casic/br/operationsite/utils/LocaleConstant.kt index 6b0718e..cb17e58 100644 --- a/app/src/main/java/com/casic/br/operationsite/utils/LocaleConstant.kt +++ b/app/src/main/java/com/casic/br/operationsite/utils/LocaleConstant.kt @@ -115,13 +115,14 @@ const val CONNECT_TCP_CODE = 20251001 const val CONNECT_CAMERA_TCP_CODE = 20251002 - const val START_ADD_POINT_CODE = 20251003 - const val SELECT_LINE_CODE = 20251004 - const val ADD_POINT_CODE = 20251005 - const val SAVE_POINT_CODE = 20251006 - const val DELETE_POINT_CODE = 20251007 - const val START_INSPECTION_CODE = 20251008 - const val STOP_INSPECTION_CODE = 20251009 + const val ADD_POINT_CODE = 20251003 + const val START_ADD_POINT_CONFIG_CODE = 20251004 + const val SELECT_LINE_CODE = 20251005 + const val SETUP_POINT_CODE = 20251006 + const val END_ADD_POINT_CONFIG_CODE = 20251007 + const val DELETE_POINT_CODE = 20251008 + const val START_INSPECTION_CODE = 20251009 + const val STOP_INSPECTION_CODE = 20251010 /*** * SP Key diff --git a/app/src/main/java/com/casic/br/operationsite/widgets/ManagePointDialog.kt b/app/src/main/java/com/casic/br/operationsite/widgets/ManagePointDialog.kt index fc9fa2d..9b92e5e 100644 --- a/app/src/main/java/com/casic/br/operationsite/widgets/ManagePointDialog.kt +++ b/app/src/main/java/com/casic/br/operationsite/widgets/ManagePointDialog.kt @@ -41,20 +41,16 @@ override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - initDialogLayoutParams(0.8f, 0.6f) + initDialogLayoutParams(0.75f, 0.55f) setCanceledOnTouchOutside(false) setCancelable(false) - val pointAdapter = ManagePointAdapter(context, pointBeanDao.firstLinePoints, 8) - binding.recyclerView.adapter = pointAdapter + binding.recyclerView.adapter = ManagePointAdapter(context, pointBeanDao.firstLinePoints, 8) binding.recyclerView.addItemDecoration(RecyclerViewItemDivider(0f, 0f, Color.LTGRAY)) - binding.textView.isSelected = true + binding.textView.isSelected = true - binding.dialogCancelButton.setOnClickListener { - listener.onCancelClick() - dismiss() - } + binding.dialogCancelButton.setOnClickListener { dismiss() } binding.dialogConfirmButton.setOnClickListener { listener.onConfirmClick() @@ -64,7 +60,5 @@ interface OnDialogButtonClickListener { fun onConfirmClick() - - fun onCancelClick() } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/br/operationsite/widgets/ShowPointDialog.kt b/app/src/main/java/com/casic/br/operationsite/widgets/ShowPointDialog.kt deleted file mode 100644 index 65ac096..0000000 --- a/app/src/main/java/com/casic/br/operationsite/widgets/ShowPointDialog.kt +++ /dev/null @@ -1,82 +0,0 @@ -package com.casic.br.operationsite.widgets - -import android.app.Dialog -import android.content.Context -import android.graphics.Color -import android.os.Bundle -import com.casic.br.operationsite.R -import com.casic.br.operationsite.base.BaseApplication -import com.casic.br.operationsite.bean.PointBean -import com.casic.br.operationsite.databinding.DialogShowPointBinding -import com.casic.br.operationsite.extensions.initDialogLayoutParams -import com.pengxh.kt.lite.adapter.NormalRecyclerAdapter -import com.pengxh.kt.lite.adapter.ViewHolder -import com.pengxh.kt.lite.divider.RecyclerViewItemDivider -import com.pengxh.kt.lite.extensions.binding - -class ShowPointDialog(builder: Builder) : Dialog(builder.context) { - - private val kTag = "ShowPointDialog" - private val pointBeanDao by lazy { BaseApplication.get().dataBase.pointBeanDao() } - private var pointBeans: MutableList = ArrayList() - private val listener = builder.listener - - class Builder { - lateinit var context: Context - lateinit var listener: OnDialogButtonClickListener - - fun setContext(context: Context): Builder { - this.context = context - return this - } - - fun setOnDialogButtonClickListener(listener: OnDialogButtonClickListener): Builder { - this.listener = listener - return this - } - - fun build(): ShowPointDialog { - return ShowPointDialog(this) - } - } - - private val binding: DialogShowPointBinding by binding() - - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - initDialogLayoutParams(0.8f, 0.6f) - setCanceledOnTouchOutside(false) - setCancelable(false) - - val beans = pointBeanDao.firstLinePoints - if (beans.isEmpty()) return - - val pointAdapter = object : NormalRecyclerAdapter( - R.layout.item_inspection_point_rv_l, beans - ) { - override fun convertView( - viewHolder: ViewHolder, position: Int, item: PointBean - ) { - viewHolder.setText(R.id.textView, "预置点位 ${item.thirdType}") - } - } - binding.recyclerView.adapter = pointAdapter - binding.recyclerView.addItemDecoration(RecyclerViewItemDivider(0f, 0f, Color.LTGRAY)) - - binding.dialogCancelButton.setOnClickListener { - listener.onCancelClick() - dismiss() - } - - binding.dialogConfirmButton.setOnClickListener { - listener.onConfirmClick() - dismiss() - } - } - - interface OnDialogButtonClickListener { - fun onConfirmClick() - - fun onCancelClick() - } -} diff --git a/app/src/main/res/layout/dialog_manage_point.xml b/app/src/main/res/layout/dialog_manage_point.xml index 1d08af8..6e9fb21 100644 --- a/app/src/main/res/layout/dialog_manage_point.xml +++ b/app/src/main/res/layout/dialog_manage_point.xml @@ -18,7 +18,7 @@ app:tbv_show_left_image="false" app:tbv_show_right_image="false" app:tbv_smaller_title="true" - app:tbv_text="安全树巡航点" + app:tbv_text="安全树巡航点管理" app:tbv_text_color="@color/mainTextColor" /> val message = handler.obtainMessage() diff --git a/app/src/main/java/com/casic/br/operationsite/fragments/DeviceControllerFragment.kt b/app/src/main/java/com/casic/br/operationsite/fragments/DeviceControllerFragment.kt index 005e44a..862a00c 100644 --- a/app/src/main/java/com/casic/br/operationsite/fragments/DeviceControllerFragment.kt +++ b/app/src/main/java/com/casic/br/operationsite/fragments/DeviceControllerFragment.kt @@ -14,10 +14,10 @@ import com.casic.br.operationsite.view.DeviceControlActivity import com.casic.br.operationsite.vm.DeviceViewModel import com.casic.br.operationsite.widgets.ManagePointDialog -import com.casic.br.operationsite.widgets.ShowPointDialog import com.pengxh.kt.lite.base.KotlinBaseFragment import com.pengxh.kt.lite.extensions.show import com.pengxh.kt.lite.widget.SteeringWheelView +import com.pengxh.kt.lite.widget.dialog.AlertControlDialog import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay import kotlinx.coroutines.launch @@ -100,55 +100,71 @@ binding.currentSpeedView.text = "速度:${speed}" } - binding.managePointButton.setOnClickListener { + binding.startInspectionButton.setOnClickListener { + val beans = pointBeanDao.firstLinePoints + //至少得2个点才能巡航 + if (beans.size < 2) { + "请至少添加两个点".show(requireContext()) + return@setOnClickListener + } lifecycleScope.launch(Dispatchers.IO) { CameraInspectionService.weakReferenceHandler?.let { - it.sendEmptyMessage(LocaleConstant.START_ADD_POINT_CODE) - delay(300) + it.sendEmptyMessage(LocaleConstant.START_INSPECTION_CODE) + delay(500) it.sendEmptyMessage(LocaleConstant.SELECT_LINE_CODE) } } + } - //弹框添加预置点 + binding.managePointButton.setOnClickListener { ManagePointDialog.Builder() .setContext(requireContext()) .setOnDialogButtonClickListener(object : ManagePointDialog.OnDialogButtonClickListener { - override fun onCancelClick() { - - } - override fun onConfirmClick() { - CameraInspectionService.weakReferenceHandler?.sendEmptyMessage( - LocaleConstant.SAVE_POINT_CODE - ) - } - }).build().show() - } - - binding.queryPointButton.setOnClickListener { - val beans = pointBeanDao.firstLinePoints - if (beans.isEmpty()) { - "请先添加巡航点".show(requireContext()) - return@setOnClickListener - } - - ShowPointDialog.Builder() - .setContext(requireContext()) - .setOnDialogButtonClickListener(object : - ShowPointDialog.OnDialogButtonClickListener { - override fun onCancelClick() { - - } - - override fun onConfirmClick() { - //至少得2个点才能巡航 + val beans = pointBeanDao.firstLinePoints if (beans.size < 2) { "请至少添加两个点".show(requireContext()) return } + + lifecycleScope.launch(Dispatchers.IO) { + CameraInspectionService.weakReferenceHandler?.let { + it.sendEmptyMessage(LocaleConstant.START_ADD_POINT_CONFIG_CODE) + delay(500) + it.sendEmptyMessage(LocaleConstant.SELECT_LINE_CODE) + //循环设置巡航点 + for (i in 1..beans.size) { + val message = it.obtainMessage() + message.what = LocaleConstant.SETUP_POINT_CODE + message.obj = i + it.sendMessageDelayed(message, 500) + } + it.sendEmptyMessage(LocaleConstant.START_ADD_POINT_CONFIG_CODE) + delay(500) + it.sendEmptyMessage(LocaleConstant.END_ADD_POINT_CONFIG_CODE) + } + } + } + }).build().show() + } + + binding.stopInspectionButton.setOnClickListener { + AlertControlDialog.Builder() + .setContext(requireContext()) + .setTitle("提示") + .setMessage("是否停止巡航?") + .setNegativeButton("取消") + .setPositiveButton("确定") + .setOnDialogButtonClickListener(object : + AlertControlDialog.OnDialogButtonClickListener { + override fun onCancelClick() { + + } + + override fun onConfirmClick() { CameraInspectionService.weakReferenceHandler?.sendEmptyMessage( - LocaleConstant.START_INSPECTION_CODE + LocaleConstant.STOP_INSPECTION_CODE ) } }).build().show() diff --git a/app/src/main/java/com/casic/br/operationsite/service/CameraInspectionService.kt b/app/src/main/java/com/casic/br/operationsite/service/CameraInspectionService.kt index 3b8dea5..c2cfbb4 100644 --- a/app/src/main/java/com/casic/br/operationsite/service/CameraInspectionService.kt +++ b/app/src/main/java/com/casic/br/operationsite/service/CameraInspectionService.kt @@ -16,11 +16,6 @@ import com.casic.br.operationsite.utils.TcpClient import com.pengxh.kt.lite.utils.SaveKeyValues import com.pengxh.kt.lite.utils.WeakReferenceHandler -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.SupervisorJob -import kotlinx.coroutines.delay -import kotlinx.coroutines.launch class CameraInspectionService : Service(), OnTcpConnectStateListener, Handler.Callback { @@ -32,7 +27,6 @@ private val notificationId = 2 private val tcpClient by lazy { TcpClient(this) } private val notificationManager by lazy { getSystemService(NOTIFICATION_SERVICE) as NotificationManager } - private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob()) private var notificationBuilder: NotificationCompat.Builder? = null override fun handleMessage(msg: Message): Boolean { @@ -41,26 +35,26 @@ tcpClient.start() } - LocaleConstant.START_ADD_POINT_CODE -> { - tcpClient.sendMessage(CommandCreator.invokeCommand(92)) - } - - LocaleConstant.ADD_LINE_CODE -> { - val line = msg.obj as Int - tcpClient.sendMessage(CommandCreator.invokeCommand(line)) - } - LocaleConstant.ADD_POINT_CODE -> { val point = msg.obj as Int - tcpClient.sendMessage(CommandCreator.setConfig(point)) + tcpClient.sendMessage(CommandCreator.setupConfig(point)) } - LocaleConstant.SAVE_POINT_CODE -> { - scope.launch(Dispatchers.IO) { - tcpClient.sendMessage(CommandCreator.invokeCommand(92)) - delay(500) - tcpClient.sendMessage(CommandCreator.invokeCommand(9)) - } + LocaleConstant.START_ADD_POINT_CONFIG_CODE -> { + tcpClient.sendMessage(CommandCreator.setupConfig(92)) + } + + LocaleConstant.SELECT_LINE_CODE -> { + tcpClient.sendMessage(CommandCreator.invokeCommand(1)) + } + + LocaleConstant.SETUP_POINT_CODE -> { + val point = msg.obj as Int + tcpClient.sendMessage(CommandCreator.invokeCommand(point)) + } + + LocaleConstant.END_ADD_POINT_CONFIG_CODE -> { + tcpClient.sendMessage(CommandCreator.invokeCommand(9)) } LocaleConstant.DELETE_POINT_CODE -> { @@ -69,12 +63,11 @@ } LocaleConstant.START_INSPECTION_CODE -> { - val line = msg.obj as Int - scope.launch(Dispatchers.IO) { - tcpClient.sendMessage(CommandCreator.invokeCommand(92)) - delay(500) - tcpClient.sendMessage(CommandCreator.invokeCommand(line)) - } + tcpClient.sendMessage(CommandCreator.invokeCommand(92)) + } + + LocaleConstant.STOP_INSPECTION_CODE -> { + tcpClient.sendMessage(CommandCreator.stopInspection()) } } return true diff --git a/app/src/main/java/com/casic/br/operationsite/utils/CommandCreator.kt b/app/src/main/java/com/casic/br/operationsite/utils/CommandCreator.kt index 60be355..5050636 100644 --- a/app/src/main/java/com/casic/br/operationsite/utils/CommandCreator.kt +++ b/app/src/main/java/com/casic/br/operationsite/utils/CommandCreator.kt @@ -1,5 +1,7 @@ package com.casic.br.operationsite.utils +import android.util.Log + object CommandCreator { /** * 查询激光状态 @@ -217,7 +219,7 @@ /** * 设置指令 - * + * 前提:先设置1/2/3....点 * * 设置巡航线路: * 1.设置92,调用1(1~8巡航线路) @@ -225,27 +227,8 @@ * 3.设置92,再调用9 (保存巡航线路) * */ fun setupConfig(index: Int): ByteArray { - val bytes = byteArrayOf( - 0xFF.toByte(), - 0x01, - 0x00, - 0x03, - 0x00, - 0x00, - 0x00 - ) - - // 将 index 转换为 2 字节的高位和低位 - bytes[4] = (index ushr 8).toByte() // 高位字节 - bytes[5] = (index and 0xFF).toByte() // 低位字节 - - //计算校验位。 - var sum = 0 - for (l in 1 until bytes.size - 1) { - sum += bytes[l].toInt() and 0xFF - } - bytes[6] = sum.toByte() - return bytes + val bytes = byteArrayOf(0xFF.toByte(), 0x01, 0x00, 0x03, 0x00, 0x00, 0x00) + return bytes.checksum(index) } /** @@ -255,54 +238,32 @@ * 1.调用92,调用1(开始跑巡航线路) * */ fun invokeCommand(index: Int): ByteArray { - val bytes = byteArrayOf( - 0xFF.toByte(), - 0x01, - 0x00, - 0x07, - 0x00, - 0x00, - 0x00 - ) - - // 将 index 转换为 2 字节的高位和低位 - bytes[4] = (index ushr 8).toByte() // 高位字节 - bytes[5] = (index and 0xFF).toByte() // 低位字节 - - //校验码SUM = (字节2+字节3+字节4+字节5+字节6)%256 - var sum = 0 - for (l in 1 until bytes.size - 1) { - sum += bytes[l].toInt() and 0xFF - } - bytes[6] = sum.toByte() - return bytes + val bytes = byteArrayOf(0xFF.toByte(), 0x01, 0x00, 0x07, 0x00, 0x00, 0x00) + return bytes.checksum(index) } fun deletePoint(index: Int): ByteArray { - val bytes = byteArrayOf( - 0xFF.toByte(), - 0x01, - 0x00, - 0x05, - 0x00, - 0x00, - 0x00 - ) - - // 将 index 转换为 2 字节的高位和低位 - bytes[4] = (index ushr 8).toByte() // 高位字节 - bytes[5] = (index and 0xFF).toByte() // 低位字节 - - //计算校验位。 - var sum = 0 - for (l in 1 until bytes.size - 1) { - sum += bytes[l].toInt() and 0xFF - } - bytes[6] = sum.toByte() - return bytes + val bytes = byteArrayOf(0xFF.toByte(), 0x01, 0x00, 0x05, 0x00, 0x00, 0x00) + return bytes.checksum(index) } fun stopInspection(): ByteArray { return byteArrayOf(0xFF.toByte(), 0x01, 0x00, 0x00, 0x00, 0x00, 0x01) } + + private fun ByteArray.checksum(value: Int): ByteArray { + // 将 index 转换为 2 字节的高位和低位 + this[4] = (value ushr 8).toByte() // 高位字节 + this[5] = (value and 0xFF).toByte() // 低位字节 + + //计算校验位。 + var sum = 0 + for (l in 1 until this.size - 1) { + sum += this[l].toInt() and 0xFF + } + this[6] = sum.toByte() + val hex = this.joinToString(" ") { "%02X".format(it) } + Log.d("CommandCreator", "指令16进制: $hex") + return this + } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/br/operationsite/utils/LocaleConstant.kt b/app/src/main/java/com/casic/br/operationsite/utils/LocaleConstant.kt index 6b0718e..cb17e58 100644 --- a/app/src/main/java/com/casic/br/operationsite/utils/LocaleConstant.kt +++ b/app/src/main/java/com/casic/br/operationsite/utils/LocaleConstant.kt @@ -115,13 +115,14 @@ const val CONNECT_TCP_CODE = 20251001 const val CONNECT_CAMERA_TCP_CODE = 20251002 - const val START_ADD_POINT_CODE = 20251003 - const val SELECT_LINE_CODE = 20251004 - const val ADD_POINT_CODE = 20251005 - const val SAVE_POINT_CODE = 20251006 - const val DELETE_POINT_CODE = 20251007 - const val START_INSPECTION_CODE = 20251008 - const val STOP_INSPECTION_CODE = 20251009 + const val ADD_POINT_CODE = 20251003 + const val START_ADD_POINT_CONFIG_CODE = 20251004 + const val SELECT_LINE_CODE = 20251005 + const val SETUP_POINT_CODE = 20251006 + const val END_ADD_POINT_CONFIG_CODE = 20251007 + const val DELETE_POINT_CODE = 20251008 + const val START_INSPECTION_CODE = 20251009 + const val STOP_INSPECTION_CODE = 20251010 /*** * SP Key diff --git a/app/src/main/java/com/casic/br/operationsite/widgets/ManagePointDialog.kt b/app/src/main/java/com/casic/br/operationsite/widgets/ManagePointDialog.kt index fc9fa2d..9b92e5e 100644 --- a/app/src/main/java/com/casic/br/operationsite/widgets/ManagePointDialog.kt +++ b/app/src/main/java/com/casic/br/operationsite/widgets/ManagePointDialog.kt @@ -41,20 +41,16 @@ override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - initDialogLayoutParams(0.8f, 0.6f) + initDialogLayoutParams(0.75f, 0.55f) setCanceledOnTouchOutside(false) setCancelable(false) - val pointAdapter = ManagePointAdapter(context, pointBeanDao.firstLinePoints, 8) - binding.recyclerView.adapter = pointAdapter + binding.recyclerView.adapter = ManagePointAdapter(context, pointBeanDao.firstLinePoints, 8) binding.recyclerView.addItemDecoration(RecyclerViewItemDivider(0f, 0f, Color.LTGRAY)) - binding.textView.isSelected = true + binding.textView.isSelected = true - binding.dialogCancelButton.setOnClickListener { - listener.onCancelClick() - dismiss() - } + binding.dialogCancelButton.setOnClickListener { dismiss() } binding.dialogConfirmButton.setOnClickListener { listener.onConfirmClick() @@ -64,7 +60,5 @@ interface OnDialogButtonClickListener { fun onConfirmClick() - - fun onCancelClick() } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/br/operationsite/widgets/ShowPointDialog.kt b/app/src/main/java/com/casic/br/operationsite/widgets/ShowPointDialog.kt deleted file mode 100644 index 65ac096..0000000 --- a/app/src/main/java/com/casic/br/operationsite/widgets/ShowPointDialog.kt +++ /dev/null @@ -1,82 +0,0 @@ -package com.casic.br.operationsite.widgets - -import android.app.Dialog -import android.content.Context -import android.graphics.Color -import android.os.Bundle -import com.casic.br.operationsite.R -import com.casic.br.operationsite.base.BaseApplication -import com.casic.br.operationsite.bean.PointBean -import com.casic.br.operationsite.databinding.DialogShowPointBinding -import com.casic.br.operationsite.extensions.initDialogLayoutParams -import com.pengxh.kt.lite.adapter.NormalRecyclerAdapter -import com.pengxh.kt.lite.adapter.ViewHolder -import com.pengxh.kt.lite.divider.RecyclerViewItemDivider -import com.pengxh.kt.lite.extensions.binding - -class ShowPointDialog(builder: Builder) : Dialog(builder.context) { - - private val kTag = "ShowPointDialog" - private val pointBeanDao by lazy { BaseApplication.get().dataBase.pointBeanDao() } - private var pointBeans: MutableList = ArrayList() - private val listener = builder.listener - - class Builder { - lateinit var context: Context - lateinit var listener: OnDialogButtonClickListener - - fun setContext(context: Context): Builder { - this.context = context - return this - } - - fun setOnDialogButtonClickListener(listener: OnDialogButtonClickListener): Builder { - this.listener = listener - return this - } - - fun build(): ShowPointDialog { - return ShowPointDialog(this) - } - } - - private val binding: DialogShowPointBinding by binding() - - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - initDialogLayoutParams(0.8f, 0.6f) - setCanceledOnTouchOutside(false) - setCancelable(false) - - val beans = pointBeanDao.firstLinePoints - if (beans.isEmpty()) return - - val pointAdapter = object : NormalRecyclerAdapter( - R.layout.item_inspection_point_rv_l, beans - ) { - override fun convertView( - viewHolder: ViewHolder, position: Int, item: PointBean - ) { - viewHolder.setText(R.id.textView, "预置点位 ${item.thirdType}") - } - } - binding.recyclerView.adapter = pointAdapter - binding.recyclerView.addItemDecoration(RecyclerViewItemDivider(0f, 0f, Color.LTGRAY)) - - binding.dialogCancelButton.setOnClickListener { - listener.onCancelClick() - dismiss() - } - - binding.dialogConfirmButton.setOnClickListener { - listener.onConfirmClick() - dismiss() - } - } - - interface OnDialogButtonClickListener { - fun onConfirmClick() - - fun onCancelClick() - } -} diff --git a/app/src/main/res/layout/dialog_manage_point.xml b/app/src/main/res/layout/dialog_manage_point.xml index 1d08af8..6e9fb21 100644 --- a/app/src/main/res/layout/dialog_manage_point.xml +++ b/app/src/main/res/layout/dialog_manage_point.xml @@ -18,7 +18,7 @@ app:tbv_show_left_image="false" app:tbv_show_right_image="false" app:tbv_smaller_title="true" - app:tbv_text="安全树巡航点" + app:tbv_text="安全树巡航点管理" app:tbv_text_color="@color/mainTextColor" /> - - - - - - - - - - - - - - -