Commit d1caa6c5 authored by duanruiming's avatar duanruiming

[update] 代码优化

parent eeb9d45d
......@@ -13,6 +13,7 @@ import net.wanji.utc.hisense.cache.netty.NettyMessageCache;
import net.wanji.utc.hisense.netty.codec.MessageDecoder;
import net.wanji.utc.hisense.netty.codec.MessageEnCoder;
import net.wanji.utc.hisense.netty.handler.TcpClientHandler;
import net.wanji.utc.hisense.util.XMLUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;
......@@ -67,12 +68,12 @@ public class TcpClient {
public static void sendMessage(Object msg) {
try {
ChannelFuture sync = tcpChannelFuture.channel().writeAndFlush(msg).sync();
if (sync.isSuccess()) {
log.debug("发送信号机命令消息:{}", msg);
ChannelFuture channelFuture = tcpChannelFuture.channel().writeAndFlush(XMLUtils.strToHex(XMLUtils.convertToXml(msg)));
if (channelFuture.isSuccess()) {
log.debug("发送信号机命令消息成功:{}", msg);
}
} catch (Exception e) {
log.error("sendMsg is error", e);
log.error("发送信号机命令消息失败", e);
}
}
......
......@@ -11,6 +11,7 @@ public class CommandResultSign {
public static Object getHexSign(String data) throws Exception{
//Object runningLightsStatusPojo = JSON.parseObject(data, RunningLightsStatusPojo.class);
return XMLUtils.convertXmlStrToObject(RunningLightsStatusPojo.class, data);
}
......
......@@ -120,7 +120,6 @@ public class TcpClientHandler extends SimpleChannelInboundHandler<DatagramPacket
super.userEventTriggered(ctx, evt);
if (evt instanceof IdleStateEvent) {
IdleStateEvent idleStateEvent = (IdleStateEvent) evt;
log.info("已经 10 秒没有发送信息,给服务端发送心跳!");
//向服务端发送消息
try {
SocketAddress socketAddress = ctx.channel().remoteAddress();
......@@ -128,10 +127,9 @@ public class TcpClientHandler extends SimpleChannelInboundHandler<DatagramPacket
//向服务端发送心跳
HeartBeatRequest heartBeatRequest = new HeartBeatRequest();
heartBeatRequest.setMessageType(MessageTypeEnum.HEARTBEAT_STATUS.getType());
log.error("心跳消息:{}", XMLUtils.convertToXml(heartBeatRequest));
ctx.channel().writeAndFlush(XMLUtils.strToHex(XMLUtils.convertToXml(heartBeatRequest)));
} catch (Exception ex) {
log.error(ex.getMessage());
log.error("心跳消息发送失败:{}", ex.getMessage());
}
}
......
package net.wanji.utc.hisense.netty.request;
import net.wanji.utc.hisense.netty.pojo.CommandPojo;
/**
* @author duanruiming
* @date 2024/11/02 20:15
......@@ -10,5 +8,5 @@ public interface CommandRequestFactory {
/**
* 发送海信请求
*/
void sendCommandRequest(CommandPojo commandPojo);
void sendCommandRequest(Object content);
}
......@@ -5,6 +5,7 @@ import net.wanji.utc.hisense.netty.pojo.CommandPojo;
import net.wanji.utc.hisense.netty.request.CommandRequestFactory;
import net.wanji.utc.hisense.netty.response.CommandResponseFactory;
import net.wanji.utc.hisense.pojo.xml.pojo.messagecontent.get.GetSchemeNoRequest;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
/**
......@@ -16,16 +17,23 @@ import org.springframework.stereotype.Service;
public class GetSchemeNoService implements CommandResponseFactory, CommandRequestFactory {
@Override
public void sendCommandRequest(CommandPojo commandPojo) {
public void sendCommandRequest(Object msg) {
GetSchemeNoRequest request = new GetSchemeNoRequest();
GetSchemeNoRequest.Content content = new GetSchemeNoRequest.Content();
content.setSpot(String.valueOf(commandPojo.getMsg()));
content.setSpot(String.valueOf(msg));
request.setMessageContent(content);
TcpClient.sendMessage(request);
}
@Override
public Object getCommandResponse(CommandPojo commandPojo) {
System.err.println("==============收到消息成功" + commandPojo.getMsg());
return commandPojo.getMsg();
}
@Scheduled(initialDelay = 30 * 1000, fixedRate = 4000)
public void test(){
sendCommandRequest(1);
System.err.println("==============发送消息成功");
}
}
\ No newline at end of file
......@@ -96,7 +96,7 @@ threadPoolConfig:
portParam:
localPort: 55051
remotePort: 10010
remoteIp: 172.24.71.68
remoteIp: 127.0.0.1
server:
port: 39003
servlet:
......
#spring:
# cloud:
# nacos:
# config:
# server-addr: 10.102.1.182:8848
# file-extension: yaml
# group: signal
# namespace: signal
# username: nacos
# password: nacos
# application:
# # dubbo启动需要程序名称
# name: utc-hisense
# main:
# allow-circular-references: true
\ No newline at end of file
spring:
cloud:
nacos:
config:
server-addr: 37.12.182.29:8848
file-extension: yaml
group: signal
namespace: signal
username: nacos
password: nacos
application:
# dubbo启动需要程序名称
name: utc-hisense
main:
allow-circular-references: true
\ No newline at end of file
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