From 4db9806cc644bd58124aa9d8f1dbf9aee60d408f Mon Sep 17 00:00:00 2001 From: riku <risaku@163.com> Date: 星期三, 13 一月 2021 11:50:27 +0800 Subject: [PATCH] 1. 根据新协议做协议解析调整 --- src/main/kotlin/com/flightfeather/uav/socket/decoder/impl/DataUnitDecoderImpl.kt | 241 ++++++++++++++++++++++++++++------------------- 1 files changed, 142 insertions(+), 99 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..a5ffbd5 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,156 @@ 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 = mutableMapOf<String?, MutableList<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>, deviceCode: String?): List<AirTypeData> { + val resultList = mutableListOf<AirTypeData>() + b.forEach { + FactorType.getByIndex(it.toInt(16))?.let { f-> + resultList.add(AirTypeData(f)) + } } + if (!types.containsKey(deviceCode)) { + types[deviceCode] = mutableListOf() + } + types[deviceCode]?.clear() + types[deviceCode]?.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>, deviceCode: 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) + if (!types.containsKey(deviceCode)) { + return resultList } - //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[deviceCode]?.forEach { + if (i > b.size - it.factorType.byteLength) { + return@forEach } + + when (it.factorType) { + FactorType.LNG -> { + val valid = b[i].toInt(16).toChar()//缁忕含搴︽槸鍚︽湁鏁堬紙鏈夋晥: A; 鏃犳晥: V锛� + + //缁忕含搴﹀師濮嬪�硷紝渚嬶細121掳30.0411鈥诧紝鍏朵腑 121 瀵瑰簲a1锛�30瀵瑰簲b1锛�04瀵瑰簲b2锛�11瀵瑰簲b3 + val a1 = b[i + 1].toInt(16)//搴� + val b1 = b[i + 2].toInt(16)//鍒嗭紙鏁存暟锛� + var b2 = b[i + 3].toInt(16).toDouble()//鍒嗭紙灏忔暟閮ㄥ垎鍓嶄袱浣嶏級 + var b3 = b[i + 4].toInt(16).toDouble()//鍒嗭紙灏忔暟閮ㄥ垎鍚庝袱浣嶏級 + +// var b2 = "${b[i + 3]}${b[i + 4]}".toInt(16).toDouble() + b2 /= 100 + b3 /= 10000 + val lng = a1 + (b1 + b2 + b3) / 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].toInt(16).toDouble()//鍒嗭紙灏忔暟閮ㄥ垎鍓嶄袱浣嶏級 + var b3 = b[i + 3].toInt(16).toDouble()//鍒嗭紙灏忔暟閮ㄥ垎鍚庝袱浣嶏級 + + b2 /= 100 + b3 /= 10000 + val lat = a1 + (b1 + b2 + b3) / 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 + }) + } + AirDataPackage.FACTOR_BIT_LENGTH_3 -> { + val data = "${b[i]}${b[i + 1]}".toInt(16) + resultList.add(AirData().apply { + factorId = it.factorType.value.toString() + factorName = it.factorType.des + factorData = data.toDouble() + }) + } + } + } + } + + 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