feiyu02
21 小时以前 8eb584869b4fd4de0f51c93f2616f12e51df9193
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package com.flightfeather.uav.biz.sourcetrace.model
 
import com.flightfeather.uav.biz.FactorFilter
import com.flightfeather.uav.biz.dataanalysis.model.ExceptionTag
import com.flightfeather.uav.biz.dataanalysis.model.ExceptionType
import com.flightfeather.uav.biz.sourcetrace.config.RTExcWindLevelConfig
import com.flightfeather.uav.common.utils.DateUtil
import com.flightfeather.uav.domain.entity.BaseRealTimeData
import com.flightfeather.uav.domain.entity.avg
import com.flightfeather.uav.lightshare.bean.DataVo
import com.flightfeather.uav.socket.eunm.FactorType
import java.util.Date
import kotlin.math.round
 
/**
 * 污染数据
 * @date 2025/5/27
 * @author feiyu02
 */
class PollutedData() {
    
    companion object {
        // 默认数据采样时间间隔,单位:秒
        const val DEFAULT_PERIOD = 4
    }
 
    /**
     * 异常数据分组情况统计
     */
    inner class ExcGroup{
        constructor(dataIndexList: List<Int>, factorType: FactorType){
            this.dataIndexList = dataIndexList
            this.factorType = factorType
            val first = getFirstDataValue()?.toDouble()
            val last = getLastDataValue()?.toDouble()
            if (first != null && last != null) {
                per = round((last - first) / first * 100) / 100
                rate = round((last - first) / DEFAULT_PERIOD * 100) / 100
            }
        }
        var factorType: FactorType? = null
        /**
         * 异常数据对应历史数据[historyDataList]中的索引值
         */
        var dataIndexList: List<Int>? = null
        // 变化幅度
        var per: Double? = null
        // 变化速率
        var rate: Double? = null
 
        /**
         * 获取异常数据的第一个数据
         * !!!!第一个数据其实是首个异常数据的前一个数据值!!!!
         */
        fun getFirstData(): BaseRealTimeData? {
            return dataIndexList?.firstOrNull()?.let {
                val i = if (it > 0) it - 1 else it
                historyDataList[i].toBaseRealTimeData(BaseRealTimeData::class.java)
            }
        }
        fun getFirstDataValue(): Float? {
            return getFirstData()?.getByFactorType(factorType)
        }
 
        /**
         * 获取异常数据的最后一个数据
         */
        fun getLastData(): BaseRealTimeData? {
            return dataIndexList?.lastOrNull()?.let {
                historyDataList[it].toBaseRealTimeData(BaseRealTimeData::class.java)
            }
        }
        fun getLastDataValue(): Float? {
            return getLastData()?.getByFactorType(factorType)
        }
    }
 
 
    /**
     * 各监测因子异常统计信息
     */
    inner class Statistic(){
        var factorId: Int? = null
        var factorName: String? = null
        var subFactorId: List<Int>? = null
        var subFactorName: List<String>? = null
        var selectedFactor: FactorFilter.SelectedFactor? = null
 
        /**
         * 异常数据对应历史数据[historyDataList]中的索引值
         */
        var dataIndexList: List<Int>? = null
 
        // 因子量级平均变化幅度
        var avgPer: Double? = null
        // 因子量级平均变化速率
        var avgRate: Double? = null
 
        var avg: Double? = null
        var min: Double? = null
        var max: Double? = null
 
        var excGroup: List<ExcGroup>? = null
 
        /**
         * 获取异常数据
         */
        fun getExceptionData(): List<BaseRealTimeData>? {
            return dataIndexList?.map { historyDataList[it].toBaseRealTimeData(BaseRealTimeData::class.java) }
        }
 
        /**
         * 获取异常数据分段情况
         * 将连续的异常数据分为一组
         */
        fun getExceptionDataGroup(): List<List<Int>> {
            val res = mutableListOf<MutableList<Int>>()
            var curGroup = mutableListOf<Int>()
            var lastIndex = -2
            dataIndexList?.forEach {
                if (curGroup.isEmpty()) {
                    curGroup.add(it)
                } else if (it - lastIndex == 1) {
                    curGroup.add(it)
                } else {
                    res.add(curGroup)
                    curGroup = mutableListOf(it)
                }
                lastIndex = it
            }
            if (curGroup.isNotEmpty()) {
                res.add(curGroup)
            }
            return res
        }
    }
 
