src/main/kotlin/com/flightfeather/uav/domain/entity/BaseRealTimeData.kt
@@ -3,12 +3,14 @@
import com.flightfeather.uav.biz.dataprocess.AvgPair
import com.flightfeather.uav.common.utils.DateUtil
import com.flightfeather.uav.lightshare.bean.DataVo
import com.flightfeather.uav.lightshare.bean.FactorStatistics
import com.flightfeather.uav.socket.bean.AirData
import com.flightfeather.uav.socket.eunm.FactorType
import java.io.Serializable
import java.math.BigDecimal
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.temporal.ChronoUnit
import java.util.*
import javax.persistence.Column
import javax.persistence.GeneratedValue
@@ -146,7 +148,7 @@
}
fun List<BaseRealTimeData>.avg(): BaseRealTimeData {
fun List<BaseRealTimeData>.avg(onEach: (BaseRealTimeData) -> Unit = {  }): BaseRealTimeData {
    if (isEmpty()) {
        return BaseRealTimeData()
    }
@@ -162,8 +164,9 @@
    }
    forEach {
        onEach(it)
        //风向
        it.windDirection?.let {w ->
        it.windDirection?.let { w ->
            val r = Math.toRadians(w.toDouble())
            u += sin(r)
            v += cos(r)
@@ -325,4 +328,89 @@
            windDirection = round(a.toFloat())
        }
    }
}
/**
 * 计算实时监测数据列表的统计信息
 * 为每种环境因子计算最小值、最大值和平均值
 * @param granularity 数据颗粒度,可选值为SECOND, MINUTE, HOUR, 默认MINUTE
 * @return 包含各环境因子统计信息的FactorStatistics列表
 *         每个FactorStatistics对象包含因子类型、最小值、最大值和平均值
 */
fun List<BaseRealTimeData>.calDataStatistics(granularity: String): List<FactorStatistics> {
    // 检查颗粒度是否有效
    if (granularity !in listOf("SECOND", "MINUTE", "HOUR")) {
        throw IllegalArgumentException("无效的颗粒度参数,可选值为SECOND, MINUTE, HOUR")
    }
    val groupedData = when (granularity) {
        "SECOND" -> this
        "MINUTE" -> groupBy { it.dataTime?.toInstant()?.truncatedTo(ChronoUnit.MINUTES) }.mapValues {
            it.value.avg().apply {
                dataTime = Date.from(it.key)
                createTime = dataTime
            }
        }.values.toList()
        "HOUR" -> groupBy { it.dataTime?.toInstant()?.truncatedTo(ChronoUnit.HOURS) }.mapValues {
            it.value.avg().apply {
                dataTime = Date.from(it.key)
                createTime = dataTime
            }
        }.values.toList()
        else -> throw IllegalArgumentException("无效的颗粒度参数,可选值为SECOND, MINUTE, HOUR")
    }
    // 初始化各环境因子的统计对象列表
    val statistics = mutableListOf<FactorStatistics>()
    listOf(
        FactorType.NO2,
        FactorType.CO,
        FactorType.H2S,
        FactorType.SO2,
        FactorType.O3,
        FactorType.PM25,
        FactorType.PM10,
        FactorType.VOC,
        FactorType.NOI,
        FactorType.VELOCITY,
        FactorType.WIND_SPEED,
        FactorType.HEIGHT,
        FactorType.NO
    ).forEach { statistics.add(FactorStatistics(it)) }
    // 计算平均值并同时更新各因子的最小值和最大值
    val avgData = groupedData.avg { item ->
        // 更新每个因子的最小和最大值
        statistics[0].updateMinAndMaxValue(item.no2)
        statistics[1].updateMinAndMaxValue(item.co)
        statistics[2].updateMinAndMaxValue(item.h2s)
        statistics[3].updateMinAndMaxValue(item.so2)
        statistics[4].updateMinAndMaxValue(item.o3)
        statistics[5].updateMinAndMaxValue(item.pm25)
        statistics[6].updateMinAndMaxValue(item.pm10)
        statistics[7].updateMinAndMaxValue(item.voc)
        statistics[8].updateMinAndMaxValue(item.noi)
        statistics[9].updateMinAndMaxValue(item.velocity)
        statistics[10].updateMinAndMaxValue(item.windSpeed)
        statistics[11].updateMinAndMaxValue(item.height)
        statistics[12].updateMinAndMaxValue(item.no)
    }
    // 将计算得到的平均值设置到对应的统计对象中
    statistics[0].avgValue = avgData.no2 ?: 0f
    statistics[1].avgValue = avgData.co ?: 0f
    statistics[2].avgValue = avgData.h2s ?: 0f
    statistics[3].avgValue = avgData.so2 ?: 0f
    statistics[4].avgValue = avgData.o3 ?: 0f
    statistics[5].avgValue = avgData.pm25 ?: 0f
    statistics[6].avgValue = avgData.pm10 ?: 0f
    statistics[7].avgValue = avgData.voc ?: 0f
    statistics[8].avgValue = avgData.noi ?: 0f
    statistics[9].avgValue = avgData.velocity ?: 0f
    statistics[10].avgValue = avgData.windSpeed ?: 0f
    statistics[11].avgValue = avgData.height ?: 0f
    statistics[12].avgValue = avgData.no ?: 0f
    return statistics
}