Browse Source

代码完整

jwy-f10
review512jwy@163.com 1 month ago
parent
commit
c1ed867e2d
  1. 6
      src/main/java/com/techsor/datacenter/receiver/clients/f10/ClientHandler.java
  2. 2
      src/main/java/com/techsor/datacenter/receiver/clients/f10/GatewayProperties.java
  3. 36
      src/main/java/com/techsor/datacenter/receiver/clients/f10/MessageHandler.java
  4. 4
      src/main/java/com/techsor/datacenter/receiver/clients/f10/SequenceState.java

6
src/main/java/com/techsor/datacenter/receiver/clients/f10/ClientHandler.java

@ -52,17 +52,17 @@ public class ClientHandler {
// 仅是不足重发5次序列号处理:不处理数据,但返回肯定响应
if (sequenceJudgement.isDuplicate()) {
logger.warn("客户端[{}]收到重复消息,序列号: {}", ctx.channel().remoteAddress(), parsedMessage.getSequence());
String response = messageHandler.formatResponse(parsedMessage.getSequence(), GatewayServerProperties.responseOk);
String response = messageHandler.formatResponse(parsedMessage.getSequence(), GatewayProperties.responseOk);
sendMessage(ctx, response);
return;
}
// 正常消息处理:根据消息模式进行处理
String response;
if (GatewayServerProperties.modeSystemStart.equals(parsedMessage.getMode())) {
if (GatewayProperties.modeSystemStart.equals(parsedMessage.getMode())) {
// 处理系统开始指示
response = messageHandler.handleSystemStartMessage(parsedMessage, ctx);
} else if (GatewayServerProperties.modeUnsent.equals(parsedMessage.getMode())) {
} else if (GatewayProperties.modeUnsent.equals(parsedMessage.getMode())) {
// 处理未发送数据
response = messageHandler.handleUnsentDataMessage(parsedMessage, ctx);
} else {

2
src/main/java/com/techsor/datacenter/receiver/clients/f10/GatewayServerProperties.java → src/main/java/com/techsor/datacenter/receiver/clients/f10/GatewayProperties.java

@ -6,7 +6,7 @@ import lombok.Data;
* 网关服务器配置类
*/
@Data
public class GatewayServerProperties {
public class GatewayProperties {
// 数据格式配置
public static char stx = 0x02; // STX字符

36
src/main/java/com/techsor/datacenter/receiver/clients/f10/MessageHandler.java

@ -90,27 +90,27 @@ public class MessageHandler {
*/
public Message parseMessage(String content) {
// 验证内容长度
if (content.length() < GatewayServerProperties.sequenceLength + GatewayServerProperties.modeLength) {
if (content.length() < GatewayProperties.sequenceLength + GatewayProperties.modeLength) {
throw new IllegalArgumentException("消息内容长度不足");
}
// 提取序列号
String sequence = content.substring(0, GatewayServerProperties.sequenceLength);
if (!sequence.matches("\\d{" + GatewayServerProperties.sequenceLength + "}")) {
throw new IllegalArgumentException("序列号必须为" + GatewayServerProperties.sequenceLength+ "位数字");
String sequence = content.substring(0, GatewayProperties.sequenceLength);
if (!sequence.matches("\\d{" + GatewayProperties.sequenceLength + "}")) {
throw new IllegalArgumentException("序列号必须为" + GatewayProperties.sequenceLength+ "位数字");
}
// 提取模式标识
String mode = content.substring(GatewayServerProperties.sequenceLength,
GatewayServerProperties.sequenceLength + GatewayServerProperties.modeLength);
String mode = content.substring(GatewayProperties.sequenceLength,
GatewayProperties.sequenceLength + GatewayProperties.modeLength);
// if (!isValidMode(mode)) {
// throw new IllegalArgumentException("无效的模式标识: " + mode);
// }
// 提取数据部分(如果有)
String data = null;
if (content.length() > GatewayServerProperties.sequenceLength + GatewayServerProperties.modeLength) {
data = content.substring(GatewayServerProperties.sequenceLength + GatewayServerProperties.modeLength);
if (content.length() > GatewayProperties.sequenceLength + GatewayProperties.modeLength) {
data = content.substring(GatewayProperties.sequenceLength + GatewayProperties.modeLength);
}
// 创建并返回消息对象
@ -128,8 +128,8 @@ public class MessageHandler {
* @return 是否有效
*/
private boolean isValidMode(String mode) {
return GatewayServerProperties.modeUnsent.equals(mode) ||
GatewayServerProperties.modeSystemStart.equals(mode);
return GatewayProperties.modeUnsent.equals(mode) ||
GatewayProperties.modeSystemStart.equals(mode);
}
/**
@ -138,7 +138,7 @@ public class MessageHandler {
* @return 错误响应消息
*/
public String createErrorMessage(String sequence) {
return formatResponse(sequence, GatewayServerProperties.responseNg);
return formatResponse(sequence, GatewayProperties.responseNg);
}
/**
@ -151,19 +151,19 @@ public class MessageHandler {
StringBuilder sb = new StringBuilder();
// 添加STX
sb.append(GatewayServerProperties.stx);
sb.append(GatewayProperties.stx);
// 添加序列号
sb.append(sequence);
//响应模式
sb.append(GatewayServerProperties.responseMode);
sb.append(GatewayProperties.responseMode);
// 添加响应代码
sb.append(responseCode);
// 添加ETX
sb.append(GatewayServerProperties.etx);
sb.append(GatewayProperties.etx);
return sb.toString();
}
@ -192,7 +192,7 @@ public class MessageHandler {
public String handleSystemStartMessage(Message message, ChannelHandlerContext ctx) {
logger.warn("客户端[{}]发送系统开始指示,序列号: {}", ctx.channel().remoteAddress(), message.getSequence());
// 创建成功响应
return formatResponse(message.getSequence(), GatewayServerProperties.responseOk);
return formatResponse(message.getSequence(), GatewayProperties.responseOk);
}
/**
@ -210,7 +210,7 @@ public class MessageHandler {
// 例如:保存到数据库、转发到其他服务等
// 假设数据处理成功
return formatResponse(message.getSequence(), GatewayServerProperties.responseOk);
return formatResponse(message.getSequence(), GatewayProperties.responseOk);
} catch (Exception e) {
logger.error("处理客户端[{}]正常数据时发生错误", ctx.channel().remoteAddress(), e);
return createErrorMessage(message.getSequence());
@ -226,7 +226,7 @@ public class MessageHandler {
// 处理未发送数据(通常需要特殊处理以避免重复处理)
// 例如:检查数据是否已经存在,如果不存在则处理,否则直接返回成功
return formatResponse(message.getSequence(), GatewayServerProperties.responseOk);
return formatResponse(message.getSequence(), GatewayProperties.responseOk);
} catch (Exception e) {
logger.error("处理客户端[{}]未发送数据时发生错误", ctx.channel().remoteAddress(), e);
return createErrorMessage(message.getSequence());
@ -244,7 +244,7 @@ public class MessageHandler {
// 处理未发送数据(通常需要特殊处理以避免重复处理)
// 例如:检查数据是否已经存在,如果不存在则处理,否则直接返回成功
return formatResponse(message.getSequence(), GatewayServerProperties.responseOk);
return formatResponse(message.getSequence(), GatewayProperties.responseOk);
} catch (Exception e) {
logger.error("处理客户端[{}]未发送数据时发生错误", ctx.channel().remoteAddress(), e);
return createErrorMessage(message.getSequence());

4
src/main/java/com/techsor/datacenter/receiver/clients/f10/SequenceState.java

@ -13,7 +13,7 @@ public class SequenceState {
result.setDuplicate(false);
//系统启动指令直接通过
if (GatewayServerProperties.startSequence.equals(sequence)){
if (GatewayProperties.startSequence.equals(sequence)){
return result;
}
@ -59,7 +59,7 @@ public class SequenceState {
}
// 循环情况:序列号从9999变为0000
if (last == GatewayServerProperties.MAX_SEQUENCE && current == GatewayServerProperties.MIN_SEQUENCE) {
if (last == GatewayProperties.MAX_SEQUENCE && current == GatewayProperties.MIN_SEQUENCE) {
return true;
}

Loading…
Cancel
Save