Commit dce6c9e0 authored by shiyubin's avatar shiyubin 🌼

http请求延迟处理

parent ffe12602
...@@ -11,7 +11,7 @@ public class NettyApplication { ...@@ -11,7 +11,7 @@ public class NettyApplication {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(NettyApplication.class, args); SpringApplication.run(NettyApplication.class, args);
new nettyServer(9999); new nettyServer(9997);
} }
} }
package com.gemho.netty.controller; package com.gemho.netty.controller;
import cn.hutool.core.util.URLUtil;
import cn.hutool.http.HttpUtil;
import com.gemho.netty.model.ResultModel; import com.gemho.netty.model.ResultModel;
import com.gemho.netty.util.CRC16Util; import com.gemho.netty.util.CRC16Util;
import com.gemho.netty.util.MapUtils; import com.gemho.netty.util.MapUtils;
...@@ -10,14 +8,9 @@ import com.gemho.netty.util.StringUtil; ...@@ -10,14 +8,9 @@ import com.gemho.netty.util.StringUtil;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import io.netty.channel.Channel; import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.net.URLEncoder;
@RestController @RestController
public class SocketController { public class SocketController {
...@@ -30,7 +23,9 @@ public class SocketController { ...@@ -30,7 +23,9 @@ public class SocketController {
*/ */
@GetMapping("/clear") @GetMapping("/clear")
public String clearDiaplay(String key, String code) { public String clearDiaplay(String key, String code) {
String CLEAR_CODE = "0000000400006050"; // 6x6大屏: C0
// 小平指令: 60
String CLEAR_CODE = "000000040000C050";
code = code + CLEAR_CODE; code = code + CLEAR_CODE;
int i = CRC16Util.calcCrc16(StringUtil.hexString2Bytes(code)); int i = CRC16Util.calcCrc16(StringUtil.hexString2Bytes(code));
code = (code + StringUtil.hightAddZero(StringUtil.encodeHEX(i))).toUpperCase(); code = (code + StringUtil.hightAddZero(StringUtil.encodeHEX(i))).toUpperCase();
...@@ -44,7 +39,6 @@ public class SocketController { ...@@ -44,7 +39,6 @@ public class SocketController {
bufff.retain(); bufff.retain();
mapChannel.writeAndFlush(bufff); mapChannel.writeAndFlush(bufff);
// bufff.retain(); // bufff.retain();
// 返回失败指令时使用 // 返回失败指令时使用
if (!ParamUtil.findMapContainerKey("clear" + mapChannel.remoteAddress().toString())) { if (!ParamUtil.findMapContainerKey("clear" + mapChannel.remoteAddress().toString())) {
ResultModel model = new ResultModel(); ResultModel model = new ResultModel();
...@@ -52,7 +46,22 @@ public class SocketController { ...@@ -52,7 +46,22 @@ public class SocketController {
model.setHexCodeByte(StringUtil.hexString2Bytes(code)); model.setHexCodeByte(StringUtil.hexString2Bytes(code));
ParamUtil.addMap("clear" + mapChannel.remoteAddress().toString(), model); ParamUtil.addMap("clear" + mapChannel.remoteAddress().toString(), model);
} }
long timeMillis = System.currentTimeMillis();
while(true) {
String resCode = nettyHandler.map.get("clear" + mapChannel.remoteAddress().toString());
if(resCode != null) {
if ("00".equals(resCode)) {
nettyHandler.map.remove("clear" + mapChannel.remoteAddress().toString());
return "success"; return "success";
}else if("01".equals(resCode)) {
nettyHandler.map.remove("clear" + mapChannel.remoteAddress().toString());
return "fail";
}
}else if (System.currentTimeMillis() -timeMillis >= 3000) {
nettyHandler.map.remove("clear" + mapChannel.remoteAddress().toString());
return "未获取到返回的指令";
}
}
} }
...@@ -67,8 +76,10 @@ public class SocketController { ...@@ -67,8 +76,10 @@ public class SocketController {
* @return String * @return String
*/ */
@GetMapping("/display") @GetMapping("/display")
public String display(String key, String code, Integer x, Integer y, String font,String content) { public synchronized String display(String key, String code, Integer x, Integer y, String font, String content) {
try{ try{
// 获取通道
Channel mapChannel = MapUtils.getMapChannel(key);
String DISPLAY_CODE = "0001"; String DISPLAY_CODE = "0001";
// 指令处理 // 指令处理
String xHex = Integer.toHexString(x).length() == 1? "0"+ Integer.toHexString(x) : Integer.toHexString(x); String xHex = Integer.toHexString(x).length() == 1? "0"+ Integer.toHexString(x) : Integer.toHexString(x);
...@@ -83,10 +94,11 @@ public class SocketController { ...@@ -83,10 +94,11 @@ public class SocketController {
// 数据封装 // 数据封装
ByteBuf bufff = Unpooled.buffer(); ByteBuf bufff = Unpooled.buffer();
bufff.writeBytes(StringUtil.hexString2Bytes(code)); bufff.writeBytes(StringUtil.hexString2Bytes(code));
// 获取通道
Channel mapChannel = MapUtils.getMapChannel(key);
mapChannel.writeAndFlush(bufff);
System.out.println("===================================是否执行");
mapChannel.writeAndFlush(bufff);
// bufff.retain(); // bufff.retain();
// 返回失败指令时使用 // 返回失败指令时使用
if (!ParamUtil.findMapContainerKey("display" + mapChannel.remoteAddress().toString())) { if (!ParamUtil.findMapContainerKey("display" + mapChannel.remoteAddress().toString())) {
...@@ -95,19 +107,37 @@ public class SocketController { ...@@ -95,19 +107,37 @@ public class SocketController {
model.setHexCodeByte(StringUtil.hexString2Bytes(code)); model.setHexCodeByte(StringUtil.hexString2Bytes(code));
ParamUtil.addMap("display" + mapChannel.remoteAddress().toString(), model); ParamUtil.addMap("display" + mapChannel.remoteAddress().toString(), model);
} }
// 获取当前时间
long time1= System.currentTimeMillis();
while (true) {
String ydData= nettyHandler.map.get("display" + mapChannel.remoteAddress().toString());
if (ydData != null) {
if (ydData.equals("00")) {
nettyHandler.map.remove("display" + mapChannel.remoteAddress().toString());
return "success"; return "success";
}catch(Exception e) { } else if (ydData.equals("01")) {
nettyHandler.map.remove("display" + mapChannel.remoteAddress().toString());
return "exefail";
}
} else if (System.currentTimeMillis()-time1 >= 3000) {
nettyHandler.map.remove("display" + mapChannel.remoteAddress().toString());
return "未获取到返回的指令";
}
}
} catch(Exception e) {
e.printStackTrace(); e.printStackTrace();
return null; return null;
} }
} }
@GetMapping("/test")
public void test(String key) throws Exception{ /* @GetMapping("/test")
public void test(String key) {
System.out.println(key + "======================="); System.out.println(key + "=======================");
String body = HttpUtil.createGet("http://localhost:9998/display?key=864333042029678&code=01&x=0&y=12&font=31&content=温 度:33.5℃").execute().body(); String body = HttpUtil.createGet("http://localhost:9998/display?key=864333046020715&code=01&x=0&y=12&font=31&content=温 度:33.5℃").execute().body();
// Thread.sleep(500); // Thread.sleep(500);
String body1 = HttpUtil.createGet("http://localhost:9998/display?key=864333042029678&code=01&x=0&y=24&font=31&content=湿 度:33.5%RH").execute().body(); String body1 = HttpUtil.createGet("http://localhost:9998/display?key=864333046020715&code=01&x=0&y=24&font=31&content=湿 度:33.5%RH").execute().body();
// Thread.sleep(500); // Thread.sleep(500);
String body2 = HttpUtil.createGet("http://localhost:9998/display?key=864333042029678&code=01&x=0&y=36&font=31&content=CO2:450ppm").execute().body(); String body2 = HttpUtil.createGet("http://localhost:9998/display?key=864333046020715&code=01&x=0&y=36&font=31&content=CO2:450ppm").execute().body();
} }*/
} }
...@@ -7,9 +7,14 @@ import io.netty.buffer.Unpooled; ...@@ -7,9 +7,14 @@ import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.channel.ChannelInboundHandlerAdapter;
import java.util.LinkedHashMap;
import java.util.Map;
public class nettyHandler extends ChannelInboundHandlerAdapter { public class nettyHandler extends ChannelInboundHandlerAdapter {
static Map<String, String> map = new LinkedHashMap<>();
@Override @Override
public void channelActive(ChannelHandlerContext ctx) { public void channelActive(ChannelHandlerContext ctx) {
System.out.println("当前连接信息:" + ctx.channel().remoteAddress()); System.out.println("当前连接信息:" + ctx.channel().remoteAddress());
...@@ -41,16 +46,24 @@ public class nettyHandler extends ChannelInboundHandlerAdapter { ...@@ -41,16 +46,24 @@ public class nettyHandler extends ChannelInboundHandlerAdapter {
byte[] newBys = new byte[1]; byte[] newBys = new byte[1];
System.arraycopy(bys, 5, newBys, 0, 1); System.arraycopy(bys, 5, newBys, 0, 1);
System.out.println("==============result:" + StringUtil.bytesToHexString(newBys)); System.out.println("==============result:" + StringUtil.bytesToHexString(newBys));
byte[] typeByte = new byte[2];
System.arraycopy(bys, 1, typeByte, 0, 2);
System.out.println("00:清屏异常==01:屏幕显示异常============返回异常信息指令:" + StringUtil.bytesToHexString(newBys));
if ("00".equals(StringUtil.bytesToHexString(newBys))) { if ("00".equals(StringUtil.bytesToHexString(newBys))) {
System.out.println("正常:大屏接收到数据"); System.out.println("正常:大屏接收到数据");
if ("0001".equals(StringUtil.bytesToHexString(typeByte))) {
map.put("display" + ctx.channel().remoteAddress().toString(), "00");
} else { } else {
byte[] typeByte = new byte[2]; map.put("clear" + ctx.channel().remoteAddress().toString(), "00");
System.arraycopy(bys, 1, typeByte, 0, 2); }
System.out.println("0000:清屏异常==0001:屏幕显示异常============返回异常信息指令:" + StringUtil.bytesToHexString(typeByte)); } else {
System.out.println("失败:大屏未接收到数据");
if ("0001".equals(StringUtil.bytesToHexString(typeByte))) { if ("0001".equals(StringUtil.bytesToHexString(typeByte))) {
this.processResult("display" + ctx.channel().remoteAddress().toString(), ctx); this.processResult("display" + ctx.channel().remoteAddress().toString(), ctx);
map.put("display" + ctx.channel().remoteAddress().toString(), "01");
} else { } else {
this.processResult("clear" + ctx.channel().remoteAddress().toString(), ctx); this.processResult("clear" + ctx.channel().remoteAddress().toString(), ctx);
map.put("clear" + ctx.channel().remoteAddress().toString(), "01");
} }
} }
}else { }else {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment