riku
2019-09-18 259512005923831d1221bd49568751bf519dc020
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
package com.flightfeather.obd.socket.decoder.impl
 
import com.flightfeather.obd.socket.bean.DataUnit
import com.flightfeather.obd.socket.decoder.DataPackageDecoder
import com.flightfeather.obd.socket.decoder.DataUnitDecoder
import com.flightfeather.obd.socket.eunm.ObdCommandUnit
 
/**
 * @author riku
 * Date: 2019/9/12
 */
class DataPackageDecoderImpl : DataPackageDecoder {
 
    private val dataUnitDecoder: DataUnitDecoder = DataUnitDecoderImpl()
 
    // 接收到的字符串是byte转码前的,其就表示字符的ASCII码;
 
    override fun getHead(b: List<String>): String? {
        return if (b.size >= 2) {
            "${b[0]}${b[1]}"
        } else {
            null
        }
    }
 
    override fun getCommandUnit(b: List<String>): Int? = if (b.size >= 3) {
        b[2].toIntOrNull(16)
    } else {
        null
    }
 
    override fun getVinCode(b: List<String>): String? {
        if (b.size < 20) return null
 
        //fixme 应该是需要将b当作ASCII码,再获取转换后的字符串
        val s = StringBuilder()
 
        for (i in 3..19) {
            s.append(b[i].toIntOrNull(16)?.toChar())
        }
 
        return s.toString()
    }
 
    override fun getSoftwareVersion(b: List<String>): Int? = if (b.size >= 21) {
        b[20].toIntOrNull(16)
    } else {
        null
    }
 
    override fun getEncryptionWay(b: List<String>): Int? = if (b.size >= 21) {
        b[21].toIntOrNull(16)
    } else {
        null
    }
 
    override fun getDataLength(b: List<String>): Int {
        if (b.size < 24) return 0
 
        val hexNum = "${b[22]}${b[23]}"
 
        return hexNum.toIntOrNull(16) ?: 0
    }
 
    override fun getDataUnit(b: List<String>): List<DataUnit> {
        if (getDataLength(b) == 0 || b.size < 26) {
            return emptyList()
        }
 
        val unit = mutableListOf<String>()
        for (i in 24..b.size - 2) {
            unit.add(b[i])
        }
        dataUnitDecoder.run {
            return when (getCommandUnit(b)) {
                ObdCommandUnit.CarRegister.value -> getCarRegisterData(unit)
                ObdCommandUnit.RealTimeData.value -> getRealTimeData(unit)
                ObdCommandUnit.ReplacementData.value -> getReplacementData(unit)
                ObdCommandUnit.CarLogOut.value -> getCarLogOutData(unit)
                ObdCommandUnit.TimeCalibration.value -> getTimeCalibrationData(unit)
                else -> emptyList()
            }
        }
    }
 
    override fun getCheckCode(b: List<String>): Int? {
        return if (b.isNotEmpty()) {
            b[b.size - 1].toIntOrNull(16)
        } else {
            null
        }
    }
 
    override fun toStringList(msg: String): List<String> {
        return msg.split(" ")
    }
 
}