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 1411b99..71d5a77 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 @@ -20,6 +20,7 @@ private val countLimit: Int ) : RecyclerView.Adapter() { + private val kTag = "ManagePointAdapter" private val layoutInflater by lazy { LayoutInflater.from(context) } private val pointBeanDao by lazy { BaseApplication.get().dataBase.pointBeanDao() } @@ -45,27 +46,53 @@ } pointBeanDao.insert(pointBean) points.add(pointBean) + notifyDataSetChanged() + CameraInspectionService.weakReferenceHandler?.let { handler -> + val message = handler.obtainMessage() + message.what = LocaleConstant.ADD_POINT_CODE + message.obj = pointBean.thirdType + handler.sendMessage(message) + } } else { find.thirdType = 1 pointBeanDao.updatePoint(find) + notifyDataSetChanged() + CameraInspectionService.weakReferenceHandler?.let { handler -> + val message = handler.obtainMessage() + message.what = LocaleConstant.ADD_POINT_CODE + message.obj = 1 + handler.sendMessage(message) + } } - notifyDataSetChanged() - CameraInspectionService.weakReferenceHandler?.sendEmptyMessage( - LocaleConstant.ADD_POINT_CODE - ) } } else { - textView.text = "预置点位 ${points[position].thirdType}" + val currentThirdType = points[position].thirdType + textView.text = "预置点位 $currentThirdType" textView.setTextColor(R.color.mainTextColor.convertColor(context)) - // 长按监听 textView.setOnLongClickListener { v -> - //长按删除巡航点 - pointBeanDao.deletePointById(points[position].id) - points.remove(points[position]) - notifyDataSetChanged() - CameraInspectionService.weakReferenceHandler?.sendEmptyMessage( - LocaleConstant.DELETE_POINT_CODE - ) + //长按删除巡航点。删除所有线路中相同的点位 + val itemsToDelete = pointBeanDao.queryPoint(currentThirdType) + if (itemsToDelete.isNotEmpty()) { + itemsToDelete.forEach { + pointBeanDao.deletePoint(it) + } + } + + val items = points.filter { item -> item.thirdType == currentThirdType } + if (items.isNotEmpty()) { + items.forEach { + points.remove(it) + } + + notifyDataSetChanged() + + CameraInspectionService.weakReferenceHandler?.let { handler -> + val message = handler.obtainMessage() + message.what = LocaleConstant.DELETE_POINT_CODE + message.obj = currentThirdType + handler.sendMessage(message) + } + } 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 1411b99..71d5a77 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 @@ -20,6 +20,7 @@ private val countLimit: Int ) : RecyclerView.Adapter() { + private val kTag = "ManagePointAdapter" private val layoutInflater by lazy { LayoutInflater.from(context) } private val pointBeanDao by lazy { BaseApplication.get().dataBase.pointBeanDao() } @@ -45,27 +46,53 @@ } pointBeanDao.insert(pointBean) points.add(pointBean) + notifyDataSetChanged() + CameraInspectionService.weakReferenceHandler?.let { handler -> + val message = handler.obtainMessage() + message.what = LocaleConstant.ADD_POINT_CODE + message.obj = pointBean.thirdType + handler.sendMessage(message) + } } else { find.thirdType = 1 pointBeanDao.updatePoint(find) + notifyDataSetChanged() + CameraInspectionService.weakReferenceHandler?.let { handler -> + val message = handler.obtainMessage() + message.what = LocaleConstant.ADD_POINT_CODE + message.obj = 1 + handler.sendMessage(message) + } } - notifyDataSetChanged() - CameraInspectionService.weakReferenceHandler?.sendEmptyMessage( - LocaleConstant.ADD_POINT_CODE - ) } } else { - textView.text = "预置点位 ${points[position].thirdType}" + val currentThirdType = points[position].thirdType + textView.text = "预置点位 $currentThirdType" textView.setTextColor(R.color.mainTextColor.convertColor(context)) - // 长按监听 textView.setOnLongClickListener { v -> - //长按删除巡航点 - pointBeanDao.deletePointById(points[position].id) - points.remove(points[position]) - notifyDataSetChanged() - CameraInspectionService.weakReferenceHandler?.sendEmptyMessage( - LocaleConstant.DELETE_POINT_CODE - ) + //长按删除巡航点。删除所有线路中相同的点位 + val itemsToDelete = pointBeanDao.queryPoint(currentThirdType) + if (itemsToDelete.isNotEmpty()) { + itemsToDelete.forEach { + pointBeanDao.deletePoint(it) + } + } + + val items = points.filter { item -> item.thirdType == currentThirdType } + if (items.isNotEmpty()) { + items.forEach { + points.remove(it) + } + + notifyDataSetChanged() + + CameraInspectionService.weakReferenceHandler?.let { handler -> + val message = handler.obtainMessage() + message.what = LocaleConstant.DELETE_POINT_CODE + message.obj = currentThirdType + handler.sendMessage(message) + } + } true } } diff --git a/app/src/main/java/com/casic/br/operationsite/dao/PointBeanDao.java b/app/src/main/java/com/casic/br/operationsite/dao/PointBeanDao.java index af8dace..d1c5623 100644 --- a/app/src/main/java/com/casic/br/operationsite/dao/PointBeanDao.java +++ b/app/src/main/java/com/casic/br/operationsite/dao/PointBeanDao.java @@ -1,6 +1,7 @@ package com.casic.br.operationsite.dao; import androidx.room.Dao; +import androidx.room.Delete; import androidx.room.Insert; import androidx.room.Query; import androidx.room.Update; @@ -23,6 +24,12 @@ @Update void updatePoint(PointBean point); + @Query("SELECT * FROM camera_inspection_point WHERE thirdType = :point") + List queryPoint(int point); + + @Delete + void deletePoint(PointBean point); + @Query("DELETE FROM camera_inspection_point WHERE id = :id") void deletePointById(long id); } 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 1411b99..71d5a77 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 @@ -20,6 +20,7 @@ private val countLimit: Int ) : RecyclerView.Adapter() { + private val kTag = "ManagePointAdapter" private val layoutInflater by lazy { LayoutInflater.from(context) } private val pointBeanDao by lazy { BaseApplication.get().dataBase.pointBeanDao() } @@ -45,27 +46,53 @@ } pointBeanDao.insert(pointBean) points.add(pointBean) + notifyDataSetChanged() + CameraInspectionService.weakReferenceHandler?.let { handler -> + val message = handler.obtainMessage() + message.what = LocaleConstant.ADD_POINT_CODE + message.obj = pointBean.thirdType + handler.sendMessage(message) + } } else { find.thirdType = 1 pointBeanDao.updatePoint(find) + notifyDataSetChanged() + CameraInspectionService.weakReferenceHandler?.let { handler -> + val message = handler.obtainMessage() + message.what = LocaleConstant.ADD_POINT_CODE + message.obj = 1 + handler.sendMessage(message) + } } - notifyDataSetChanged() - CameraInspectionService.weakReferenceHandler?.sendEmptyMessage( - LocaleConstant.ADD_POINT_CODE - ) } } else { - textView.text = "预置点位 ${points[position].thirdType}" + val currentThirdType = points[position].thirdType + textView.text = "预置点位 $currentThirdType" textView.setTextColor(R.color.mainTextColor.convertColor(context)) - // 长按监听 textView.setOnLongClickListener { v -> - //长按删除巡航点 - pointBeanDao.deletePointById(points[position].id) - points.remove(points[position]) - notifyDataSetChanged() - CameraInspectionService.weakReferenceHandler?.sendEmptyMessage( - LocaleConstant.DELETE_POINT_CODE - ) + //长按删除巡航点。删除所有线路中相同的点位 + val itemsToDelete = pointBeanDao.queryPoint(currentThirdType) + if (itemsToDelete.isNotEmpty()) { + itemsToDelete.forEach { + pointBeanDao.deletePoint(it) + } + } + + val items = points.filter { item -> item.thirdType == currentThirdType } + if (items.isNotEmpty()) { + items.forEach { + points.remove(it) + } + + notifyDataSetChanged() + + CameraInspectionService.weakReferenceHandler?.let { handler -> + val message = handler.obtainMessage() + message.what = LocaleConstant.DELETE_POINT_CODE + message.obj = currentThirdType + handler.sendMessage(message) + } + } true } } diff --git a/app/src/main/java/com/casic/br/operationsite/dao/PointBeanDao.java b/app/src/main/java/com/casic/br/operationsite/dao/PointBeanDao.java index af8dace..d1c5623 100644 --- a/app/src/main/java/com/casic/br/operationsite/dao/PointBeanDao.java +++ b/app/src/main/java/com/casic/br/operationsite/dao/PointBeanDao.java @@ -1,6 +1,7 @@ package com.casic.br.operationsite.dao; import androidx.room.Dao; +import androidx.room.Delete; import androidx.room.Insert; import androidx.room.Query; import androidx.room.Update; @@ -23,6 +24,12 @@ @Update void updatePoint(PointBean point); + @Query("SELECT * FROM camera_inspection_point WHERE thirdType = :point") + List queryPoint(int point); + + @Delete + void deletePoint(PointBean point); + @Query("DELETE FROM camera_inspection_point WHERE id = :id") void deletePointById(long id); } 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 299e0c4..3b8dea5 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 @@ -10,7 +10,6 @@ import android.util.Log import androidx.core.app.NotificationCompat import com.casic.br.operationsite.R -import com.casic.br.operationsite.base.BaseApplication import com.casic.br.operationsite.utils.CommandCreator import com.casic.br.operationsite.utils.LocaleConstant import com.casic.br.operationsite.utils.OnTcpConnectStateListener @@ -32,7 +31,6 @@ private val kTag = "InspectionService" private val notificationId = 2 private val tcpClient by lazy { TcpClient(this) } - private val pointBeanDao by lazy { BaseApplication.get().dataBase.pointBeanDao() } private val notificationManager by lazy { getSystemService(NOTIFICATION_SERVICE) as NotificationManager } private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob()) private var notificationBuilder: NotificationCompat.Builder? = null @@ -44,24 +42,24 @@ } LocaleConstant.START_ADD_POINT_CODE -> { - tcpClient.sendMessage(CommandCreator.setConfig(92)) + tcpClient.sendMessage(CommandCreator.invokeCommand(92)) } LocaleConstant.ADD_LINE_CODE -> { val line = msg.obj as Int - tcpClient.sendMessage(CommandCreator.addPoint(line)) + tcpClient.sendMessage(CommandCreator.invokeCommand(line)) } LocaleConstant.ADD_POINT_CODE -> { val point = msg.obj as Int - tcpClient.sendMessage(CommandCreator.addPoint(point)) + tcpClient.sendMessage(CommandCreator.setConfig(point)) } LocaleConstant.SAVE_POINT_CODE -> { scope.launch(Dispatchers.IO) { - tcpClient.sendMessage(CommandCreator.setConfig(92)) + tcpClient.sendMessage(CommandCreator.invokeCommand(92)) delay(500) - tcpClient.sendMessage(CommandCreator.setConfig(9)) + tcpClient.sendMessage(CommandCreator.invokeCommand(9)) } } @@ -73,9 +71,9 @@ LocaleConstant.START_INSPECTION_CODE -> { val line = msg.obj as Int scope.launch(Dispatchers.IO) { - tcpClient.sendMessage(CommandCreator.setConfig(92)) + tcpClient.sendMessage(CommandCreator.invokeCommand(92)) delay(500) - tcpClient.sendMessage(CommandCreator.addPoint(line)) + tcpClient.sendMessage(CommandCreator.invokeCommand(line)) } } } 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 1411b99..71d5a77 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 @@ -20,6 +20,7 @@ private val countLimit: Int ) : RecyclerView.Adapter() { + private val kTag = "ManagePointAdapter" private val layoutInflater by lazy { LayoutInflater.from(context) } private val pointBeanDao by lazy { BaseApplication.get().dataBase.pointBeanDao() } @@ -45,27 +46,53 @@ } pointBeanDao.insert(pointBean) points.add(pointBean) + notifyDataSetChanged() + CameraInspectionService.weakReferenceHandler?.let { handler -> + val message = handler.obtainMessage() + message.what = LocaleConstant.ADD_POINT_CODE + message.obj = pointBean.thirdType + handler.sendMessage(message) + } } else { find.thirdType = 1 pointBeanDao.updatePoint(find) + notifyDataSetChanged() + CameraInspectionService.weakReferenceHandler?.let { handler -> + val message = handler.obtainMessage() + message.what = LocaleConstant.ADD_POINT_CODE + message.obj = 1 + handler.sendMessage(message) + } } - notifyDataSetChanged() - CameraInspectionService.weakReferenceHandler?.sendEmptyMessage( - LocaleConstant.ADD_POINT_CODE - ) } } else { - textView.text = "预置点位 ${points[position].thirdType}" + val currentThirdType = points[position].thirdType + textView.text = "预置点位 $currentThirdType" textView.setTextColor(R.color.mainTextColor.convertColor(context)) - // 长按监听 textView.setOnLongClickListener { v -> - //长按删除巡航点 - pointBeanDao.deletePointById(points[position].id) - points.remove(points[position]) - notifyDataSetChanged() - CameraInspectionService.weakReferenceHandler?.sendEmptyMessage( - LocaleConstant.DELETE_POINT_CODE - ) + //长按删除巡航点。删除所有线路中相同的点位 + val itemsToDelete = pointBeanDao.queryPoint(currentThirdType) + if (itemsToDelete.isNotEmpty()) { + itemsToDelete.forEach { + pointBeanDao.deletePoint(it) + } + } + + val items = points.filter { item -> item.thirdType == currentThirdType } + if (items.isNotEmpty()) { + items.forEach { + points.remove(it) + } + + notifyDataSetChanged() + + CameraInspectionService.weakReferenceHandler?.let { handler -> + val message = handler.obtainMessage() + message.what = LocaleConstant.DELETE_POINT_CODE + message.obj = currentThirdType + handler.sendMessage(message) + } + } true } } diff --git a/app/src/main/java/com/casic/br/operationsite/dao/PointBeanDao.java b/app/src/main/java/com/casic/br/operationsite/dao/PointBeanDao.java index af8dace..d1c5623 100644 --- a/app/src/main/java/com/casic/br/operationsite/dao/PointBeanDao.java +++ b/app/src/main/java/com/casic/br/operationsite/dao/PointBeanDao.java @@ -1,6 +1,7 @@ package com.casic.br.operationsite.dao; import androidx.room.Dao; +import androidx.room.Delete; import androidx.room.Insert; import androidx.room.Query; import androidx.room.Update; @@ -23,6 +24,12 @@ @Update void updatePoint(PointBean point); + @Query("SELECT * FROM camera_inspection_point WHERE thirdType = :point") + List queryPoint(int point); + + @Delete + void deletePoint(PointBean point); + @Query("DELETE FROM camera_inspection_point WHERE id = :id") void deletePointById(long id); } 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 299e0c4..3b8dea5 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 @@ -10,7 +10,6 @@ import android.util.Log import androidx.core.app.NotificationCompat import com.casic.br.operationsite.R -import com.casic.br.operationsite.base.BaseApplication import com.casic.br.operationsite.utils.CommandCreator import com.casic.br.operationsite.utils.LocaleConstant import com.casic.br.operationsite.utils.OnTcpConnectStateListener @@ -32,7 +31,6 @@ private val kTag = "InspectionService" private val notificationId = 2 private val tcpClient by lazy { TcpClient(this) } - private val pointBeanDao by lazy { BaseApplication.get().dataBase.pointBeanDao() } private val notificationManager by lazy { getSystemService(NOTIFICATION_SERVICE) as NotificationManager } private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob()) private var notificationBuilder: NotificationCompat.Builder? = null @@ -44,24 +42,24 @@ } LocaleConstant.START_ADD_POINT_CODE -> { - tcpClient.sendMessage(CommandCreator.setConfig(92)) + tcpClient.sendMessage(CommandCreator.invokeCommand(92)) } LocaleConstant.ADD_LINE_CODE -> { val line = msg.obj as Int - tcpClient.sendMessage(CommandCreator.addPoint(line)) + tcpClient.sendMessage(CommandCreator.invokeCommand(line)) } LocaleConstant.ADD_POINT_CODE -> { val point = msg.obj as Int - tcpClient.sendMessage(CommandCreator.addPoint(point)) + tcpClient.sendMessage(CommandCreator.setConfig(point)) } LocaleConstant.SAVE_POINT_CODE -> { scope.launch(Dispatchers.IO) { - tcpClient.sendMessage(CommandCreator.setConfig(92)) + tcpClient.sendMessage(CommandCreator.invokeCommand(92)) delay(500) - tcpClient.sendMessage(CommandCreator.setConfig(9)) + tcpClient.sendMessage(CommandCreator.invokeCommand(9)) } } @@ -73,9 +71,9 @@ LocaleConstant.START_INSPECTION_CODE -> { val line = msg.obj as Int scope.launch(Dispatchers.IO) { - tcpClient.sendMessage(CommandCreator.setConfig(92)) + tcpClient.sendMessage(CommandCreator.invokeCommand(92)) delay(500) - tcpClient.sendMessage(CommandCreator.addPoint(line)) + tcpClient.sendMessage(CommandCreator.invokeCommand(line)) } } } 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 f72a1ab..b939e7a 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 @@ -216,7 +216,34 @@ } /** - * 添加预置点,范围:1~255 + * 调用指令 + * */ + fun invokeCommand(index: Int): ByteArray { + val bytes = byteArrayOf( + 0xFF.toByte(), + 0x01, + 0x00, + 0x07.toByte(), + 0x00.toByte(), + 0x00.toByte(), + 0x00.toByte() + ) + + // 将 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 + } + + /** + * 设置指令 * * * 整个流程就是: @@ -243,31 +270,7 @@ //计算校验位。 var sum = 0 for (l in 1 until bytes.size - 1) { - sum += bytes[l] - } - bytes[6] = sum.toByte() - return bytes - } - - fun addPoint(index: Int): ByteArray { - val bytes = byteArrayOf( - 0xFF.toByte(), - 0x01, - 0x00, - 0x03.toByte(), - 0x00.toByte(), - 0x00.toByte(), - 0x00.toByte() - ) - - // 将 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] + sum += bytes[l].toInt() and 0xFF } bytes[6] = sum.toByte() return bytes @@ -291,31 +294,27 @@ //计算校验位。 var sum = 0 for (l in 1 until bytes.size - 1) { - sum += bytes[l] + sum += bytes[l].toInt() and 0xFF } bytes[6] = sum.toByte() return bytes } - fun startLine(index: Int): ByteArray { + fun stopLine(): ByteArray { val bytes = byteArrayOf( 0xFF.toByte(), 0x01, 0x00, - 0x05.toByte(), + 0x00.toByte(), 0x00.toByte(), 0x00.toByte(), 0x00.toByte() ) - // 将 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] + sum += bytes[l].toInt() and 0xFF } bytes[6] = sum.toByte() return bytes 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 1411b99..71d5a77 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 @@ -20,6 +20,7 @@ private val countLimit: Int ) : RecyclerView.Adapter() { + private val kTag = "ManagePointAdapter" private val layoutInflater by lazy { LayoutInflater.from(context) } private val pointBeanDao by lazy { BaseApplication.get().dataBase.pointBeanDao() } @@ -45,27 +46,53 @@ } pointBeanDao.insert(pointBean) points.add(pointBean) + notifyDataSetChanged() + CameraInspectionService.weakReferenceHandler?.let { handler -> + val message = handler.obtainMessage() + message.what = LocaleConstant.ADD_POINT_CODE + message.obj = pointBean.thirdType + handler.sendMessage(message) + } } else { find.thirdType = 1 pointBeanDao.updatePoint(find) + notifyDataSetChanged() + CameraInspectionService.weakReferenceHandler?.let { handler -> + val message = handler.obtainMessage() + message.what = LocaleConstant.ADD_POINT_CODE + message.obj = 1 + handler.sendMessage(message) + } } - notifyDataSetChanged() - CameraInspectionService.weakReferenceHandler?.sendEmptyMessage( - LocaleConstant.ADD_POINT_CODE - ) } } else { - textView.text = "预置点位 ${points[position].thirdType}" + val currentThirdType = points[position].thirdType + textView.text = "预置点位 $currentThirdType" textView.setTextColor(R.color.mainTextColor.convertColor(context)) - // 长按监听 textView.setOnLongClickListener { v -> - //长按删除巡航点 - pointBeanDao.deletePointById(points[position].id) - points.remove(points[position]) - notifyDataSetChanged() - CameraInspectionService.weakReferenceHandler?.sendEmptyMessage( - LocaleConstant.DELETE_POINT_CODE - ) + //长按删除巡航点。删除所有线路中相同的点位 + val itemsToDelete = pointBeanDao.queryPoint(currentThirdType) + if (itemsToDelete.isNotEmpty()) { + itemsToDelete.forEach { + pointBeanDao.deletePoint(it) + } + } + + val items = points.filter { item -> item.thirdType == currentThirdType } + if (items.isNotEmpty()) { + items.forEach { + points.remove(it) + } + + notifyDataSetChanged() + + CameraInspectionService.weakReferenceHandler?.let { handler -> + val message = handler.obtainMessage() + message.what = LocaleConstant.DELETE_POINT_CODE + message.obj = currentThirdType + handler.sendMessage(message) + } + } true } } diff --git a/app/src/main/java/com/casic/br/operationsite/dao/PointBeanDao.java b/app/src/main/java/com/casic/br/operationsite/dao/PointBeanDao.java index af8dace..d1c5623 100644 --- a/app/src/main/java/com/casic/br/operationsite/dao/PointBeanDao.java +++ b/app/src/main/java/com/casic/br/operationsite/dao/PointBeanDao.java @@ -1,6 +1,7 @@ package com.casic.br.operationsite.dao; import androidx.room.Dao; +import androidx.room.Delete; import androidx.room.Insert; import androidx.room.Query; import androidx.room.Update; @@ -23,6 +24,12 @@ @Update void updatePoint(PointBean point); + @Query("SELECT * FROM camera_inspection_point WHERE thirdType = :point") + List queryPoint(int point); + + @Delete + void deletePoint(PointBean point); + @Query("DELETE FROM camera_inspection_point WHERE id = :id") void deletePointById(long id); } 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 299e0c4..3b8dea5 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 @@ -10,7 +10,6 @@ import android.util.Log import androidx.core.app.NotificationCompat import com.casic.br.operationsite.R -import com.casic.br.operationsite.base.BaseApplication import com.casic.br.operationsite.utils.CommandCreator import com.casic.br.operationsite.utils.LocaleConstant import com.casic.br.operationsite.utils.OnTcpConnectStateListener @@ -32,7 +31,6 @@ private val kTag = "InspectionService" private val notificationId = 2 private val tcpClient by lazy { TcpClient(this) } - private val pointBeanDao by lazy { BaseApplication.get().dataBase.pointBeanDao() } private val notificationManager by lazy { getSystemService(NOTIFICATION_SERVICE) as NotificationManager } private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob()) private var notificationBuilder: NotificationCompat.Builder? = null @@ -44,24 +42,24 @@ } LocaleConstant.START_ADD_POINT_CODE -> { - tcpClient.sendMessage(CommandCreator.setConfig(92)) + tcpClient.sendMessage(CommandCreator.invokeCommand(92)) } LocaleConstant.ADD_LINE_CODE -> { val line = msg.obj as Int - tcpClient.sendMessage(CommandCreator.addPoint(line)) + tcpClient.sendMessage(CommandCreator.invokeCommand(line)) } LocaleConstant.ADD_POINT_CODE -> { val point = msg.obj as Int - tcpClient.sendMessage(CommandCreator.addPoint(point)) + tcpClient.sendMessage(CommandCreator.setConfig(point)) } LocaleConstant.SAVE_POINT_CODE -> { scope.launch(Dispatchers.IO) { - tcpClient.sendMessage(CommandCreator.setConfig(92)) + tcpClient.sendMessage(CommandCreator.invokeCommand(92)) delay(500) - tcpClient.sendMessage(CommandCreator.setConfig(9)) + tcpClient.sendMessage(CommandCreator.invokeCommand(9)) } } @@ -73,9 +71,9 @@ LocaleConstant.START_INSPECTION_CODE -> { val line = msg.obj as Int scope.launch(Dispatchers.IO) { - tcpClient.sendMessage(CommandCreator.setConfig(92)) + tcpClient.sendMessage(CommandCreator.invokeCommand(92)) delay(500) - tcpClient.sendMessage(CommandCreator.addPoint(line)) + tcpClient.sendMessage(CommandCreator.invokeCommand(line)) } } } 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 f72a1ab..b939e7a 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 @@ -216,7 +216,34 @@ } /** - * 添加预置点,范围:1~255 + * 调用指令 + * */ + fun invokeCommand(index: Int): ByteArray { + val bytes = byteArrayOf( + 0xFF.toByte(), + 0x01, + 0x00, + 0x07.toByte(), + 0x00.toByte(), + 0x00.toByte(), + 0x00.toByte() + ) + + // 将 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 + } + + /** + * 设置指令 * * * 整个流程就是: @@ -243,31 +270,7 @@ //计算校验位。 var sum = 0 for (l in 1 until bytes.size - 1) { - sum += bytes[l] - } - bytes[6] = sum.toByte() - return bytes - } - - fun addPoint(index: Int): ByteArray { - val bytes = byteArrayOf( - 0xFF.toByte(), - 0x01, - 0x00, - 0x03.toByte(), - 0x00.toByte(), - 0x00.toByte(), - 0x00.toByte() - ) - - // 将 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] + sum += bytes[l].toInt() and 0xFF } bytes[6] = sum.toByte() return bytes @@ -291,31 +294,27 @@ //计算校验位。 var sum = 0 for (l in 1 until bytes.size - 1) { - sum += bytes[l] + sum += bytes[l].toInt() and 0xFF } bytes[6] = sum.toByte() return bytes } - fun startLine(index: Int): ByteArray { + fun stopLine(): ByteArray { val bytes = byteArrayOf( 0xFF.toByte(), 0x01, 0x00, - 0x05.toByte(), + 0x00.toByte(), 0x00.toByte(), 0x00.toByte(), 0x00.toByte() ) - // 将 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] + sum += bytes[l].toInt() and 0xFF } bytes[6] = sum.toByte() return bytes 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 4fef23f..8f15cb2 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 @@ -22,7 +22,7 @@ class Builder { lateinit var context: Context - var line = 0 + var line = 1 lateinit var listener: OnDialogButtonClickListener fun setContext(context: Context): Builder { 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 1411b99..71d5a77 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 @@ -20,6 +20,7 @@ private val countLimit: Int ) : RecyclerView.Adapter() { + private val kTag = "ManagePointAdapter" private val layoutInflater by lazy { LayoutInflater.from(context) } private val pointBeanDao by lazy { BaseApplication.get().dataBase.pointBeanDao() } @@ -45,27 +46,53 @@ } pointBeanDao.insert(pointBean) points.add(pointBean) + notifyDataSetChanged() + CameraInspectionService.weakReferenceHandler?.let { handler -> + val message = handler.obtainMessage() + message.what = LocaleConstant.ADD_POINT_CODE + message.obj = pointBean.thirdType + handler.sendMessage(message) + } } else { find.thirdType = 1 pointBeanDao.updatePoint(find) + notifyDataSetChanged() + CameraInspectionService.weakReferenceHandler?.let { handler -> + val message = handler.obtainMessage() + message.what = LocaleConstant.ADD_POINT_CODE + message.obj = 1 + handler.sendMessage(message) + } } - notifyDataSetChanged() - CameraInspectionService.weakReferenceHandler?.sendEmptyMessage( - LocaleConstant.ADD_POINT_CODE - ) } } else { - textView.text = "预置点位 ${points[position].thirdType}" + val currentThirdType = points[position].thirdType + textView.text = "预置点位 $currentThirdType" textView.setTextColor(R.color.mainTextColor.convertColor(context)) - // 长按监听 textView.setOnLongClickListener { v -> - //长按删除巡航点 - pointBeanDao.deletePointById(points[position].id) - points.remove(points[position]) - notifyDataSetChanged() - CameraInspectionService.weakReferenceHandler?.sendEmptyMessage( - LocaleConstant.DELETE_POINT_CODE - ) + //长按删除巡航点。删除所有线路中相同的点位 + val itemsToDelete = pointBeanDao.queryPoint(currentThirdType) + if (itemsToDelete.isNotEmpty()) { + itemsToDelete.forEach { + pointBeanDao.deletePoint(it) + } + } + + val items = points.filter { item -> item.thirdType == currentThirdType } + if (items.isNotEmpty()) { + items.forEach { + points.remove(it) + } + + notifyDataSetChanged() + + CameraInspectionService.weakReferenceHandler?.let { handler -> + val message = handler.obtainMessage() + message.what = LocaleConstant.DELETE_POINT_CODE + message.obj = currentThirdType + handler.sendMessage(message) + } + } true } } diff --git a/app/src/main/java/com/casic/br/operationsite/dao/PointBeanDao.java b/app/src/main/java/com/casic/br/operationsite/dao/PointBeanDao.java index af8dace..d1c5623 100644 --- a/app/src/main/java/com/casic/br/operationsite/dao/PointBeanDao.java +++ b/app/src/main/java/com/casic/br/operationsite/dao/PointBeanDao.java @@ -1,6 +1,7 @@ package com.casic.br.operationsite.dao; import androidx.room.Dao; +import androidx.room.Delete; import androidx.room.Insert; import androidx.room.Query; import androidx.room.Update; @@ -23,6 +24,12 @@ @Update void updatePoint(PointBean point); + @Query("SELECT * FROM camera_inspection_point WHERE thirdType = :point") + List queryPoint(int point); + + @Delete + void deletePoint(PointBean point); + @Query("DELETE FROM camera_inspection_point WHERE id = :id") void deletePointById(long id); } 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 299e0c4..3b8dea5 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 @@ -10,7 +10,6 @@ import android.util.Log import androidx.core.app.NotificationCompat import com.casic.br.operationsite.R -import com.casic.br.operationsite.base.BaseApplication import com.casic.br.operationsite.utils.CommandCreator import com.casic.br.operationsite.utils.LocaleConstant import com.casic.br.operationsite.utils.OnTcpConnectStateListener @@ -32,7 +31,6 @@ private val kTag = "InspectionService" private val notificationId = 2 private val tcpClient by lazy { TcpClient(this) } - private val pointBeanDao by lazy { BaseApplication.get().dataBase.pointBeanDao() } private val notificationManager by lazy { getSystemService(NOTIFICATION_SERVICE) as NotificationManager } private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob()) private var notificationBuilder: NotificationCompat.Builder? = null @@ -44,24 +42,24 @@ } LocaleConstant.START_ADD_POINT_CODE -> { - tcpClient.sendMessage(CommandCreator.setConfig(92)) + tcpClient.sendMessage(CommandCreator.invokeCommand(92)) } LocaleConstant.ADD_LINE_CODE -> { val line = msg.obj as Int - tcpClient.sendMessage(CommandCreator.addPoint(line)) + tcpClient.sendMessage(CommandCreator.invokeCommand(line)) } LocaleConstant.ADD_POINT_CODE -> { val point = msg.obj as Int - tcpClient.sendMessage(CommandCreator.addPoint(point)) + tcpClient.sendMessage(CommandCreator.setConfig(point)) } LocaleConstant.SAVE_POINT_CODE -> { scope.launch(Dispatchers.IO) { - tcpClient.sendMessage(CommandCreator.setConfig(92)) + tcpClient.sendMessage(CommandCreator.invokeCommand(92)) delay(500) - tcpClient.sendMessage(CommandCreator.setConfig(9)) + tcpClient.sendMessage(CommandCreator.invokeCommand(9)) } } @@ -73,9 +71,9 @@ LocaleConstant.START_INSPECTION_CODE -> { val line = msg.obj as Int scope.launch(Dispatchers.IO) { - tcpClient.sendMessage(CommandCreator.setConfig(92)) + tcpClient.sendMessage(CommandCreator.invokeCommand(92)) delay(500) - tcpClient.sendMessage(CommandCreator.addPoint(line)) + tcpClient.sendMessage(CommandCreator.invokeCommand(line)) } } } 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 f72a1ab..b939e7a 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 @@ -216,7 +216,34 @@ } /** - * 添加预置点,范围:1~255 + * 调用指令 + * */ + fun invokeCommand(index: Int): ByteArray { + val bytes = byteArrayOf( + 0xFF.toByte(), + 0x01, + 0x00, + 0x07.toByte(), + 0x00.toByte(), + 0x00.toByte(), + 0x00.toByte() + ) + + // 将 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 + } + + /** + * 设置指令 * * * 整个流程就是: @@ -243,31 +270,7 @@ //计算校验位。 var sum = 0 for (l in 1 until bytes.size - 1) { - sum += bytes[l] - } - bytes[6] = sum.toByte() - return bytes - } - - fun addPoint(index: Int): ByteArray { - val bytes = byteArrayOf( - 0xFF.toByte(), - 0x01, - 0x00, - 0x03.toByte(), - 0x00.toByte(), - 0x00.toByte(), - 0x00.toByte() - ) - - // 将 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] + sum += bytes[l].toInt() and 0xFF } bytes[6] = sum.toByte() return bytes @@ -291,31 +294,27 @@ //计算校验位。 var sum = 0 for (l in 1 until bytes.size - 1) { - sum += bytes[l] + sum += bytes[l].toInt() and 0xFF } bytes[6] = sum.toByte() return bytes } - fun startLine(index: Int): ByteArray { + fun stopLine(): ByteArray { val bytes = byteArrayOf( 0xFF.toByte(), 0x01, 0x00, - 0x05.toByte(), + 0x00.toByte(), 0x00.toByte(), 0x00.toByte(), 0x00.toByte() ) - // 将 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] + sum += bytes[l].toInt() and 0xFF } bytes[6] = sum.toByte() return bytes 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 4fef23f..8f15cb2 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 @@ -22,7 +22,7 @@ class Builder { lateinit var context: Context - var line = 0 + var line = 1 lateinit var listener: OnDialogButtonClickListener fun setContext(context: Context): Builder { diff --git a/app/src/main/res/layout/item_inspection_line_rv_l.xml b/app/src/main/res/layout/item_inspection_line_rv_l.xml index af84902..83ead44 100644 --- a/app/src/main/res/layout/item_inspection_line_rv_l.xml +++ b/app/src/main/res/layout/item_inspection_line_rv_l.xml @@ -1,7 +1,7 @@ () { + private val kTag = "ManagePointAdapter" private val layoutInflater by lazy { LayoutInflater.from(context) } private val pointBeanDao by lazy { BaseApplication.get().dataBase.pointBeanDao() } @@ -45,27 +46,53 @@ } pointBeanDao.insert(pointBean) points.add(pointBean) + notifyDataSetChanged() + CameraInspectionService.weakReferenceHandler?.let { handler -> + val message = handler.obtainMessage() + message.what = LocaleConstant.ADD_POINT_CODE + message.obj = pointBean.thirdType + handler.sendMessage(message) + } } else { find.thirdType = 1 pointBeanDao.updatePoint(find) + notifyDataSetChanged() + CameraInspectionService.weakReferenceHandler?.let { handler -> + val message = handler.obtainMessage() + message.what = LocaleConstant.ADD_POINT_CODE + message.obj = 1 + handler.sendMessage(message) + } } - notifyDataSetChanged() - CameraInspectionService.weakReferenceHandler?.sendEmptyMessage( - LocaleConstant.ADD_POINT_CODE - ) } } else { - textView.text = "预置点位 ${points[position].thirdType}" + val currentThirdType = points[position].thirdType + textView.text = "预置点位 $currentThirdType" textView.setTextColor(R.color.mainTextColor.convertColor(context)) - // 长按监听 textView.setOnLongClickListener { v -> - //长按删除巡航点 - pointBeanDao.deletePointById(points[position].id) - points.remove(points[position]) - notifyDataSetChanged() - CameraInspectionService.weakReferenceHandler?.sendEmptyMessage( - LocaleConstant.DELETE_POINT_CODE - ) + //长按删除巡航点。删除所有线路中相同的点位 + val itemsToDelete = pointBeanDao.queryPoint(currentThirdType) + if (itemsToDelete.isNotEmpty()) { + itemsToDelete.forEach { + pointBeanDao.deletePoint(it) + } + } + + val items = points.filter { item -> item.thirdType == currentThirdType } + if (items.isNotEmpty()) { + items.forEach { + points.remove(it) + } + + notifyDataSetChanged() + + CameraInspectionService.weakReferenceHandler?.let { handler -> + val message = handler.obtainMessage() + message.what = LocaleConstant.DELETE_POINT_CODE + message.obj = currentThirdType + handler.sendMessage(message) + } + } true } } diff --git a/app/src/main/java/com/casic/br/operationsite/dao/PointBeanDao.java b/app/src/main/java/com/casic/br/operationsite/dao/PointBeanDao.java index af8dace..d1c5623 100644 --- a/app/src/main/java/com/casic/br/operationsite/dao/PointBeanDao.java +++ b/app/src/main/java/com/casic/br/operationsite/dao/PointBeanDao.java @@ -1,6 +1,7 @@ package com.casic.br.operationsite.dao; import androidx.room.Dao; +import androidx.room.Delete; import androidx.room.Insert; import androidx.room.Query; import androidx.room.Update; @@ -23,6 +24,12 @@ @Update void updatePoint(PointBean point); + @Query("SELECT * FROM camera_inspection_point WHERE thirdType = :point") + List queryPoint(int point); + + @Delete + void deletePoint(PointBean point); + @Query("DELETE FROM camera_inspection_point WHERE id = :id") void deletePointById(long id); } 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 299e0c4..3b8dea5 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 @@ -10,7 +10,6 @@ import android.util.Log import androidx.core.app.NotificationCompat import com.casic.br.operationsite.R -import com.casic.br.operationsite.base.BaseApplication import com.casic.br.operationsite.utils.CommandCreator import com.casic.br.operationsite.utils.LocaleConstant import com.casic.br.operationsite.utils.OnTcpConnectStateListener @@ -32,7 +31,6 @@ private val kTag = "InspectionService" private val notificationId = 2 private val tcpClient by lazy { TcpClient(this) } - private val pointBeanDao by lazy { BaseApplication.get().dataBase.pointBeanDao() } private val notificationManager by lazy { getSystemService(NOTIFICATION_SERVICE) as NotificationManager } private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob()) private var notificationBuilder: NotificationCompat.Builder? = null @@ -44,24 +42,24 @@ } LocaleConstant.START_ADD_POINT_CODE -> { - tcpClient.sendMessage(CommandCreator.setConfig(92)) + tcpClient.sendMessage(CommandCreator.invokeCommand(92)) } LocaleConstant.ADD_LINE_CODE -> { val line = msg.obj as Int - tcpClient.sendMessage(CommandCreator.addPoint(line)) + tcpClient.sendMessage(CommandCreator.invokeCommand(line)) } LocaleConstant.ADD_POINT_CODE -> { val point = msg.obj as Int - tcpClient.sendMessage(CommandCreator.addPoint(point)) + tcpClient.sendMessage(CommandCreator.setConfig(point)) } LocaleConstant.SAVE_POINT_CODE -> { scope.launch(Dispatchers.IO) { - tcpClient.sendMessage(CommandCreator.setConfig(92)) + tcpClient.sendMessage(CommandCreator.invokeCommand(92)) delay(500) - tcpClient.sendMessage(CommandCreator.setConfig(9)) + tcpClient.sendMessage(CommandCreator.invokeCommand(9)) } } @@ -73,9 +71,9 @@ LocaleConstant.START_INSPECTION_CODE -> { val line = msg.obj as Int scope.launch(Dispatchers.IO) { - tcpClient.sendMessage(CommandCreator.setConfig(92)) + tcpClient.sendMessage(CommandCreator.invokeCommand(92)) delay(500) - tcpClient.sendMessage(CommandCreator.addPoint(line)) + tcpClient.sendMessage(CommandCreator.invokeCommand(line)) } } } 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 f72a1ab..b939e7a 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 @@ -216,7 +216,34 @@ } /** - * 添加预置点,范围:1~255 + * 调用指令 + * */ + fun invokeCommand(index: Int): ByteArray { + val bytes = byteArrayOf( + 0xFF.toByte(), + 0x01, + 0x00, + 0x07.toByte(), + 0x00.toByte(), + 0x00.toByte(), + 0x00.toByte() + ) + + // 将 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 + } + + /** + * 设置指令 * * * 整个流程就是: @@ -243,31 +270,7 @@ //计算校验位。 var sum = 0 for (l in 1 until bytes.size - 1) { - sum += bytes[l] - } - bytes[6] = sum.toByte() - return bytes - } - - fun addPoint(index: Int): ByteArray { - val bytes = byteArrayOf( - 0xFF.toByte(), - 0x01, - 0x00, - 0x03.toByte(), - 0x00.toByte(), - 0x00.toByte(), - 0x00.toByte() - ) - - // 将 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] + sum += bytes[l].toInt() and 0xFF } bytes[6] = sum.toByte() return bytes @@ -291,31 +294,27 @@ //计算校验位。 var sum = 0 for (l in 1 until bytes.size - 1) { - sum += bytes[l] + sum += bytes[l].toInt() and 0xFF } bytes[6] = sum.toByte() return bytes } - fun startLine(index: Int): ByteArray { + fun stopLine(): ByteArray { val bytes = byteArrayOf( 0xFF.toByte(), 0x01, 0x00, - 0x05.toByte(), + 0x00.toByte(), 0x00.toByte(), 0x00.toByte(), 0x00.toByte() ) - // 将 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] + sum += bytes[l].toInt() and 0xFF } bytes[6] = sum.toByte() return bytes 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 4fef23f..8f15cb2 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 @@ -22,7 +22,7 @@ class Builder { lateinit var context: Context - var line = 0 + var line = 1 lateinit var listener: OnDialogButtonClickListener fun setContext(context: Context): Builder { diff --git a/app/src/main/res/layout/item_inspection_line_rv_l.xml b/app/src/main/res/layout/item_inspection_line_rv_l.xml index af84902..83ead44 100644 --- a/app/src/main/res/layout/item_inspection_line_rv_l.xml +++ b/app/src/main/res/layout/item_inspection_line_rv_l.xml @@ -1,7 +1,7 @@