Riku
2025-06-02 e731486b50c4ea6e2d28f302df449b4bd0b2be57
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package com.flightfeather.uav.socket.decoder
 
import com.flightfeather.uav.socket.bean.*
import com.flightfeather.uav.socket.eunm.AirCommandUnit
import com.flightfeather.uav.socket.eunm.FactorType
import org.slf4j.LoggerFactory
import java.text.SimpleDateFormat
 
/**
 * uav 第二层数据单元解码器
 * @author riku
 * Date: 2019/9/12
 *
 * 数据单元按照命令单元的类型共有以下几种类型:
 * 命令单元 @see [AirCommandUnit]
 */
class DataUnitDecoder {
 
    private val logger = LoggerFactory.getLogger(javaClass.name)
 
    private val types = mutableMapOf<String?, MutableList<AirTypeData>>()
 
    fun getAirConfirmData(b: List<String>, deviceCode: String?): List<AirTypeData> {
        val resultList = mutableListOf<AirTypeData>()
        b.forEach {
            FactorType.getByValue(it.toInt(16))?.let { f->
                resultList.add(AirTypeData(f))
            }
        }
        if (!types.containsKey(deviceCode)) {
            types[deviceCode] = mutableListOf()
        }
        types[deviceCode]?.clear()
        types[deviceCode]?.addAll(resultList)
 
        return resultList
    }
 
    fun getAirData(b: List<String>, deviceCode: String?): List<AirData> {
        val resultList = mutableListOf<AirData>()
 
        if (!types.containsKey(deviceCode)) {
            return resultList
        }
 
        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
    }
 
    private fun numberFormat(num: String) =
        if (num.length < 2) {
            "0$num"
        } else {
            num
        }
 
}