feiyu02
2025-06-04 cc2a28ad6b99795d05cd9c923d8f7da27b4509e3
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
package com.flightfeather.uav.biz.dataanalysis
 
import com.flightfeather.uav.biz.FactorFilter
import com.flightfeather.uav.biz.dataanalysis.exceptiontype.ExceptionSlideAverage
import com.flightfeather.uav.biz.dataanalysis.exceptiontype.ExceptionValueMutation
import com.flightfeather.uav.biz.dataanalysis.model.DataAnalysisConfig
import com.flightfeather.uav.biz.dataanalysis.model.ExceptionResult
import com.flightfeather.uav.biz.dataanalysis.model.ExceptionSetting
import com.flightfeather.uav.common.location.LocationRoadNearby
import com.flightfeather.uav.domain.entity.Mission
import com.flightfeather.uav.domain.repository.RealTimeDataRep
import com.flightfeather.uav.domain.repository.SegmentInfoRep
import com.flightfeather.uav.socket.eunm.UWDeviceType
import java.time.LocalDateTime
import java.time.ZoneId
 
/**
 * 数据异常分析控制器
 */
class  ExceptionAnalysisController(
    private val realTimeDataRep: RealTimeDataRep,
    private val locationRoadNearby: LocationRoadNearby,
    private val segmentInfoRep: SegmentInfoRep,
) {
 
    var running = false
 
    private val taskList = mutableListOf<BaseExceptionAnalysis<DataAnalysisConfig, ExceptionResult>>()
 
    private fun initTask(config: DataAnalysisConfig) {
        taskList.clear()
        taskList.apply {
//            add(ExceptionDataExceed(config))
            add(ExceptionValueMutation(config))
            add(ExceptionSlideAverage(config))
        }
    }
 
    fun execute(mission: Mission, factorFilter: FactorFilter): List<ExceptionResult> {
        running = true
        val config = DataAnalysisConfig(mission, ExceptionSetting(), factorFilter)
        initTask(config)
 
        val result = mutableListOf<ExceptionResult>()
        taskList.forEach { it.init() }
        // 轮询数据,计算各个异常
        realTimeDataRep.fetchData(UWDeviceType.fromValue(mission.deviceType),
            mission.deviceCode,
            mission.startTime,
            mission.endTime
        ).forEach { d ->
            taskList.forEach { it.onNextData(d) }
        }
        // 各个异常分析分别结束
        taskList.forEach { it.onDone() }
 
        taskList.forEach {
            it.resultList.forEach { r->
                // 查询异常周边可能污染源
                nearBy(r, config)
                // 查询时段所在路段
//                road(r)
                // 将数据转换为通用格式
                r.dataVoList = r.dataList.map { e-> e.toDataVo() }
            }
            // 存储分析结果
            result.addAll(it.resultList)
        }
        running = false
        return result
    }
 
//    private fun
 
    private fun nearBy(r: ExceptionResult, config: DataAnalysisConfig) {
        if (r.longitude != null && r.latitude != null) {
            val sceneList = locationRoadNearby.searchByRadius(
                r.longitude!!.toDouble() to r.latitude!!.toDouble(), config.radius)
            if (sceneList.isNotEmpty()) {
                val idList = mutableListOf<String>()
                val nameList = mutableListOf<String>()
                sceneList.forEach { s->
                    idList.add(s?.guid?:"")
                    nameList.add(s?.name ?: "")
                }
                r.relatedSceneId = idList
                r.relatedSceneName = nameList
                r.relatedSceneList = sceneList
            }
        }
    }
 
    private fun road(r: ExceptionResult) {
        val sT = LocalDateTime.ofInstant(r.startDate?.toInstant(), ZoneId.systemDefault())
        val eT = LocalDateTime.ofInstant(r.endDate?.toInstant(), ZoneId.systemDefault())
        val segments = segmentInfoRep.findPeriod(r.missionCode, sT, eT)
        var txt = ""
        val size = segments.size
        segments.forEachIndexed { i, s ->
            txt += if (i == 0) {
                if (size == 1) {
                    "在${s?.street}"
                } else {
                    "从${s?.street}"
                }
            } else if (i == size - 1 && i >= 2) {
                ",至${s?.street}"
            } else {
                if (i == 1) {
                    ",经${s?.street}"
                } else {
                    "、${s?.street}"
                }
            }
        }
        r.road = txt
    }
}