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/DeviceSession.kt       |    5 ++
 src/main/kotlin/com/flightfeather/obd/socket/eunm/ObdCommandUnit.kt |    4 +
 src/main/kotlin/com/flightfeather/obd/socket/MessageManager.kt      |   86 +++++++++++++++++++++++++++++++++++++++---
 src/main/kotlin/com/flightfeather/obd/socket/ServerHandler.kt       |    2 
 src/main/kotlin/com/flightfeather/obd/socket/SocketServerClient.kt  |    3 +
 5 files changed, 89 insertions(+), 11 deletions(-)

diff --git a/src/main/kotlin/com/flightfeather/obd/socket/DeviceSession.kt b/src/main/kotlin/com/flightfeather/obd/socket/DeviceSession.kt
index 8cd64a8..b53955e 100644
--- a/src/main/kotlin/com/flightfeather/obd/socket/DeviceSession.kt
+++ b/src/main/kotlin/com/flightfeather/obd/socket/DeviceSession.kt
@@ -20,7 +20,10 @@
         }
 
         fun getDevice(deviceCode: String?): ChannelHandlerContext? {
-            return if (deviceMap.contains(deviceCode)) deviceMap[deviceCode] else null
+            return if (deviceMap.containsKey(deviceCode))
+                deviceMap[deviceCode]
+            else
+                null
         }
     }
 }
\ No newline at end of file
diff --git a/src/main/kotlin/com/flightfeather/obd/socket/MessageManager.kt b/src/main/kotlin/com/flightfeather/obd/socket/MessageManager.kt
index e57d960..ba99110 100644
--- a/src/main/kotlin/com/flightfeather/obd/socket/MessageManager.kt
+++ b/src/main/kotlin/com/flightfeather/obd/socket/MessageManager.kt
@@ -9,6 +9,7 @@
 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
@@ -57,16 +58,23 @@
     }
 
     fun dealStringMsg(msg: String, ctx: ChannelHandlerContext?) {
+        //瑙e寘
+        val packageData = vehicleDataDecoder.decode(msg)
+
         saveToTxt(msg)
 
-        if (bccCheck(msg)) {
-            //瑙e寘
-            val packageData = vehicleDataDecoder.decode(msg)
-            //淇濆瓨
-            DeviceSession.saveDevice(packageData.deviceCode, ctx)
-            saveToDataBase(packageData, msg)
+        val isCommand = command(packageData, ctx)
+
+        if (isCommand) {
+            println("------杩滅▼鎺у埗鎸囦护 [${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())}]")
         } else {
-            println("------鏁版嵁BCC鏍¢獙澶辫触锛岃垗寮� [${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())}]")
+            if (bccCheck(msg)) {
+                //淇濆瓨
+                DeviceSession.saveDevice(packageData.deviceCode, ctx)
+                saveToDataBase(packageData, msg)
+            } else {
+                println("------鏁版嵁BCC鏍¢獙澶辫触锛岃垗寮� [${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())}]")
+            }
         }
     }
 
@@ -130,4 +138,68 @@
         //杩斿洖鏍¢獙缁撴灉鏄惁姝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
diff --git a/src/main/kotlin/com/flightfeather/obd/socket/ServerHandler.kt b/src/main/kotlin/com/flightfeather/obd/socket/ServerHandler.kt
index 475a87b..018a932 100644
--- a/src/main/kotlin/com/flightfeather/obd/socket/ServerHandler.kt
+++ b/src/main/kotlin/com/flightfeather/obd/socket/ServerHandler.kt
@@ -40,7 +40,7 @@
                 } else {
                     a.toString(16)
                 }
-                print(s)
+                print("$s ")
                 sb.append(s).append(" ")
             }
             sb.deleteCharAt(sb.length - 1)
diff --git a/src/main/kotlin/com/flightfeather/obd/socket/SocketServerClient.kt b/src/main/kotlin/com/flightfeather/obd/socket/SocketServerClient.kt
index 80904b7..5ee34b1 100644
--- a/src/main/kotlin/com/flightfeather/obd/socket/SocketServerClient.kt
+++ b/src/main/kotlin/com/flightfeather/obd/socket/SocketServerClient.kt
@@ -13,6 +13,7 @@
 import io.netty.handler.codec.bytes.ByteArrayEncoder
 import io.netty.handler.codec.string.StringDecoder
 import io.netty.handler.codec.string.StringEncoder
+import java.nio.charset.Charset
 
 /**
  * socket闀胯繛鎺ユ湇鍔$
@@ -46,8 +47,8 @@
                         override fun initChannel(p0: NioSocketChannel?) {
                             p0?.pipeline()
 //                                    ?.addLast("decoder", StringDecoder())
-//                                    ?.addLast("encoder", StringEncoder())
                                     ?.addLast(ObdByteDataDecoder())
+                                    ?.addLast("encoder", StringEncoder(Charset.forName("UTF-8")))
                                     ?.addLast(ServerHandler())
                         }
                     })
diff --git a/src/main/kotlin/com/flightfeather/obd/socket/eunm/ObdCommandUnit.kt b/src/main/kotlin/com/flightfeather/obd/socket/eunm/ObdCommandUnit.kt
index 83956ca..5ef15c2 100644
--- a/src/main/kotlin/com/flightfeather/obd/socket/eunm/ObdCommandUnit.kt
+++ b/src/main/kotlin/com/flightfeather/obd/socket/eunm/ObdCommandUnit.kt
@@ -15,11 +15,13 @@
  * 0x04            杞﹁締鐧诲嚭                    涓婅 @see [CarLogOutData]
  * 0x05            缁堢鏍℃椂                    涓婅 @see [TimeCalibrationData]
  * 0x06~0x7F 涓婅鏁版嵁绯荤粺棰勭暀      涓婅
+ * 0x7F            鍥轰欢杩滅▼鏇存柊
  */
 enum class ObdCommandUnit constructor(val value: Int) {
     CarRegister(1),
     RealTimeData(2),
     ReplacementData(3),
     CarLogOut(4),
-    TimeCalibration(5)
+    TimeCalibration(5),
+    Update(127)
 }
\ No newline at end of file

--
Gitblit v1.9.3