riku
2021-11-10 665a2e1098fb52800ac7624d4a32dfeb6ce15151
src/main/kotlin/com/flightfeather/uav/domain/entity/BaseRealTimeData.kt
@@ -12,8 +12,7 @@
import java.util.*
import javax.persistence.Column
import javax.persistence.Id
import kotlin.math.cos
import kotlin.math.sin
import kotlin.math.*
/**
 * 实时监测数据基类
@@ -111,10 +110,10 @@
    }
}
fun List<RealTimeDataGrid>.avg(): RealTimeDataGrid {
fun List<RealTimeDataGrid>.avg(): RealTimeDataGridMin {
    //风向采用单位矢量法求取均值
    var u = 0f//东西方位分量总和
    var v = 0f//南北方位分量总和
    var u = .0//东西方位分量总和
    var v = .0//南北方位分量总和
    var c = 0//风向数据计数
    //除风向外的其他因子采用算术平均法求取均值
@@ -126,8 +125,9 @@
    forEach {
        //风向
        it.windDirection?.let {w ->
            u += sin(w)
            v += cos(w)
            val r = Math.toRadians(w.toDouble())
            u += sin(r)
            v += cos(r)
            c++
        }
        //其余因子
@@ -235,7 +235,7 @@
        }
    }
    return RealTimeDataGrid().apply {
    return RealTimeDataGridMin().apply {
        val time = LocalDateTime.ofInstant(get(0).dataTime?.toInstant(), ZoneId.systemDefault()).withSecond(0)
        deviceCode = get(0).deviceCode
        dataTime = Date.from(time.atZone(ZoneId.systemDefault()).toInstant())
@@ -257,5 +257,23 @@
        velocity = tmpList[14].avg()
        windSpeed = tmpList[15].avg()
        height = tmpList[16].avg()
        if (c != 0) {
            val avgU = u / c
            val avgV = v / c
            var a = atan(avgU / avgV)
            /**
             * avgU>0;avgV>0: 真实角度处于第一象限,修正值为+0°
             * avgU>0;avgV<0: 真实角度处于第二象限,修正值为+180°
             * avgU<0;avgV<0: 真实角度处于第三象限,修正值为+180°
             * avgU<0;avgV>0: 真实角度处于第四象限,修正值为+360°
             */
            a += if (avgV > 0) {
                if (avgU > 0) 0 else 360
            } else {
                180
            }
            windDirection = round(a.toFloat())
        }
    }
}