diff --git a/app/src/main/java/com/casic/common/detector/gd/base/SerialPortBaseActivity.kt b/app/src/main/java/com/casic/common/detector/gd/base/SerialPortBaseActivity.kt index b6eeab5..786561a 100644 --- a/app/src/main/java/com/casic/common/detector/gd/base/SerialPortBaseActivity.kt +++ b/app/src/main/java/com/casic/common/detector/gd/base/SerialPortBaseActivity.kt @@ -93,26 +93,20 @@ override fun handleMessage(msg: Message): Boolean { if (msg.what == 2025012001) { val buffer = msg.obj as ByteArray - Log.d(kTag, "${RuntimeCache.currentSegment} ${buffer.contentToString()}") +// Log.d(kTag, "${RuntimeCache.currentSegment} ${buffer.contentToString()}") val flag = (buffer.first().toInt() and 0xFF).toString(16).uppercase() if (RuntimeCache.currentSegment == CurrentSegment.InstallMarker && flag == "30" && buffer.size == 10) { onMarkerIdDetected(buffer.toMarkerId()) - } else if (RuntimeCache.currentSegment == CurrentSegment.FreeInspection) { - if (flag == "30" && buffer.size == 10) { - onMarkerIdDetected(buffer.toMarkerId()) - } else if (flag == "4E" && buffer.size == 5) { - onMarkerSignalDetected(buffer.toSignalStrength()) - } + } else if (RuntimeCache.currentSegment == CurrentSegment.FreeInspection && flag == "30" && buffer.size == 10) { + onMarkerIdDetected(buffer.toMarkerId()) } else if (RuntimeCache.currentSegment == CurrentSegment.SearchMarker) { if (flag == "30" && buffer.size == 10) { onMarkerIdDetected(buffer.toMarkerId()) } else if (flag == "4E" && buffer.size == 5) { onMarkerSignalDetected(buffer.toSignalStrength()) } - } else if (RuntimeCache.currentSegment == CurrentSegment.DetectDepth) { - if (flag == "53" && buffer.size == 5) { - onMarkerDepthDetected(buffer.toBuryDepth()) - } + } else if (RuntimeCache.currentSegment == CurrentSegment.DetectDepth && flag == "53" && buffer.size == 5) { + onMarkerDepthDetected(buffer.toBuryDepth()) } } return true diff --git a/app/src/main/java/com/casic/common/detector/gd/base/SerialPortBaseActivity.kt b/app/src/main/java/com/casic/common/detector/gd/base/SerialPortBaseActivity.kt index b6eeab5..786561a 100644 --- a/app/src/main/java/com/casic/common/detector/gd/base/SerialPortBaseActivity.kt +++ b/app/src/main/java/com/casic/common/detector/gd/base/SerialPortBaseActivity.kt @@ -93,26 +93,20 @@ override fun handleMessage(msg: Message): Boolean { if (msg.what == 2025012001) { val buffer = msg.obj as ByteArray - Log.d(kTag, "${RuntimeCache.currentSegment} ${buffer.contentToString()}") +// Log.d(kTag, "${RuntimeCache.currentSegment} ${buffer.contentToString()}") val flag = (buffer.first().toInt() and 0xFF).toString(16).uppercase() if (RuntimeCache.currentSegment == CurrentSegment.InstallMarker && flag == "30" && buffer.size == 10) { onMarkerIdDetected(buffer.toMarkerId()) - } else if (RuntimeCache.currentSegment == CurrentSegment.FreeInspection) { - if (flag == "30" && buffer.size == 10) { - onMarkerIdDetected(buffer.toMarkerId()) - } else if (flag == "4E" && buffer.size == 5) { - onMarkerSignalDetected(buffer.toSignalStrength()) - } + } else if (RuntimeCache.currentSegment == CurrentSegment.FreeInspection && flag == "30" && buffer.size == 10) { + onMarkerIdDetected(buffer.toMarkerId()) } else if (RuntimeCache.currentSegment == CurrentSegment.SearchMarker) { if (flag == "30" && buffer.size == 10) { onMarkerIdDetected(buffer.toMarkerId()) } else if (flag == "4E" && buffer.size == 5) { onMarkerSignalDetected(buffer.toSignalStrength()) } - } else if (RuntimeCache.currentSegment == CurrentSegment.DetectDepth) { - if (flag == "53" && buffer.size == 5) { - onMarkerDepthDetected(buffer.toBuryDepth()) - } + } else if (RuntimeCache.currentSegment == CurrentSegment.DetectDepth && flag == "53" && buffer.size == 5) { + onMarkerDepthDetected(buffer.toBuryDepth()) } } return true diff --git a/app/src/main/java/com/casic/common/detector/gd/view/MainActivity.kt b/app/src/main/java/com/casic/common/detector/gd/view/MainActivity.kt index 2c48cc9..c2116e7 100644 --- a/app/src/main/java/com/casic/common/detector/gd/view/MainActivity.kt +++ b/app/src/main/java/com/casic/common/detector/gd/view/MainActivity.kt @@ -7,6 +7,9 @@ import android.media.AudioAttributes import android.media.SoundPool import android.os.Bundle +import android.os.CountDownTimer +import android.os.Handler +import android.os.Looper import android.util.Log import android.view.KeyEvent import android.view.View @@ -28,7 +31,7 @@ import com.amap.api.maps.model.MyLocationStyle import com.casic.common.detector.gd.R import com.casic.common.detector.gd.base.BaseApplication -import com.casic.common.detector.gd.base.SerialPortActivity +import com.casic.common.detector.gd.base.SerialPortBaseActivity import com.casic.common.detector.gd.bean.MarkerLocalBean import com.casic.common.detector.gd.bean.TaskLocalBean import com.casic.common.detector.gd.callback.OnGetAddressListener @@ -37,8 +40,6 @@ import com.casic.common.detector.gd.extensions.appendDownloadUrl import com.casic.common.detector.gd.extensions.createTaskCode import com.casic.common.detector.gd.extensions.initImmersionBar -import com.casic.common.detector.gd.extensions.toMarkerId -import com.casic.common.detector.gd.extensions.toSignalStrength import com.casic.common.detector.gd.model.TaskDetailLocalModel import com.casic.common.detector.gd.model.TaskModel import com.casic.common.detector.gd.service.NtripConnectService @@ -49,6 +50,8 @@ import com.casic.common.detector.gd.utils.LocaleConstant import com.casic.common.detector.gd.utils.LocationKit import com.casic.common.detector.gd.utils.RouteOnMap +import com.casic.common.detector.gd.utils.RuntimeCache +import com.casic.common.detector.gd.utils.SerialPortCommand import com.casic.common.detector.gd.vm.TaskViewModel import com.casic.common.detector.gd.widgets.MarkerDetailDialog import com.casic.common.detector.gd.widgets.QueryMarkerDialog @@ -74,7 +77,7 @@ import java.io.File -class MainActivity : SerialPortActivity(), AMap.OnMarkerClickListener, +class MainActivity : SerialPortBaseActivity(), AMap.OnMarkerClickListener, OnMapLongClickListener { private val kTag = "MainActivity" @@ -89,7 +92,7 @@ private var clickTime: Long = 0 private var isFreeTask = false private var freeTaskTitle = "" - private var ids = HashSet() + private var ids = ArrayList() private var freeTaskId: String? = null private var soundResourceId = 0 private var isExecuteTask = false @@ -107,9 +110,9 @@ //地图初始化 initMapConfig(savedInstanceState) -// Intent(this, NtripConnectService::class.java).also { -// startService(it) -// } + Intent(this, NtripConnectService::class.java).also { + startService(it) + } soundResourceId = soundPool.load(this, R.raw.ring3, 1) //右上角菜单 @@ -372,8 +375,8 @@ AlertMessageDialog.OnDialogButtonClickListener { override fun onConfirmClick() { isFreeTask = false - soundPool.autoPause() - closeSerialPort() + handler.removeCallbacks(detectMarkerRunnable) + stopSerialPortCommand() binding.stopFreeTaskButton.visibility = View.GONE if (freeTaskId.isNullOrBlank()) { @@ -382,7 +385,7 @@ } freeTaskId?.apply { - taskViewModel.uploadTaskMarker(context, this, ArrayList(ids)) + taskViewModel.uploadTaskMarker(context, this, ids) } } }).build().show() @@ -454,63 +457,67 @@ }).build().show() } - private fun detectMarker() { - binding.stopFreeTaskButton.visibility = View.VISIBLE - isFreeTask = true - openSerialPort() - searchMarker(CurrentSegment.FreeInspection) + private val handler = Handler(Looper.getMainLooper()) + private val detectMarkerRunnable = object : Runnable { + override fun run() { + soundPool.play(soundResourceId, 1f, 1f, 0, 0, 1f) + handler.postDelayed(this, 50) + } } - override fun onDataReceived(buffer: ByteArray, segment: CurrentSegment) { - if (segment == CurrentSegment.FreeInspection) { - soundPool.play(soundResourceId, 1f, 1f, 0, 0, 1f) - when ((buffer.first().toInt() and 0xFF).toString(16).uppercase()) { - "4E" -> { - val signalEnergy = buffer.toSignalStrength() - if (signalEnergy <= 1500) { - detailDialog.show() - } - } + private fun detectMarker() { + RuntimeCache.currentSegment = CurrentSegment.FreeInspection + binding.stopFreeTaskButton.visibility = View.VISIBLE + isFreeTask = true + handler.post(detectMarkerRunnable) + sendSerialPortCommand(SerialPortCommand.SEARCH_MARKER_ALWAYS) + } - "30" -> { - val markerId = buffer.toMarkerId() - //添加地图Marker - if (!ids.contains(markerId)) { - val markers = DataBaseManager.get.queryMarkerById(markerId) - if (markers.isNotEmpty()) { - val marker = markers.first() - val latLng = LatLng( - marker.lat.toDouble(), marker.lng.toDouble() - ) - val resource = BitmapDescriptorFactory.fromResource( - R.mipmap.label_blue1 - ) - val options = MarkerOptions().position(latLng).icon(resource) - aMap.addMarker(options) - } - } - ids.add(markerId) + override fun onMarkerIdDetected(markerId: String) { + if (RuntimeCache.currentSegment == CurrentSegment.FreeInspection) { + //显示标识器详细信息 + if (!detailDialog.isShowing) { + DataBaseManager.get.queryMarkerById(markerId).firstOrNull().apply { + if (this == null) { + "无法查询到此ID【${markerId}】的信息".show(context) + } else { + detailDialog.setMarker(this).show() + object : CountDownTimer(3000, 1000) { + override fun onTick(millisUntilFinished: Long) { - //显示标识器详细信息 - if (!detailDialog.isShowing) { - DataBaseManager.get.queryMarkerById(markerId).firstOrNull().apply { - if (this == null) { - "无法查询到此ID【${markerId}】的信息".show(context) - } else { - detailDialog.setMarker(this) - detailDialog.show() } - } - } - } - else -> { - Log.d(kTag, "乱码数据: ${buffer.contentToString()}") + override fun onFinish() { + detailDialog.dismiss() + } + }.start() + } } } + + //不重复标注Marker + if (!ids.contains(markerId)) { + val markers = DataBaseManager.get.queryMarkerById(markerId) + if (markers.isNotEmpty()) { + val marker = markers.first() + val latLng = LatLng(marker.lat.toDouble(), marker.lng.toDouble()) + val resource = BitmapDescriptorFactory.fromResource(R.mipmap.label_blue1) + val options = MarkerOptions().position(latLng).icon(resource) + aMap.addMarker(options) + } + ids.add(markerId) + } } } + override fun onMarkerDepthDetected(depth: Int) { + + } + + override fun onMarkerSignalDetected(signalEnergy: Int) { + + } + override fun observeRequestState() { taskViewModel.loadState.observe(this) { when (it) { @@ -735,40 +742,6 @@ val markers = DataBaseManager.get.queryMarkerByState("0") if (markers.isNotEmpty()) { //TODO 逻辑没梳理明白 -// LoadingDialog.show(this, "标识器补全中,请稍后...") -// markers.forEach { marker -> -// taskViewModel.installLabel( -// this, -// companyId, -// labelBean.objectType, -// labelBean.pipelineType, -// labelBean.pipelineMaterial, -// labelBean.pipelineDiameter, -// labelBean.buryDeep, -// labelBean.lowerType, -// labelBean.lowerMaterial, -// labelBean.lowerDiameter, -// labelBean.lowerDeep, -// labelBean.buryMethod, -// labelBean.area, -// labelBean.line, -// labelBean.road, -// labelBean.constructTime, -// labelBean.owner, -// labelBean.objectId, -// labelBean.identifierId, -// labelBean.identifierType, -// labelBean.identifierDeep, -// labelBean.person, -// labelBean.installTime, -// labelBean.lng, -// labelBean.lat, -// labelBean.color, -// labelBean.remark, -// arrayListOf() -// ) -// } -// LoadingDialog.dismiss() } else { "没有需要补传的标识器!".show(this) } @@ -806,7 +779,6 @@ override fun onDestroy() { super.onDestroy() binding.mapView.onDestroy() - soundPool.autoPause() locationKit.stopLocation() BaseApplication.get().closeSerialPort() } diff --git a/app/src/main/java/com/casic/common/detector/gd/base/SerialPortBaseActivity.kt b/app/src/main/java/com/casic/common/detector/gd/base/SerialPortBaseActivity.kt index b6eeab5..786561a 100644 --- a/app/src/main/java/com/casic/common/detector/gd/base/SerialPortBaseActivity.kt +++ b/app/src/main/java/com/casic/common/detector/gd/base/SerialPortBaseActivity.kt @@ -93,26 +93,20 @@ override fun handleMessage(msg: Message): Boolean { if (msg.what == 2025012001) { val buffer = msg.obj as ByteArray - Log.d(kTag, "${RuntimeCache.currentSegment} ${buffer.contentToString()}") +// Log.d(kTag, "${RuntimeCache.currentSegment} ${buffer.contentToString()}") val flag = (buffer.first().toInt() and 0xFF).toString(16).uppercase() if (RuntimeCache.currentSegment == CurrentSegment.InstallMarker && flag == "30" && buffer.size == 10) { onMarkerIdDetected(buffer.toMarkerId()) - } else if (RuntimeCache.currentSegment == CurrentSegment.FreeInspection) { - if (flag == "30" && buffer.size == 10) { - onMarkerIdDetected(buffer.toMarkerId()) - } else if (flag == "4E" && buffer.size == 5) { - onMarkerSignalDetected(buffer.toSignalStrength()) - } + } else if (RuntimeCache.currentSegment == CurrentSegment.FreeInspection && flag == "30" && buffer.size == 10) { + onMarkerIdDetected(buffer.toMarkerId()) } else if (RuntimeCache.currentSegment == CurrentSegment.SearchMarker) { if (flag == "30" && buffer.size == 10) { onMarkerIdDetected(buffer.toMarkerId()) } else if (flag == "4E" && buffer.size == 5) { onMarkerSignalDetected(buffer.toSignalStrength()) } - } else if (RuntimeCache.currentSegment == CurrentSegment.DetectDepth) { - if (flag == "53" && buffer.size == 5) { - onMarkerDepthDetected(buffer.toBuryDepth()) - } + } else if (RuntimeCache.currentSegment == CurrentSegment.DetectDepth && flag == "53" && buffer.size == 5) { + onMarkerDepthDetected(buffer.toBuryDepth()) } } return true diff --git a/app/src/main/java/com/casic/common/detector/gd/view/MainActivity.kt b/app/src/main/java/com/casic/common/detector/gd/view/MainActivity.kt index 2c48cc9..c2116e7 100644 --- a/app/src/main/java/com/casic/common/detector/gd/view/MainActivity.kt +++ b/app/src/main/java/com/casic/common/detector/gd/view/MainActivity.kt @@ -7,6 +7,9 @@ import android.media.AudioAttributes import android.media.SoundPool import android.os.Bundle +import android.os.CountDownTimer +import android.os.Handler +import android.os.Looper import android.util.Log import android.view.KeyEvent import android.view.View @@ -28,7 +31,7 @@ import com.amap.api.maps.model.MyLocationStyle import com.casic.common.detector.gd.R import com.casic.common.detector.gd.base.BaseApplication -import com.casic.common.detector.gd.base.SerialPortActivity +import com.casic.common.detector.gd.base.SerialPortBaseActivity import com.casic.common.detector.gd.bean.MarkerLocalBean import com.casic.common.detector.gd.bean.TaskLocalBean import com.casic.common.detector.gd.callback.OnGetAddressListener @@ -37,8 +40,6 @@ import com.casic.common.detector.gd.extensions.appendDownloadUrl import com.casic.common.detector.gd.extensions.createTaskCode import com.casic.common.detector.gd.extensions.initImmersionBar -import com.casic.common.detector.gd.extensions.toMarkerId -import com.casic.common.detector.gd.extensions.toSignalStrength import com.casic.common.detector.gd.model.TaskDetailLocalModel import com.casic.common.detector.gd.model.TaskModel import com.casic.common.detector.gd.service.NtripConnectService @@ -49,6 +50,8 @@ import com.casic.common.detector.gd.utils.LocaleConstant import com.casic.common.detector.gd.utils.LocationKit import com.casic.common.detector.gd.utils.RouteOnMap +import com.casic.common.detector.gd.utils.RuntimeCache +import com.casic.common.detector.gd.utils.SerialPortCommand import com.casic.common.detector.gd.vm.TaskViewModel import com.casic.common.detector.gd.widgets.MarkerDetailDialog import com.casic.common.detector.gd.widgets.QueryMarkerDialog @@ -74,7 +77,7 @@ import java.io.File -class MainActivity : SerialPortActivity(), AMap.OnMarkerClickListener, +class MainActivity : SerialPortBaseActivity(), AMap.OnMarkerClickListener, OnMapLongClickListener { private val kTag = "MainActivity" @@ -89,7 +92,7 @@ private var clickTime: Long = 0 private var isFreeTask = false private var freeTaskTitle = "" - private var ids = HashSet() + private var ids = ArrayList() private var freeTaskId: String? = null private var soundResourceId = 0 private var isExecuteTask = false @@ -107,9 +110,9 @@ //地图初始化 initMapConfig(savedInstanceState) -// Intent(this, NtripConnectService::class.java).also { -// startService(it) -// } + Intent(this, NtripConnectService::class.java).also { + startService(it) + } soundResourceId = soundPool.load(this, R.raw.ring3, 1) //右上角菜单 @@ -372,8 +375,8 @@ AlertMessageDialog.OnDialogButtonClickListener { override fun onConfirmClick() { isFreeTask = false - soundPool.autoPause() - closeSerialPort() + handler.removeCallbacks(detectMarkerRunnable) + stopSerialPortCommand() binding.stopFreeTaskButton.visibility = View.GONE if (freeTaskId.isNullOrBlank()) { @@ -382,7 +385,7 @@ } freeTaskId?.apply { - taskViewModel.uploadTaskMarker(context, this, ArrayList(ids)) + taskViewModel.uploadTaskMarker(context, this, ids) } } }).build().show() @@ -454,63 +457,67 @@ }).build().show() } - private fun detectMarker() { - binding.stopFreeTaskButton.visibility = View.VISIBLE - isFreeTask = true - openSerialPort() - searchMarker(CurrentSegment.FreeInspection) + private val handler = Handler(Looper.getMainLooper()) + private val detectMarkerRunnable = object : Runnable { + override fun run() { + soundPool.play(soundResourceId, 1f, 1f, 0, 0, 1f) + handler.postDelayed(this, 50) + } } - override fun onDataReceived(buffer: ByteArray, segment: CurrentSegment) { - if (segment == CurrentSegment.FreeInspection) { - soundPool.play(soundResourceId, 1f, 1f, 0, 0, 1f) - when ((buffer.first().toInt() and 0xFF).toString(16).uppercase()) { - "4E" -> { - val signalEnergy = buffer.toSignalStrength() - if (signalEnergy <= 1500) { - detailDialog.show() - } - } + private fun detectMarker() { + RuntimeCache.currentSegment = CurrentSegment.FreeInspection + binding.stopFreeTaskButton.visibility = View.VISIBLE + isFreeTask = true + handler.post(detectMarkerRunnable) + sendSerialPortCommand(SerialPortCommand.SEARCH_MARKER_ALWAYS) + } - "30" -> { - val markerId = buffer.toMarkerId() - //添加地图Marker - if (!ids.contains(markerId)) { - val markers = DataBaseManager.get.queryMarkerById(markerId) - if (markers.isNotEmpty()) { - val marker = markers.first() - val latLng = LatLng( - marker.lat.toDouble(), marker.lng.toDouble() - ) - val resource = BitmapDescriptorFactory.fromResource( - R.mipmap.label_blue1 - ) - val options = MarkerOptions().position(latLng).icon(resource) - aMap.addMarker(options) - } - } - ids.add(markerId) + override fun onMarkerIdDetected(markerId: String) { + if (RuntimeCache.currentSegment == CurrentSegment.FreeInspection) { + //显示标识器详细信息 + if (!detailDialog.isShowing) { + DataBaseManager.get.queryMarkerById(markerId).firstOrNull().apply { + if (this == null) { + "无法查询到此ID【${markerId}】的信息".show(context) + } else { + detailDialog.setMarker(this).show() + object : CountDownTimer(3000, 1000) { + override fun onTick(millisUntilFinished: Long) { - //显示标识器详细信息 - if (!detailDialog.isShowing) { - DataBaseManager.get.queryMarkerById(markerId).firstOrNull().apply { - if (this == null) { - "无法查询到此ID【${markerId}】的信息".show(context) - } else { - detailDialog.setMarker(this) - detailDialog.show() } - } - } - } - else -> { - Log.d(kTag, "乱码数据: ${buffer.contentToString()}") + override fun onFinish() { + detailDialog.dismiss() + } + }.start() + } } } + + //不重复标注Marker + if (!ids.contains(markerId)) { + val markers = DataBaseManager.get.queryMarkerById(markerId) + if (markers.isNotEmpty()) { + val marker = markers.first() + val latLng = LatLng(marker.lat.toDouble(), marker.lng.toDouble()) + val resource = BitmapDescriptorFactory.fromResource(R.mipmap.label_blue1) + val options = MarkerOptions().position(latLng).icon(resource) + aMap.addMarker(options) + } + ids.add(markerId) + } } } + override fun onMarkerDepthDetected(depth: Int) { + + } + + override fun onMarkerSignalDetected(signalEnergy: Int) { + + } + override fun observeRequestState() { taskViewModel.loadState.observe(this) { when (it) { @@ -735,40 +742,6 @@ val markers = DataBaseManager.get.queryMarkerByState("0") if (markers.isNotEmpty()) { //TODO 逻辑没梳理明白 -// LoadingDialog.show(this, "标识器补全中,请稍后...") -// markers.forEach { marker -> -// taskViewModel.installLabel( -// this, -// companyId, -// labelBean.objectType, -// labelBean.pipelineType, -// labelBean.pipelineMaterial, -// labelBean.pipelineDiameter, -// labelBean.buryDeep, -// labelBean.lowerType, -// labelBean.lowerMaterial, -// labelBean.lowerDiameter, -// labelBean.lowerDeep, -// labelBean.buryMethod, -// labelBean.area, -// labelBean.line, -// labelBean.road, -// labelBean.constructTime, -// labelBean.owner, -// labelBean.objectId, -// labelBean.identifierId, -// labelBean.identifierType, -// labelBean.identifierDeep, -// labelBean.person, -// labelBean.installTime, -// labelBean.lng, -// labelBean.lat, -// labelBean.color, -// labelBean.remark, -// arrayListOf() -// ) -// } -// LoadingDialog.dismiss() } else { "没有需要补传的标识器!".show(this) } @@ -806,7 +779,6 @@ override fun onDestroy() { super.onDestroy() binding.mapView.onDestroy() - soundPool.autoPause() locationKit.stopLocation() BaseApplication.get().closeSerialPort() } diff --git a/app/src/main/java/com/casic/common/detector/gd/widgets/MarkerDetailDialog.kt b/app/src/main/java/com/casic/common/detector/gd/widgets/MarkerDetailDialog.kt index c3cc1de..ef593f5 100644 --- a/app/src/main/java/com/casic/common/detector/gd/widgets/MarkerDetailDialog.kt +++ b/app/src/main/java/com/casic/common/detector/gd/widgets/MarkerDetailDialog.kt @@ -18,8 +18,9 @@ private lateinit var marker: MarkerLocalBean - fun setMarker(marker: MarkerLocalBean) { + fun setMarker(marker: MarkerLocalBean): MarkerDetailDialog { this.marker = marker + return this } private val binding: DialogMarkerDetailBinding by binding()