| | |
| | | import com.flightfeather.obd.socket.decoder.VehicleDataDecoder |
| | | import com.flightfeather.obd.socket.decoder.impl.DataPackageDecoderImpl |
| | | import com.flightfeather.obd.socket.eunm.ObdCommandUnit |
| | | import io.netty.buffer.Unpooled |
| | | import io.netty.channel.ChannelHandlerContext |
| | | import org.springframework.beans.factory.annotation.Autowired |
| | | import org.springframework.stereotype.Component |
| | |
| | | } |
| | | |
| | | fun dealStringMsg(msg: String, ctx: ChannelHandlerContext?) { |
| | | saveToTxt(msg) |
| | | |
| | | if (bccCheck(msg)) { |
| | | //解包 |
| | | val packageData = vehicleDataDecoder.decode(msg) |
| | | |
| | | saveToTxt(msg) |
| | | |
| | | val isCommand = command(packageData, ctx) |
| | | |
| | | if (isCommand) { |
| | | println("------远程控制指令 [${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())}]") |
| | | } else { |
| | | if (bccCheck(msg)) { |
| | | //保存 |
| | | DeviceSession.saveDevice(packageData.deviceCode, ctx) |
| | | saveToDataBase(packageData, msg) |
| | | } else { |
| | | println("------数据BCC校验失败,舍弃 [${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())}]") |
| | | } |
| | | } |
| | | } |
| | | |
| | |
| | | //返回校验结果是否正确 |
| | | return oldBcc == newBcc |
| | | } |
| | | |
| | | /** |
| | | * 远程指令 |
| | | */ |
| | | fun command(packageData: ObdPackageData, ctx: ChannelHandlerContext?): Boolean { |
| | | if (packageData.commandUnit == ObdCommandUnit.Update.value) { |
| | | val channel = DeviceSession.getDevice(packageData.deviceCode) |
| | | if (channel != null) { |
| | | val s = getDataPackage(packageData.deviceCode) |
| | | if (s == null) { |
| | | ctx?.writeAndFlush("[${packageData.deviceCode}] 设备编号为空") |
| | | } else { |
| | | val bytes = encodeToBytes(s) |
| | | channel.writeAndFlush(Unpooled.copiedBuffer(bytes)) |
| | | ctx?.writeAndFlush("[${packageData.deviceCode}] 指令发送成功") |
| | | } |
| | | } else { |
| | | ctx?.writeAndFlush("[${packageData.deviceCode}] 设备未连接或不存在") |
| | | } |
| | | return true |
| | | } else if (packageData.commandUnit != ObdCommandUnit.CarRegister.value |
| | | && packageData.commandUnit != ObdCommandUnit.RealTimeData.value |
| | | && packageData.commandUnit != ObdCommandUnit.ReplacementData.value |
| | | && packageData.commandUnit != ObdCommandUnit.CarLogOut.value |
| | | && packageData.commandUnit != ObdCommandUnit.TimeCalibration.value) { |
| | | ctx?.writeAndFlush("[${packageData.deviceCode}] 命令不存在") |
| | | |
| | | return true |
| | | } else { |
| | | return false |
| | | } |
| | | } |
| | | |
| | | 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() |
| | | } |
| | | } |