From cf160e28026ed1ed8bea82701d66e79a1085c503 Mon Sep 17 00:00:00 2001
From: Riku <risaku@163.com>
Date: 星期四, 15 五月 2025 00:24:39 +0800
Subject: [PATCH] 1. 新增走航动态溯源功能

---
 src/main/kotlin/com/flightfeather/uav/biz/dataanalysis/BaseExceptionContinuous.kt |  194 +++++++++++++++++++++++++++++++++---------------
 1 files changed, 133 insertions(+), 61 deletions(-)

diff --git a/src/main/kotlin/com/flightfeather/uav/biz/dataanalysis/BaseExceptionContinuous.kt b/src/main/kotlin/com/flightfeather/uav/biz/dataanalysis/BaseExceptionContinuous.kt
index 9177f77..bf4f1d1 100644
--- a/src/main/kotlin/com/flightfeather/uav/biz/dataanalysis/BaseExceptionContinuous.kt
+++ b/src/main/kotlin/com/flightfeather/uav/biz/dataanalysis/BaseExceptionContinuous.kt
@@ -1,84 +1,130 @@
 package com.flightfeather.uav.biz.dataanalysis
 
-import com.flightfeather.uav.biz.dataanalysis.model.DataAnalysisConfig
+import com.flightfeather.uav.biz.FactorFilter
+import com.flightfeather.uav.biz.dataanalysis.model.ExceptionTag
 import com.flightfeather.uav.domain.entity.BaseRealTimeData
+import com.flightfeather.uav.lightshare.eunm.ExceptionStatusType
+import com.flightfeather.uav.socket.eunm.FactorType
+import java.time.Duration
 
 /**
  * 杩炵画绫诲瀷鐨勫紓甯稿垎鏋愬熀绫�,閫傜敤浜庡綋鍓嶆暟鎹笌鐩搁偦鏁版嵁涔嬮棿鏈夊叧鑱斿叧绯荤殑鎯呭喌
  */
