From 58a16c3340f92a1ec0362565020f31de56faaf3e Mon Sep 17 00:00:00 2001
From: riku <risaku@163.com>
Date: 星期四, 19 九月 2019 16:35:48 +0800
Subject: [PATCH] 1. 增加远程查看版本指令逻辑

---
 src/main/kotlin/com/flightfeather/obd/socket/MessageManager.kt |  186 ++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 171 insertions(+), 15 deletions(-)

diff --git a/src/main/kotlin/com/flightfeather/obd/socket/MessageManager.kt b/src/main/kotlin/com/flightfeather/obd/socket/MessageManager.kt
index 5d99a6e..ba99110 100644
--- a/src/main/kotlin/com/flightfeather/obd/socket/MessageManager.kt
+++ b/src/main/kotlin/com/flightfeather/obd/socket/MessageManager.kt
@@ -1,14 +1,21 @@
 package com.flightfeather.obd.socket
 
-import com.flightfeather.obd.lightshare.bean.BaseJson
-import com.flightfeather.obd.lightshare.bean.ObdDataVo
-import com.flightfeather.obd.repository.ObdDataRepository
-import com.google.gson.Gson
+import com.flightfeather.obd.common.utils.FileUtil
+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.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
+import java.text.SimpleDateFormat
+import java.util.*
 import javax.annotation.PostConstruct
-import javax.annotation.Resource
 
 /**
  * 澶勭悊socket鎺ユ敹鐨勬秷鎭�
@@ -25,25 +32,174 @@
 
     @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 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)
+    fun dealStringMsg(msg: String, ctx: ChannelHandlerContext?) {
+        //瑙e寘
+        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())}]")
+            }
+        }
+    }
+
+    /**
+     * 淇濆瓨鑷硉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, 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
                 }
             }
-        } catch (e: Throwable) {
-            println("------鏀跺埌鏍煎紡閿欒鐨勬暟鎹細$msg")
+            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)
+        }
+        //鍙栧緱鏁版嵁鍖呬腑鐨刡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
+    }
+
+    /**
+     * 杩滅▼鎸囦护
+     */
+    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()
+    }
 }
\ No newline at end of file

--
Gitblit v1.9.3