From 7269d4a4755fa48c45e827bdc5b5ac56c6eca99c Mon Sep 17 00:00:00 2001
From: riku <risaku@163.com>
Date: 星期五, 11 十二月 2020 17:27:01 +0800
Subject: [PATCH] 修改监测因子类型

---
 src/main/kotlin/com/flightfeather/uav/socket/decoder/impl/DataUnitDecoderImpl.kt |  225 +++++++++++++++++++++++++++++++-------------------------
 1 files changed, 124 insertions(+), 101 deletions(-)

diff --git a/src/main/kotlin/com/flightfeather/uav/socket/decoder/impl/DataUnitDecoderImpl.kt b/src/main/kotlin/com/flightfeather/uav/socket/decoder/impl/DataUnitDecoderImpl.kt
index ef8f26d..1af1944 100644
--- a/src/main/kotlin/com/flightfeather/uav/socket/decoder/impl/DataUnitDecoderImpl.kt
+++ b/src/main/kotlin/com/flightfeather/uav/socket/decoder/impl/DataUnitDecoderImpl.kt
@@ -1,12 +1,12 @@
 package com.flightfeather.uav.socket.decoder.impl
 
-import com.flightfeather.uav.socket.bean.*
+import com.flightfeather.uav.socket.bean.AirData
+import com.flightfeather.uav.socket.bean.AirDataPackage
+import com.flightfeather.uav.socket.bean.AirTypeData
 import com.flightfeather.uav.socket.decoder.DataUnitDecoder
-import com.flightfeather.uav.socket.decoder.RealTimeDataDecoder
-import com.flightfeather.uav.socket.eunm.ObdDataType
+import com.flightfeather.uav.socket.eunm.FactorType
 import org.slf4j.LoggerFactory
