| | |
| | | |
| | | import com.flightfeather.uav.common.utils.FileUtil |
| | | import com.flightfeather.uav.repository.* |
| | | import com.flightfeather.uav.socket.bean.EngineDataStream |
| | | import com.flightfeather.uav.socket.bean.ObdInfo |
| | | import com.flightfeather.uav.socket.bean.ObdPackageData |
| | | import com.flightfeather.uav.socket.bean.SupplementDataStream |
| | | import com.flightfeather.uav.socket.decoder.VehicleDataDecoder |
| | | 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.ObdCommandUnit |
| | | import com.flightfeather.uav.socket.eunm.AirCommandUnit |
| | | import io.netty.buffer.Unpooled |
| | | import io.netty.channel.ChannelHandlerContext |
| | | import org.springframework.beans.factory.annotation.Autowired |
| | |
| | | } |
| | | |
| | | @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 |
| | | lateinit var airDataRepository: AirDataRepository |
| | | |
| | | val vehicleDataDecoder = VehicleDataDecoder() |
| | | val vehicleDataDecoder = AirDataDecoder() |
| | | 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 |
| | | airDataRepository = this.airDataRepository |
| | | } |
| | | |
| | | fun dealStringMsg(msg: String, ctx: ChannelHandlerContext?) { |
| | | //解包 |
| | | 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())}]") |
| | | if (bccCheck(msg)) { |
| | | if (packageData.commandUnit == AirCommandUnit.Confirm.value) { |
| | | val t = mutableListOf<AirTypeData>() |
| | | packageData.dataUnit.forEach { |
| | | if (it is AirTypeData) { |
| | | t.add(it) |
| | | } |
| | | } |
| | | DeviceSession.saveAirType(packageData.deviceCode, t) |
| | | } else if (packageData.commandUnit == AirCommandUnit.AirData.value) { |
| | | val types = DeviceSession.getAirType(packageData.deviceCode) |
| | | } |
| | | //保存 |
| | | DeviceSession.saveDevice(packageData.deviceCode, ctx) |
| | | // saveToTxt(msg) |
| | | saveToDataBase(packageData) |
| | | } else { |
| | | println("------数据BCC校验失败,舍弃 [${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())}]") |
| | | } |
| | | } |
| | | |
| | |
| | | /** |
| | | * 保存至数据库 |
| | | */ |
| | | fun saveToDataBase(packageData: ObdPackageData, msg: String) { |
| | | // instance.obdDataRepository.saveObdData(packageData) |
| | | instance.originDataRepository.saveOriginData(packageData, msg) |
| | | fun saveToDataBase(packageData: AirPackageData) { |
| | | 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) |
| | | AirCommandUnit.AirData.value -> instance.airDataRepository.saveAirData(packageData) |
| | | } |
| | | } |
| | | |
| | |
| | | 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(" ") |