| | |
| | | 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() { |
| | | |
| | | companion object { |
| | | private const val TAG = "UAV" |
| | | } |
| | | class ServerHandler(private val processor: BaseProcessor) : ChannelInboundHandlerAdapter() { |
| | | |
| | | val attributeKey = AttributeKey.valueOf<String>("deviceCode") |
| | | val messageManager = MessageManager() |
| | | |
| | | override fun channelRegistered(ctx: ChannelHandlerContext?) { |
| | | super.channelRegistered(ctx) |
| | | println() |
| | | println("------${TAG}端口有IP连接:[ip:${ctx?.channel()?.remoteAddress()}] ${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())}") |
| | | println("------【${processor.tag}】IP连接:[ip:${ctx?.channel()?.remoteAddress()}] ${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())}") |
| | | // ctx?.fireChannelActive() |
| | | } |
| | | |
| | | override fun channelActive(ctx: ChannelHandlerContext?) { |
| | | println() |
| | | println("------${TAG}端口有IP激活:[ip:${ctx?.channel()?.remoteAddress()}] ${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())}") |
| | | println("------【${processor.tag}】IP激活:[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() |
| | | println("------${TAG}收到的原始数据:[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?) { |
| | |
| | | } |
| | | |
| | | override fun channelInactive(ctx: ChannelHandlerContext?) { |
| | | println("------${TAG}端口有IP不活动:[ip:${ctx?.channel()?.remoteAddress()}] ${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())}") |
| | | println("------【${processor.tag}】端口有IP不活动:[ip:${ctx?.channel()?.remoteAddress()}] ${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())}") |
| | | super.channelInactive(ctx) |
| | | } |
| | | |
| | | @Deprecated("Deprecated in Java") |
| | | override fun exceptionCaught(ctx: ChannelHandlerContext?, cause: Throwable?) { |
| | | cause?.printStackTrace() |
| | | ctx?.close() |