netty优化
This commit is contained in:
parent
23a93ac991
commit
dfb7492403
@ -1,4 +1,4 @@
|
||||
package com.zhgd.xmgl.modules.highformwork.netty.tcp.constant;
|
||||
package com.zhgd.netty.constant;
|
||||
|
||||
/**
|
||||
* 高支模常量
|
||||
110
src/main/java/com/zhgd/netty/handler/TcpNettyHandler.java
Normal file
110
src/main/java/com/zhgd/netty/handler/TcpNettyHandler.java
Normal file
@ -0,0 +1,110 @@
|
||||
package com.zhgd.netty.handler;
|
||||
|
||||
import com.zhgd.netty.constant.HighFormworkSupport;
|
||||
import com.zhgd.netty.service.HighFormworkSupportService;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import io.netty.channel.ChannelHandler;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.SimpleChannelInboundHandler;
|
||||
import io.netty.util.CharsetUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
|
||||
|
||||
@ChannelHandler.Sharable
|
||||
@Slf4j
|
||||
@Component
|
||||
public class TcpNettyHandler extends SimpleChannelInboundHandler<Object> {
|
||||
|
||||
@Autowired
|
||||
private HighFormworkSupportService highFormworkSupportService;
|
||||
|
||||
@Override
|
||||
public void channelRead0(ChannelHandlerContext ctx, Object msg) {
|
||||
byte[] bytes = readBytes(msg);
|
||||
|
||||
//字符串判断
|
||||
String str = getString(bytes);
|
||||
if (StringUtils.startsWith(str, HighFormworkSupport.TCP_DATA_PREFIX) && StringUtils.endsWith(str, HighFormworkSupport.TCP_DATA_END)) {
|
||||
log.info("I get the message >>> \r\n {} ", str);
|
||||
//接收高支模数据保存到mysql中
|
||||
str = str.trim();
|
||||
try {
|
||||
highFormworkSupportService.saveTcpData(str);
|
||||
} catch (Exception e) {
|
||||
log.error("error:接收高支模数据保存到mysql中exception: >>> \r\n ", e);
|
||||
} finally {
|
||||
highFormworkSupportResponseOk(ctx);
|
||||
}
|
||||
}
|
||||
|
||||
//16进制判断
|
||||
String hexString = readToHexString(bytes);
|
||||
if (StringUtils.equals(hexString, "7E")) {
|
||||
//有害气体
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private byte[] readBytes(Object msg) {
|
||||
//1.转换ByteBuf
|
||||
ByteBuf buffer = (ByteBuf) msg;
|
||||
//创建字节数组,buffer.readableBytes可读字节长度
|
||||
byte[] b = new byte[buffer.readableBytes()];
|
||||
//复制内容到字节数组b
|
||||
buffer.readBytes(b);
|
||||
return b;
|
||||
}
|
||||
|
||||
private String readToHexString(byte[] bytes) {
|
||||
return bytesToHexString(bytes);
|
||||
}
|
||||
|
||||
private String bytesToHexString(byte[] bArray) {
|
||||
StringBuffer sb = new StringBuffer(bArray.length);
|
||||
String sTemp;
|
||||
for (int i = 0; i < bArray.length; i++) {
|
||||
sTemp = Integer.toHexString(0xFF & bArray[i]);
|
||||
if (sTemp.length() < 2)
|
||||
sb.append(0);
|
||||
sb.append(sTemp.toUpperCase());
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private String getString(byte[] bytes) {
|
||||
String s = null;
|
||||
try {
|
||||
s = new String(bytes, "GBK");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
private void highFormworkSupportResponseOk(ChannelHandlerContext ctx) {
|
||||
ctx.writeAndFlush(Unpooled.copiedBuffer("$LRKKJ$ MSG:\"\"; OK;END", CharsetUtil.UTF_8));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelReadComplete(ChannelHandlerContext ctx) {
|
||||
//刷新缓冲区,把消息发出去
|
||||
ctx.flush();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
||||
//异常处理,如果该handler不处理,将会传递到下一个handler
|
||||
cause.printStackTrace();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
||||
log.info("channelActive:connect success!");
|
||||
}
|
||||
}
|
||||
@ -1,4 +1,4 @@
|
||||
package com.zhgd.xmgl.modules.highformwork.netty.tcp.listener;
|
||||
package com.zhgd.netty.listener;
|
||||
|
||||
import io.netty.util.concurrent.Future;
|
||||
import io.netty.util.concurrent.GenericFutureListener;
|
||||
@ -1,4 +1,4 @@
|
||||
package com.zhgd.xmgl.modules.highformwork.netty.tcp.listener;
|
||||
package com.zhgd.netty.listener;
|
||||
|
||||
import io.netty.util.concurrent.Future;
|
||||
import io.netty.util.concurrent.GenericFutureListener;
|
||||
@ -1,8 +1,8 @@
|
||||
package com.zhgd.xmgl.modules.highformwork.netty.tcp.server;
|
||||
package com.zhgd.netty.server;
|
||||
|
||||
import com.zhgd.xmgl.modules.highformwork.netty.tcp.handler.TcpNettyHandler;
|
||||
import com.zhgd.xmgl.modules.highformwork.netty.tcp.listener.BindListener;
|
||||
import com.zhgd.xmgl.modules.highformwork.netty.tcp.listener.CloseListener;
|
||||
import com.zhgd.netty.handler.TcpNettyHandler;
|
||||
import com.zhgd.netty.listener.BindListener;
|
||||
import com.zhgd.netty.listener.CloseListener;
|
||||
import io.netty.bootstrap.ServerBootstrap;
|
||||
import io.netty.channel.ChannelFuture;
|
||||
import io.netty.channel.ChannelInitializer;
|
||||
@ -50,8 +50,8 @@ public class TcpNettyServer {
|
||||
protected void initChannel(SocketChannel socketChannel) throws Exception {
|
||||
//添加自定义处理器
|
||||
socketChannel.pipeline()
|
||||
.addLast(new StringEncoder(StandardCharsets.UTF_8))
|
||||
.addLast(new StringDecoder(Charset.forName("GBK")))
|
||||
//.addLast(new StringEncoder(StandardCharsets.UTF_8))
|
||||
//.addLast(new StringDecoder(Charset.forName("GBK")))
|
||||
.addLast(tcpNettyHandler);
|
||||
}
|
||||
});
|
||||
@ -1,11 +1,10 @@
|
||||
package com.zhgd.xmgl.modules.highformwork.netty.tcp.service;
|
||||
package com.zhgd.netty.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.zhgd.xmgl.modules.highformwork.entity.*;
|
||||
import com.zhgd.xmgl.modules.highformwork.mapper.*;
|
||||
import com.zhgd.xmgl.modules.highformwork.netty.tcp.constant.HighFormworkSupport;
|
||||
import com.zhgd.netty.constant.HighFormworkSupport;
|
||||
import com.zhgd.xmgl.modules.highformwork.service.IHighFormworkMeasureCurrentDataService;
|
||||
import io.swagger.models.auth.In;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
@ -1,62 +0,0 @@
|
||||
package com.zhgd.xmgl.modules.highformwork.netty.tcp.handler;
|
||||
|
||||
import com.zhgd.xmgl.modules.highformwork.netty.tcp.constant.HighFormworkSupport;
|
||||
import com.zhgd.xmgl.modules.highformwork.netty.tcp.service.HighFormworkSupportService;
|
||||
import io.netty.channel.ChannelHandler;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.SimpleChannelInboundHandler;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* Created by chenws on 2020/3/24.
|
||||
*/
|
||||
@ChannelHandler.Sharable
|
||||
@Slf4j
|
||||
@Component
|
||||
public class TcpNettyHandler extends SimpleChannelInboundHandler<String> {
|
||||
|
||||
@Autowired
|
||||
private HighFormworkSupportService highFormworkSupportService;
|
||||
|
||||
@Override
|
||||
public void channelRead0(ChannelHandlerContext ctx, String msg) {
|
||||
log.info("I get the message >>> \r\n {} ", msg);
|
||||
//把消息写到缓冲区
|
||||
ctx.writeAndFlush("ok");
|
||||
//接收高支模数据保存到mysql中
|
||||
msg = msg.trim();
|
||||
if (StringUtils.startsWith(msg, HighFormworkSupport.TCP_DATA_PREFIX) && StringUtils.endsWith(msg, HighFormworkSupport.TCP_DATA_END)) {
|
||||
try {
|
||||
highFormworkSupportService.saveTcpData(msg);
|
||||
} catch (Exception e) {
|
||||
log.error("error:接收高支模数据保存到mysql中exception: >>> \r\n ", e);
|
||||
} finally {
|
||||
highFormworkSupportResponseOk(ctx);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void highFormworkSupportResponseOk(ChannelHandlerContext ctx) {
|
||||
ctx.writeAndFlush("$LRKKJ$ MSG:\"\"; OK;END");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelReadComplete(ChannelHandlerContext ctx) {
|
||||
//刷新缓冲区,把消息发出去
|
||||
ctx.flush();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
||||
//异常处理,如果该handler不处理,将会传递到下一个handler
|
||||
cause.printStackTrace();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
||||
ctx.writeAndFlush("connect success!");
|
||||
}
|
||||
}
|
||||
@ -202,7 +202,7 @@ public class WorkerAttendanceController {
|
||||
return Result.success(workerAttendanceService.queryTodayAttendanceTrend(map));
|
||||
}
|
||||
|
||||
@ApiOperation(value = "查询最近一周的出勤人数趋势", notes = "查询最近一周的出勤人数趋势(时间不传就默认查一周的)", httpMethod = "GET")
|
||||
@ApiOperation(value = "查询一段时间的出勤人数趋势", notes = "查询一段时间的出勤人数趋势", httpMethod = "GET")
|
||||
@ApiImplicitParams({
|
||||
@ApiImplicitParam(name = "projectSn", value = "项目唯一标识", dataType = "String", paramType = "query", required = true),
|
||||
@ApiImplicitParam(name = "queryStartTime", value = "查询开始时间,格式:2023-05-22", paramType = "query", required = true, dataType = "String"),
|
||||
@ -210,7 +210,7 @@ public class WorkerAttendanceController {
|
||||
})
|
||||
@GetMapping(value = "/queryAttendanceTrendOfTheLastWeek")
|
||||
public Result<List<NumberTimeTableVo>> queryAttendanceTrendOfTheLastWeek(@RequestParam Map<String, Object> map) {
|
||||
return Result.success(workerAttendanceService.queryAttendanceTrendOfTheLastWeek(map));
|
||||
return Result.success(workerAttendanceService.queryAttendanceTrend(map));
|
||||
}
|
||||
|
||||
@ApiOperation(value = "查询每个公司的出勤人数和统计人数", notes = "查询每个公司的出勤人数和统计人数", httpMethod = "GET")
|
||||
|
||||
@ -82,5 +82,5 @@ public interface WorkerAttendanceMapper extends BaseMapper<WorkerAttendance> {
|
||||
|
||||
Page<WorkerAttendanceBo> getPassRecord(@Param("q") GetPassRecordDto dto, Page<WorkerAttendanceBo> page);
|
||||
|
||||
List<NumberTimeTableVo> queryAttendanceTrendOfTheLastWeek(Map<String, Object> map);
|
||||
List<NumberTimeTableVo> queryAttendanceTrend(Map<String, Object> map);
|
||||
}
|
||||
|
||||
@ -771,7 +771,7 @@
|
||||
</if>
|
||||
</select>
|
||||
|
||||
<select id="queryAttendanceTrendOfTheLastWeek" resultType="com.zhgd.xmgl.entity.vo.NumberTimeTableVo">
|
||||
<select id="queryAttendanceTrend" resultType="com.zhgd.xmgl.entity.vo.NumberTimeTableVo">
|
||||
SELECT count(t1.daytime) num, DATE_FORMAT(t1.daytime, '%m-%d') time
|
||||
FROM (
|
||||
SELECT wa.create_time,
|
||||
@ -784,7 +784,7 @@
|
||||
and DATE_FORMAT(wa.create_time, '%Y-%m-%d') >= #{queryStartTime}
|
||||
</if>
|
||||
<if test="queryEndTime != null and queryEndTime != ''">
|
||||
and DATE_FORMAT(wa.create_time, '%Y-%m-%d') <![CDATA[<=]]> #{queryEndTime}
|
||||
and DATE_FORMAT(wa.create_time, '%Y-%m-%d') <![CDATA[<=]]> concat(#{queryEndTime},' 23:59:59')
|
||||
</if>
|
||||
and wa.project_sn = #{projectSn}
|
||||
AND wi.inService_type = 1
|
||||
|
||||
@ -72,7 +72,7 @@ public interface IWorkerAttendanceService extends IService<WorkerAttendance> {
|
||||
|
||||
List<NumberTimeTableVo> queryTodayAttendanceTrend(Map<String, Object> map);
|
||||
|
||||
List<NumberTimeTableVo> queryAttendanceTrendOfTheLastWeek(Map<String, Object> map);
|
||||
List<NumberTimeTableVo> queryAttendanceTrend(Map<String, Object> map);
|
||||
|
||||
AttendanceOfEachCompanyVo queryAttendanceOfEachCompany(Map<String, Object> map);
|
||||
}
|
||||
|
||||
@ -1027,11 +1027,11 @@ status 状态码 String 1表示成功;其余表示失败
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<NumberTimeTableVo> queryAttendanceTrendOfTheLastWeek(Map<String, Object> map) {
|
||||
public List<NumberTimeTableVo> queryAttendanceTrend(Map<String, Object> map) {
|
||||
if (StringUtils.isBlank(MapUtils.getString(map, "queryStartTime")) || StringUtils.isBlank(MapUtils.getString(map, "queryEndTime"))) {
|
||||
throw new OpenAlertException("queryStartTime和queryEndTime都不能为空");
|
||||
}
|
||||
List<NumberTimeTableVo> attendances = workerAttendanceMapper.queryAttendanceTrendOfTheLastWeek(map);
|
||||
List<NumberTimeTableVo> attendances = workerAttendanceMapper.queryAttendanceTrend(map);
|
||||
completeData(attendances, map);
|
||||
return attendances;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user