|
|
@@ -0,0 +1,150 @@
|
|
|
+package com.zd.laboratory.netty;
|
|
|
+
|
|
|
+import com.zd.common.core.redis.RedisService;
|
|
|
+import com.zd.common.core.utils.ReUtil;
|
|
|
+import com.zd.common.core.utils.SpringUtils;
|
|
|
+import com.zd.laboratory.socket.runner.TCPServer;
|
|
|
+import com.zd.laboratory.utils.CRCCHECK;
|
|
|
+import io.netty.channel.ChannelHandlerContext;
|
|
|
+import io.netty.channel.ChannelInboundHandler;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+
|
|
|
+import java.nio.ByteBuffer;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 服务器端handler
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+public class NettyServerHandler implements ChannelInboundHandler {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 读取客户端发送的消息
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void channelRead(ChannelHandlerContext channelHandlerContext, Object o) throws Exception {
|
|
|
+ byte[] msg = (byte[]) o;
|
|
|
+ String data = TCPServer.bytesToHexString(msg).toUpperCase();
|
|
|
+ log.info("netty服务端接受消息为:" + data);
|
|
|
+ if(data.equals("6862")){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ log.info("netty服务端接受消息转10进制为:" + ByteBuffer.wrap(msg).getLong());
|
|
|
+// if(data.startsWith(SocketTypes.LOCK_PREFIX)){
|
|
|
+// if(data.startsWith("33")){
|
|
|
+
|
|
|
+ String relayCode = data.substring(0, 108);
|
|
|
+// 33 31 20 33 30 20 36 33 20 33 36 20 36 34 20 33 37 20 33 36 20 33 33 20 33 31 20 33 35 20 33 31 20 33 35 20 33 30 20 36 33 20 33 33 20 33 35 20 33 32 20 33 37 20
|
|
|
+
|
|
|
+// 05 05 00 01 ee 00 d0 2e
|
|
|
+
|
|
|
+ // 将通道加入ChannelMap
|
|
|
+ ChannelMap.getChannelMap().put(data, channelHandlerContext);
|
|
|
+// }else {
|
|
|
+// if(data.length() != 12){
|
|
|
+// log.info("netty柜锁回调指令非状态指令!" + data);
|
|
|
+// return;
|
|
|
+// }
|
|
|
+
|
|
|
+// AtomicReference<String> relayCode = new AtomicReference<>("");
|
|
|
+// ChannelMap.getChannelMap().entrySet().forEach(a -> {
|
|
|
+// if(a.getValue() == channelHandlerContext){
|
|
|
+// relayCode.set(a.getKey());
|
|
|
+// }
|
|
|
+// });
|
|
|
+
|
|
|
+ if(data.length() > relayCode.length()) {
|
|
|
+ data = data.replaceAll(relayCode, "");
|
|
|
+ // 柜锁bit 位
|
|
|
+ long bit = CRCCHECK.getBitByCommand(data);
|
|
|
+ // 1开锁状态 0关锁状态
|
|
|
+ int status = CRCCHECK.getLockStatus(data);
|
|
|
+
|
|
|
+ log.info("netty柜锁回调:" + relayCode + ":" + bit + ",回调结果" + (status == 1 ? "开启": "关闭") + ",指令:" + data);
|
|
|
+
|
|
|
+ RedisService redisService = SpringUtils.getBean(RedisService.class);
|
|
|
+ redisService.setCacheObject(relayCode + ":" + bit, status, 3 * 60L, TimeUnit.SECONDS);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @description: 读取消息后开始的操作
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void channelReadComplete(ChannelHandlerContext channelHandlerContext) throws Exception {
|
|
|
+ String instruct = CRCCHECK.getOpenLockOrder(1);
|
|
|
+
|
|
|
+ byte[] bytes = ReUtil.hexStringToByteArray(instruct);
|
|
|
+// log.info("netty服务端数据发送数据:" + instruct);
|
|
|
+// ChannelFuture channelFuture = channelHandlerContext.writeAndFlush(bytes);
|
|
|
+// ChannelFuture channelFuture = channelHandlerContext.writeAndFlush(instruct);
|
|
|
+// channelFuture.addListener(new ChannelFutureListener() {
|
|
|
+// @Override
|
|
|
+// public void operationComplete(ChannelFuture channelFuture) throws Exception {
|
|
|
+// if(channelFuture.isSuccess()){
|
|
|
+// log.info("服务端数据发送成功");
|
|
|
+// }else{
|
|
|
+// log.info("服务端数据发送失败");
|
|
|
+// }
|
|
|
+// }
|
|
|
+// });
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @description: 异常发生时
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void exceptionCaught(ChannelHandlerContext channelHandlerContext, Throwable throwable) throws Exception {
|
|
|
+ throwable.printStackTrace();
|
|
|
+ channelHandlerContext.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void channelRegistered(ChannelHandlerContext channelHandlerContext) throws Exception {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void channelUnregistered(ChannelHandlerContext channelHandlerContext) throws Exception {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void channelActive(ChannelHandlerContext channelHandlerContext) throws Exception {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void channelInactive(ChannelHandlerContext channelHandlerContext) throws Exception {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void userEventTriggered(ChannelHandlerContext channelHandlerContext, Object o) throws Exception {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void channelWritabilityChanged(ChannelHandlerContext channelHandlerContext) throws Exception {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void handlerAdded(ChannelHandlerContext channelHandlerContext) throws Exception {
|
|
|
+ log.info("netty客户端开始连接" + channelHandlerContext.toString());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void handlerRemoved(ChannelHandlerContext channelHandlerContext) throws Exception {
|
|
|
+ log.info("netty客户端断开连接" + channelHandlerContext.toString());
|
|
|
+ ChannelMap.getChannelMap().entrySet().forEach(a -> {
|
|
|
+ if(a.getValue() == channelHandlerContext.channel()){
|
|
|
+ ChannelMap.getChannelMap().remove(a.getKey());
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|