-import java.lang.StringBuilder
-import java.util.*
+import java.text.SimpleDateFormat
 
 /**
  * @author riku
@@ -15,113 +15,136 @@
 class DataUnitDecoderImpl : DataUnitDecoder {
 
     private val logger = LoggerFactory.getLogger(javaClass.name)
-    private var realTimeDataDecoder: RealTimeDataDecoder = RealTimeDataDecoderImpl()
 
-    override fun getCarRegisterData(b: List<String>): List<CarRegisterData> {
-        val time = decodeDataTime(b)
+    private val types = mutableListOf<AirTypeData>()
 
-        val serialNum = "${b[6]}${b[7]}".toInt(16)
-
-        val simCode = StringBuilder()
-        for (i in 10 until b.size) {
-            simCode.append(b[i].toInt(16).toChar())
+    override fun getAirConfirmData(b: List<String>): List<AirTypeData> {
+        val resultList = mutableListOf<AirTypeData>()
+        b.forEach {
+            FactorType.getByIndex(it.toInt(16))?.let { f->
+                resultList.add(AirTypeData(f))
+            }
         }
+        types.clear()
+        types.addAll(resultList)
 
-        return mutableListOf(CarRegisterData(time, serialNum, simCode.toString()))
+        return resultList
     }
 
-    override fun getRealTimeData(b: List<String>): List<RealTimeData> {
-        val time = decodeDataTime(b)
+    override fun getAirData(b: List<String>): List<AirData> {
+        val resultList = mutableListOf<AirData>()
 
-        //鍥芥爣娉�: 璧峰瀛楄妭涓�7 寮�濮嬬殑2涓瓧鑺傝〃绀� 娴佹按鍙�
-//        val serialNum = "${b[7]}${b[8]}".toInt(16)
-
-        //涓婃捣娉�: 璧峰瀛楄妭涓�6 寮�濮嬬殑2涓瓧鑺傝〃绀� 娴佹按鍙�
-        val serialNum = "${b[6]}${b[7]}".toInt(16)
-
-        val dataList = mutableListOf<String>().apply { addAll(b) }
-        //鍒犲幓鍓�6浣嶆椂闂�
-        for (i in 0..5) {
-            if (dataList.isNotEmpty()) dataList.removeAt(0)
-        }
-
-        //fixme 2019.9.15 鐩墠涓ょ瑙勫垯鏈夊啿绐侊紝骞朵笖鍙粠鏁版嵁鍐呭鏃犳硶鍖哄垎銆傛殏鏃朵娇鐢� 鍥芥爣娉�
-        /*
-        * 鍒犲幓娴佹按鍙凤紝寰楀埌淇℃伅鏍囧織涓庝俊鎭綋鐨勭粍鍚�
-        * (鍥芥爣娉曪細鍘婚櫎鍓�6浣嶆椂闂村悗锛岃捣濮嬪瓧鑺備负1 寮�濮嬬殑2涓瓧鑺傝〃绀� 娴佹按鍙�)
-        */
-//        if (dataList.size >= 2) dataList.removeAt(1)
-//        if (dataList.size >= 2) dataList.removeAt(1)
-
-        /*
-        * 鍒犲幓娴佹按鍙凤紝寰楀埌淇℃伅鏍囧織涓庝俊鎭綋鐨勭粍鍚�
-        * (涓婃捣娉曪細鍘婚櫎鍓�6浣嶆椂闂村悗锛岃捣濮嬪瓧鑺備负0 寮�濮嬬殑2涓瓧鑺傝〃绀� 娴佹按鍙�)
-        */
-        if (dataList.isNotEmpty()) dataList.removeAt(0)
-        if (dataList.isNotEmpty()) dataList.removeAt(0)
-
-        val resultList = mutableListOf<RealTimeData>()
-
-        /*
-         * 鏈�缁堝緱鍒扮殑鏁版嵁缁撴瀯涓� [ 淇℃伅绫诲瀷鏍囧織, 淇℃伅浣�, ...... , 淇℃伅绫诲瀷鏍囧織, 淇℃伅浣� ]
-         */
-        while (dataList.isNotEmpty()) {
-            //寰楀埌鏈�鍓嶉潰鐨勪竴缁勪俊鎭被鍨�+淇℃伅浣撳垪琛�
-            val data = realTimeDataDecoder.getDataListByDataType(dataList)
-            if (data.isNotEmpty()) {
-                val r = when (data[0].toInt(16)) {
-                    ObdDataType.ObdData.value -> realTimeDataDecoder.getObdData(time, serialNum, data)
-                    ObdDataType.EngineDataFlow.value -> realTimeDataDecoder.getEngineDataStream(time, serialNum, data)
-                    ObdDataType.SupplementDataFlow.value -> realTimeDataDecoder.getSupplementDataStream(time, serialNum, data)
-                    else -> null
-                }
-
-                if (r != null) {
-                    resultList.add(r)
-                }
-
-                for (i in 0 until data.size) {
-                    if (dataList.isNotEmpty()) dataList.removeAt(0)
-                }
-            }else if (dataList.isNotEmpty()) {
-                //鎸夌収瑙勫垯娌℃湁鍙栧埌淇℃伅浣擄紝骞朵笖鍘熸暟鎹笉涓虹┖锛岃〃绀哄師鏁版嵁鏍煎紡閿欒锛岄��鍑哄惊鐜�
-                logger.error("-------鏁版嵁淇℃伅娴佹垨琛ュ厖鏁版嵁娴佹牸寮忛敊璇�")
-                break
+        var i = 0
+        types.forEach {
+            if (i > b.size - it.factorType.byteLength) {
+                return@forEach
             }
+
+            when (it.factorType) {
+                FactorType.LNG -> {
+                    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()
+
+                    resultList.add(AirData().apply {
+                        factorId = it.factorType.value?.toString()
+                        factorName = it.factorType.des
+                        factorData = lng
+                        statusList = listOf(valid.toString(), s.toString())
+                    })
+                }
+                FactorType.LAT -> {
+                    val a1 = b[i].toInt(16)
+                    val b1 = b[i + 1].toInt(16)
+                    var b2 = "${b[i + 2]}${b[i + 3]}".toInt(16).toDouble()
+                    while (b2 >= 1) {
+                        b2 /= 10
+                    }
+                    val lat = a1 + (b1 + b2) / 60
+                    val s = b[i + 4].toInt(16).toChar()
+                    resultList.add(AirData().apply {
+                        factorId = it.factorType.value.toString()
+                        factorName = it.factorType.des
+                        factorData = lat
+                        statusList = listOf(s.toString())
+                    })
+                }
+                FactorType.TIME -> {
+                    val year = b[i].toInt(16).toString().run { numberFormat(this) }
+                    val month = b[i+1].toInt(16).toString().run { numberFormat(this) }
+                    val day = b[i+2].toInt(16).toString().run { numberFormat(this) }
+                    val hour = b[i+3].toInt(16).toString().run { numberFormat(this) }
+                    val minute = b[i+4].toInt(16).toString().run { numberFormat(this) }
+                    val second = b[i + 5].toInt(16).toString().run { numberFormat(this) }
+                    val date = "20$year-$month-$day $hour:$minute:$second"
+                    val time = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date).time
+                    resultList.add(AirData().apply {
+                        factorId = it.factorType.value.toString()
+                        factorName = it.factorType.des
+                        factorData = time.toDouble()
+                        statusList = listOf(date)
+                    })
+                }
+                else -> {
+                    when (it.factorType.byteLength) {
+                        AirDataPackage.FACTOR_BIT_LENGTH_2 -> {
+                            val a1 = "${b[i]}${b[i + 1]}".toInt(16)
+                            var b1 = b[i + 2].toInt(16).toDouble()
+                            while (b1 >= 1) {
+                                b1 /= 10
+                            }
+                            val data1 = a1 + b1
+
+                            resultList.add(AirData().apply {
+                                factorId = it.factorType.value?.toString()
+                                factorName = it.factorType.des
+                                factorData = data1
+                            })
+                        }
+                        AirDataPackage.FACTOR_BIT_LENGTH_1 -> {
+                            //鏁版嵁瀹為檯鍊硷紙3浣嶅瓧鑺傝〃绀猴級
+                            val a1 = "${b[i]}${b[i + 1]}".toInt(16)
+                            var b1 = b[i + 2].toInt(16).toDouble()
+                            while (b1 >= 1) {
+                                b1 /= 10
+                            }
+                            val data1 = a1 + b1
+
+                            //鏁版嵁鐗╃悊閲忥紙3浣嶅瓧鑺傝〃绀猴級
+                            val a2 = "${b[i+3]}${b[i + 4]}".toInt(16)
+                            var b2 = b[i + 5].toInt(16).toDouble()
+                            while (b2 >= 1) {
+                                b2 /= 10
+                            }
+                            val data2 = a2 + b2
+
+                            resultList.add(AirData().apply {
+                                factorId = it.factorType.value?.toString()
+                                factorName = it.factorType.des
+                                factorData = data1
+                                physicalQuantity = data2
+                            })
+                        }
+                    }
+                }
+            }
+
+            i += it.factorType.byteLength
         }
 
         return resultList
     }
 
-    override fun getReplacementData(b: List<String>): List<ReplacementData> = getRealTimeData(b)
-
-    override fun getCarLogOutData(b: List<String>): List<CarLogOutData> {
-        val time = decodeDataTime(b)
-
-        val serialNum = "${b[6]}${b[7]}".toInt(16)
-
-        return mutableListOf(CarLogOutData(time, serialNum))
-    }
-
-    override fun getTimeCalibrationData(b: List<String>): List<TimeCalibrationData> = emptyList()
-
-    override fun decodeDataTime(b: List<String>): Date? {
-        if (b.size < 6) {
-            return null
-        }
-
-        val year = b[0].toInt(16) + 2000
-        if (year < 2000 || year > 2099) return null
-        val month = b[1].toInt(16)
-        val day = b[2].toInt(16)
-        val hour = b[3].toInt(16)
-        val min = b[4].toInt(16)
-        val sec = b[5].toInt(16)
-        val cal  = Calendar.getInstance().apply {
-            set(year, month - 1, day, hour, min, sec)
-            set(Calendar.MILLISECOND, 0)
-        }
-
-        return cal.time
-    }
+    private fun numberFormat(num: String) =
+            if (num.length < 2) {
+                "0$num"
+            } else {
+                num
+            }
 }
\ No newline at end of file

--
Gitblit v1.9.3