From 7a3654aaebf1d75303a90f3dc574881b0199779c Mon Sep 17 00:00:00 2001
From: riku <risaku@163.com>
Date: 星期一, 01 三月 2021 14:27:04 +0800
Subject: [PATCH] 1. 更新数据查询接口

---
 src/main/kotlin/com/flightfeather/uav/lightshare/web/RealTimeDataController.kt           |   19 +++
 src/main/kotlin/com/flightfeather/uav/socket/bean/DataUnit.kt                            |    2 
 src/main/kotlin/com/flightfeather/uav/lightshare/service/RealTimeDataService.kt          |    9 +
 src/main/kotlin/com/flightfeather/uav/socket/ServerHandler.kt                            |   15 ++
 src/main/kotlin/com/flightfeather/uav/lightshare/bean/DataVo.kt                          |   22 ++++
 src/main/kotlin/com/flightfeather/uav/common/utils/FileUtil.kt                           |    2 
 src/main/kotlin/com/flightfeather/uav/common/config/CorsConfig.kt                        |   29 +++++
 src/main/kotlin/com/flightfeather/uav/lightshare/bean/BaseResponse.kt                    |   34 ++++++
 /dev/null                                                                                |    9 -
 src/test/kotlin/com/flightfeather/uav/Test.kt                                            |   23 ++++
 src/main/kotlin/com/flightfeather/uav/lightshare/service/impl/RealTimeDataServiceImpl.kt |   40 ++++++++
 src/main/kotlin/com/flightfeather/uav/socket/MessageManager.kt                           |    6 
 src/main/kotlin/com/flightfeather/uav/common/utils/GsonUtils.kt                          |   85 +++++++++++++++++
 src/main/resources/application.yml                                                       |    5 +
 14 files changed, 283 insertions(+), 17 deletions(-)

