package com.northglass.service.message;
|
|
import java.util.Date;
|
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
|
import com.northglass.constants.FunctionNumber;
|
import com.northglass.util.HexUtil;
|
|
|
public abstract class AbstractMessageProcessor {
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractMessageProcessor.class);
|
|
/**
|
* 信息头,字符串“<STA>”的16进制表示
|
*/
|
protected static final String HEAD = "3c5354413e";
|
|
/**
|
* 信息尾,字符串“<EOF>”的16进制表示
|
*/
|
protected static final String END = "3c454f463e";
|
|
/**
|
* 备用,共8个字节
|
*/
|
protected static final String BACKUP = "0000000000000000";
|
|
/**
|
* 加密方式00,表示不加密
|
*/
|
protected static final String NO_ENCRYPT = "00";
|
|
/**
|
* 数据区内容分隔符
|
*/
|
protected static final String SEPARATOR_HEX = "7c";
|
|
protected static final int START_SIGN_START = 0;
|
protected static final int START_SIGN_END = 9;
|
|
protected static final int MESSAGE_LENGTH_START = 10;
|
protected static final int MESSAGE_LENGTH_END = 13;
|
|
protected static final int ORDER_NUMBER_START = 14;
|
protected static final int ORDER_NUMBER_END = 37;
|
|
protected static final int FUNCTION_NUMBER_START = 38;
|
protected static final int FUNCTION_NUMBER_END = 41;
|
|
protected static final int ENCRYPT_START = 42;
|
protected static final int ENCRYPT_END = 43;
|
|
protected static final int SEND_TIME_START = 44;
|
protected static final int SEND_TIME_END = 57;
|
|
protected static final int DATA_START = 74;
|
|
protected static final String SEPARATOR = "|";
|
|
protected String parseString(String messageHex, int start, int end, String attributeName) {
|
if (end >= messageHex.length()) {
|
return attributeName + "=超出长度";
|
}
|
|
String message = messageHex.substring(start, end + 1);
|
return attributeName + "=" + message + SEPARATOR;
|
}
|
|
protected String parseInt(String messageHex, int start, int end, String attributeName) {
|
if (end >= messageHex.length()) {
|
return attributeName + "=超出长度";
|
}
|
|
String hex = messageHex.substring(start, end + 1);
|
return attributeName + "=" + HexUtil.hexToInt(hex) + SEPARATOR;
|
}
|
|
protected String parseTime(String messageHex, int start, int end, String attributeName) {
|
if (end >= messageHex.length()) {
|
return attributeName + "=超出长度";
|
}
|
|
String hexTime = messageHex.substring(start, end + 1);
|
return attributeName + "=" + HexUtil.hexToTime(hexTime) + SEPARATOR;
|
}
|
|
/**
|
* 构造消息头部
|
*
|
* 通信头部 = 开始标志 + 信息长度 + 订单编号 + 功能号 + 加密方式 + 发送时刻 + 备用
|
*
|
* @param data
|
* @param functionNumber
|
* @return
|
*/
|
protected String generateHead(String data, String functionNumber) {
|
// 信息长度
|
int length = (data + END).length() / 2;
|
|
// TODO 订单编号待定
|
String orderNumber = "000000000000000000000000";
|
|
String head = HEAD
|
+ HexUtil.intTo2ByteHex(length)
|
+ orderNumber
|
+ functionNumber
|
+ NO_ENCRYPT
|
+ HexUtil.timeToHex(new Date())
|
+ BACKUP;
|
|
return head;
|
}
|
|
public String getHeartBeatMessage() {
|
String data = "";
|
String head = generateHead(data, FunctionNumber.HEART_BEAT);
|
return head + data + END;
|
}
|
|
public String getUpdateRTCMessage() {
|
String data = "";
|
String head = generateHead(data, FunctionNumber.UPDATE_RTC);
|
return head + data + END;
|
}
|
|
public String getResetMessage() {
|
String data = "";
|
String head = generateHead(data, FunctionNumber.RESET);
|
return head + data + END;
|
}
|
|
protected String parseHead(String messageHex) {
|
if (messageHex.length() < 84) {
|
LOGGER.error("消息无效:消息长度为至少42个字节!");
|
LOGGER.error("无效消息:" + messageHex);
|
return "无效消息:" + messageHex;
|
}
|
|
String startSign = messageHex.substring(START_SIGN_START, START_SIGN_END + 1);
|
if (!startSign.equalsIgnoreCase("3c5354413e")) {
|
LOGGER.error("消息无效:消息没有以<STA>开头!");
|
LOGGER.error("无效消息:" + messageHex);
|
return "无效消息:" + messageHex;
|
}
|
|
StringBuffer messageDescription = new StringBuffer();
|
|
// 消息长度
|
messageDescription.append(parseInt(messageHex, MESSAGE_LENGTH_START, MESSAGE_LENGTH_END, "消息长度"));
|
|
// 订单编号
|
messageDescription.append(parseString(messageHex, ORDER_NUMBER_START, ORDER_NUMBER_END, "订单编号"));
|
|
// 功能号
|
messageDescription.append(parseString(messageHex, FUNCTION_NUMBER_START, FUNCTION_NUMBER_END, "功能号"));
|
|
// 加密方式
|
messageDescription.append(parseString(messageHex, ENCRYPT_START, ENCRYPT_END, "加密方式"));
|
|
// 发送时刻
|
messageDescription.append(parseTime(messageHex, SEND_TIME_START, SEND_TIME_END, "发送时刻"));
|
|
return messageDescription.toString();
|
}
|
}
|