feiyu02
2025-08-14 b10c22af595bd995e56946bff63b8f2f984b13e8
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
295
package com.flightfeather.uav.domain.repository.impl
 
import com.flightfeather.uav.common.utils.GsonUtils
import com.flightfeather.uav.domain.entity.*
import com.flightfeather.uav.domain.mapper.*
import com.flightfeather.uav.lightshare.bean.DataVo
import com.flightfeather.uav.domain.repository.AirDataRep
import com.flightfeather.uav.socket.bean.AirData
import com.flightfeather.uav.socket.bean.AirDataPackage
import com.flightfeather.uav.socket.eunm.FactorType
import com.flightfeather.uav.socket.eunm.UWDeviceType
import com.github.pagehelper.PageHelper
import com.google.gson.Gson
import org.springframework.stereotype.Repository
import tk.mybatis.mapper.entity.Example
import java.math.BigDecimal
import java.text.SimpleDateFormat
import java.time.LocalDateTime
import java.util.*
 
/**
 * @author riku
 * Date: 2020/6/11
 */
@Repository
class AirDataRepImpl(
    private val realTimeDataMapper: RealTimeDataMapper,
    private val realTimeDataVehicleMapper: RealTimeDataVehicleMapper,
    private val realTimeDataUavMapper: RealTimeDataUavMapper,
    private val realTimeDataGridMapper: RealTimeDataGridMapper,
    private val factorCalibrationMapper: FactorCalibrationMapper
): AirDataRep {
 
    // FIXME: 2021/10/25 临时车载数据,由于无人机部分监测因子数据无效,因此暂时采用车载数据作为填充
    private val tmpVehicleDataList = mutableListOf<BaseRealTimeData>()
 
    // 走航监测校准系数
    private val calibrationMap = mutableMapOf<String, MutableMap<Int, Float>>()
    // 走航监测校准系数更新时间
    private var cUpdateTime = LocalDateTime.now()
    // 走航监测校准系数更新时间间隔(分钟)
    private val cInterval = 5L
 
    override fun saveAirData(dataPackage: AirDataPackage): Int {
        val data = RealTimeData().apply {
            deviceCode = dataPackage.deviceCode
            latitude = BigDecimal.ZERO
            longitude = BigDecimal.ZERO
            altitude
            height
            factors = Gson().toJson(dataPackage.dataUnit)
            dataTime = dataPackage.dataTime
        }
        dataPackage.dataUnit.forEach {
            if (it is AirData) {
                when (it.factorId?.toInt()) {
                    FactorType.LAT.value -> {
                        data.latitude = it.factorData?.toBigDecimal()
                    }
                    FactorType.LNG.value -> {
                        data.longitude = it.factorData?.toBigDecimal()
                    }
                    FactorType.TIME.value -> {
                        it.statusList?.takeIf {l-> l.isNotEmpty() }?.get(0)?.let {d ->
                            data.dataTime = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(d)
                        }
                    }
                }
            }
        }
        realTimeDataMapper.insert(data)
 
        return 0
    }
 
    override fun savePrepData(dataPackage: AirDataPackage): Int {
        var count = 0
        when (UWDeviceType.getType(dataPackage.deviceCode)) {
            UWDeviceType.VEHICLE -> {
                val d = RealTimeDataVehicle()
                dataTransform(dataPackage, d)
                realTimeDataVehicleMapper.insert(d)
                count++
            }
            UWDeviceType.UAV -> {
                val d = RealTimeDataUav()
                dataTransform(dataPackage, d)
                realTimeDataUavMapper.insert(d)
                count++
            }
            UWDeviceType.GRID -> {
                val d = RealTimeDataGrid()
                dataTransform(dataPackage, d)
                realTimeDataGridMapper.insert(d)
                count++
            }
            else -> Unit
        }
        return count
    }
 
    override fun savePrepData(dataList: List<RealTimeData>): Int {
        var count = 0
        dataList.forEach {vo ->
            when (UWDeviceType.getType(vo.deviceCode)) {
                UWDeviceType.VEHICLE -> {
                    val d = RealTimeDataVehicle()
                    dataTransform(vo, d)
                    realTimeDataVehicleMapper.insert(d)
                    count++
                }
                UWDeviceType.UAV -> {
                    val d = RealTimeDataUav()
                    dataTransform(vo, d)
                    realTimeDataUavMapper.insert(d)
                    count++
                }
                UWDeviceType.GRID -> {
                    val d = RealTimeDataGrid()
                    dataTransform(vo, d)
                    realTimeDataGridMapper.insert(d)
                    count++
                }
                else -> Unit
            }
        }
        return count
    }
 
    override fun savePrepData2(dataList: List<DataVo>): List<BaseRealTimeData> {
        val res = mutableListOf<BaseRealTimeData>()
        dataList.forEach {vo ->
            when (UWDeviceType.getType(vo.deviceCode)) {
                UWDeviceType.VEHICLE -> {
                    val d = vo.toBaseRealTimeData(RealTimeDataVehicle::class.java)
                    /***************************************************************************************************/
                    // FIXME: 2021/10/27 车载监测部分因子量级调整
                    calibration(d, UWDeviceType.VEHICLE)
                    /***************************************************************************************************/
                    realTimeDataVehicleMapper.insert(d)
                    res.add(d)
                }
                UWDeviceType.UAV -> {
                    val d = vo.toBaseRealTimeData(RealTimeDataUav::class.java)
                    /***************************************************************************************************/
                    // FIXME: 2021/10/25 无人机部分因子采用车载数据填充,取最新的15分钟的数据
                    if (tmpVehicleDataList.isEmpty()) {
                        val p = PageHelper.startPage<RealTimeDataVehicle>(1, 225)
                        realTimeDataVehicleMapper.selectByExample(Example(RealTimeDataVehicle::class.java).apply {
                            orderBy("dataTime").desc()
                        }).let { tmpVehicleDataList.addAll(it) }
                    }
                    if (tmpVehicleDataList.isNotEmpty()) {
                        tmpVehicleDataList[0].let {
                            d.no2 = it.no2
                            d.co = it.co
                            d.h2s = it.h2s
                            d.so2 = it.so2
                            d.o3 = it.o3
                        }
                        tmpVehicleDataList.removeFirst()
                    }
                    /***************************************************************************************************/
                    realTimeDataUavMapper.insert(d)
                    res.add(d)
                }
                UWDeviceType.GRID -> {
                    val d = vo.toBaseRealTimeData(RealTimeDataGrid::class.java)
                    /**************************************************************************/
                    // FIXME: 2021/11/8 针对历史网格化原始数据,进行临时校准处理
//                    val dTime = LocalDateTime.ofInstant(d.dataTime?.toInstant(), ZoneId.systemDefault())
//                    // CO: 2021.8.28 17:27起 *0.25
//                    val coTime = LocalDateTime.of(2021, 8, 28, 17, 27, 0)
//                    if (dTime.isAfter(coTime)) {
//                        d.co = d.co?.times(0.25f)
//                    }
//
//                    // NO2
//                    d.no2 = d.no2?.times(0.6f)
//
//                    // O3
//                    d.o3 = abs(d.o3?.minus(d.no2?.div(2) ?: 0f) ?: 0f) * 1.5f
//
//                    // SO2: *0.2, 2021.8.29 6:00起 *0.08
//                    val so2Time = LocalDateTime.of(2021, 8, 29, 6, 0, 0)
//                    d.so2 = if (dTime.isAfter(so2Time)) {
//                        d.so2?.times(0.08f)
//                    } else {
//                        d.so2?.times(0.2f)
//                    }
//
//                    // H2S
//                    d.h2s = d.h2s?.let { sqrt(it) * 2 }
                    /**************************************************************************/
                    realTimeDataGridMapper.insert(d)
                    res.add(d)
                }
                UWDeviceType.BOAT -> {
 
                }
                else -> Unit
            }
        }
        return res
    }
 
    private fun dataTransform(vo: RealTimeData, bean: BaseRealTimeData) {
        bean.apply {
            deviceCode = vo.deviceCode
            latitude = vo.latitude
            longitude = vo.longitude
            dataTime = vo.dataTime
            createTime = vo.createTime
            GsonUtils.parserJsonToArrayBeans(vo.factors, AirData::class.java).forEach {
                when (it.factorId?.toInt()) {
                    FactorType.NO2.value -> no2 = it.factorData?.toFloat()
                    FactorType.CO.value -> co = it.factorData?.toFloat()
                    FactorType.H2S.value -> h2s = it.factorData?.toFloat()
                    FactorType.SO2.value -> so2 = it.factorData?.toFloat()
                    FactorType.O3.value -> o3 = it.factorData?.toFloat()
                    FactorType.PM25.value -> pm25 = it.factorData?.toFloat()
                    FactorType.PM10.value -> pm10 = it.factorData?.toFloat()
                    FactorType.TEMPERATURE.value -> temperature = it.factorData?.toFloat()
                    FactorType.HUMIDITY.value -> humidity = it.factorData?.toFloat()
                    FactorType.VOC.value -> voc = it.factorData?.toFloat()
                    FactorType.NOI.value -> noi = it.factorData?.toFloat()
                    FactorType.VELOCITY.value -> velocity = it.factorData?.toFloat()
                    FactorType.WIND_SPEED.value -> windSpeed = it.factorData?.toFloat()
                    FactorType.WIND_DIRECTION.value -> windDirection = it.factorData?.toFloat()
                    FactorType.HEIGHT.value -> height = it.factorData?.toFloat()
                }
            }
 
        }
    }
 
    private fun dataTransform(dataPackage: AirDataPackage, bean: BaseRealTimeData) {
        bean.apply {
            deviceCode = dataPackage.deviceCode
            dataPackage.dataUnit.forEach {
                if (it is AirData) {
                    when (it.factorId?.toInt()) {
                        FactorType.NO2.value -> no2 = it.factorData?.toFloat()
                        FactorType.CO.value -> co = it.factorData?.toFloat()
                        FactorType.H2S.value -> h2s = it.factorData?.toFloat()
                        FactorType.SO2.value -> so2 = it.factorData?.toFloat()
                        FactorType.O3.value -> o3 = it.factorData?.toFloat()
                        FactorType.PM25.value -> pm25 = it.factorData?.toFloat()
                        FactorType.PM10.value -> pm10 = it.factorData?.toFloat()
                        FactorType.TEMPERATURE.value -> temperature = it.factorData?.toFloat()
                        FactorType.HUMIDITY.value -> humidity = it.factorData?.toFloat()
                        FactorType.VOC.value -> voc = it.factorData?.toFloat()
                        FactorType.NOI.value -> noi = it.factorData?.toFloat()
                        FactorType.VELOCITY.value -> velocity = it.factorData?.toFloat()
                        FactorType.WIND_SPEED.value -> windSpeed = it.factorData?.toFloat()
                        FactorType.WIND_DIRECTION.value -> windDirection = it.factorData?.toFloat()
                        FactorType.HEIGHT.value -> height = it.factorData?.toFloat()
 
                        FactorType.LAT.value -> latitude = it.factorData?.toBigDecimal()
                        FactorType.LNG.value -> longitude = it.factorData?.toBigDecimal()
                        FactorType.TIME.value -> it.statusList?.takeIf { l -> l.isNotEmpty() }?.get(0)?.let { d ->
                            dataTime = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(d)
                        }
                    }
                }
            }
        }
    }
 
    private fun calibration(data: BaseRealTimeData, type: UWDeviceType) {
        //1. 校准系数按照一定时间间隔进行刷新
        val now = LocalDateTime.now()
        if (calibrationMap.isEmpty() || now.minusMinutes(cInterval).isAfter(cUpdateTime)) {
            cUpdateTime = now
            calibrationMap[type.value] = mutableMapOf()
            factorCalibrationMapper.selectByExample(Example(FactorCalibration::class.java).apply {
                createCriteria().andEqualTo("deviceType", type.value)
            }).forEach {
                calibrationMap[type.value]?.put(it.factorId, it.factorScale)
            }
        }
        //2. 根据校准系数计算
        calibrationMap[type.value]?.let{
            data.voc = data.voc?.times(it[FactorType.VOC.value] ?: 1f)
            data.co = data.co?.times(it[FactorType.CO.value] ?: 1f)
            data.pm25 = data.pm25?.times(it[FactorType.PM25.value] ?: 1f)
            data.pm10 = data.pm10?.times(it[FactorType.PM10.value] ?: 1f)
 
            data.no2 = data.no2?.times(it[FactorType.NO2.value] ?: 1f)
            data.h2s = data.h2s?.times(it[FactorType.H2S.value] ?: 1f)
            data.so2 = data.so2?.times(it[FactorType.SO2.value] ?: 1f)
            data.o3 = data.o3?.times(it[FactorType.O3.value] ?: 1f)
        }
    }
}