Riku
2025-06-02 e731486b50c4ea6e2d28f302df449b4bd0b2be57
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package com.flightfeather.uav.biz.dataanalysis
 
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<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 val tagMap = mutableMapOf<FactorType, T>()
 
    // 起始数据与末尾数据间隔
    open var durationCount = 1
 
    // 末尾数据对象
    protected var lastData: BaseRealTimeData? = null
 
    /**
     * 后置判断:当相邻数据时间不连续时,或者满足自定义条件时,对之前已有的异常进行记录
     */
    open fun afterExcCheck(isContinue: Boolean, tag: T, hasException: Boolean?): Boolean {
        return !isContinue || needCut(tag, hasException)
    }
 
    /**
     * 立即判断:当出现异常时,缓存异常数据的同时,立即对已有异常进行判断是否满足异常结果要求
     */
    open fun immeExcCheck(tag: T): Boolean {
        return false
    }
 
    /**
     * 判断相邻数据是否连续
     */
    open fun isContinuous(d1: BaseRealTimeData?, d2: BaseRealTimeData?): Boolean {
        if (d1 == null || d2 == null) return true
 
        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): MutableMap<FactorType, Boolean>
 
    /**
     * 判断异常出现的连续个数是否满足条件
     * @param tag 异常数据对象
     */
    abstract fun judgeExceptionCount(tag: T): Boolean
 
    /**
     * 异常数据的截取判断
     * @return 默认不需要截取
     */
    open fun needCut(tag: T, hasException: Boolean?): Boolean {
        // 默认判断条件为 当异常不再重复出现时,形成异常结果
        return tag.exceptionExisted && hasException == false
    }
 
    override fun init() {
        super.init()
        lastData = null
        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)
        config.factorFilter.selectedList.forEach { s ->
            val f = s.main
            tagMap[f]?.let {
                it.eIndex++
                // 起始数据
                it.endData = data
                if (it.startData == null) {
                    it.refreshWithNextException(data)
                }
 
                // 对于异常的生成分别执行后置判断、和立即判断
                // 1. 后置判断:当相邻数据时间不连续时,或者满足自定义条件时,对之前已有的异常进行记录,形成异常结果
                if (afterExcCheck(isContinue, it, hasException[f])) {
                    // 数据不连续时或者满足主动截断条件时,记录异常情况
                    recordException(s, it, data)
                }
                // 2. 立即判断:当出现异常时,缓存异常数据的同时,立即对已有异常进行判断是否满足异常结果要求
                else if (hasException[f] == true) {
                    // 有异常出现时,记录异常数据
                    it.addExceptionData(data)
                    // 当立即判断通过时,形成异常结果
                    if (immeExcCheck(it)) {
                        recordException(s, it, data)
                    }
                }
            }
        }
        lastData = data
    }
 
    override fun onDone() {
        checkResult(exceptionStatus = ExceptionStatusType.Ended)
    }
 
    /**
     * 异常结束,记录异常
     * 判断已有的异常数据是否满足异常条件,满足则记录,不满足则略过
     */
    fun recordException(factor: FactorFilter.SelectedFactor, tag: T, data: BaseRealTimeData) {
        checkResult(factor, ExceptionStatusType.Ended)
        tag.refreshWithNextException(data)
    }
 
    /**
     * 检查连续异常结束时,是否符合异常存储条件
     */
    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 {
            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) {
            // 将最新的异常的guid赋值给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
        }
    }
 
}