From b77a69357da82a965cd63e76e8ceaf625ec4009b Mon Sep 17 00:00:00 2001
From: riku <risaku@163.com>
Date: 星期四, 15 七月 2021 16:37:47 +0800
Subject: [PATCH] 1. 新增信访信息获取接口 2. 新增用电量信息获取接口 3. 新增风险评估结果接口 4. 新增污染权重实时分析接口

---
 src/main/kotlin/com/flightfeather/uav/socket/ServerHandler.kt |   63 +++++++++++++++++++------------
 1 files changed, 39 insertions(+), 24 deletions(-)

diff --git a/src/main/kotlin/com/flightfeather/uav/socket/ServerHandler.kt b/src/main/kotlin/com/flightfeather/uav/socket/ServerHandler.kt
index 9a9b790..27954b2 100644
--- a/src/main/kotlin/com/flightfeather/uav/socket/ServerHandler.kt
+++ b/src/main/kotlin/com/flightfeather/uav/socket/ServerHandler.kt
@@ -1,56 +1,71 @@
 package com.flightfeather.uav.socket
 
+import com.flightfeather.uav.socket.processor.BaseProcessor
 import io.netty.channel.ChannelHandlerContext
 import io.netty.channel.ChannelInboundHandlerAdapter
 import io.netty.util.AttributeKey
+import org.ietf.jgss.MessageProp
 import java.lang.StringBuilder
 import java.text.SimpleDateFormat
 import java.util.*
 
 
-class ServerHandler : ChannelInboundHandlerAdapter() {
+class ServerHandler(private val processor: BaseProcessor) : ChannelInboundHandlerAdapter() {
 
     val attributeKey = AttributeKey.valueOf<String>("deviceCode")
 
     override fun channelRegistered(ctx: ChannelHandlerContext?) {
         super.channelRegistered(ctx)
-        println("------绔彛鏈塈P杩炴帴锛歔ip:${ctx?.channel()?.remoteAddress()}] ${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())}")
+        println("------銆�${processor.tag}銆慖P杩炴帴锛歔ip:${ctx?.channel()?.remoteAddress()}] ${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())}")
 //        ctx?.fireChannelActive()
     }
 
     override fun channelActive(ctx: ChannelHandlerContext?) {
-        println("------绔彛鏈塈P婵�娲伙細[ip:${ctx?.channel()?.remoteAddress()}] ${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())}")
+        println("------銆�${processor.tag}銆慖P婵�娲伙細[ip:${ctx?.channel()?.remoteAddress()}] ${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())}")
         super.channelActive(ctx)
     }
 
     override fun channelRead(ctx: ChannelHandlerContext?, msg: Any?) {
         super.channelRead(ctx, msg)
+        println("------銆�${processor.tag}銆戞敹鍒扮殑鍘熷鏁版嵁锛歔ip:${ctx?.channel()?.remoteAddress()}] ${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())}")
 
-        val sb = StringBuilder()
+        var str = ""
 
-        if (msg is ByteArray) {
-            println("------鏀跺埌鐨勫師濮嬫暟鎹細[ip:${ctx?.channel()?.remoteAddress()}] ${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())}")
-            msg.forEach {
-                val a: Int = if (it < 0) {
-                    it + 256
-                } else {
-                    it.toInt()
+        when (msg) {
+            is ByteArray -> {
+                val sb = StringBuilder()
+
+                msg.forEach {
+                    var a = 0
+                    a = if (it < 0) {
+                        it + 256
+                    } else {
+                        it.toInt()
+                    }
+                    val s = if (a < 16) {
+                        "0${a.toString(16)}"
+                    } else {
+                        a.toString(16)
+                    }
+                    sb.append(s).append(" ")
                 }
-                val s = if (a < 16) {
-                    "0${a.toString(16)}"
-                } else {
-                    a.toString(16)
-                }
-                print("$s ")
-                sb.append(s).append(" ")
+                sb.deleteCharAt(sb.length - 1)
+
+                str = sb.toString()
             }
-            sb.deleteCharAt(sb.length - 1)
-        }
-        val str = sb.toString()
-        if (str.isNotEmpty()) {
-            MessageManager().dealStringMsg(str, ctx)
+            is String -> str = msg
         }
 
+        println(str)
+
+        if (str == "01 04 00 00 00 00 00 00 00 00 00") {
+            ctx?.writeAndFlush("trigger")
+            return
+        }
+        if (str.isNotEmpty()) {
+            ctx?.writeAndFlush("data")
+            processor.dealStringMsg(str, ctx)
+        }
     }
 
     override fun channelReadComplete(ctx: ChannelHandlerContext?) {
@@ -58,7 +73,7 @@
     }
 
     override fun channelInactive(ctx: ChannelHandlerContext?) {
-        println("------绔彛鏈塈P涓嶆椿鍔細[ip:${ctx?.channel()?.remoteAddress()}] ${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())}")
+        println("------銆�${processor.tag}銆戠鍙f湁IP涓嶆椿鍔細[ip:${ctx?.channel()?.remoteAddress()}] ${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())}")
         super.channelInactive(ctx)
     }
 

--
Gitblit v1.9.3