From bf3bf9ff25ac106b556b2427cc382c8fcca63bff Mon Sep 17 00:00:00 2001
From: feiyu02 <risaku@163.com>
Date: 星期二, 02 七月 2024 17:38:33 +0800
Subject: [PATCH] 1. 新增走航报告自动道路识别模块

---
 src/main/kotlin/com/flightfeather/uav/biz/dataanalysis/model/DataAnalysisConfig.kt       |    5 
 src/main/kotlin/com/flightfeather/uav/biz/dataanalysis/BaseExceptionAnalysis.kt          |    8 +
 src/main/kotlin/com/flightfeather/uav/common/net/AMapService.kt                          |   19 +++
 src/main/kotlin/com/flightfeather/uav/domain/entity/BaseRealTimeData.kt                  |    9 +
 src/main/resources/templates/report-underway.ftl                                         |    2 
 src/main/kotlin/com/flightfeather/uav/common/net/SkipCertificateValidation.kt            |  150 +++++++++++++++++++++++++
 src/main/kotlin/com/flightfeather/uav/biz/dataanalysis/ExceptionAnalysisController.kt    |   28 ++++
 src/main/kotlin/com/flightfeather/uav/common/net/HttpMethod.kt                           |   92 +++++++++++++++
 src/main/kotlin/com/flightfeather/uav/lightshare/service/impl/RealTimeDataServiceImpl.kt |    2 
 pom.xml                                                                                  |    6 +
 10 files changed, 309 insertions(+), 12 deletions(-)

diff --git a/pom.xml b/pom.xml
index c6f7fd6..1db1d29 100644
--- a/pom.xml
+++ b/pom.xml
@@ -211,6 +211,12 @@
             <artifactId>jfreechart</artifactId>
             <version>1.5.3</version>
         </dependency>
+
+        <dependency>
+            <groupId>commons-httpclient</groupId>
+            <artifactId>commons-httpclient</artifactId>
+            <version>3.0</version>
+        </dependency>
     </dependencies>
 
     <build>
diff --git a/src/main/kotlin/com/flightfeather/uav/biz/dataanalysis/BaseExceptionAnalysis.kt b/src/main/kotlin/com/flightfeather/uav/biz/dataanalysis/BaseExceptionAnalysis.kt
index 139166f..41323f0 100644
--- a/src/main/kotlin/com/flightfeather/uav/biz/dataanalysis/BaseExceptionAnalysis.kt
+++ b/src/main/kotlin/com/flightfeather/uav/biz/dataanalysis/BaseExceptionAnalysis.kt
@@ -6,6 +6,7 @@
 import com.flightfeather.uav.biz.dataanalysis.model.ExceptionType
 import com.flightfeather.uav.common.utils.DateUtil
 import com.flightfeather.uav.domain.entity.BaseRealTimeData
+import com.flightfeather.uav.domain.entity.avg
 import com.flightfeather.uav.socket.eunm.FactorType
 import java.time.Duration
 
@@ -54,6 +55,11 @@
             startData = start.getByFactorType(factor.main)
             endData = end?.getByFactorType(factor.main) ?: startData
 
+            val avgData = exceptionData.avg()
+            // 姹傚彇姹℃煋鏁版嵁鐨勪腑蹇冨潗鏍�
+            longitude = avgData.longitude
+            latitude = avgData.latitude
+            // 姹傚彇涓绘薄鏌撳洜瀛愮殑鍧囧�煎拰鑼冨洿
             val s = dataSummary(exceptionData, factor.main)
             avg = s.first
             min = s.second
@@ -63,7 +69,7 @@
         }
     }
 
