From c9a3c06b37b5b2eb9b02d4e6348e5c53145284d9 Mon Sep 17 00:00:00 2001 From: riku <risaku@163.com> Date: 星期一, 16 九月 2019 17:40:20 +0800 Subject: [PATCH] 1. 添加注释 2. 添加粘包分包解码器 3. 修改数据单元信息体获取逻辑 4. 修改接收数据转换后的string列表,展示为16进制数时,小于16的应该在前面补0,否则之后计算会出错 --- src/main/kotlin/com/flightfeather/obd/socket/MessageManager.kt | 77 ++++++++++++++++++++++++++++++-------- 1 files changed, 61 insertions(+), 16 deletions(-) diff --git a/src/main/kotlin/com/flightfeather/obd/socket/MessageManager.kt b/src/main/kotlin/com/flightfeather/obd/socket/MessageManager.kt index b709839..626194f 100644 --- a/src/main/kotlin/com/flightfeather/obd/socket/MessageManager.kt +++ b/src/main/kotlin/com/flightfeather/obd/socket/MessageManager.kt @@ -1,14 +1,16 @@ package com.flightfeather.obd.socket -import com.flightfeather.obd.lightshare.bean.BaseJson -import com.flightfeather.obd.lightshare.bean.ObdDataVo +import com.flightfeather.obd.common.utils.FileUtil import com.flightfeather.obd.repository.ObdDataRepository -import com.google.gson.Gson +import com.flightfeather.obd.socket.bean.ObdPackageData +import com.flightfeather.obd.socket.decoder.VehicleDataDecoder +import com.flightfeather.obd.socket.decoder.impl.DataPackageDecoderImpl 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 -import javax.annotation.Resource /** * 澶勭悊socket鎺ユ敹鐨勬秷鎭� @@ -26,24 +28,67 @@ @Autowired lateinit var obdDataRepository: ObdDataRepository + val vehicleDataDecoder = VehicleDataDecoder() + val dataPackageDecoder = DataPackageDecoderImpl() + @PostConstruct fun init() { instance = this instance.obdDataRepository = this.obdDataRepository } - fun dealMsg(msg: String, ctx: ChannelHandlerContext?) { - try { - val baseJson = Gson().fromJson<BaseJson>(msg, BaseJson::class.java) - when (baseJson.cmdCode) { - 2001 -> { - val data = Gson().fromJson(msg, ObdDataVo::class.java) - DeviceSession.saveDevice(data.obdVin, ctx) - instance.obdDataRepository.saveObdData(data) - } - } - } catch (e: Throwable) { -// println("------鏀跺埌鏍煎紡閿欒鐨勬暟鎹細$msg") + fun dealStringMsg(msg: String, ctx: ChannelHandlerContext?) { + saveToTxt(msg) + + if (bccCheck(msg)) { + //瑙e寘 + val packageData = vehicleDataDecoder.decode(msg) + //淇濆瓨 + DeviceSession.saveDevice(packageData.deviceCode, ctx) + saveToDataBase(packageData) + } else { + println("------鏁版嵁BCC鏍¢獙澶辫触锛岃垗寮� [${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())}]") } } + + /** + * 淇濆瓨鑷硉xt鏂囨湰 + */ + fun saveToTxt(msg: String) { + val data = "[${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())}]data=> $msg" + FileUtil.instance?.saveObdData(data) + } + + /** + * 淇濆瓨鑷虫暟鎹簱 + */ + fun saveToDataBase(packageData: ObdPackageData) { + instance.obdDataRepository.saveObdData(packageData) + } + + /** + * BCC锛堝紓鎴栨牎楠岋級 + */ + fun bccCheck(msg: String):Boolean { + val list = mutableListOf<String>().apply { + addAll(dataPackageDecoder.toStringList(msg)) + //鍘婚櫎2 浣嶈捣濮嬬 + removeAt(0) + removeAt(0) + } + //鍙栧緱鏁版嵁鍖呬腑鐨刡cc鏍¢獙缁撴灉 + val oldBcc = list[list.size - 1].toInt(16) + + //鍘婚櫎鏍¢獙缁撴灉 + list.removeAt(list.size-1) + + //璁$畻bcc鏍¢獙缁撴灉 + var newBcc = 0x00 + list.forEach { + newBcc = newBcc.xor(it.toInt(16)) + } + + //杩斿洖鏍¢獙缁撴灉鏄惁姝g‘ + return oldBcc == newBcc + } } \ No newline at end of file -- Gitblit v1.9.3