riku
2021-11-10 665a2e1098fb52800ac7624d4a32dfeb6ce15151
1. 网格化均值计算逻辑完成
已修改6个文件
已添加3个文件
496 ■■■■■ 文件已修改
src/main/kotlin/com/flightfeather/uav/dataprocess/AverageUtil.kt 6 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/kotlin/com/flightfeather/uav/dataprocess/AvgPair.kt 4 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/kotlin/com/flightfeather/uav/domain/entity/BaseRealTimeData.kt 34 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/kotlin/com/flightfeather/uav/domain/entity/RealTimeDataGridMin.java 375 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/kotlin/com/flightfeather/uav/domain/mapper/RealTimeDataGridMinMapper.kt 8 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/kotlin/com/flightfeather/uav/lightshare/service/impl/RealTimeDataServiceImpl.kt 18 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/generator/generatorConfig.xml 3 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/mapper/RealTimeDataGridMinMapper.xml 39 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/test/kotlin/com/flightfeather/uav/lightshare/service/impl/RealTimeDataServiceImplTest.kt 9 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/kotlin/com/flightfeather/uav/dataprocess/AverageUtil.kt
@@ -6,21 +6,21 @@
 * @param onTag å®šä¹‰æ•°æ®æ ‡ç­¾èŽ·å–å›žè°ƒå‡½æ•°ï¼Œå½“å½“å‰æ•°æ®æ ‡ç­¾ä¸Žä¸Šä¸ªæ•°æ®æ ‡ç­¾ä¸åŒæ—¶ï¼Œå³è®¤ä¸ºä¸Šä¸€ç»„æ•°æ®ä¸ºåŒä¸€ç»„æ•°æ®ï¼Œéœ€æ±‚å‡ºå‡å€¼
 * @param onAvg å®šä¹‰å‡å€¼è®¡ç®—方法
 */
