| | |
| | | package com.flightfeather.obd.socket |
| | | |
| | | import com.flightfeather.obd.common.utils.FileUtil |
| | | import com.flightfeather.obd.lightshare.bean.BaseJson |
| | | import com.flightfeather.obd.lightshare.bean.ObdDataVo |
| | | import com.flightfeather.obd.repository.ObdDataRepository |
| | | import com.flightfeather.obd.repository.* |
| | | import com.flightfeather.obd.socket.bean.EngineDataStream |
| | | import com.flightfeather.obd.socket.bean.ObdInfo |
| | | import com.flightfeather.obd.socket.bean.ObdPackageData |
| | | import com.flightfeather.obd.socket.bean.SupplementDataStream |
| | | import com.flightfeather.obd.socket.decoder.VehicleDataDecoder |
| | | import com.google.gson.Gson |
| | | import com.flightfeather.obd.socket.decoder.impl.DataPackageDecoderImpl |
| | | import com.flightfeather.obd.socket.eunm.ObdCommandUnit |
| | | import io.netty.channel.ChannelHandlerContext |
| | | import org.springframework.beans.factory.annotation.Autowired |
| | | import org.springframework.stereotype.Component |
| | | import org.springframework.util.FileSystemUtils |
| | | import java.text.SimpleDateFormat |
| | | import java.util.* |
| | | import javax.annotation.PostConstruct |
| | | import javax.annotation.Resource |
| | | |
| | | /** |
| | | * 处理socket接收的消息 |
| | |
| | | |
| | | @Autowired |
| | | lateinit var obdDataRepository: ObdDataRepository |
| | | @Autowired |
| | | lateinit var originDataRepository: OriginDataRepository |
| | | @Autowired |
| | | lateinit var obdInfoRepository: ObdInfoRepository |
| | | @Autowired |
| | | lateinit var dataStreamRepository: DataStreamRepository |
| | | @Autowired |
| | | lateinit var carLogoutRepository: CarLogoutRepository |
| | | @Autowired |
| | | lateinit var carLoginRepository: CarLoginRepository |
| | | |
| | | val vehicleDataDecoder = VehicleDataDecoder() |
| | | val dataPackageDecoder = DataPackageDecoderImpl() |
| | | |
| | | @PostConstruct |
| | | fun init() { |
| | | instance = this |
| | | instance.obdDataRepository = this.obdDataRepository |
| | | instance.originDataRepository = this.originDataRepository |
| | | instance.obdInfoRepository = this.obdInfoRepository |
| | | instance.dataStreamRepository = this.dataStreamRepository |
| | | instance.carLogoutRepository = this.carLogoutRepository |
| | | instance.carLoginRepository = this.carLoginRepository |
| | | } |
| | | |
| | | fun dealStringMsg(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) { |
| | | // } |
| | | |
| | | saveToTxt(msg) |
| | | val packageData = vehicleDataDecoder.decode(msg) |
| | | |
| | | if (bccCheck(msg)) { |
| | | //解包 |
| | | val packageData = vehicleDataDecoder.decode(msg) |
| | | //保存 |
| | | DeviceSession.saveDevice(packageData.deviceCode, ctx) |
| | | saveToDataBase(packageData, msg) |
| | | } else { |
| | | println("------数据BCC校验失败,舍弃 [${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())}]") |
| | | } |
| | | } |
| | | |
| | | fun dealByteArrayMsg(msg: ByteArray, ctx: ChannelHandlerContext?) { |
| | | val b = ByteArray(20) {19} |
| | | println(b) |
| | | } |
| | | |
| | | /** |
| | | * 保存至txt文本 |
| | | */ |
| | | fun saveToTxt(msg: String) { |
| | | val data = "data=> $msg" |
| | | val data = "[${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())}]data=> $msg" |
| | | FileUtil.instance?.saveObdData(data) |
| | | } |
| | | |
| | | /** |
| | | * 保存至数据库 |
| | | */ |
| | | fun saveToDataBase(packageData: ObdPackageData, msg: String) { |
| | | instance.obdDataRepository.saveObdData(packageData) |
| | | instance.originDataRepository.saveOriginData(packageData, msg) |
| | | when (packageData.commandUnit) { |
| | | ObdCommandUnit.CarRegister.value -> instance.carLoginRepository.saveCarLogin(packageData) |
| | | ObdCommandUnit.RealTimeData.value, |
| | | ObdCommandUnit.ReplacementData.value -> { |
| | | var done = false |
| | | for (i in 0 until packageData.dataUnit.size) { |
| | | when (packageData.dataUnit[i]) { |
| | | is ObdInfo -> instance.obdInfoRepository.saveObdInfo(packageData) |
| | | is EngineDataStream, |
| | | is SupplementDataStream -> { |
| | | instance.dataStreamRepository.saveDataStream(packageData) |
| | | done = true |
| | | } |
| | | } |
| | | if (done) break |
| | | } |
| | | } |
| | | ObdCommandUnit.CarLogOut.value-> instance.carLogoutRepository.saveCarLogout(packageData) |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * BCC(异或校验) |
| | | */ |
| | | fun bccCheck(msg: String):Boolean { |
| | | val list = mutableListOf<String>().apply { |
| | | addAll(dataPackageDecoder.toStringList(msg)) |
| | | //去除2 位起始符 |
| | | removeAt(0) |
| | | removeAt(0) |
| | | } |
| | | //取得数据包中的bcc校验结果 |
| | | 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)) |
| | | } |
| | | |
| | | //返回校验结果是否正确 |
| | | return oldBcc == newBcc |
| | | } |
| | | } |