feiyu02
2025-06-11 f7bdafb7cddd049bbb1bbf265fa006683b4ac693
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package com.flightfeather.uav.biz.dataanalysis.exceptiontype
 
import com.flightfeather.uav.biz.FactorFilter
import com.flightfeather.uav.biz.dataanalysis.BaseExceptionAnalysis
import com.flightfeather.uav.biz.dataanalysis.model.DataAnalysisConfig
import com.flightfeather.uav.biz.dataanalysis.model.ExceptionResult
import com.flightfeather.uav.biz.dataanalysis.model.ExceptionType
import com.flightfeather.uav.domain.entity.BaseRealTimeData
import com.flightfeather.uav.socket.eunm.FactorType
import kotlin.math.abs
 
/**
 * 滑动平均值突变异常
 */
class ExceptionSlideAverage(config: DataAnalysisConfig) :
    BaseExceptionAnalysis<DataAnalysisConfig, ExceptionResult>(config) {
 
    private val historyDataList = mutableListOf<BaseRealTimeData>()
    private val tempDataList = mutableListOf<BaseRealTimeData>()
    private var lastData: BaseRealTimeData? = null
//    private val avgListReverse = mutableListOf<Pair<Double, Boolean>>()
//    private var startData: BaseRealTimeData? = null
//    private var sIndex = 0
//    private var eIndex = -1
//    private var existException = false
 
    inner class Tag {
        // 起始数据下标
        var sIndex = 0
 
        // 起始数据对象
        var startData: BaseRealTimeData? = null
 
        // 末尾数据下标
        var eIndex = -1
 
        // 末尾数据对象
        var endData: BaseRealTimeData? = null
 
        // 数据组均值的集合
        val avgListReverse = mutableListOf<Pair<Double, Boolean>>()
 
        // 异常数据段
        var exceptionData = mutableListOf<BaseRealTimeData>()
 
        // 是否存在异常
        var exceptionExisted = false
 
        fun refreshAfterCheckResult(data: BaseRealTimeData) {
            // 判断并更新起始点位置
//            val len = config.changeTrendGroup - 1 + config.changeTrendTimes + config.changeTrendInterval
            val len = config.changeTrendGroup
            if ((eIndex - sIndex + 1) > len) {
                sIndex = eIndex + 1 - len
                startData = historyDataList[sIndex]
                exceptionData.clear()
                exceptionData.addAll(historyDataList.subList(sIndex, eIndex + 1))
            }
        }
    }
 
    protected val tagMap = mutableMapOf<FactorType, Tag>()
 
    override fun init() {
        super.init()
        historyDataList.clear()
        tempDataList.clear()
        lastData = null
 
        tagMap.clear()
        config.factorFilter.mainList().forEach { f ->
            tagMap[f] = Tag()
        }
//        avgListReverse.clear()
//        startData = null
//        sIndex = 0
//        eIndex = -1
//        existException = false
    }
 
    override fun getExceptionType(): ExceptionType = ExceptionType.TYPE7
 
    override fun onNextData(data: BaseRealTimeData) {
        historyDataList.add(data)
        // 数据加入临时数组
        tempDataList.add(data)
        // 数据量超出设置数量时,去除当前数据组首个数据
        if (tempDataList.size > config.changeTrendGroup) {
            tempDataList.removeAt(0)
        }
        config.factorFilter.selectedList.forEach { s ->
            val f = s.main
            tagMap[f]?.let {
                it.eIndex++
                it.endData = lastData
                if (it.startData == null) {
                    it.startData = data
                }
                // 数据量等于设置数量时,计算当前数据组均值
                if (tempDataList.size == config.changeTrendGroup) {
                    calAvg(f, tempDataList)
                    if (checkSlideAvg(f)) {
                        it.exceptionExisted = true
                        it.exceptionData.add(data)
                    } else {
                        checkResult(s)
                        it.refreshAfterCheckResult(data)
                    }
                }
            }
        }
        lastData = data
    }
 
    override fun onDone() {
        checkResult()
    }
 
    /**
     * 计算一组数据的均值
     */
    private fun calAvg(type: FactorType, list: List<BaseRealTimeData>) {
        var total = .0
        var valid = true
        val count = list.size
        if (count == 0) return
        list.forEach {
            val v = it.getByFactorType(type)
            if (v == null) {
                valid = false
            } else {
                total += v
            }
        }
        val avg = total / count
        tagMap[type]?.avgListReverse?.add(0, Pair(avg, valid))
    }
 
    /**
     * 计算数据组之间的均值差异是否连续超过限定比率
     */
    private fun checkSlideAvg(type: FactorType): Boolean {
        val tag = tagMap[type] ?: return false
        // 计算滑动均值最低要求个数
        val minSize = config.changeTrendTimes + config.changeTrendInterval
        if (tag.avgListReverse.size < minSize) {
            return false
        } else {
            // 滑动均值满足数量时,计算均值之间是否连续超过限定比率
            val rateList = mutableListOf<Pair<Double, Boolean>>()
            for (i in tag.avgListReverse.indices) {
                if (i >= config.changeTrendTimes) break
                val r = calAvgChangeRate(tag.avgListReverse[i], tag.avgListReverse[i + config.changeTrendInterval])
                rateList.add(r)
            }
            for (y in rateList) {
                if (!y.second || y.first < config.changeTrendRate) {
                    return false
                }
            }
            return true
        }
    }
 
    /**
     * 计算滑动均值变化率
     * 求a1相对于a2的变化率
     */
    private fun calAvgChangeRate(a1: Pair<Double, Boolean>, a2: Pair<Double, Boolean>): Pair<Double, Boolean> {
        val valid = a1.second && a2.second
        return if (a2.first == .0) {
            Pair(1.0, valid)
        } else {
            Pair(abs(a1.first - a2.first) / a2.first, valid)
        }
    }
 
    /**
     * 当前数据未出现异常时,或数据循环结束时,判断后续步骤
     */
    private fun checkResult(factor: FactorFilter.SelectedFactor? = null) {
        val tag = tagMap[factor?.main]
        if (factor != null && tag != null) {
            if (tag.exceptionExisted) {
                tag.startData?.let {
                    resultList.add(newResult(it, tag.endData, factor, tag.exceptionData))
                }
                tag.exceptionExisted = false
            }
        } else {
            config.factorFilter.selectedList.forEach { f ->
                val tag1 = tagMap[f.main] ?: return@forEach
                if (tag1.exceptionExisted) {
                    tag1.startData?.let {
                        resultList.add(newResult(it, tag1.endData, f, tag1.exceptionData))
                    }
                    tag1.exceptionExisted = false
                }
            }
        }
 
    }
 
 
 
    fun newResult(
        start: BaseRealTimeData,
        end: BaseRealTimeData?,
        factor: FactorFilter.SelectedFactor,
        exceptionData: List<BaseRealTimeData>,
    ): ExceptionResult {
        val eType = getExceptionType()
        return ExceptionResult(start, end, factor, exceptionData, config.mission.missionCode, eType)
    }
}