From 01eae19a4870033e879a3faa6749eece92926cab Mon Sep 17 00:00:00 2001
From: feiyu02 <risaku@163.com>
Date: 星期五, 26 一月 2024 17:10:55 +0800
Subject: [PATCH] 1. 新增多项yml配置文件 2. 新增导入静安监测数据功能模块(未完成)

---
 src/main/kotlin/com/flightfeather/uav/model/epw/EPWDataPrep.kt |  129 +++++++++++++++++++++++++++++++++++++++----
 1 files changed, 117 insertions(+), 12 deletions(-)

diff --git a/src/main/kotlin/com/flightfeather/uav/model/epw/EPWDataPrep.kt b/src/main/kotlin/com/flightfeather/uav/model/epw/EPWDataPrep.kt
index ed0a404..2af8a49 100644
--- a/src/main/kotlin/com/flightfeather/uav/model/epw/EPWDataPrep.kt
+++ b/src/main/kotlin/com/flightfeather/uav/model/epw/EPWDataPrep.kt
@@ -1,14 +1,20 @@
 package com.flightfeather.uav.model.epw
 
+import com.flightfeather.uav.common.utils.DateUtil
+import com.flightfeather.uav.common.utils.GsonUtils
 import com.flightfeather.uav.lightshare.bean.CompanySOP
 import com.flightfeather.uav.lightshare.bean.DataVo
 import com.flightfeather.uav.model.BaseDataPrep
+import com.flightfeather.uav.model.BaseSOP
+import com.flightfeather.uav.socket.bean.AirData
+import com.flightfeather.uav.socket.bean.AirDataPackage
 import com.flightfeather.uav.socket.eunm.FactorType
 import kotlin.math.max
 import kotlin.math.min
+import kotlin.math.round
 import kotlin.math.sqrt
 