diff --git a/src/main/kotlin/com/flightfeather/uav/common/config/CorsConfig.kt b/src/main/kotlin/com/flightfeather/uav/common/config/CorsConfig.kt
new file mode 100644
index 0000000..746592c
--- /dev/null
+++ b/src/main/kotlin/com/flightfeather/uav/common/config/CorsConfig.kt
@@ -0,0 +1,29 @@
+package com.flightfeather.uav.common.config
+
+import org.springframework.context.annotation.Bean
+import org.springframework.context.annotation.Configuration
+import org.springframework.web.cors.CorsConfiguration
+import org.springframework.web.cors.UrlBasedCorsConfigurationSource
+import org.springframework.web.filter.CorsFilter
+
+@Configuration
+class CorsConfig {
+
+    private fun buildConfig(): CorsConfiguration {
+        return CorsConfiguration().apply {
+            addAllowedOrigin("*")
+            addAllowedHeader("*")
+            addAllowedMethod("*")
+            allowCredentials = true
+        }
+    }
+
+    @Bean
+    fun corsFilter(): CorsFilter {
+        val source = UrlBasedCorsConfigurationSource().apply {
+            registerCorsConfiguration("/**", buildConfig())
+        }
+
+        return CorsFilter(source)
+    }
+}
\ No newline at end of file
diff --git a/src/main/kotlin/com/flightfeather/uav/common/utils/FileUtil.kt b/src/main/kotlin/com/flightfeather/uav/common/utils/FileUtil.kt
index f290f76..f825d2d 100644
--- a/src/main/kotlin/com/flightfeather/uav/common/utils/FileUtil.kt
+++ b/src/main/kotlin/com/flightfeather/uav/common/utils/FileUtil.kt
@@ -13,7 +13,7 @@
 class FileUtil {
 
     private var file: File
-    private var basePath:String = "${File.separator}ObdData${File.separator}"
+    private var basePath:String = "${File.separator}UAVData${File.separator}"
     private var closeThread: Thread? = null
     private var fw: FileWriter? = null
     private var bw: BufferedWriter? = null
diff --git a/src/main/kotlin/com/flightfeather/uav/common/utils/GsonUtils.kt b/src/main/kotlin/com/flightfeather/uav/common/utils/GsonUtils.kt
new file mode 100644
index 0000000..387be46
--- /dev/null
+++ b/src/main/kotlin/com/flightfeather/uav/common/utils/GsonUtils.kt
@@ -0,0 +1,85 @@
+package com.flightfeather.uav.common.utils
+
+import com.google.gson.Gson
+import com.google.gson.JsonParser
+import java.util.ArrayList
+
+/**
+ * @author riku
+ * Date: 2019/4/28
+ * GSON搴忓垪鍖栧伐鍏风被
+ */
+object GsonUtils {
+
+    fun getNoteJsonString(jsonString: String, note: String): String {
+        if (jsonString.isEmpty()) {
+            throw RuntimeException("getNoteJsonString jsonString empty")
+        }
+        if (note.isEmpty()) {
+            throw RuntimeException("getNoteJsonString note empty")
+        }
+        val element = JsonParser().parse(jsonString)
+        if (element.isJsonNull) {
+            throw RuntimeException("getNoteJsonString element empty")
+        }
+        return element.asJsonObject.get(note).toString()
+    }
+
+
+    fun <T> parserJsonToArrayBeans(jsonString: String, note: String, beanClazz: Class<T>): List<T> {
+        val noteJsonString = getNoteJsonString(jsonString, note)
+        return parserJsonToArrayBeans(noteJsonString, beanClazz)
+    }
+
+
+    fun <T> parserJsonToArrayBeans(jsonString: String, beanClazz: Class<T>): List<T> {
+        if (jsonString.isEmpty()) {
+            throw RuntimeException("parserJsonToArrayBeans jsonString empty")
+        }
+        val jsonElement = JsonParser().parse(jsonString)
+        if (jsonElement.isJsonNull) {
+            throw RuntimeException("parserJsonToArrayBeans jsonElement empty")
+        }
+        if (!jsonElement.isJsonArray) {
+            throw RuntimeException("parserJsonToArrayBeans jsonElement is not JsonArray")
+        }
+        val jsonArray = jsonElement.asJsonArray
+        val beans = ArrayList<T>()
+        for (jsonElement2 in jsonArray) {
+            val bean = Gson().fromJson(jsonElement2, beanClazz)
+            beans.add(bean)
+        }
+        return beans
+    }
+
+
+    fun <T> parserJsonToBean(jsonString: String, clazzBean: Class<T>): T {
+        if (jsonString.isEmpty()) {
+            throw RuntimeException("parserJsonToBean jsonString empty")
+        }
+        val jsonElement = JsonParser().parse(jsonString)
+        if (jsonElement.isJsonNull) {
+            throw RuntimeException("parserJsonToBean jsonElement empty")
+        }
+        if (!jsonElement.isJsonObject) {
+            throw RuntimeException("parserJsonToBean is not object")
+        }
+        return Gson().fromJson(jsonElement, clazzBean)
+    }
+
+
+    fun <T> parserJsonToBean(jsonString: String, note: String, clazzBean: Class<T>): T {
+        val noteJsonString = getNoteJsonString(jsonString, note)
+        return parserJsonToBean(noteJsonString, clazzBean)
+    }
+
+
+    fun toJsonString(obj: Any?): String {
+        return if (obj != null) {
+            Gson().toJson(obj)
+        } else {
+            throw RuntimeException("obj could not be empty")
+        }
+    }
+
+}
\ No newline at end of file
diff --git a/src/main/kotlin/com/flightfeather/uav/lightshare/bean/BaseJson.kt b/src/main/kotlin/com/flightfeather/uav/lightshare/bean/BaseJson.kt
deleted file mode 100644
index c8bdae3..0000000
--- a/src/main/kotlin/com/flightfeather/uav/lightshare/bean/BaseJson.kt
+++ /dev/null
@@ -1,15 +0,0 @@
-package com.flightfeather.uav.lightshare.bean
-
-import com.fasterxml.jackson.annotation.JsonIgnoreProperties
-import com.fasterxml.jackson.annotation.JsonInclude
-
-/**
- * 鍩虹Json缁撴瀯锛屾墍鏈夌殑鏁版嵁浠ユ涓哄熀绫�
- * @author riku
- * Date: 2019/8/27
- */
-@JsonInclude(JsonInclude.Include.NON_NULL)
-@JsonIgnoreProperties(ignoreUnknown = true)
-open class BaseJson{
-    val cmdCode: Int? = null
-}
\ No newline at end of file
diff --git a/src/main/kotlin/com/flightfeather/uav/lightshare/bean/BaseResponse.kt b/src/main/kotlin/com/flightfeather/uav/lightshare/bean/BaseResponse.kt
new file mode 100644
index 0000000..791825c
--- /dev/null
+++ b/src/main/kotlin/com/flightfeather/uav/lightshare/bean/BaseResponse.kt
@@ -0,0 +1,34 @@
+package com.flightfeather.uav.lightshare.bean
+
+import com.fasterxml.jackson.annotation.JsonInclude
+
+
+/**
+ * @author riku
+ * Date: 2020/10/9
+ * 缃戠粶璇锋眰杩斿洖鏁版嵁鍩虹被
+ */
+//"璇锋眰杩斿洖鍩烘湰缁撴瀯"
+@JsonInclude(JsonInclude.Include.NON_NULL)
+data class BaseResponse<T>(
+        var success: Boolean,
+        var message: String = "",
+        val head: DataHead? = null,
+        val data: T? = null
+){
+    init {
+        if (message.isBlank()) {
+            message = if (success) {
+                "璇锋眰鎴愬姛"
+            } else {
+                "璇锋眰澶辫触"
+            }
+        }
+    }
+}
+
+//"鍒嗛〉淇℃伅"
+data class DataHead(
+        var page: Int = 1,
+        var totalPage: Int = 1
+)
\ No newline at end of file
diff --git a/src/main/kotlin/com/flightfeather/uav/lightshare/bean/DataVo.kt b/src/main/kotlin/com/flightfeather/uav/lightshare/bean/DataVo.kt
new file mode 100644
index 0000000..c3b6f14
--- /dev/null
+++ b/src/main/kotlin/com/flightfeather/uav/lightshare/bean/DataVo.kt
@@ -0,0 +1,22 @@
+package com.flightfeather.uav.lightshare.bean
+
+import com.fasterxml.jackson.annotation.JsonInclude
+import com.flightfeather.uav.socket.bean.AirData
+
+/**
+ * @author riku
+ * Date: 2020/9/10
+ */
+@JsonInclude(JsonInclude.Include.NON_NULL)
+data class DataVo(
+        //鏃堕棿,  yyyy-MM-dd HH:mm:ss
+        var time: String? = null,
+        //绔欑偣缂栧彿
+        var deviceCode: String? = null,
+        //鏁版嵁鍊�
+        var values: List<AirData>? = null,
+        //缁忓害
+        var lng: Double? = null,
+        //绾害
+        var lat: Double? = null
+)
\ No newline at end of file
diff --git a/src/main/kotlin/com/flightfeather/uav/lightshare/bean/VehicleInfoVo.kt b/src/main/kotlin/com/flightfeather/uav/lightshare/bean/VehicleInfoVo.kt
deleted file mode 100644
index 8cf166e..0000000
--- a/src/main/kotlin/com/flightfeather/uav/lightshare/bean/VehicleInfoVo.kt
+++ /dev/null
@@ -1,20 +0,0 @@
-package com.flightfeather.uav.lightshare.bean
-
-/**
- * @author riku
- * Date: 2019/10/25
- */
-class VehicleInfoVo : BaseJson() {
-    var id: Int? = null
-
-    var obdDeviceCode: String? = null
-
-    var obdVin: String? = null
-
-    var obdLicencePlate: String? = null
-
-    /**
-     * 0: 闆嗗崱锛� 1锛氭福鍦熻溅
-     */
-    var obdVehicleType: Int? = null
-}
\ No newline at end of file
diff --git a/src/main/kotlin/com/flightfeather/uav/lightshare/service/RealTimeDataService.kt b/src/main/kotlin/com/flightfeather/uav/lightshare/service/RealTimeDataService.kt
new file mode 100644
index 0000000..880a3b9
--- /dev/null
+++ b/src/main/kotlin/com/flightfeather/uav/lightshare/service/RealTimeDataService.kt
@@ -0,0 +1,9 @@
+package com.flightfeather.uav.lightshare.service
+
+import com.flightfeather.uav.lightshare.bean.BaseResponse
+import com.flightfeather.uav.lightshare.bean.DataVo
+
+interface RealTimeDataService {
+
+    fun getSecondData(deviceCode: String?, page: Int?, perPage: Int?): BaseResponse<List<DataVo>>
+}
\ No newline at end of file
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
new file mode 100644
index 0000000..182ac66
--- /dev/null
+++ b/src/main/kotlin/com/flightfeather/uav/lightshare/service/impl/RealTimeDataServiceImpl.kt
@@ -0,0 +1,40 @@
+package com.flightfeather.uav.lightshare.service.impl
+
+import com.flightfeather.uav.common.utils.GsonUtils
+import com.flightfeather.uav.domain.entity.RealTimeData
+import com.flightfeather.uav.domain.mapper.RealTimeDataMapper
+import com.flightfeather.uav.lightshare.bean.BaseResponse
+import com.flightfeather.uav.lightshare.bean.DataHead
+import com.flightfeather.uav.lightshare.bean.DataVo
+import com.flightfeather.uav.lightshare.service.RealTimeDataService
+import com.flightfeather.uav.socket.bean.AirData
+import com.github.pagehelper.PageHelper
+import org.springframework.stereotype.Service
+import tk.mybatis.mapper.entity.Example
+import java.text.SimpleDateFormat
+
+@Service
+class RealTimeDataServiceImpl(val realTimeDataMapper: RealTimeDataMapper) : RealTimeDataService {
+
+    override fun getSecondData(deviceCode: String?, page: Int?, perPage: Int?): BaseResponse<List<DataVo>> {
+        val _perPage = perPage ?: 60
+        val _page = page ?: 1
+        val pageInfo = PageHelper.startPage<RealTimeData>(_page, _perPage)
+        val result = mutableListOf<DataVo>()
+        realTimeDataMapper.selectByExample(Example(RealTimeData::class.java).apply {
+            createCriteria().apply {
+                deviceCode?.let { andEqualTo("deviceCode", it) }
+            }
+            orderBy("dataTime").desc()
+        }).forEach {
+            result.add(DataVo(
+                    SimpleDateFormat.getDateTimeInstance().format(it.dataTime),
+                    it.deviceCode,
+                    GsonUtils.parserJsonToArrayBeans(it.factors, AirData::class.java),
+                    it.longitude.toDouble(), it.latitude.toDouble()
+            ))
+        }
+        result.reverse()
+        return BaseResponse(true, head = DataHead(pageInfo.pageNum, pageInfo.pages), data = result)
+    }
+}
\ No newline at end of file
diff --git a/src/main/kotlin/com/flightfeather/uav/lightshare/web/RealTimeDataController.kt b/src/main/kotlin/com/flightfeather/uav/lightshare/web/RealTimeDataController.kt
new file mode 100644
index 0000000..f72e1e0
--- /dev/null
+++ b/src/main/kotlin/com/flightfeather/uav/lightshare/web/RealTimeDataController.kt
@@ -0,0 +1,19 @@
+package com.flightfeather.uav.lightshare.web
+
+import com.flightfeather.uav.lightshare.service.RealTimeDataService
+import org.springframework.web.bind.annotation.GetMapping
+import org.springframework.web.bind.annotation.RequestMapping
+import org.springframework.web.bind.annotation.RequestParam
+import org.springframework.web.bind.annotation.RestController
+
+@RestController
+@RequestMapping("air/realtime")
+class RealTimeDataController(val realTimeDataService: RealTimeDataService) {
+
+    @GetMapping("/sec")
+    fun getSecondData(
+            @RequestParam(value = "deviceCode", required = false) deviceCode: String?,
+            @RequestParam(value = "page", required = false) page: Int?,
+            @RequestParam(value = "perPage", required = false) perPage: Int?
+    ) = realTimeDataService.getSecondData(deviceCode,page, perPage)
+}
\ No newline at end of file
diff --git a/src/main/kotlin/com/flightfeather/uav/repository/VehicleRepository.kt b/src/main/kotlin/com/flightfeather/uav/repository/VehicleRepository.kt
deleted file mode 100644
index 910ebab..0000000
--- a/src/main/kotlin/com/flightfeather/uav/repository/VehicleRepository.kt
+++ /dev/null
@@ -1,9 +0,0 @@
-package com.flightfeather.uav.repository
-
-/**
- * @author riku
- * Date: 2019/10/25
- */
-interface VehicleRepository {
-
-}
\ No newline at end of file
diff --git a/src/main/kotlin/com/flightfeather/uav/socket/MessageManager.kt b/src/main/kotlin/com/flightfeather/uav/socket/MessageManager.kt
index 504b3ca..4f153e3 100644
--- a/src/main/kotlin/com/flightfeather/uav/socket/MessageManager.kt
+++ b/src/main/kotlin/com/flightfeather/uav/socket/MessageManager.kt
@@ -24,6 +24,8 @@
 
     companion object{
         private lateinit var instance: MessageManager
+
+        private const val TAG = "UAV"
     }
 
     @Autowired
@@ -46,10 +48,10 @@
         if (bccCheck(msg)) {
             //淇濆瓨
             DeviceSession.saveDevice(packageData.deviceCode, ctx)
-//            saveToTxt(msg)
+            saveToTxt(msg)
             saveToDataBase(packageData)
         } else {
-            println("------鏁版嵁BCC鏍¢獙澶辫触锛岃垗寮� [${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())}]")
+            println("------${TAG}鏁版嵁BCC鏍¢獙澶辫触锛岃垗寮� [${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())}]")
         }
     }
 