-abstract class BaseExceptionContinuous(config: DataAnalysisConfig) : BaseExceptionAnalysis(config) {
+abstract class BaseExceptionContinuous<T : ExceptionTag, V : BaseAnalysisConfig, Y : BaseExceptionResult>(
+    config: V, private val tagClz: Class<T>
+) : BaseExceptionAnalysis<V, Y>(config) {
 
     companion object {
         // 璁板綍寮傚父鏁版嵁娈垫椂锛屽垎鍒悜璧峰鍓嶅拰鏈熬鍚庨澶栬褰曠殑鏁版嵁涓暟鍋忕Щ閲�
         private const val OFFSET = 10
     }
 
-    // 璧峰鏁版嵁涓嬫爣
-    protected var sIndex = mutableListOf<Int>()
+    inner class Tag {
+        // 璧峰鏁版嵁涓嬫爣
+        var sIndex = 0
 
-    // 璧峰鏁版嵁瀵硅薄
-    protected var startData = mutableListOf<BaseRealTimeData?>()
+        // 璧峰鏁版嵁瀵硅薄
+        var startData: BaseRealTimeData? = null
 
-    // 鏈熬鏁版嵁涓嬫爣
-    protected var eIndex = mutableListOf<Int>()
+        // 鏈熬鏁版嵁涓嬫爣
+        var eIndex = -1
+
+        // 鏈熬鏁版嵁瀵硅薄
+        var endData: BaseRealTimeData? = null
+
+        // 寮傚父鏁版嵁娈�
+        var exceptionData = mutableListOf<BaseRealTimeData>()
+
+        // 鏄惁瀛樺湪寮傚父
+        var exceptionExisted = false
+
+        // 寮傚父缁撴灉鏄惁鍒涘缓
+        var exceptionCreated = false
+
+        fun addExceptionData(data: BaseRealTimeData) {
+            exceptionExisted = true
+            exceptionData.add(data)
+        }
+
+        fun refreshWithNextException(data: BaseRealTimeData) {
+            sIndex = eIndex
+            startData = data
+            exceptionData.clear()
+            exceptionExisted = false
+            exceptionCreated = false
+        }
+    }
+
+    protected val tagMap = mutableMapOf<FactorType, T>()
+
+    // 璧峰鏁版嵁涓庢湯灏炬暟鎹棿闅�
+    open var durationCount = 1
 
     // 鏈熬鏁版嵁瀵硅薄
     protected var lastData: BaseRealTimeData? = null
 
-    // 寮傚父鏁版嵁娈�
-    protected var exceptionData = mutableListOf<MutableList<BaseRealTimeData>>()
+    /**
+     * 鍒ゆ柇鐩搁偦鏁版嵁鏄惁杩炵画
+     */
+    open fun isContinuous(d1: BaseRealTimeData?, d2: BaseRealTimeData?): Boolean {
+        if (d1 == null || d2 == null) return true
 
-    // 璧峰鏁版嵁涓庢湯灏炬暟鎹棿闅�
-    open var durationCount = 1
-    protected var existException = mutableListOf<Boolean>()
+        val t1 = d1.dataTime
+        val t2 = d2.dataTime
+        return Duration.between(t1?.toInstant(), t2?.toInstant()).toMillis() <= (20 * 1000)
+    }
 
     /**
      * 鍒ゆ柇鏄惁婊¤冻寮傚父鏉′欢
      */
-    abstract fun judgeException(p: BaseRealTimeData?, n: BaseRealTimeData): List<Boolean>
+    abstract fun judgeException(p: BaseRealTimeData?, n: BaseRealTimeData): MutableMap<FactorType, Boolean>
 
     /**
-     * 鍒ゆ柇寮傚父鍑虹幇鐨勮繛缁椂闀挎槸鍚︽弧瓒虫潯浠�
+     * 鍒ゆ柇寮傚父鍑虹幇鐨勮繛缁釜鏁版槸鍚︽弧瓒虫潯浠�
+     * @param tag 寮傚父鏁版嵁瀵硅薄
      */
-    abstract fun judgeDuration(sIndex: Int, eIndex: Int): Boolean
+    abstract fun judgeExceptionCount(tag: T): Boolean
+
+    /**
+     * 寮傚父鏁版嵁鐨勬埅鍙栧垽鏂�
+     * 鏄惁闇�瑕侀檺鍒朵竴缁勫紓甯告暟鎹殑闀垮害
+     * @return 榛樿涓嶉渶瑕佹埅鍙�
+     */
+    open fun needCut(tag: T): Boolean {
+        return false
+    }
 
     override fun init() {
         super.init()
         lastData = null
-        repeat(config.factorCount) {
-            startData.add(null)
-            sIndex.add(0)
-            eIndex.add(-1)
-            existException.add(false)
-            exceptionData.add(mutableListOf())
+        tagMap.clear()
+        config.factorFilter.mainList().forEach { f ->
+            tagMap[f] = tagClz.newInstance()
         }
     }
 
     override fun onNextData(data: BaseRealTimeData) {
         val isContinue = isContinuous(lastData, data)
         val hasException = judgeException(lastData, data)
-        repeat(config.factorCount) { i ->
-            eIndex[i]++
-            // 璧峰鏁版嵁
-            if (lastData == null) {
-                refreshAfterCheckResult(i, data)
-            }
-            // 鍒ゆ柇鐩搁偦鏁版嵁鏄惁杩炵画骞朵笖鏄惁婊¤冻寮傚父鍒ゆ柇
-            if (!isContinue) {
-                checkResult()
-                // 鏁版嵁涓嶈繛缁椂锛岃褰曞紓甯告儏鍐�
-                if (eIndex[i] - sIndex[i] >= durationCount) {
-                    refreshAfterCheckResult(i, data)
+        config.factorFilter.selectedList.forEach { s ->
+            val f = s.main
+            tagMap[f]?.let {
+                it.eIndex++
+                // 璧峰鏁版嵁
+                it.endData = data
+                if (it.startData == null) {
+                    it.refreshWithNextException(data)
                 }
-            } else {
-                if (hasException[i]) {
-                    existException[i] = true
-                    exceptionData[i].add(data)
+                // 鍒ゆ柇鐩搁偦鏁版嵁鏄惁杩炵画骞朵笖鏄惁婊¤冻寮傚父鍒ゆ柇
+                if (!isContinue || needCut(it)) {
+                    // 鏁版嵁涓嶈繛缁椂锛岃褰曞紓甯告儏鍐�
+                    recordException(s, it, data)
                 } else {
-                    // 寮傚父涓嶅啀閲嶅鍑虹幇鏃讹紝璁板綍寮傚父鎯呭喌
-                    checkResult()
-                    if (eIndex[i] - sIndex[i] >= durationCount) {
-                        refreshAfterCheckResult(i, data)
+                    if (hasException[f] == true) {
+                        it.addExceptionData(data)
+                    } else {
+                        // 寮傚父涓嶅啀閲嶅鍑虹幇鏃讹紝璁板綍寮傚父鎯呭喌
+                        recordException(s, it, data)
                     }
                 }
             }
@@ -87,36 +133,62 @@
     }
 
     override fun onDone() {
-        checkResult()
+        checkResult(exceptionStatus = ExceptionStatusType.Ended)
     }
 
-    fun refreshAfterCheckResult(i:Int, data: BaseRealTimeData) {
-        sIndex[i] = eIndex[i]
-        startData[i] = data
-        exceptionData[i].clear()
-        exceptionData[i].add(data)
+    /**
+     * 寮傚父缁撴潫锛岃褰曞紓甯�
+     */
+    fun recordException(factor: FactorFilter.SelectedFactor, tag: T, data: BaseRealTimeData) {
+        checkResult(factor, ExceptionStatusType.Ended)
+//        if (tag.eIndex - tag.sIndex >= durationCount) {
+        tag.refreshWithNextException(data)
+//        }
     }
 
     /**
      * 妫�鏌ヨ繛缁紓甯哥粨鏉熸椂锛屾槸鍚︾鍚堝紓甯稿瓨鍌ㄦ潯浠�
      */
-    open fun checkResult(index: Int? = null) {
-        if (index != null) {
-            if (existException[index] && judgeDuration(sIndex[index], eIndex[index])) {
-                startData[index]?.let {
-                    resultList.add(newResult(it, lastData, index, exceptionData[index]))
-                }
-                existException[index] = false
+    open fun checkResult(
+        factor: FactorFilter.SelectedFactor? = null,
+        exceptionStatus: ExceptionStatusType = ExceptionStatusType.InProgress
+    ) {
+        val tag = tagMap[factor?.main]
+        if (factor != null && tag != null) {
+            if (tag.exceptionExisted && judgeExceptionCount(tag)) {
+                onNewException(tag, factor, exceptionStatus)
             }
         } else {
-            repeat(config.factorCount) { i ->
-                if (existException[i] && judgeDuration(sIndex[i], eIndex[i])) {
-                    startData[i]?.let {
-                        resultList.add(newResult(it, lastData, i, exceptionData[i]))
-                    }
-                    existException[i] = false
+            config.factorFilter.selectedList.forEach { f ->
+                val tag1 = tagMap[f.main] ?: return@forEach
+                if (tag1.exceptionExisted && judgeExceptionCount(tag1)) {
+                    onNewException(tag1, f, exceptionStatus)
                 }
             }
         }
     }
+
+    /**
+     * 鏂板鎴栨洿鏂颁竴鏉″紓甯�
+     */
+    open fun onNewException(tag: T, factor: FactorFilter.SelectedFactor, exceptionStatus: ExceptionStatusType) {
+        if (tag.startData == null) return
+        val ex = newResult(tag.startData!!, lastData, factor, tag.exceptionData)
+            .apply { status = exceptionStatus.value }
+        // 寮傚父宸插垱寤烘椂锛屾洿鏂板紓甯镐俊鎭�
+        if (tag.exceptionCreated) {
+            // 灏嗘渶鏂扮殑寮傚父鐨刧uid璧嬪�肩粰ex
+            val lastEx = tag.exceptionResult.last()
+            ex.guid = lastEx.guid
+            tag.exceptionResult.removeLast()
+            tag.exceptionResult.add(ex)
+        }
+        // 寮傚父鏈垱寤烘椂锛屾柊寤哄紓甯镐俊鎭�
+        else {
+            tag.exceptionResult.add(ex)
+//            resultList.add(ex)
+            tag.exceptionCreated = true
+        }
+    }
+
 }
\ No newline at end of file

--
Gitblit v1.9.3