-    fun dataSummary(exceptionData: List<BaseRealTimeData?>, factorType: FactorType): Triple<Float, Float, Float> {
+    private fun dataSummary(exceptionData: List<BaseRealTimeData?>, factorType: FactorType): Triple<Float, Float, Float> {
         var min = -1f
         var max = -1f
         var total = 0f
diff --git a/src/main/kotlin/com/flightfeather/uav/biz/dataanalysis/ExceptionAnalysisController.kt b/src/main/kotlin/com/flightfeather/uav/biz/dataanalysis/ExceptionAnalysisController.kt
index d73f3a7..5080123 100644
--- a/src/main/kotlin/com/flightfeather/uav/biz/dataanalysis/ExceptionAnalysisController.kt
+++ b/src/main/kotlin/com/flightfeather/uav/biz/dataanalysis/ExceptionAnalysisController.kt
@@ -7,6 +7,7 @@
 import com.flightfeather.uav.biz.dataanalysis.model.DataAnalysisConfig
 import com.flightfeather.uav.biz.dataanalysis.model.ExceptionResult
 import com.flightfeather.uav.biz.dataanalysis.model.ExceptionSetting
+import com.flightfeather.uav.common.location.LocationRoadNearby
 import com.flightfeather.uav.domain.entity.Mission
 import com.flightfeather.uav.domain.repository.RealTimeDataRep
 import org.springframework.stereotype.Component
@@ -17,6 +18,7 @@
 @Component
 class ExceptionAnalysisController(
     private val realTimeDataRep: RealTimeDataRep,
+    private val locationRoadNearby: LocationRoadNearby,
 ) {
 
     var running = false
@@ -26,15 +28,15 @@
     private fun initTask(config: DataAnalysisConfig) {
         taskList.clear()
         taskList.apply {
-//            add(ExceptionDataExceed(config))
-//            add(ExceptionValueMutation(config))
+            add(ExceptionDataExceed(config))
+            add(ExceptionValueMutation(config))
             add(ExceptionSlideAverage(config))
         }
     }
 
     fun run(mission: Mission, factorFilter: FactorFilter): List<ExceptionResult> {
         running = true
-        val config = DataAnalysisConfig(mission, ExceptionSetting(), 8, factorFilter)
+        val config = DataAnalysisConfig(mission, ExceptionSetting(), factorFilter)
         initTask(config)
 
         val result = mutableListOf<ExceptionResult>()
@@ -45,8 +47,26 @@
         }
         // 鍚勪釜寮傚父鍒嗘瀽鍒嗗埆缁撴潫
         taskList.forEach { it.onDone() }
-        // 瀛樺偍鍒嗘瀽缁撴灉
+
         taskList.forEach {
+            // 鏌ヨ寮傚父鍛ㄨ竟鍙兘姹℃煋婧�
+            it.resultList.forEach { r->
+                if (r.longitude != null && r.latitude != null) {
+                    val sceneList = locationRoadNearby.searchByRadius(
+                        r.longitude!!.toDouble() to r.latitude!!.toDouble(), config.radius)
+                    if (sceneList.isNotEmpty()) {
+                        val idList = mutableListOf<String>()
+                        val nameList = mutableListOf<String>()
+                        sceneList.forEach { s->
+                            idList.add(s?.guid?:"")
+                            nameList.add(s?.name ?: "")
+                        }
+                        r.relatedSceneId = idList
+                        r.relatedSceneName = nameList
+                    }
+                }
+            }
+            // 瀛樺偍鍒嗘瀽缁撴灉
             result.addAll(it.resultList)
         }
         running = false
diff --git a/src/main/kotlin/com/flightfeather/uav/biz/dataanalysis/model/DataAnalysisConfig.kt b/src/main/kotlin/com/flightfeather/uav/biz/dataanalysis/model/DataAnalysisConfig.kt
index 877df87..bbc7b00 100644
--- a/src/main/kotlin/com/flightfeather/uav/biz/dataanalysis/model/DataAnalysisConfig.kt
+++ b/src/main/kotlin/com/flightfeather/uav/biz/dataanalysis/model/DataAnalysisConfig.kt
@@ -11,8 +11,6 @@
     val mission: Mission,
     // 鏁版嵁寮傚父閰嶇疆
     val exceptionSetting: ExceptionSetting,
-    // 娑夊強鐩戞祴鍥犲瓙鏁伴噺
-    val factorCount: Int,
     // 鍥犲瓙绛涢��
     val factorFilter: FactorFilter,
 ){
@@ -28,4 +26,7 @@
     var changeTrendRate = 1
     // 婊戝姩骞冲潎鍊煎彉鍖栫巼寮傚父杩炵画娆℃暟
     var changeTrendTimes = 3
+
+    // 寮傚父鍛ㄨ竟鍙兘姹℃煋婧愭煡璇㈣寖鍥村崐寰勶紙绫筹級
+    var radius = 500.0
 }
\ No newline at end of file
diff --git a/src/main/kotlin/com/flightfeather/uav/common/net/AMapService.kt b/src/main/kotlin/com/flightfeather/uav/common/net/AMapService.kt
new file mode 100644
index 0000000..f4aa432
--- /dev/null
+++ b/src/main/kotlin/com/flightfeather/uav/common/net/AMapService.kt
@@ -0,0 +1,19 @@
+package com.flightfeather.uav.common.net
+
+/**
+ * 楂樺痉鍦板浘Web鏈嶅姟API
+ */
+object AMapService {
+
+    private val httpMethod = HttpMethod("restapi.amap.com", 443, true)
+    private val KEY = ""
+
+    /**
+     * 鍦扮悊閫嗙紪鐮�
+     */
+    fun reGeo(location:List<Pair<Double, Double>>) {
+        httpMethod.get("v3/geocode/regeo", listOf(
+
+        ))
+    }
+}
\ No newline at end of file
diff --git a/src/main/kotlin/com/flightfeather/uav/common/net/HttpMethod.kt b/src/main/kotlin/com/flightfeather/uav/common/net/HttpMethod.kt
new file mode 100644
index 0000000..350f640
--- /dev/null
+++ b/src/main/kotlin/com/flightfeather/uav/common/net/HttpMethod.kt
@@ -0,0 +1,92 @@
+package com.flightfeather.uav.common.net
+
+import org.apache.commons.httpclient.HttpClient
+import org.apache.commons.httpclient.HttpMethodBase
+import org.apache.commons.httpclient.methods.GetMethod
+import org.apache.commons.httpclient.methods.PostMethod
+import org.apache.commons.httpclient.methods.StringRequestEntity
+import org.apache.commons.httpclient.protocol.Protocol
+import org.slf4j.LoggerFactory
+
+/**
+ * @author riku
+ * Date: 2020/10/14
+ */
+class HttpMethod(
+        private val host: String, private val port: Int, private val isHttps: Boolean = false
+) {
+    private val logger = LoggerFactory.getLogger(HttpMethod::class.java)
+
+    data class MyResponse(
+            val success: Boolean,
+            val m: HttpMethodBase
+    )
+
+    object Head {
+        val TEXT_PLAIN = Pair("Content-Type", "text/plain")
+    }
+
+    private val httpClient = HttpClient()
+
+    init {
+        Protocol.registerProtocol("https", Protocol("https", SkipCertificateValidation.MySecureProtocolSocketFactory(), port))
+        if (isHttps) {
+            httpClient.hostConfiguration.setHost(host, port, Protocol.getProtocol("https"))
+        } else {
+            httpClient.hostConfiguration.setHost(host, port)
+        }
+    }
+
+    fun get(url: String, params: List<Pair<String, String>> = emptyList(), headers: List<Pair<String, String>> = emptyList()): MyResponse {
+        var p: String = ""
+        params.forEach {
+            p += if (p.isBlank()) {
+                it.first + "=" + it.second
+            } else {
+                "&" + it.first + "=" + it.second
+            }
+        }
+        val getMethod = GetMethod("$url?$p")
+        defaultConfig(getMethod)
+        headers.forEach {
+            getMethod.setRequestHeader(it.first, it.second)
+        }
+
+        return when (httpClient.executeMethod(getMethod)) {
+            200 -> MyResponse(true, getMethod)
+            else -> MyResponse(false, getMethod)
+        }
+    }
+
+    fun post(url: String, data: String = "", headers: List<Pair<String, String>> = emptyList()): MyResponse {
+        val postMethod = PostMethod(url)
+        defaultConfig(postMethod)
+        headers.forEach {
+            postMethod.setRequestHeader(it.first, it.second)
+        }
+        if (data.isNotBlank()) {
+            postMethod.requestEntity = StringRequestEntity(data, "application/json", "utf-8")
+        }
+
+        return try {
+            when (httpClient.executeMethod(postMethod)) {
+                200 -> MyResponse(true, postMethod)
+                else -> MyResponse(false, postMethod)
+            }
+        } catch (e: Exception) {
+            logger.error(e.message)
+            MyResponse(false, postMethod)
+        }
+    }
+
+    private fun defaultConfig(method: HttpMethodBase) {
+        method.setRequestHeader("Accept", "*/*");
+        method.setRequestHeader("Connection", "Keep-Alive");
+//        method.setRequestHeader("Accept-Language", "zh-cn,zh;q=0.5");
+        method.setRequestHeader("Accept-Encoding", "gzip,deflate,br");
+//        method.setRequestHeader("Content-Type", "application/json;charset=utf-8")
+        method.setRequestHeader("Content-Type", "application/json")
+//        method.setRequestHeader("Content-Type", "application/json;charset=GBK")
+    }
+
+}
\ No newline at end of file
diff --git a/src/main/kotlin/com/flightfeather/uav/common/net/SkipCertificateValidation.kt b/src/main/kotlin/com/flightfeather/uav/common/net/SkipCertificateValidation.kt
new file mode 100644
index 0000000..8cc7cb1
--- /dev/null
+++ b/src/main/kotlin/com/flightfeather/uav/common/net/SkipCertificateValidation.kt
@@ -0,0 +1,150 @@
+package com.flightfeather.uav.common.net
+
+import org.apache.commons.httpclient.ConnectTimeoutException
+import org.apache.commons.httpclient.HttpClientError
+import org.apache.commons.httpclient.params.HttpConnectionParams
+import org.apache.commons.httpclient.protocol.ControllerThreadSocketFactory
+import org.apache.commons.httpclient.protocol.ProtocolSocketFactory
+import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory
+import java.io.IOException
+import java.net.InetAddress
+import java.net.Socket
+import java.net.UnknownHostException
+import java.security.cert.CertificateException
+import java.security.cert.X509Certificate
+import javax.net.ssl.*
+
+
+/**
+ * 閫氳繃java 杩涜璁块棶鏈夋椂鍊欎細鏈夎瘉涔﹁繘琛屾嫤鎴�
+ */
+object SkipCertificateValidation {
+    /**
+     * 蹇界暐HTTPS璇锋眰鐨凷SL璇佷功锛屽繀椤诲湪openConnection涔嬪墠璋冪敤
+     * @throws Exception
+     */
+    @Throws(Exception::class)
+    fun ignoreSsl() {
+        val hv = HostnameVerifier { urlHostName, session ->
+            println("Warning: URL Host: " + urlHostName + " vs. " + session.peerHost)
+            true
+        }
+        trustAllHttpsCertificates()
+        HttpsURLConnection.setDefaultHostnameVerifier(hv)
+    }
+
+    @Throws(Exception::class)
+    private fun trustAllHttpsCertificates() {
+        val trustAllCerts = arrayOfNulls<TrustManager>(1)
+        val tm: TrustManager = MiTM()
+        trustAllCerts[0] = tm
+        val sc = SSLContext.getInstance("SSL")
+        sc.init(null, trustAllCerts, null)
+        HttpsURLConnection.setDefaultSSLSocketFactory(sc.socketFactory)
+    }
+
+    internal class MiTM : X509TrustManager {
+        override fun getAcceptedIssuers(): Array<X509Certificate>? {
+            return null
+        }
+
+        @Throws(CertificateException::class)
+        override fun checkServerTrusted(certs: Array<X509Certificate>, authType: String) {
+            return
+        }
+
+        @Throws(CertificateException::class)
+        override fun checkClientTrusted(certs: Array<X509Certificate>, authType: String) {
+            return
+        }
+    }
+
+    class MySecureProtocolSocketFactory : ProtocolSocketFactory {
+        //杩欓噷娣诲姞涓�涓睘鎬э紝涓昏鐩殑灏辨槸鏉ヨ幏鍙杝sl璺宠繃楠岃瘉
+        private var sslContext: SSLContext? = null
+
+        /**
+         * 鍒ゆ柇鑾峰彇SSLContext
+         * @return
+         */
+        private val sSLContext: SSLContext?
+            get() {
+                if (sslContext == null) {
+                    sslContext = createEasySSLContext()
+                }
+                return sslContext
+            }
+
+        //鍚庨潰鐨勬柟娉曞熀鏈笂灏辨槸甯﹀叆鐩稿叧鍙傛暟灏卞彲浠ヤ簡
+        /*
+     * (non-Javadoc)
+     *
+     * @see org.apache.commons.httpclient.protocol.ProtocolSocketFactory#createSocket(java.lang.String,
+     *      int, java.net.InetAddress, int)
+     */
+        @Throws(IOException::class, UnknownHostException::class)
+        override fun createSocket(host: String, port: Int, clientHost: InetAddress?, clientPort: Int): Socket {
+            return sSLContext!!.socketFactory.createSocket(host, port, clientHost, clientPort)
+        }
+
+        /*
+     * (non-Javadoc)
+     *
+     * @see org.apache.commons.httpclient.protocol.ProtocolSocketFactory#createSocket(java.lang.String,
+     *      int, java.net.InetAddress, int,
+     *      org.apache.commons.httpclient.params.HttpConnectionParams)
+     */
+        @Throws(IOException::class, UnknownHostException::class, ConnectTimeoutException::class)
+        override fun createSocket(
+            host: String, port: Int, localAddress: InetAddress?, localPort: Int,
+            params: HttpConnectionParams?
+        ): Socket {
+            requireNotNull(params) { "Parameters may not be null" }
+            val timeout: Int = params.connectionTimeout
+            return if (timeout == 0) {
+                createSocket(host, port, localAddress, localPort)
+            } else {
+                ControllerThreadSocketFactory.createSocket(this, host, port, localAddress, localPort, timeout)
+            }
+        }
+
+        /*
+     * (non-Javadoc)
+     *
+     * @see SecureProtocolSocketFactory#createSocket(java.lang.String,int)
+     */
+        @Throws(IOException::class, UnknownHostException::class)
+        override fun createSocket(host: String, port: Int): Socket {
+            return sSLContext!!.socketFactory.createSocket(host, port)
+        }
+
+        /*
+     * (non-Javadoc)
+     *
+     * @see SecureProtocolSocketFactory#createSocket(java.net.Socket,java.lang.String,int,boolean)
+     */
+//        @Throws(IOException::class, UnknownHostException::class)
+//        override fun createSocket(socket: Socket?, host: String?, port: Int, autoClose: Boolean): Socket {
+//            return sSLContext!!.socketFactory.createSocket(socket, host, port, autoClose)
+//        }
+
+        companion object {
+            /**
+             * 杩欎釜鍒涘缓涓�涓幏鍙朣SLContext鐨勬柟娉曪紝瀵煎叆MyX509TrustManager杩涜鍒濆鍖�
+             * @return
+             */
+            private fun createEasySSLContext(): SSLContext {
+                return try {
+                    val context = SSLContext.getInstance("SSL")
+                    context.init(
+                        null, arrayOf(MiTM()),
+                        null
+                    )
+                    context
+                } catch (e: Exception) {
+                    throw HttpClientError(e.toString())
+                }
+            }
+        }
+    }
+}
\ No newline at end of file
diff --git a/src/main/kotlin/com/flightfeather/uav/domain/entity/BaseRealTimeData.kt b/src/main/kotlin/com/flightfeather/uav/domain/entity/BaseRealTimeData.kt
index 8c9f9df..3628094 100644
--- a/src/main/kotlin/com/flightfeather/uav/domain/entity/BaseRealTimeData.kt
+++ b/src/main/kotlin/com/flightfeather/uav/domain/entity/BaseRealTimeData.kt
@@ -1,7 +1,7 @@
 package com.flightfeather.uav.domain.entity
 
-import com.flightfeather.uav.common.utils.DateUtil
 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.socket.bean.AirData
 import com.flightfeather.uav.socket.eunm.FactorType
@@ -11,7 +11,10 @@
 import java.util.*
 import javax.persistence.Column
 import javax.persistence.Id
-import kotlin.math.*
+import kotlin.math.atan
+import kotlin.math.cos
+import kotlin.math.round
+import kotlin.math.sin
 
 /**
  * 瀹炴椂鐩戞祴鏁版嵁鍩虹被
@@ -148,7 +151,7 @@
 
 }
 
-fun List<BaseRealTimeData>.avg(): RealTimeDataGridMin {
+fun List<BaseRealTimeData>.avg(): BaseRealTimeData {
     //椋庡悜閲囩敤鍗曚綅鐭㈤噺娉曟眰鍙栧潎鍊�
     var u = .0//涓滆タ鏂逛綅鍒嗛噺鎬诲拰
     var v = .0//鍗楀寳鏂逛綅鍒嗛噺鎬诲拰
diff --git a/src/main/kotlin/com/flightfeather/uav/lightshare/service/impl/RealTimeDataServiceImpl.kt b/src/main/kotlin/com/flightfeather/uav/lightshare/service/impl/RealTimeDataServiceImpl.kt
index 7e541ff..f90a5e6 100644
--- a/src/main/kotlin/com/flightfeather/uav/lightshare/service/impl/RealTimeDataServiceImpl.kt
+++ b/src/main/kotlin/com/flightfeather/uav/lightshare/service/impl/RealTimeDataServiceImpl.kt
@@ -374,7 +374,7 @@
         val averageUtil = AverageUtil<RealTimeDataGridOpt, RealTimeDataGridMin>({ d ->
             minFormatter.format(d.dataTime)
         }, { list ->
-            list.avg()
+            list.avg() as RealTimeDataGridMin
         })
 
         while (total == -1 || page <= total) {
diff --git a/src/main/resources/templates/report-underway.ftl b/src/main/resources/templates/report-underway.ftl
index 73f9bad..0077dd0 100644
--- a/src/main/resources/templates/report-underway.ftl
+++ b/src/main/resources/templates/report-underway.ftl
@@ -1252,7 +1252,7 @@
                                 <w:szCs w:val="28" />
                                 <w:lang w:val="en-US" w:eastAsia="zh-CN" />
                             </w:rPr>
-                            <w:t>${item_index + 1}. 浠�${item.startTime}鑷�${item.endTime}锛�${item.factorName}鍑虹幇${item.exception}<#if item.relatedSceneName??>锛岀粨鍚堢洃娴嬫暟鎹彉鍖栧拰鍙楃偣鍙嶅悜婧簮锛�<#list item.relatedSceneName as scene>${scene}銆�</#list>绛夊彲鑳藉瓨鍦ㄦ薄鏌撻闄┿��<#else>銆�</#if></w:t>
+                            <w:t>${item_index + 1}. 浠�${item.startTime}鑷�${item.endTime}锛�${item.factorName}鍑虹幇${item.exception}<#if item.relatedSceneName??>锛岀粨鍚堢洃娴嬫暟鎹彉鍖栧拰鍙楃偣鍙嶅悜婧簮锛�<#list item.relatedSceneName as scene><#if (scene_index > 0)>銆�</#if>${scene}</#list>绛夊彲鑳藉瓨鍦ㄦ薄鏌撻闄┿��<#else>銆�</#if></w:t>
                         </w:r>
                     </w:p>
                     <w:p>

--
Gitblit v1.9.3