Browse Source

refactor(界面和锁仓状态调账)
- 锁定站修改
- 如果不存在但是还在充电就是02的时候就不处理

周文健 5 months ago
parent
commit
cbdac40914

+ 1 - 1
app/src/main/java/com/grkj/iscs/BusinessManager.kt

@@ -374,7 +374,7 @@ object BusinessManager {
                                     }
                                 }
                             }
-                        } else {
+                        } else if (!keyBean.isCharging) {//增加充电判断,防止无线充电干扰锁仓状态导致判断为取出
                             // 移出待连监听集合,防止connectKey循环失败
                             keyBean.mac?.let {
                                 unregisterConnectListener(it)

+ 10 - 0
app/src/main/java/com/grkj/iscs/modbus/ModBusCMDHelper.kt

@@ -139,4 +139,14 @@ object ModBusCMDHelper {
             byteArrayOf(addr[0], addr[1], 0x00, 0x01)
         )
     }
+
+    /**
+     * 钥匙充电指令
+     */
+    fun generateKeyChargeCmd(addr: ByteArray): MBFrame {
+        return MBFrame(
+            FRAME_TYPE_WRITE,
+            byteArrayOf(addr[0], addr[1], 0x00, 0x01)
+        )
+    }
 }

+ 0 - 1
app/src/main/java/com/grkj/iscs/view/fragment/StepFragment.kt

@@ -185,7 +185,6 @@ class StepFragment(val goBack: () -> Unit, val changePage: (PageChangeBO) -> Uni
                 })
                 mBinding?.mapview?.addLayer(stationLayer)
                 stationLayer?.setRatio(mapRatio)