    constructor(exceptions: List<Pair<FactorFilter.SelectedFactor, ExceptionTag>>, eType: ExceptionType,) : this() {
        // 遍历所有的因子的异常,整合统一的异常结果,具体如下
        var startData: BaseRealTimeData? = null
        var endData: BaseRealTimeData? = null
        var historyData = mutableListOf<BaseRealTimeData>()
        var _times = 0
        exceptions.forEach { e ->
            // 将采样时间最早的数据作为开始数据
            if (startData == null) {
                startData = e.second.startData
            } else {
                if (e.second.startData?.dataTime!! < startData!!.dataTime) {
                    startData = e.second.startData
                }
            }
 
            // 将采样时间最晚的作为结束数据
            if (endData == null) {
                endData = e.second.endData
            } else {
                if (e.second.endData?.dataTime!! > endData!!.dataTime) {
                    endData = e.second.endData
                }
            }
            // 将所有历史数据去重合并
            if (historyData.isEmpty()) {
                historyData = e.second.historyData
            } else {
                e.second.historyData.forEach {
                    if (historyData.find { d -> d.dataTime == it.dataTime } == null) {
                        historyData.add(it)
                    }
                }
            }
 
            _times += e.second.exceptionData.size
        }
        // 按照采样时间升序排列
        historyData.sortBy { it.dataTime }
 
        exception = eType.des
        exceptionType = eType.value
        startTime = startData?.dataTime
        endTime = endData?.dataTime
        windSpeed = historyData.avg().windSpeed?.toDouble()
        times = _times
        historyDataList.addAll(historyData.map { it.toDataVo() })
 
        // 再次整合异常数据,分别计算各因子的异常结果统计
        exceptions.forEach {e ->
            statisticMap[e.first.main] = Statistic().apply {
                factorId = e.first.main.value
                factorName = e.first.main.des
                subFactorId = e.first.subs.map { it.value }
                subFactorName = e.first.subs.map { it.des }
                selectedFactor = e.first
                dataIndexList = e.second.exceptionData.map {
                    historyDataList.indexOfFirst { d ->
                        d.time == DateUtil.instance.dateToString(it.dataTime, DateUtil.DateStyle.YYYY_MM_DD_HH_MM_SS)
                    }
                }
 
                val s = dataSummary(e.second.exceptionData, e.first.main)
                avg = s.first
                min = s.second
                max = s.third
 
                excGroup = getExceptionDataGroup().map { ExcGroup(it, e.first.main) }
                avgPer = excGroup?.mapNotNull { it.per }?.average()
                avgRate = excGroup?.mapNotNull { it.rate }?.average()
            }
        }
    }
 
    var deviceCode: String? = null
    var exception: String? = null
    var exceptionType: Int? = null
    var startTime: Date? = null
    var endTime: Date? = null
    // 风速
    var windSpeed: Double? = null
    // 发生次数
    var times: Int? = null
    var historyDataList = mutableListOf<DataVo>()
    // 异常监测数据,包含单次异常中所有发生了异常的数据值(可能不是时间连续的数据)
//    var dataList: MutableList<BaseRealTimeData> = mutableListOf()
//    var dataVoList: MutableList<DataVo> = mutableListOf()
    var statisticMap = mutableMapOf<FactorType, Statistic>()
 
    /**
     * 获取所有异常因子名称
     */
    fun toFactorNames(): String {
        val factors = statisticMap.entries.map { it.key }.sortedBy { it.value }.joinToString(";") { it.des }
        return factors
    }
 
    fun getExceptionAvgData(): BaseRealTimeData {
        val exceptionDataList = statisticMap.flatMap { it.value.getExceptionData() ?: emptyList() }
        val avgData = exceptionDataList.avg()
        return avgData
    }
    /**
     * 获取异常数据中心坐标(异常数据中经度纬度的平均值)
     */
    fun getExceptionCenter(): Pair<Double, Double>? {
        val avgData = getExceptionAvgData()
        val wgs84Lng = avgData.longitude?.toDouble()
        val wgs84Lat = avgData.latitude?.toDouble()
        return if (wgs84Lng == null || wgs84Lat == null) null else Pair(wgs84Lng, wgs84Lat)
    }
 
    private fun calPer(exceptionData: List<BaseRealTimeData?>, factorType: FactorType): Double? {
        if (exceptionData.size < 2) return null
 
        var total = .0
        for (i in 0 until exceptionData.size - 1) {
            val p = exceptionData[i]?.getByFactorType(factorType) ?: .0f
            val n = exceptionData[i + 1]?.getByFactorType(factorType) ?: .0f
            total += (n - p) / p
        }
        return total / (exceptionData.size - 1)
    }
 
    private fun calRate(exceptionData: List<BaseRealTimeData?>, factorType: FactorType): Double? {
        if (exceptionData.size < 2) return null
 
        var total = .0
        for (i in 0 until exceptionData.size - 1) {
            val p = exceptionData[i]?.getByFactorType(factorType) ?: .0f
            val n = exceptionData[i + 1]?.getByFactorType(factorType) ?: .0f
            total += (n - p) / 4
        }
        return total / (exceptionData.size - 1)
    }
 
    private fun dataSummary(exceptionData: List<BaseRealTimeData?>, factorType: FactorType): Triple<Double, Double,
            Double> {
        var min = -1.0
        var max = -1.0
        var total = .0
        var count = 0
        exceptionData.forEach {
            val value = it?.getByFactorType(factorType)?.toDouble() ?: return@forEach
            if (min == -1.0 || min > value) {
                min = round(value * 1000) / 1000
            }
            if (max == -1.0 || max < value) {
                max = round(value * 1000) / 1000
            }
            total += value
            count++
        }
        val avg = if (count == 0) .0 else round(total / count * 1000) / 1000
        return Triple(avg, min, max)
    }
}