From 4cb662daa3d2760cc4c892e58f73cbf10b265f6f Mon Sep 17 00:00:00 2001 From: riku <risaku@163.com> Date: 星期五, 11 十二月 2020 11:55:56 +0800 Subject: [PATCH] 新增经纬度、和时间的解析逻辑 --- src/main/kotlin/com/flightfeather/uav/socket/decoder/impl/DataUnitDecoderImpl.kt | 122 +++++++++++++++++++++++++++++++--------- 1 files changed, 93 insertions(+), 29 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 dac72d9..b6eba76 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 @@ -6,6 +6,7 @@ import com.flightfeather.uav.socket.decoder.DataUnitDecoder import com.flightfeather.uav.socket.eunm.FactorType import org.slf4j.LoggerFactory +import java.text.SimpleDateFormat /** * @author riku @@ -38,49 +39,112 @@ if (i > b.size - it.factorType.byteLength) { return@forEach } - 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]}${b[i + 1]}".toInt(16) - var b2 = b[i + 2].toInt(16).toDouble() + 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).toLong() while (b2 >= 1) { b2 /= 10 } - val data2 = a2 + b2 + 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 = data1 - physicalQuantity = data2 + 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).toLong() + 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 + 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).toLong() + 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).toLong() + 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).toLong() + 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 } + + private fun numberFormat(num: String) = + if (num.length < 2) { + "0$num" + } else { + num + } } \ No newline at end of file -- Gitblit v1.9.3