-                stationLayer?.startAnimation()
             }
 
             override fun onMapLoadFail() {

+ 5 - 4
app/src/main/java/com/grkj/iscs/view/fragment/SwitchStatusFragment.kt

@@ -10,14 +10,15 @@ import com.grkj.iscs.view.base.BaseMvpFragment
 import com.grkj.iscs.view.iview.ISwitchStatusView
 import com.grkj.iscs.view.presenter.SwitchStatusPresenter
 import com.grkj.iscs.view.widget.CustomStationLayer
+import com.grkj.iscs.view.widget.CustomSwitchStationLayer
 import com.onlylemi.mapview.library.MapViewListener
 import com.sik.sikcore.thread.ThreadUtils
 
 class SwitchStatusFragment :
     BaseMvpFragment<ISwitchStatusView, SwitchStatusPresenter, FragmentSwitchStatusBinding>() {
     private var mapRatio: Float = 1f
-    private var stationLayer: CustomStationLayer? = null
-    private val mStationList = mutableListOf<CustomStationLayer.IsolationPoint>()
+    private var stationLayer: CustomSwitchStationLayer? = null
+    private val mStationList = mutableListOf<CustomSwitchStationLayer.IsolationPoint>()
     override fun initPresenter(): SwitchStatusPresenter {
         return SwitchStatusPresenter()
     }
@@ -90,7 +91,7 @@ class SwitchStatusFragment :
                     val finalY = localY * ratioY
                     // 异步加载点位图标,固定请求尺寸
                     mStationList.add(
-                        CustomStationLayer.IsolationPoint(
+                        CustomSwitchStationLayer.IsolationPoint(
                             PointF(finalX, finalY),
                             pt.entityName!!,
                             null,
@@ -120,7 +121,7 @@ class SwitchStatusFragment :
                     mBinding?.mapview?.currentRotateDegrees = 0f
                     return
                 }
-                stationLayer = CustomStationLayer(mBinding?.mapview, mStationList)
+                stationLayer = CustomSwitchStationLayer(mBinding?.mapview, mStationList)
                 mBinding?.mapview?.addLayer(stationLayer)
                 stationLayer?.setRatio(mapRatio)
                 stationLayer?.stopAnimation()

+ 0 - 14
app/src/main/java/com/grkj/iscs/view/fragment/WorkshopFragment.kt

@@ -83,20 +83,6 @@ class WorkshopFragment(val changePage: (PageChangeBO) -> Unit) :
                         }
                         mPointList.forEach { itPoint ->
                             itPoint.ticketList = it.filter { it.workstationId == itPoint.workstationId }.toMutableList()
-//                            itPoint.ticketList.forEach { itTicket ->
-//                                if (itTicket.ticketType == null) {
-//                                    itTicket.bitmap = BitmapUtil.getResizedBitmapFromMipmap(requireContext(), R.mipmap.ticket_type_placeholder, 60, 60)
-//                                } else {
-//                                    BitmapUtil.loadBitmapFromUrl(
-//                                        requireContext(),
-//                                        SPUtils.getAttributeValue(requireContext(), Constants.getTicketKey(itTicket.ticketType.toInt())),
-//                                        R.mipmap.ticket_type_placeholder,
-//                                        60, 60
-//                                    ) { itBitmap ->
-//                                        itTicket.bitmap = itBitmap ?: BitmapUtil.getResizedBitmapFromMipmap(requireContext(), R.mipmap.ticket_type_placeholder, 60, 60)
-//                                    }
-//                                }
-//                            }
                             itPoint.ticketList.take(4).forEachIndexed { index, itTicket ->
                                 if (itPoint.ticketList.size > 3 && index == 3) {
                                     BitmapUtil.loadBitmapFromUrl(

+ 7 - 65
app/src/main/java/com/grkj/iscs/view/widget/CustomStationLayer.kt

@@ -22,29 +22,10 @@ import kotlin.math.sin
 class CustomStationLayer @JvmOverloads constructor(
     mapView: MapView?, private var pointList: List<IsolationPoint> = mutableListOf()
 ) : MapBaseLayer(mapView) {
-    // 呼吸灯周期(毫秒)
-    private val breathePeriod = 1200f
-    private val FRAME_INTERVAL = 32L   // 约 30fps
-    private var alpha = 255
-    private val refreshRunnable = Runnable {
-        // 2. uptimeMillis 保证单调递增
-        val now = SystemClock.uptimeMillis()
-        // 3. 先在 Long 上做模,再转 Float 计算 phase
-        val phase = ((now % breathePeriod).toFloat()) / breathePeriod
-        val normalized = ((sin(phase * 2 * Math.PI) + 1) / 2).toFloat()
-        alpha = (normalized * (255 - 50) + 50).toInt()
-        mapView?.refresh()
-        mapView?.post {
-            startAnimation()
-        }
-    }
 
     private var listener: MarkIsClickListener? = null
     private var radiusMark = 0f
-    private var isClickMark: Boolean = false
-    private var num: Int = -1
     private lateinit var paint: Paint
-    private var btnIndex: Int = -1
     private var currentZoom = 0f
     private var currentDegree = 0f
     private var bgBitmap: Bitmap? = null
@@ -80,18 +61,6 @@ class CustomStationLayer @JvmOverloads constructor(
         switchSize = setValue(4 * ratio)
     }
 
-    fun startAnimation() {
-        // 先干掉前一次没执行的
-        mapView.removeCallbacks(refreshRunnable)
-        // 延后 16ms 再刷新,自动合并一堆连续的调用
-        mapView.postDelayed(refreshRunnable, FRAME_INTERVAL)
-    }
-
-    fun stopAnimation() {
-        // 先干掉前一次没执行的
-        mapView.removeCallbacks(refreshRunnable)
-    }
-
     override fun onTouch(event: MotionEvent) {
 
     }
@@ -119,40 +88,13 @@ class CustomStationLayer @JvmOverloads constructor(
                 canvas.drawBitmap(
                     it, x, y, paint
                 )
-                paint.alpha = 255
-                if (switchStatus != null) {
-                    // 再画 icon
-                    if (switchStatus) {
-                        paint.color = ContextCompat.getColor(
-                            MyApplication.instance?.applicationContext!!,
-                            R.color.common_switch_enable
-                        )
-                        paint.alpha = alpha
-                        canvas.drawCircle(
-                            x + (it.width - switchSize) / 2 + switchSize / 2,
-                            y + (it.width - switchSize) / 2 + switchSize / 2,
-                            switchSize, paint
-                        )
-                        paint.alpha = 255
-                    } else {
-                        paint.color = ContextCompat.getColor(
-                            MyApplication.instance?.applicationContext!!,
-                            R.color.common_switch_disable
-                        )
-                        canvas.drawCircle(
-                            x + (it.width - switchSize) / 2 + switchSize / 2,
-                            y + (it.width - switchSize) / 2 + switchSize / 2,
-                            switchSize, paint
-                        )
-                    }
-//                    point.icon?.let { icon ->
-//                        canvas.drawBitmap(
-//                            icon,
-//                            x + (it.width - icon.width) / 2,
-//                            y + (it.width - icon.width) / 2,
-//                            paint
-//                        )
-//                    }
+                point.icon?.let { icon ->
+                    canvas.drawBitmap(
+                        icon,
+                        x + (it.width - icon.width) / 2,
+                        y + (it.width - icon.width) / 2,
+                        paint
+                    )
                 }
                 // 然后画文字
                 paint.style = Paint.Style.FILL

+ 221 - 0
app/src/main/java/com/grkj/iscs/view/widget/CustomSwitchStationLayer.kt

@@ -0,0 +1,221 @@
+package com.grkj.iscs.view.widget
+
+import android.graphics.Bitmap
+import android.graphics.Canvas
+import android.graphics.Color
+import android.graphics.Matrix
+import android.graphics.Paint
+import android.graphics.PointF
+import android.os.SystemClock
+import android.util.Pair
+import android.view.MotionEvent
+import androidx.core.content.ContextCompat
+import com.grkj.iscs.MyApplication
+import com.grkj.iscs.R
+import com.grkj.iscs.modbus.ModBusController
+import com.grkj.iscs.util.BitmapUtil
+import com.onlylemi.mapview.library.MapView
+import com.onlylemi.mapview.library.layer.MapBaseLayer
+import kotlin.math.cos
+import kotlin.math.sin
+
+class CustomSwitchStationLayer @JvmOverloads constructor(
+    mapView: MapView?, private var pointList: List<IsolationPoint> = mutableListOf()
+) : MapBaseLayer(mapView) {
+    // 呼吸灯周期(毫秒)
+    private val breathePeriod = 1200f
+    private val FRAME_INTERVAL = 32L   // 约 30fps
+    private var alpha = 255
+    private val refreshRunnable = Runnable {
+        // 2. uptimeMillis 保证单调递增
+        val now = SystemClock.uptimeMillis()
+        // 3. 先在 Long 上做模,再转 Float 计算 phase
+        val phase = ((now % breathePeriod).toFloat()) / breathePeriod
+        val normalized = ((sin(phase * 2 * Math.PI) + 1) / 2).toFloat()
+        alpha = (normalized * (255 - 50) + 50).toInt()
+        mapView?.refresh()
+        mapView?.post {
+            startAnimation()
+        }
+    }
+
+    private var listener: MarkIsClickListener? = null
+    private var radiusMark = 0f
+    private var isClickMark: Boolean = false
+    private var num: Int = -1
+    private lateinit var paint: Paint
+    private var btnIndex: Int = -1
+    private var currentZoom = 0f
+    private var currentDegree = 0f
+    private var bgBitmap: Bitmap? = null
+    private var coverBitmap: Bitmap? = null
+    private var ratio: Float = 1f
+    private var switchSize: Float = 1f
+
+    init {
+        initLayer()
+    }
+
+    private fun initLayer() {
+        radiusMark = setValue(6.0f)
+        paint = Paint()
+        paint.isAntiAlias = true
+        paint.style = Paint.Style.FILL_AND_STROKE
+    }
+
+    fun setRatio(ratio: Float) {
+        this.ratio = ratio
+        bgBitmap = BitmapUtil.getResizedBitmapFromDrawable(
+            mapView.context,
+            R.drawable.red_stroke_bg,
+            (50 * ratio).toInt(),
+            (78 * ratio).toInt()
+        )!!
+        coverBitmap = BitmapUtil.getResizedBitmapFromDrawable(
+            mapView.context,
+            R.drawable.map_item_cover_bg,
+            (50 * ratio).toInt(),
+            (78 * ratio).toInt()
+        )!!
+        switchSize = setValue(4 * ratio)
+    }
+
+    fun startAnimation() {
+        // 先干掉前一次没执行的
+        mapView.removeCallbacks(refreshRunnable)
+        // 延后 16ms 再刷新,自动合并一堆连续的调用
+        mapView.postDelayed(refreshRunnable, FRAME_INTERVAL)
+    }
+
+    fun stopAnimation() {
+        // 先干掉前一次没执行的
+        mapView.removeCallbacks(refreshRunnable)
+    }
+
+    override fun onTouch(event: MotionEvent) {
+
+    }
+
+    override fun draw(
+        canvas: Canvas, currentMatrix: Matrix, currentZoom: Float, currentRotateDegrees: Float
+    ) {
+        this.currentZoom = currentZoom
+        currentDegree = 360 - currentRotateDegrees
+
+        if (!isVisible) return
+
+        canvas.save()
+        // 把 mapView 本身的缩放/平移/旋转一次性 concat 到 Canvas
+        canvas.concat(currentMatrix)
+        val switchData = ModBusController.getSwitchData()
+        pointList.forEach { point ->
+            val switchStatus = switchData
+                .find { it.idx == point.pointSerialNumber?.toInt() }?.enabled
+            // point.pos.x/y 已经是「图内像素坐标」
+            val x = point.pos.x
+            val y = point.pos.y
+            // 先画背景(它会被 currentMatrix 自动缩放)
+            bgBitmap?.let {
+                canvas.drawBitmap(
+                    it, x, y, paint
+                )
+                paint.alpha = 255
+                if (switchStatus != null) {
+                    // 再画 icon
+                    if (switchStatus) {
+                        paint.color = ContextCompat.getColor(
+                            MyApplication.instance?.applicationContext!!,
+                            R.color.common_switch_enable
+                        )
+                        paint.alpha = alpha
+                        canvas.drawCircle(
+                            x + (it.width - switchSize) / 2 + switchSize / 2,
+                            y + (it.width - switchSize) / 2 + switchSize / 2,
+                            switchSize, paint
+                        )
+                        paint.alpha = 255
+                    } else {
+                        paint.color = ContextCompat.getColor(
+                            MyApplication.instance?.applicationContext!!,
+                            R.color.common_switch_disable
+                        )
+                        canvas.drawCircle(
+                            x + (it.width - switchSize) / 2 + switchSize / 2,
+                            y + (it.width - switchSize) / 2 + switchSize / 2,
+                            switchSize, paint
+                        )
+                    }
+//                    point.icon?.let { icon ->
+//                        canvas.drawBitmap(
+//                            icon,
+//                            x + (it.width - icon.width) / 2,
+//                            y + (it.width - icon.width) / 2,
+//                            paint
+//                        )
+//                    }
+                }
+                // 然后画文字
+                paint.style = Paint.Style.FILL
+                paint.strokeWidth = 1f
+                paint.color = Color.RED
+                paint.textSize = radiusMark * ratio  // 这里是「图内」的文字大小,后面会跟着缩放
+                val textW = paint.measureText(point.entityName)
+                canvas.drawText(
+                    point.entityName,
+                    x + (it.width - textW) / 2,
+                    y + (it.height - radiusMark / 2 - it.height / 10),
+                    paint
+                )
+
+                // 如果选中,再叠加一个标记
+                if (point.isSelected) {
+                    coverBitmap?.let {
+                        canvas.drawBitmap(
+                            it, x, y, paint
+                        )
+                    }
+                    val checkW = paint.measureText("√")
+                    paint.color = Color.WHITE
+                    canvas.drawText(
+                        "√",
+                        x + (it.width - checkW) / 2,
+                        y + (it.height / 2 + radiusMark / 2),
+                        paint
+                    )
+                }
+            }
+        }
+
+        canvas.restore()
+    }
+
+    private fun rotatePoint(
+        oriX: Float, oriY: Float, desX: Float, desY: Float, rotateDegrees: Float
+    ): Pair<Float, Float> {
+        // 将度数转换为弧度
+        val theta = Math.toRadians(rotateDegrees.toDouble())
+
+        // 计算旋转后的坐标
+        val newX = (oriX - desX) * cos(theta) - (oriY - desY) * sin(theta) + desX
+        val newY = (oriX - desX) * sin(theta) + ((oriY - desY) * cos(theta)) + desY
+
+        return Pair(newX.toFloat(), newY.toFloat())
+    }
+
+    fun setMarkIsClickListener(listener: MarkIsClickListener?) {
+        this.listener = listener
+    }
+
+    interface MarkIsClickListener {
+        fun markIsClick(index: Int, btnIndex: Int)
+    }
+
+    data class IsolationPoint(
+        val pos: PointF,
+        val entityName: String,
+        val icon: Bitmap?,
+        val entityId: Long,
+        val pointSerialNumber: String?,
+        val isSelected: Boolean,
+    )
+}