package com.panzhihua.service_property.netty;
|
|
import cn.hutool.core.date.DateUtil;
|
import com.panzhihua.common.utlis.DateUtils;
|
import com.panzhihua.service_property.dao.ComPropertyAlarmDao;
|
import com.panzhihua.service_property.entity.ComPropertyAlarm;
|
import com.panzhihua.service_property.util.MyTools;
|
import io.netty.channel.ChannelHandlerContext;
|
import io.netty.channel.ChannelInboundHandlerAdapter;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.stereotype.Component;
|
|
import javax.annotation.Resource;
|
import java.util.Date;
|
|
@Slf4j
|
@Component
|
public class NettyServerHandler extends ChannelInboundHandlerAdapter {
|
@Resource
|
private ComPropertyAlarmDao comPropertyAlarmDao;
|
/**
|
* 客户端连接会触发
|
*/
|
@Override
|
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
log.info("Channel active......");
|
}
|
|
/**
|
* 客户端发消息会触发
|
*/
|
@Override
|
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
MyTools myTools=new MyTools();
|
log.info("服务器收到消息: {}", msg.toString());
|
if(msg.toString().startsWith("4A1802")){
|
myTools.writeToClient("404A021823",ctx,"状态包");
|
}
|
if(msg.toString().startsWith("4A0C0134")){
|
myTools.writeToClient("404A01"+ DateUtils.getDateFormatString(new Date(),"HHmmss")+"23",ctx,"心跳包");
|
}
|
if(msg.toString().startsWith("4A1803")){
|
String serial=msg.toString().substring(12,22);
|
myTools.writeToClient("404A03"+msg.toString().substring(msg.toString().length()-2)+"23",ctx,"事件包");
|
ComPropertyAlarm comPropertyAlarm=new ComPropertyAlarm();
|
comPropertyAlarm.setCreateTime(DateUtil.date());
|
comPropertyAlarm.setSerialNo(serial);
|
comPropertyAlarm.setType(ComPropertyAlarm.type.one);
|
comPropertyAlarmDao.insert(comPropertyAlarm);
|
}
|
ctx.flush();
|
}
|
|
/**
|
* 发生异常触发
|
*/
|
@Override
|
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
cause.printStackTrace();
|
ctx.close();
|
}
|
}
|