class AverageUtil<T : Any>(var onTag: (d: T) -> String, var onAvg: (list: List<T>) -> T) {
class AverageUtil<T, K>(var onTag: (d: T) -> String, var onAvg: (list: List<T>) -> K) {
    // ç¼“存最新的tag
    private var lastTag: String? = null
    // ä¸´æ—¶æ•°æ®ç¼“å­˜
    private val dataSet = mutableListOf<T>()
    // è½¬æ¢ç»“æžœ
    private val result = mutableListOf<T>()
    private val result = mutableListOf<K>()
    /**
     * å°†æ•°æ®é›†è½¬æ¢ä¸ºå‡å€¼æ•°æ®
     * @param list åŽŸå§‹æ•°æ®
     * @return å‡å€¼æ•°æ®
     */
    fun avg(list: List<T>): List<T> {
    fun avg(list: List<T>): List<K> {
        // åˆå§‹åŒ–所有变量
        clear()
        // è®¡ç®—均值
src/main/kotlin/com/flightfeather/uav/dataprocess/AvgPair.kt
@@ -1,11 +1,13 @@
package com.flightfeather.uav.dataprocess
import kotlin.math.round
data class AvgPair(
    var t: Float, var c: Int
){
    fun avg(): Float = if (c == 0) {
        0f
    } else {
        t / c
        round(t / c * 1000) / 1000
    }
}
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())
        }
    }
}
src/main/kotlin/com/flightfeather/uav/domain/entity/RealTimeDataGridMin.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,375 @@
package com.flightfeather.uav.domain.entity;
import java.math.BigDecimal;
import java.util.Date;
import javax.persistence.*;
@Table(name = "real_time_data_grid_min")
public class RealTimeDataGridMin {
    @Id
    private Integer id;
    @Column(name = "device_code")
    private String deviceCode;
    private BigDecimal latitude;
    private BigDecimal longitude;
    private Float altitude;
    @Column(name = "data_time")
    private Date dataTime;
    @Column(name = "create_time")
    private Date createTime;
    @Column(name = "NO2")
    private Float no2;
    @Column(name = "CO")
    private Float co;
    @Column(name = "H2S")
    private Float h2s;
    @Column(name = "SO2")
    private Float so2;
    @Column(name = "O3")
    private Float o3;
    @Column(name = "PM25")
    private Float pm25;
    @Column(name = "PM10")
    private Float pm10;
    private Float temperature;
    private Float humidity;
    @Column(name = "VOC")
    private Float voc;
    @Column(name = "NOI")
    private Float noi;
    private Float velocity;
    @Column(name = "wind_speed")
    private Float windSpeed;
    @Column(name = "wind_direction")
    private Float windDirection;
    private Float height;
    /**
     * @return id
     */
    public Integer getId() {
        return id;
    }
    /**
     * @param id
     */
    public void setId(Integer id) {
        this.id = id;
    }
    /**
     * @return device_code
     */
    public String getDeviceCode() {
        return deviceCode;
    }
    /**
     * @param deviceCode
     */
    public void setDeviceCode(String deviceCode) {
        this.deviceCode = deviceCode == null ? null : deviceCode.trim();
    }
    /**
     * @return latitude
     */
    public BigDecimal getLatitude() {
        return latitude;
    }
    /**
     * @param latitude
     */
    public void setLatitude(BigDecimal latitude) {
        this.latitude = latitude;
    }
    /**
     * @return longitude
     */
    public BigDecimal getLongitude() {
        return longitude;
    }
    /**
     * @param longitude
     */
    public void setLongitude(BigDecimal longitude) {
        this.longitude = longitude;
    }
    /**
     * @return altitude
     */
    public Float getAltitude() {
        return altitude;
    }
    /**
     * @param altitude
     */
    public void setAltitude(Float altitude) {
        this.altitude = altitude;
    }
    /**
     * @return data_time
     */
    public Date getDataTime() {
        return dataTime;
    }
    /**
     * @param dataTime
     */
    public void setDataTime(Date dataTime) {
        this.dataTime = dataTime;
    }
    /**
     * @return create_time
     */
    public Date getCreateTime() {
        return createTime;
    }
    /**
     * @param createTime
     */
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }
    /**
     * @return NO2
     */
    public Float getNo2() {
        return no2;
    }
    /**
     * @param no2
     */
    public void setNo2(Float no2) {
        this.no2 = no2;
    }
    /**
     * @return CO
     */
    public Float getCo() {
        return co;
    }
    /**
     * @param co
     */
    public void setCo(Float co) {
        this.co = co;
    }
    /**
     * @return H2S
     */
    public Float getH2s() {
        return h2s;
    }
    /**
     * @param h2s
     */
    public void setH2s(Float h2s) {
        this.h2s = h2s;
    }
    /**
     * @return SO2
     */
    public Float getSo2() {
        return so2;
    }
    /**
     * @param so2
     */
    public void setSo2(Float so2) {
        this.so2 = so2;
    }
    /**
     * @return O3
     */
    public Float getO3() {
        return o3;
    }
    /**
     * @param o3
     */
    public void setO3(Float o3) {
        this.o3 = o3;
    }
    /**
     * @return PM25
     */
    public Float getPm25() {
        return pm25;
    }
    /**
     * @param pm25
     */
    public void setPm25(Float pm25) {
        this.pm25 = pm25;
    }
    /**
     * @return PM10
     */
    public Float getPm10() {
        return pm10;
    }
    /**
     * @param pm10
     */
    public void setPm10(Float pm10) {
        this.pm10 = pm10;
    }
    /**
     * @return temperature
     */
    public Float getTemperature() {
        return temperature;
    }
    /**
     * @param temperature
     */
    public void setTemperature(Float temperature) {
        this.temperature = temperature;
    }
    /**
     * @return humidity
     */
    public Float getHumidity() {
        return humidity;
    }
    /**
     * @param humidity
     */
    public void setHumidity(Float humidity) {
        this.humidity = humidity;
    }
    /**
     * @return VOC
     */
    public Float getVoc() {
        return voc;
    }
    /**
     * @param voc
     */
    public void setVoc(Float voc) {
        this.voc = voc;
    }
    /**
     * @return NOI
     */
    public Float getNoi() {
        return noi;
    }
    /**
     * @param noi
     */
    public void setNoi(Float noi) {
        this.noi = noi;
    }
    /**
     * @return velocity
     */
    public Float getVelocity() {
        return velocity;
    }
    /**
     * @param velocity
     */
    public void setVelocity(Float velocity) {
        this.velocity = velocity;
    }
    /**
     * @return wind_speed
     */
    public Float getWindSpeed() {
        return windSpeed;
    }
    /**
     * @param windSpeed
     */
    public void setWindSpeed(Float windSpeed) {
        this.windSpeed = windSpeed;
    }
    /**
     * @return wind_direction
     */
    public Float getWindDirection() {
        return windDirection;
    }
    /**
     * @param windDirection
     */
    public void setWindDirection(Float windDirection) {
        this.windDirection = windDirection;
    }
    /**
     * @return height
     */
    public Float getHeight() {
        return height;
    }
    /**
     * @param height
     */
    public void setHeight(Float height) {
        this.height = height;
    }
}
src/main/kotlin/com/flightfeather/uav/domain/mapper/RealTimeDataGridMinMapper.kt
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,8 @@
package com.flightfeather.uav.domain.mapper
import com.flightfeather.uav.domain.MyMapper
import com.flightfeather.uav.domain.entity.RealTimeDataGridMin
import org.apache.ibatis.annotations.Mapper
@Mapper
interface RealTimeDataGridMinMapper : MyMapper<RealTimeDataGridMin?>
src/main/kotlin/com/flightfeather/uav/lightshare/service/impl/RealTimeDataServiceImpl.kt
@@ -6,10 +6,7 @@
import com.flightfeather.uav.common.utils.GsonUtils
import com.flightfeather.uav.dataprocess.AverageUtil
import com.flightfeather.uav.domain.entity.*
import com.flightfeather.uav.domain.mapper.RealTimeDataGridMapper
import com.flightfeather.uav.domain.mapper.RealTimeDataMapper
import com.flightfeather.uav.domain.mapper.RealTimeDataUavMapper
import com.flightfeather.uav.domain.mapper.RealTimeDataVehicleMapper
import com.flightfeather.uav.domain.mapper.*
import com.flightfeather.uav.lightshare.bean.*
import com.flightfeather.uav.lightshare.service.RealTimeDataService
import com.flightfeather.uav.model.epw.EPWDataPrep
@@ -35,7 +32,8 @@
    private val airDataRepository: AirDataRepository,
    private val realTimeDataVehicleMapper: RealTimeDataVehicleMapper,
    private val realTimeDataUavMapper: RealTimeDataUavMapper,
    private val realTimeDataGridMapper: RealTimeDataGridMapper
    private val realTimeDataGridMapper: RealTimeDataGridMapper,
    private val realTimeDataGridMinMapper: RealTimeDataGridMinMapper
) : RealTimeDataService {
    private var dateFormatter = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
@@ -253,7 +251,7 @@
        var count = 0
        while (total == -1 || page <= total) {
            println("------start------")
            println("------数据预处理start------")
            val res = getOriginData("0d0000000001", "2021-07-05 19:47:01", "2021-11-05 00:00:00", page, 50000)
            res.head?.let {
                total = it.totalPage
@@ -272,20 +270,19 @@
    }
    override fun averageData(): BaseResponse<String> {
        val epwDataPrep = EPWDataPrep()
        var page = 1
        var total = -1
        var count = 0
        val minFormatter = SimpleDateFormat("yyyy-MM-dd HH:mm")
        val averageUtil = AverageUtil<RealTimeDataGrid>({d ->
        val averageUtil = AverageUtil<RealTimeDataGrid, RealTimeDataGridMin>({d ->
            minFormatter.format(d.dataTime)
        },{list ->
            list.avg()
        })
        while (total == -1 || page <= total) {
            println("------start------")
            println("------均值计算start------")
            val p = PageHelper.startPage<RealTimeDataGrid>(page, 50000)
            val res = realTimeDataGridMapper.selectByExample(Example(RealTimeDataGrid::class.java).apply {
@@ -299,7 +296,8 @@
            }
            println("当前页数:$page")
            averageUtil.avg(res).forEach {
                realTimeDataGridMinMapper.insert(it)
                count++
            }
            page++
src/main/resources/generator/generatorConfig.xml
@@ -55,6 +55,7 @@
<!--        <table tableName="real_time_data_uav" domainObjectName="RealTimeDataUav" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>-->
<!--        <table tableName="real_time_data_vehicle" domainObjectName="RealTimeDataVehicle" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>-->
<!--        <table tableName="device_info" domainObjectName="DeviceInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>-->
        <table tableName="factor_calibration" domainObjectName="FactorCalibration" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>
<!--        <table tableName="factor_calibration" domainObjectName="FactorCalibration" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>-->
        <table tableName="real_time_data_grid_min" domainObjectName="RealTimeDataGridMin" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>
    </context>
</generatorConfiguration>
src/main/resources/mapper/RealTimeDataGridMinMapper.xml
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.flightfeather.uav.domain.mapper.RealTimeDataGridMinMapper">
  <resultMap id="BaseResultMap" type="com.flightfeather.uav.domain.entity.RealTimeDataGridMin">
    <!--
      WARNING - @mbg.generated
    -->
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="device_code" jdbcType="VARCHAR" property="deviceCode" />
    <result column="latitude" jdbcType="DECIMAL" property="latitude" />
    <result column="longitude" jdbcType="DECIMAL" property="longitude" />
    <result column="altitude" jdbcType="REAL" property="altitude" />
    <result column="data_time" jdbcType="TIMESTAMP" property="dataTime" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="NO2" jdbcType="REAL" property="no2" />
    <result column="CO" jdbcType="REAL" property="co" />
    <result column="H2S" jdbcType="REAL" property="h2s" />
    <result column="SO2" jdbcType="REAL" property="so2" />
    <result column="O3" jdbcType="REAL" property="o3" />
    <result column="PM25" jdbcType="REAL" property="pm25" />
    <result column="PM10" jdbcType="REAL" property="pm10" />
    <result column="temperature" jdbcType="REAL" property="temperature" />
    <result column="humidity" jdbcType="REAL" property="humidity" />
    <result column="VOC" jdbcType="REAL" property="voc" />
    <result column="NOI" jdbcType="REAL" property="noi" />
    <result column="velocity" jdbcType="REAL" property="velocity" />
    <result column="wind_speed" jdbcType="REAL" property="windSpeed" />
    <result column="wind_direction" jdbcType="REAL" property="windDirection" />
    <result column="height" jdbcType="REAL" property="height" />
  </resultMap>
  <sql id="Base_Column_List">
    <!--
      WARNING - @mbg.generated
    -->
    id, device_code, latitude, longitude, altitude, data_time, create_time, NO2, CO,
    H2S, SO2, O3, PM25, PM10, temperature, humidity, VOC, NOI, velocity, wind_speed,
    wind_direction, height
  </sql>
</mapper>
src/test/kotlin/com/flightfeather/uav/lightshare/service/impl/RealTimeDataServiceImplTest.kt
@@ -18,6 +18,9 @@
import java.io.File
import java.io.FileOutputStream
import java.util.*
import kotlin.math.atan
import kotlin.math.cos
import kotlin.math.sin
@RunWith(SpringRunner::class)
@SpringBootTest
@@ -122,4 +125,10 @@
        val r = realTimeDataService.dataPreprocessing()
        println(r.data)
    }
    @Test
    fun averageData() {
        val r = realTimeDataService.averageData()
        println(r.data)
    }
}