package com.flightfeather.uav.socket
|
|
import com.flightfeather.uav.common.utils.FileUtil
|
import com.flightfeather.uav.repository.*
|
import com.flightfeather.uav.socket.bean.*
|
import com.flightfeather.uav.socket.decoder.AirDataDecoder
|
import com.flightfeather.uav.socket.decoder.impl.DataPackageDecoderImpl
|
import com.flightfeather.uav.socket.eunm.AirCommandUnit
|
import io.netty.channel.ChannelHandlerContext
|
import org.springframework.beans.factory.annotation.Autowired
|
import org.springframework.stereotype.Component
|
import java.text.SimpleDateFormat
|
import java.util.*
|
import javax.annotation.PostConstruct
|
|
/**
|
* 处理socket接收的消息
|
* Date: 2019.8.27
|
* @author riku
|
*/
|
|
@Component
|
class MessageManager{
|
|
companion object{
|
private lateinit var instance: MessageManager
|
}
|
|
@Autowired
|
lateinit var airDataRepository: AirDataRepository
|
|
val airDataDecoder = AirDataDecoder.instance
|
val dataPackageDecoder = DataPackageDecoderImpl()
|
|
@PostConstruct
|
fun init() {
|
instance = this
|
airDataRepository = this.airDataRepository
|
}
|
|
fun dealStringMsg(msg: String, ctx: ChannelHandlerContext?) {
|
//解包
|
val packageData = airDataDecoder.decode(msg)
|
|
|
if (bccCheck(msg)) {
|
//保存
|
DeviceSession.saveDevice(packageData.deviceCode, ctx)
|
// saveToTxt(msg)
|
saveToDataBase(packageData)
|
} else {
|
println("------数据BCC校验失败,舍弃 [${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())}]")
|
}
|
}
|
|
/**
|
* 保存至txt文本
|
*/
|
fun saveToTxt(msg: String) {
|
val data = "[${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())}]data=> $msg"
|
FileUtil.instance?.saveObdData(data)
|
}
|
|
/**
|
* 保存至数据库
|
*/
|
fun saveToDataBase(dataPackage: AirDataPackage) {
|
when (dataPackage.commandUnit) {
|
AirCommandUnit.AirData.value -> instance.airDataRepository.saveAirData(dataPackage)
|
}
|
}
|
|
/**
|
* BCC(异或校验)
|
*/
|
fun bccCheck(msg: String):Boolean {
|
val list = mutableListOf<String>().apply {
|
addAll(dataPackageDecoder.toStringList(msg))
|
}
|
//取得数据包中的bcc校验结果
|
val oldBcc = "${list[list.lastIndex - 1]}${list[list.lastIndex]}".toInt(16)
|
|
//去除校验结果
|
list.removeAt(list.lastIndex)
|
list.removeAt(list.lastIndex)
|
|
//计算bcc校验结果
|
var newBcc = 0x00
|
list.forEach {
|
newBcc = newBcc.xor(it.toInt(16))
|
}
|
|
//返回校验结果是否正确
|
return oldBcc == newBcc
|
}
|
|
|
fun encodeToBytes(msg:String): ByteArray {
|
val list = msg.split(" ")
|
val bytes = ByteArray(list.size)
|
for (i in 0 until list.size) {
|
bytes[i]=list[i].toInt(16).toByte()
|
}
|
|
return bytes
|
}
|
|
fun getDataPackage(deviceCode: String?): String? {
|
if (deviceCode == null) return null
|
//23 23 7f 31 37 36 39 31 35 33 31 39 30 39 31 32 30 30 31 31 01 01 00 00 39
|
val sb = StringBuilder("23 23 10 ")
|
deviceCode.forEach {
|
sb.append(it.toInt().toString(16)).append(" ")
|
}
|
sb.append("01 01 00 00 00 0A 41 54 2B 56 45 52 53 49 4F 4E")
|
|
val list = sb.split(" ")
|
|
//计算bcc校验结果
|
var bcc = 0x00
|
list.forEach {
|
bcc = bcc.xor(it.toInt(16))
|
}
|
|
sb.append(" ").append(bcc.toString(16))
|
|
return sb.toString()
|
}
|
}
|