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
package com.flightfeather.uav.socket.decoder
 
import com.flightfeather.uav.common.utils.CommonUtil
import com.flightfeather.uav.socket.bean.BaseMessage
import java.text.SimpleDateFormat
import java.util.*
 
/**
 * @author riku
 * Date: 2020/9/10
 */
typealias Decode<T> = (m: T, s: String) -> Unit
 
abstract class BaseDataDecoder<T : BaseMessage> {
 
    companion object {
        const val INVALID_VALUE = -1.0
    }
 
    private var isInit = false
 
    private lateinit var message: T
 
    abstract fun messageClass(): Class<T>
 
    abstract fun getCpDecode(): List<Decode<T>>
 
    private val decodeList = mutableListOf<Decode<T>>()
 
    private val getHead: Decode<T> = {m, s ->
        m.head = s.substring(0, 2)
    }
 
    private val getLength: Decode<T> = {m, s ->
        m.length = s.substring(2, 6)
    }
 
    private val getQN: Decode<T> = {m, s ->
        m.qn = getData(s, "QN=", ";")
    }
 
    private val getST: Decode<T> = {m, s ->
        m.st = getData(s, "ST=", ";")
    }
 
    private val getCN: Decode<T> = {m, s ->
        m.cn = getData(s, "CN=", ";")
    }
 
    private val getPW: Decode<T> = {m, s ->
        m.pw = getData(s, "PW=", ";")
    }
 
    private val getMN: Decode<T> = {m, s ->
        m.mn = getData(s, "MN=", ";")
    }
 
    private val getCP: Decode<T> = {m, s ->
        m.cp = getData(s, "CP=&&", "&&")
    }
 
    private val getExtra:Decode<T> = {m, s ->
        val start = s.lastIndexOf("&&")
        val end = s.length - 4
        m.extra = s.substring(start, end).substring(2)
    }
 
 
    private val getCrc: Decode<T> = {m, s ->
        m.crc = s.substring(s.length - 4, s.length)
    }
 
    protected fun getData(msg: String, start: String, end: String) =
            CommonUtil.getSubStr(msg, start, end)?.substring(start.length) ?: ""
 
    private fun init() {
        if (!isInit) {
 
            decodeList.add(getHead)
            decodeList.add(getLength)
            decodeList.add(getQN)
            decodeList.add(getST)
            decodeList.add(getCN)
            decodeList.add(getPW)
            decodeList.add(getMN)
            decodeList.add(getCP)
            decodeList.add(getExtra)
            decodeList.add(getCrc)
 
            decodeList.addAll(this.getCpDecode())
 
            isInit = true
        }
        message = this.messageClass().newInstance()
    }
 
    fun decode(msg: String): T {
        init()
 
        decodeList.forEach {
            try {
                it.invoke(message, msg)
            } catch (e: Exception) {
                println("${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())}: 命令无法获取[${it.javaClass.name}]")
            }
        }
        return message
    }
}