riku
2021-03-01 7a3654aaebf1d75303a90f3dc574881b0199779c
1. 更新数据查询接口
已修改6个文件
已删除3个文件
已添加7个文件
335 ■■■■ 文件已修改
src/main/kotlin/com/flightfeather/uav/common/config/CorsConfig.kt 29 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/kotlin/com/flightfeather/uav/common/utils/FileUtil.kt 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/kotlin/com/flightfeather/uav/common/utils/GsonUtils.kt 85 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/kotlin/com/flightfeather/uav/lightshare/bean/BaseJson.kt 15 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/kotlin/com/flightfeather/uav/lightshare/bean/BaseResponse.kt 34 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/kotlin/com/flightfeather/uav/lightshare/bean/DataVo.kt 22 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/kotlin/com/flightfeather/uav/lightshare/bean/VehicleInfoVo.kt 20 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/kotlin/com/flightfeather/uav/lightshare/service/RealTimeDataService.kt 9 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/kotlin/com/flightfeather/uav/lightshare/service/impl/RealTimeDataServiceImpl.kt 40 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/kotlin/com/flightfeather/uav/lightshare/web/RealTimeDataController.kt 19 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/kotlin/com/flightfeather/uav/repository/VehicleRepository.kt 9 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/kotlin/com/flightfeather/uav/socket/MessageManager.kt 6 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/kotlin/com/flightfeather/uav/socket/ServerHandler.kt 15 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/kotlin/com/flightfeather/uav/socket/bean/DataUnit.kt 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/application.yml 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/test/kotlin/com/flightfeather/uav/Test.kt 23 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
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)
    }
}
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
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")
        }
    }
}
src/main/kotlin/com/flightfeather/uav/lightshare/bean/BaseJson.kt
ÎļþÒÑɾ³ý
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
)
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
)
src/main/kotlin/com/flightfeather/uav/lightshare/bean/VehicleInfoVo.kt
ÎļþÒÑɾ³ý
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>>
}
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)
    }
}
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)
}
src/main/kotlin/com/flightfeather/uav/repository/VehicleRepository.kt
ÎļþÒÑɾ³ý
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())}]")
        }
    }
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("------端口有IP连接:[ip:${ctx?.channel()?.remoteAddress()}] ${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())}")
        println()
        println("------${TAG}端口有IP连接:[ip:${ctx?.channel()?.remoteAddress()}] ${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())}")
//        ctx?.fireChannelActive()
    }
    override fun channelActive(ctx: ChannelHandlerContext?) {
        println("------端口有IP激活:[ip:${ctx?.channel()?.remoteAddress()}] ${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())}")
        println()
        println("------${TAG}端口有IP激活:[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("------端口有IP不活动:[ip:${ctx?.channel()?.remoteAddress()}] ${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())}")
        println("------${TAG}端口有IP不活动:[ip:${ctx?.channel()?.remoteAddress()}] ${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())}")
        super.channelInactive(ctx)
    }
src/main/kotlin/com/flightfeather/uav/socket/bean/DataUnit.kt
@@ -11,5 +11,5 @@
 * æ ¹æ®å‘½ä»¤å•å…ƒ @see [AirCommandUnit] çš„分类,不同类型的结构不同,见各子类
 */
open class DataUnit {
    var time: Date? = null
}
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
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()
    }
}