diff --git a/src/main/kotlin/com/flightfeather/uav/socket/ServerHandler.kt b/src/main/kotlin/com/flightfeather/uav/socket/ServerHandler.kt
index 9a9b790..1950bb4 100644
--- a/src/main/kotlin/com/flightfeather/uav/socket/ServerHandler.kt
+++ b/src/main/kotlin/com/flightfeather/uav/socket/ServerHandler.kt
@@ -10,16 +10,22 @@
 
 class ServerHandler : ChannelInboundHandlerAdapter() {
 
+    companion object {
+        private const val TAG = "UAV"
+    }
+
     val attributeKey = AttributeKey.valueOf<String>("deviceCode")
 
     override fun channelRegistered(ctx: ChannelHandlerContext?) {
         super.channelRegistered(ctx)
-        println("------绔彛鏈塈P杩炴帴锛歔ip:${ctx?.channel()?.remoteAddress()}] ${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())}")
+        println()
+        println("------${TAG}绔彛鏈塈P杩炴帴锛歔ip:${ctx?.channel()?.remoteAddress()}] ${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())}")
 //        ctx?.fireChannelActive()
     }
 
     override fun channelActive(ctx: ChannelHandlerContext?) {
-        println("------绔彛鏈塈P婵�娲伙細[ip:${ctx?.channel()?.remoteAddress()}] ${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())}")
+        println()
+        println("------${TAG}绔彛鏈塈P婵�娲伙細[ip:${ctx?.channel()?.remoteAddress()}] ${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())}")
         super.channelActive(ctx)
     }
 
@@ -29,7 +35,8 @@
         val sb = StringBuilder()
 
         if (msg is ByteArray) {
-            println("------鏀跺埌鐨勫師濮嬫暟鎹細[ip:${ctx?.channel()?.remoteAddress()}] ${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())}")
+            println()
+            println("------${TAG}鏀跺埌鐨勫師濮嬫暟鎹細[ip:${ctx?.channel()?.remoteAddress()}] ${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())}")
             msg.forEach {
                 val a: Int = if (it < 0) {
                     it + 256
@@ -58,7 +65,7 @@
     }
 
     override fun channelInactive(ctx: ChannelHandlerContext?) {
-        println("------绔彛鏈塈P涓嶆椿鍔細[ip:${ctx?.channel()?.remoteAddress()}] ${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())}")
+        println("------${TAG}绔彛鏈塈P涓嶆椿鍔細[ip:${ctx?.channel()?.remoteAddress()}] ${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())}")
         super.channelInactive(ctx)
     }
 
diff --git a/src/main/kotlin/com/flightfeather/uav/socket/bean/DataUnit.kt b/src/main/kotlin/com/flightfeather/uav/socket/bean/DataUnit.kt
index 11edd95..40a5d94 100644
--- a/src/main/kotlin/com/flightfeather/uav/socket/bean/DataUnit.kt
+++ b/src/main/kotlin/com/flightfeather/uav/socket/bean/DataUnit.kt
@@ -11,5 +11,5 @@
  * 鏍规嵁鍛戒护鍗曞厓 @see [AirCommandUnit] 鐨勫垎绫伙紝涓嶅悓绫诲瀷鐨勭粨鏋勪笉鍚岋紝瑙佸悇瀛愮被
  */
 open class DataUnit {
-    var time: Date? = null
+
 }
\ No newline at end of file
diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml
index b44db72..ece910b 100644
--- a/src/main/resources/application.yml
+++ b/src/main/resources/application.yml
@@ -8,6 +8,11 @@
     url: jdbc:mysql://114.215.109.124:3306/dronemonitor?serverTimezone=Asia/Shanghai&prepStmtCacheSize=517&cachePrepStmts=true&autoReconnect=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false
     username: root
     password: 123456
+    hikari:
+      maximum-pool-size: 500
+      minimum-idle: 20
+      idle-timeout: 60000
+      connection-timeout: 60000
   jmx:
     enabled: false
 
diff --git a/src/test/kotlin/com/flightfeather/uav/Test.kt b/src/test/kotlin/com/flightfeather/uav/Test.kt
index 6152cdb..7927e80 100644
--- a/src/test/kotlin/com/flightfeather/uav/Test.kt
+++ b/src/test/kotlin/com/flightfeather/uav/Test.kt
@@ -1,10 +1,33 @@
 package com.flightfeather.uav
 
+import org.junit.Test
+import java.text.SimpleDateFormat
+import java.util.*
+
 /**
  * @author riku
  * Date: 2019/9/12
  */
 class Test {
 
+    @Test
+    fun foo1() {
+        val s = SimpleDateFormat.getDateTimeInstance().format(Date())
+        println(s)
+    }
 
+    @Test
+    fun foo2() {
+        val b = arrayOf("41", "79", "24", "04", "0B", "45")
+        val i = 0
+        val valid = b[i].toInt(16).toChar()//缁忕含搴︽槸鍚︽湁鏁堬紙鏈夋晥: A; 鏃犳晥: V锛�
+        val a1 = b[i + 1].toInt(16)
+        val b1 = b[i + 2].toInt(16)
+        var b2 = "${b[i + 3]}${b[i + 4]}".toInt(16).toDouble()
+        while (b2 >= 1) {
+            b2 /= 10
+        }
+        val lng = a1 + (b1 + b2) / 60
+        val s = b[i + 5].toInt(16).toChar()
+    }
 }
\ No newline at end of file

--
Gitblit v1.9.3