Skip to content

Commit

Permalink
更新内容
Browse files Browse the repository at this point in the history
提交内容添加backNameCode,作为中介键,充当数据处理的Key
反馈内容String统一改为byte[],减少中间转化产生的内存消耗。
  • Loading branch information
sunxudong committed Apr 23, 2020
1 parent 346c892 commit d6cf4a1
Show file tree
Hide file tree
Showing 20 changed files with 341 additions and 166 deletions.
1 change: 1 addition & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ android {
}
buildTypes {
release {
debuggable true
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
Expand Down
58 changes: 18 additions & 40 deletions app/src/main/java/org/sheedon/demo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,57 +24,35 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);

final SerialClient client = new SerialClient.Builder()
.path("/dev/ttyS2")
.path("/dev/ttyS1")
.baudRate(115200)
.name("115200")
.addConverterFactory(DataConverterFactory.create())
.callback(this)
.build();

new Thread(new Runnable() {
@Override
public void run() {
while (true){
Request request = new RequestBuilder()
.backName("0007")
.data("7A07000006BC7C")
.build();
Request request = new RequestBuilder()
.backName("01FF")
.data("")
.build();

Call call = client.newCall(request);
call.enqueue(new Callback<Response>() {
@Override
public void onFailure(Throwable e) {

}

@Override
public void onResponse(Response response) {

}
});
Call call = client.newCall(request);
Observable observable = client.newObservable(request);
observable.subscribe(new Callback<Response>() {
@Override
public void onFailure(Throwable e) {
System.out.println(e);
}

try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public void onResponse(Response response) {
ResponseBody body = response.body();
System.out.println(body == null ? "" : body.getBody());
}
}).start();
// Call call = client.newCall(request);
// Observable observable = client.newObservable(request);
// observable.subscribe(new Callback<Response>() {
// @Override
// public void onFailure(Throwable e) {
// System.out.println(e);
// }
//
// @Override
// public void onResponse(Response response) {
// ResponseBody body = response.body();
// System.out.println(body == null ? "" : body.getBody());
// }
// });
});

// call.enqueue(new Callback() {
// @Override
Expand All @@ -98,6 +76,6 @@ public void onResponse(Response response) {

@Override
public void onCallback(ResponseBody data) {
Log.v("SXD",data.getBody());
Log.v("SXD",data.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand All @@ -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;
}
}
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;

/**
* 反馈内容解析器
Expand All @@ -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() {

Expand All @@ -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);
}
Expand All @@ -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);
}
}

Expand All @@ -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;

Expand All @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import org.sheedon.serial.DataCheckBean;
import org.sheedon.serial.DataConverter;
import org.sheedon.serial.SafetyByteBuffer;

/**
* @Description: java类作用描述
Expand All @@ -28,13 +29,13 @@ private DataConverterFactory() {

@Nullable
@Override
public DataConverter<String, String> callbackNameConverter(String data) {
public DataConverter<byte[], Long> callbackNameCodeConverter(byte[] data) {
return ruleConverter == null ? ruleConverter = new CallbackRuleConverter() : ruleConverter;
}

@Nullable
@Override
public DataConverter<StringBuffer, DataCheckBean> checkDataConverter() {
public DataConverter<SafetyByteBuffer, DataCheckBean> checkDataConverter() {
return checkConverter == null ? checkConverter = new CheckDataConverter() : checkConverter;
}
}
4 changes: 2 additions & 2 deletions serial/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
defaultConfig {
minSdkVersion 15
targetSdkVersion 28
versionCode 4
versionName "1.1.2"
versionCode 5
versionName "1.2"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'consumer-rules.pro'
Expand Down
3 changes: 3 additions & 0 deletions serial/src/main/java/org/sheedon/serial/AsyncCallImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,7 @@ public interface AsyncCallImpl {

// 反馈名称
String backName();

// 反馈名称Code
long backNameCode();
}
2 changes: 1 addition & 1 deletion serial/src/main/java/org/sheedon/serial/DataConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public interface DataConverter<F, T> {
T convert(F value);

abstract class Factory {
public @Nullable DataConverter<String, String> callbackNameConverter(String topic) {
public @Nullable DataConverter<byte[], Long> callbackNameCodeConverter(byte[] topic) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/
public abstract class DataConverterFactory extends DataConverter.Factory {
public @Nullable
DataConverter<StringBuffer, DataCheckBean> checkDataConverter() {
DataConverter<SafetyByteBuffer, DataCheckBean> checkDataConverter() {
return null;
}
}
Loading

0 comments on commit d6cf4a1

Please sign in to comment.