-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
提交内容添加backNameCode,作为中介键,充当数据处理的Key 反馈内容String统一改为byte[],减少中间转化产生的内存消耗。
- Loading branch information
sunxudong
committed
Apr 23, 2020
1 parent
346c892
commit d6cf4a1
Showing
20 changed files
with
341 additions
and
166 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,14 +2,17 @@ | |
|
||
|
||
import org.sheedon.serial.DataConverter; | ||
import org.sheedon.serial.internal.CharsUtils; | ||
|
||
import java.util.Arrays; | ||
|
||
/** | ||
* @Description: java类作用描述 | ||
* @Author: sheedon | ||
* @Email: [email protected] | ||
* @Date: 2020/3/11 0:45 | ||
*/ | ||
public class CallbackRuleConverter implements DataConverter<String, String> { | ||
public class CallbackRuleConverter implements DataConverter<byte[], Long> { | ||
|
||
CallbackRuleConverter() { | ||
|
||
|
@@ -19,10 +22,16 @@ public class CallbackRuleConverter implements DataConverter<String, String> { | |
// 协议头 数据长度位 子控设备地址 命令类型 消息体 CRC16校验 | ||
// 7A 0800 01 03 01 B07A | ||
@Override | ||
public String convert(String value) { | ||
if(value == null || value.isEmpty() || value.length()<10) | ||
return ""; | ||
public Long convert(byte[] value) { | ||
if (value == null || value.length < 3) | ||
return -1L; | ||
|
||
return (long) (byteToHex(value[1]) * 16 * 16 + byteToHex(value[2])); | ||
} | ||
|
||
return value.substring(6,10); | ||
private int byteToHex(byte b) { | ||
if (b < 0) | ||
return b & 0xff; | ||
return b; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
package org.sheedon.demo.converters; | ||
|
||
|
||
import org.sheedon.serial.DataCheckBean; | ||
import org.sheedon.serial.DataConverter; | ||
import org.sheedon.serial.ResponseBody; | ||
import org.sheedon.serial.utils.CRC16M; | ||
import org.sheedon.serial.utils.CRC_16; | ||
import org.sheedon.serial.SafetyByteBuffer; | ||
import org.sheedon.serial.internal.CharsUtils; | ||
|
||
import java.util.Arrays; | ||
|
||
/** | ||
* 反馈内容解析器 | ||
|
@@ -14,10 +15,10 @@ | |
* @Email: [email protected] | ||
* @Date: 2020/3/11 0:45 | ||
*/ | ||
public class CheckDataConverter implements DataConverter<StringBuffer, DataCheckBean> { | ||
public class CheckDataConverter implements DataConverter<SafetyByteBuffer, DataCheckBean> { | ||
|
||
private static final String STARTBIT = "7A"; | ||
private static final String ENDBIT = ""; | ||
private static final byte STARTBIT = (byte) 0xBB; | ||
private static final byte ENDBIT = 0x7E; | ||
|
||
CheckDataConverter() { | ||
|
||
|
@@ -27,7 +28,7 @@ public class CheckDataConverter implements DataConverter<StringBuffer, DataCheck | |
// 协议头 数据长度位 子控设备地址 命令类型 消息体 CRC16校验 | ||
// 7A 0800 01 03 01 B07A | ||
@Override | ||
public DataCheckBean convert(StringBuffer value) { | ||
public DataCheckBean convert(SafetyByteBuffer value) { | ||
if (value == null || value.length() == 0) { | ||
return DataCheckBean.build(null, 0); | ||
} | ||
|
@@ -37,28 +38,32 @@ public DataCheckBean convert(StringBuffer value) { | |
return DataCheckBean.build(null, 0); | ||
} | ||
|
||
if (index + 6 >= value.length()) { | ||
if (index + 5 >= value.length()) { | ||
return DataCheckBean.build(null, index); | ||
} | ||
|
||
// 一个内容到总长度 | ||
String lengthStr = value.substring(index + 2, index + 6); | ||
int length = calcLength(lengthStr) * 2; | ||
if (length < 0 || index + length > value.length()) { | ||
byte[] lengthStr = value.substring(index + 3, index + 5); | ||
int length = calcLength(lengthStr); | ||
if (length < 0 || index + length + 7 > value.length()) { | ||
return DataCheckBean.build(null, index); | ||
} | ||
|
||
String content = value.substring(index, index + length); | ||
byte[] content = value.substring(index + 1, index + length + 6); | ||
boolean check = checkContent(content); | ||
if (check) { | ||
ResponseBody body = ResponseBody.build(STARTBIT, | ||
content.substring(STARTBIT.length(), content.length() - 4), | ||
content.substring(content.length() - 4), | ||
ENDBIT, content); | ||
|
||
return DataCheckBean.build(body, index + length); | ||
byte[] startBit = new byte[]{STARTBIT}; | ||
byte[] messageBit = Arrays.copyOf(content, content.length - 1); | ||
byte[] parityBit = Arrays.copyOfRange(content, content.length - 1, content.length); | ||
byte[] endBit = new byte[]{ENDBIT}; | ||
|
||
ResponseBody body = ResponseBody.build(startBit, messageBit, parityBit, endBit, | ||
value.substring(index, index + length + 7)); | ||
|
||
return DataCheckBean.build(body, index + length + 7); | ||
} else { | ||
return DataCheckBean.build(null, index + length); | ||
return DataCheckBean.build(null, index + length + 7); | ||
} | ||
} | ||
|
||
|
@@ -67,12 +72,12 @@ public DataCheckBean convert(StringBuffer value) { | |
* | ||
* @param str 字符 | ||
*/ | ||
private int calcLength(String str) { | ||
String lowPosition = str.substring(0, 2); | ||
String highPosition = str.substring(2, 4); | ||
private int calcLength(byte[] str) { | ||
byte highPosition = str[0]; | ||
byte lowPosition = str[1]; | ||
|
||
int low = Integer.parseInt(lowPosition, 16); | ||
int high = Integer.parseInt(highPosition, 16); | ||
int high = highPosition & 0xFF; | ||
int low = lowPosition & 0xFF; | ||
|
||
return high * 16 * 16 + low; | ||
|
||
|
@@ -81,24 +86,21 @@ private int calcLength(String str) { | |
|
||
/** | ||
* 核实内容校验码 | ||
* 拿到校验码 后四位 | ||
* 拿到内容 除了后四位外的数据 | ||
* 拿到校验码 后两位 | ||
* 拿到内容 除了后两位外的数据 | ||
* | ||
* @param content 内容 | ||
* @return 校验是否一致 | ||
*/ | ||
private boolean checkContent(String content) { | ||
if (content.length() <= 4) | ||
private boolean checkContent(byte[] content) { | ||
if (content.length <= 1) | ||
return false; | ||
|
||
String checkStr = content.substring(content.length() - 4); | ||
String contentStr = content.substring(0, content.length() - 4); | ||
StringBuilder checkResult = new StringBuilder(CRC16M.getBufHexStr(CRC_16.getSendBuf(contentStr))); | ||
checkResult = checkResult.delete(0, content.length() - 4); | ||
for (int index = checkResult.length(); index < 4; index++) { | ||
checkResult.insert(0, "0"); | ||
} | ||
byte checkByte = content[content.length - 1]; | ||
byte[] contentBytes = Arrays.copyOf(content, content.length - 1); | ||
byte checkResult = CharsUtils.sumCheck(contentBytes); | ||
|
||
return checkResult.toString().equalsIgnoreCase(checkStr); | ||
return checkByte == checkResult; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,4 +22,7 @@ public interface AsyncCallImpl { | |
|
||
// 反馈名称 | ||
String backName(); | ||
|
||
// 反馈名称Code | ||
long backNameCode(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.