feiyu02
2025-07-16 8fc27dba6719041402e3e3c099e2f3e01d9d52c7
src/main/kotlin/com/flightfeather/uav/biz/dataanalysis/BaseExceptionContinuous.kt
@@ -5,13 +5,14 @@
import com.flightfeather.uav.domain.entity.BaseRealTimeData
import com.flightfeather.uav.lightshare.eunm.ExceptionStatusType
import com.flightfeather.uav.socket.eunm.FactorType
import org.springframework.beans.BeanUtils
import java.time.Duration
/**
 * 连续类型的异常分析基类,适用于当前数据与相邻数据之间有关联关系的情况
 */
abstract class BaseExceptionContinuous<T : ExceptionTag, V : BaseAnalysisConfig, Y : BaseExceptionResult>(
    config: V, private val tagClz: Class<T>
    config: V, private val tagClz: Class<T>,
) : BaseExceptionAnalysis<V, Y>(config) {
    companion object {
@@ -27,11 +28,18 @@
    // 末尾数据对象
    protected var lastData: BaseRealTimeData? = null
    // 最新的一组异常,根据设定参数,将相关联的因子产生的异常合并
    protected val latestExceptionResult = mutableListOf<BaseExceptionResult>()
    // 最新的一组异常,记录单因子异常
    protected val latestExceptions = mutableListOf<Pair<FactorFilter.SelectedFactor, T>>()
    // 最新的一组合并异常
    protected val latestCombinedResult = mutableListOf<List<BaseExceptionResult>>()
    /**
     * 最新的一组合并异常,根据配置参数从[latestExceptions]单因子异常中,合并异常
     */
    protected val latestCombinedExc = mutableListOf<List<Pair<FactorFilter.SelectedFactor, T>>>()
    /**
     * 异常结果
     */
    protected val result = mutableListOf<Y>()
    /**
     * 立即判断:当出现异常时,缓存异常数据的同时,立即对已有异常进行判断是否满足异常结果要求
@@ -148,6 +156,7 @@
        lastData = data
        mergeExceptionResult()
        clearExceptions(data)
    }
    override fun onDone() {
@@ -160,7 +169,7 @@
     */
    fun recordException(factor: FactorFilter.SelectedFactor, tag: T, data: BaseRealTimeData) {
        checkResult(factor, ExceptionStatusType.Ended)
        tag.refreshWithNextException(data)
//        tag.refreshWithNextException(data)
    }
    /**
@@ -168,7 +177,7 @@
     */
    open fun checkResult(
        factor: FactorFilter.SelectedFactor? = null,
        exceptionStatus: ExceptionStatusType = ExceptionStatusType.InProgress
        exceptionStatus: ExceptionStatusType = ExceptionStatusType.InProgress,
    ) {
        val tag = tagMap[factor?.main]
        if (factor != null && tag != null) {
@@ -191,23 +200,24 @@
    open fun onNewException(tag: T, factor: FactorFilter.SelectedFactor, exceptionStatus: ExceptionStatusType) {
        if (tag.startData == null) return
//        val ex = newResult(tag.startData!!, tag.endData, factor, tag.exceptionData)
        val ex = newResult(tag, factor)
            .apply { status = exceptionStatus.value }
        // 异常已创建时,更新异常信息
        if (tag.exceptionCreated) {
            // 将最新的异常的guid赋值给ex
            val lastEx = tag.exceptionResult.last()
            ex.guid = lastEx.guid
            tag.exceptionResult.removeLast()
            tag.exceptionResult.add(ex)
        }
        // 异常未创建时,新建异常信息
        else {
            tag.exceptionResult.add(ex)
            tag.exceptionCreated = true
        }
        latestExceptionResult.add(ex)
//        val ex = newResult(tag, factor)
//            .apply { status = exceptionStatus.value }
//        // 异常已创建时,更新异常信息
//        if (tag.exceptionCreated) {
//            // 将最新的异常的guid赋值给ex
//            val lastEx = tag.exceptionResult.last()
//            ex.guid = lastEx.guid
//            tag.exceptionResult.removeLast()
//            tag.exceptionResult.add(ex)
//        }
//        // 异常未创建时,新建异常信息
//        else {
//            tag.exceptionResult.add(ex)
//            tag.exceptionCreated = true
//        }
//        val tagClone = tagClz.newInstance()
//        BeanUtils.copyProperties(tag, tagClone)
        latestExceptions.add(factor to tag)
    }
    /**
@@ -215,13 +225,13 @@
     */
    open fun mergeExceptionResult() {
        // 遍历所有的因子组合
        config.combination?.forEach {c ->
            val res = mutableListOf<BaseExceptionResult>()
        config.factorFilter.combination.forEach { c ->
            val res = mutableListOf<Pair<FactorFilter.SelectedFactor, T>>()
            var exist = true
            // 查看组合内的所有因子是否都同时出现异常
            c.forEach { f->
                val r = latestExceptionResult.find { e->
                    e.factorId == f.value
                val r = latestExceptions.find { e ->
                    e.first.main == f
                }
                if (r != null) {
                    res.add(r)
@@ -233,12 +243,33 @@
            if (exist) {
                // 将合并异常从单个异常集合中去除
                res.forEach { r->
                    latestExceptionResult.removeIf { e-> e.factorId == r.factorId }
                    latestExceptions.removeIf { e -> e.first.main == r.first.main }
                }
                // 将合并异常存储
                latestCombinedResult.add(res)
                latestCombinedExc.add(res)
            }
        }
        // 存储异常结果
        latestExceptions.forEach {
            result.add(newResult(listOf(it)))
        }
        latestCombinedExc.forEach {
            result.add(newResult(it))
        }
    }
    private fun clearExceptions(data: BaseRealTimeData) {
        latestExceptions.forEach {
            it.second.refreshWithNextException(data)
        }
        latestExceptions.clear()
        latestCombinedExc.forEach {
            it.forEach { e ->
                e.second.refreshWithNextException(data)
            }
        }
        latestCombinedExc.clear()
        result.clear()
    }
    /**
@@ -246,4 +277,6 @@
     */
    abstract fun newResult(tag:T, factor: FactorFilter.SelectedFactor): Y
    abstract fun newResult(exceptions: List<Pair<FactorFilter.SelectedFactor, ExceptionTag>>): Y
}