-class EPWDataPrep : BaseDataPrep<DataVo, CompanySOP>() {
+class EPWDataPrep : BaseDataPrep<DataVo, BaseSOP>() {
 
     // 鍚戝墠妫�绱㈢殑鏁版嵁璁板綍鏁�
     private val ncal = 15
@@ -21,6 +27,8 @@
 //        emptyList<String>()
         WeightType.prep
 
+    private val lastData = mutableListOf<DataVo>()
+
     override fun mDataPrep(mDataList: List<DataVo>): List<DataVo> {
         mDataList.forEach {
             it.values?.forEach v@{a ->
@@ -28,7 +36,7 @@
 
                 val range = FactorType.getRange(a.factorName) ?: return@v
                 // 鍒ゆ柇鏁版嵁鏄惁鍦ㄥ悎鐞嗚寖鍥村唴
-                if (a.factorData ?: 0.0 < range.first || a.factorData ?: 0.0 > range.second) {
+                if (a.factorData != null && (a.factorData!! < range.first || a.factorData!! > range.second)) {
                     a.factorData = null
                 }
             }
@@ -40,6 +48,9 @@
 //        }
 
         var i = ncal
+        if (lastData.isNotEmpty()) {
+            i = 0
+        }
         while (i < mDataList.size) {
             for (y in mDataList[i].values?.indices ?: 0..0) {
                 val it = mDataList[i].values?.get(y) ?: continue
@@ -49,9 +60,16 @@
                 it.factorData ?: continue
 
                 if (it.factorData!! > vMax) {
-                    val list = mDataList.subList(i - ncal, i)
+                    val lastDataIndex = i
+                    val thisIndex = if (i-ncal<0) 0 else i - ncal
+                    val list = mutableListOf<DataVo>()
+                    if (lastDataIndex < lastData.size) {
+                        list.addAll(lastData.subList(lastDataIndex, lastData.lastIndex + 1))
+                    }
+                    list.addAll(mDataList.subList(thisIndex, i))
+
                     // 鍘婚櫎鏃犳晥鍊肩殑骞冲潎
-                    val avg = average(list, it.factorName)
+                    val avg = average(list, it.factorName) ?: continue
                     // 鍘婚櫎鏃犳晥鍊肩殑鏍囧噯宸�
                     val std = standardDeviation(avg.first, list, it.factorName)
                     // 鍚堢悊鏈�澶у��
@@ -73,11 +91,91 @@
             i++
         }
 
+        lastData.clear()
+        val s = if ((mDataList.lastIndex - ncal + 1) < 0) 0 else mDataList.lastIndex - ncal + 1
+        mDataList.subList(s, mDataList.lastIndex + 1).forEach {
+            lastData.add(it.copy())
+        }
+
         return mDataList
     }
 
-    override fun sopPrep(sopList: List<CompanySOP>): List<CompanySOP> {
+    override fun sopPrep(sopList: List<BaseSOP>): List<BaseSOP> {
         return sopList
+    }
+
+    fun mDataPrep2(dataPackage: AirDataPackage): List<DataVo> {
+        val vo = dataPackage.toDataVo()
+        return mDataPrep2(listOf(vo))
+    }
+
+    fun mDataPrep2(mDataList: List<DataVo>): List<DataVo> {
+        var i = ncal
+        if (lastData.isNotEmpty()) {
+            i = 0
+        }
+        while (i < mDataList.size) {
+            for (y in mDataList[i].values?.indices ?: 0..0) {
+                val it = mDataList[i].values?.get(y) ?: continue
+
+                if (!calTypes.contains(it.factorName)) continue
+                val vMax = FactorType.getVMax(it.factorName) ?: continue
+//                it.factorData ?: continue
+
+                if (it.factorData!! > vMax) {
+                    val lastDataIndex = i
+                    val thisIndex = if (i-ncal<0) 0 else i - ncal
+                    val list = mutableListOf<DataVo>()
+                    if (lastDataIndex < lastData.size) {
+                        list.addAll(lastData.subList(lastDataIndex, lastData.lastIndex + 1))
+                    }
+                    list.addAll(mDataList.subList(thisIndex, i))
+
+                    // 鍘婚櫎鏃犳晥鍊肩殑骞冲潎锛屽綋鎵�鏈夋暟鎹兘鏄棤鏁堝�兼椂锛屾殏涓嶅仛澶勭悊
+                    average(list, it.factorName)?.let { avg ->
+                        // 鍘婚櫎鏃犳晥鍊肩殑鏍囧噯宸�
+                        val std = standardDeviation(avg.first, list, it.factorName)
+                        // 鍚堢悊鏈�澶у��
+                        val max = max(avg.first + std * nstd, avg.first + avg.first * xratio)
+                        // 鍚堢悊鏈�灏忓��
+                        val min = min(avg.first - std * nstd, avg.first / (1 + xratio))
+
+                        // 鏁版嵁涓嶅浜庡悎鐞嗚寖鍥村苟涓旀湁鏁堜釜鏁拌揪鏍囨椂锛岄噰鐢ㄨ绠楁墍寰楀潎鍊间唬鏇垮師濮嬪��
+                        if (avg.second > max(ncal / 5, 2)
+                            && (it.factorData!! < min || it.factorData!! > max)
+                        ) {
+                            // 鍘熷鏁版嵁
+//                        it.factorData = null
+                            it.factorData = avg.first
+                        }
+                    }
+                }
+            }
+
+            i++
+        }
+
+        // 灏嗘柊鏁版嵁鐨勮嚦澶氭渶鍚�15涓繚瀛樹笅鏉ワ紙宸茬粡杩囬澶勭悊锛夛紝鐢ㄤ簬涓嬩竴娆$殑鍒ゆ柇
+        val newList = mutableListOf<DataVo>()
+        val s = if ((mDataList.lastIndex - ncal + 1) < 0) 0 else mDataList.lastIndex - ncal + 1
+        mDataList.subList(s, mDataList.lastIndex + 1).forEach {
+            newList.add(it.copy())
+        }
+        // 褰撴柊鏁版嵁涓庢棫鏁版嵁閲囨牱鏃堕棿宸秴杩�1鍒嗛挓鏃讹紝璁や负涓ょ粍鏁版嵁宸叉棤鍏宠仈鎬э紝娓呯┖鏃ф暟鎹�
+        if (lastData.isNotEmpty() && newList.isNotEmpty()) {
+            val lastTime = DateUtil.instance.StringToDate(lastData.last().time)
+            val thisTime = DateUtil.instance.StringToDate(newList.first().time)
+            if ((thisTime?.time?.minus(lastTime?.time ?: 0) ?: 0) >= (60 * 1000)) {
+                lastData.clear()
+            }
+        }
+        lastData.addAll(newList)
+        // 纭繚淇濆瓨鐨勬暟鎹渶澶氬彧鏈夋渶鏂扮殑15涓�
+        while (lastData.size > ncal) {
+            lastData.removeAt(0)
+        }
+
+        return mDataList
     }
 
     /**
@@ -85,15 +183,17 @@
      * @param list 鐩戞祴鏁版嵁
      * @return 鍧囧�煎拰鏈夋晥鏁版嵁涓暟
      */
-    private fun average(list: List<DataVo>, factorName:String?): Pair<Double, Int> {
+    private fun average(list: List<DataVo>, factorName:String?): Pair<Double, Int>? {
         var t = 0.0
         var c = 0
         list.forEach {
             for (i in it.values?.indices ?: 0..0) {
                 val f = it.values?.get(i)
                 if (f?.factorName == factorName) {
-                    if (f?.factorData != null) {
-                        t += f.factorData!!
+                    val range = FactorType.getRange(f?.factorName) ?: continue
+                    //鍒ゆ柇鏁版嵁鏄惁鍦ㄥ悎鐞嗚寖鍥村唴
+                    if (f?.factorData ?: 0.0 in range.first..range.second) {
+                        t += f?.factorData!!
                         c++
                     }
                     break
@@ -104,10 +204,14 @@
         val avg = if (c == 0) {
             0.0
         } else {
-            t / c
+            round(t / c * 1000) / 1000
         }
 
-        return Pair(avg, c)
+        return if (c == 0) {
+            null
+        } else {
+            Pair(avg, c)
+        }
     }
 
     /**
@@ -120,8 +224,9 @@
             for (i in it.values?.indices ?: 0..0) {
                 val f = it.values?.get(i)
                 if (f?.factorName == factorName) {
-                    if (f?.factorData != null) {
-                        t += (f.factorData!! - avg) * (f.factorData!! - avg)
+                    val range = FactorType.getRange(f?.factorName) ?: continue
+                    if ((f?.factorData ?: 0.0) in range.first..range.second) {
+                        t += (f?.factorData!! - avg) * (f.factorData!! - avg)
                         c++
                     }
                     break

--
Gitblit v1.9.3