Skip to content

Commit

Permalink
更改规则
Browse files Browse the repository at this point in the history
由字符长度改为起始位和结束位
原因:通过动态代理获取属性存在字典排序,非按照类属性内容顺序填充,存在问题
  • Loading branch information
sunxudong committed Apr 22, 2020
1 parent a362cc6 commit ae13fe5
Show file tree
Hide file tree
Showing 13 changed files with 541 additions and 73 deletions.
10 changes: 5 additions & 5 deletions app/src/main/java/org/sheedon/demo/BoxModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
*/
public class BoxModel {

@RULES(length = 2)
@RULES(end = 2)
private String name;
@RULES(length = 2)
@RULES(begin = 2,end = 4)
private String age;
@RULES(length = 6)
@RULES(begin = 4,end = 10)
private String other;

@RULES(length = 2)
@RULES(begin = 10,end = 12)
private Body body;

public String getName() {
Expand Down Expand Up @@ -47,7 +47,7 @@ public String toString() {
}

private class Body{
@RULES(length = 2)
@RULES(end = 2)
private String id;

public String getId() {
Expand Down
239 changes: 239 additions & 0 deletions app/src/main/java/org/sheedon/demo/CharsUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
package org.sheedon.demo;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Created by sheedon on 2017/9/28.
*/

public class CharsUtils {
/**
* byte转hexString
*
* @param buffer 数据
* @param size 字符数
* @return
*/
public static String bytesToHexString(final byte[] buffer, final int size) {
StringBuilder stringBuilder = new StringBuilder("");
if (buffer == null || size <= 0) return null;
for (int i = 0; i < size; i++) {
String hex = Integer.toHexString(buffer[i] & 0xff);
if (hex.length() < 2) stringBuilder.append(0);
stringBuilder.append(hex);
}
return stringBuilder.toString();
}

/**
* hexString转byte
*
* @param hexString
* @return
*/
public static byte[] hexStringToBytes(String hexString) {
if (hexString == null || hexString.equals("")) return null;
hexString = hexString.toUpperCase();
int length = hexString.length() / 2;
char[] hexChars = hexString.toCharArray();
byte[] d = new byte[length];
for (int i = 0; i < length; i++) {
int pos = i * 2;
d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
}
return d;
}

private static byte charToByte(char c) {
return (byte) "0123456789ABCDEF".indexOf(c);
}

/**
* 字符串转换为ASCII码
*/
public static String getAscii(String str) {
StringBuilder sb = new StringBuilder();
byte[] bs = str.getBytes();
for (int i = 0; i < bs.length; i++)
sb.append(toHex(bs[i]));
return sb.toString();
}

public static String toHex(int n) {
StringBuilder sb = new StringBuilder();
if (n / 16 == 0) {
return toHexUtil(n);
} else {
String t = toHex(n / 16);
int nn = n % 16;
sb.append(t).append(toHexUtil(nn));
}
return sb.toString();
}

private static String toHexUtil(int n) {
String rt = "";
switch (n) {
case 10:
rt += "A";
break;
case 11:
rt += "B";
break;
case 12:
rt += "C";
break;
case 13:
rt += "D";
break;
case 14:
rt += "E";
break;
case 15:
rt += "F";
break;
default:
rt += n;
}
return rt;
}

/**
* 十六进制转换字符串
*
* @param hexStr
* @return String 对应的字符串
*/
public static String hexStr2Str(String hexStr) {
String str = "0123456789ABCDEF";
char[] hexs = hexStr.toCharArray();
byte[] bytes = new byte[hexStr.length() / 2];
int n;

for (int i = 0; i < bytes.length; i++) {
n = str.indexOf(hexs[2 * i]) * 16;
n += str.indexOf(hexs[2 * i + 1]);
bytes[i] = (byte) (n & 0xff);
}
return new String(bytes);
}

/**
* bytes转换成十六进制字符串
*
* @return String 每个Byte值之间空格分隔
*/
public static String byte2HexStr(byte[] b) {
String stmp = "";
StringBuilder sb = new StringBuilder("");
for (int n = 0; n < b.length; n++) {
stmp = Integer.toHexString(b[n] & 0xFF);
sb.append((stmp.length() == 1) ? "0" + stmp : stmp);
sb.append(" ");
}
return sb.toString().toUpperCase().trim();
}

/**
* bytes转换成十六进制字符串
*
* @return String 每个Byte值之间空格分隔
*/
public static String byte2HexStr2(byte[] b) {
String stmp = "";
StringBuilder sb = new StringBuilder("");
for (int n = 0; n < b.length; n++) {
stmp = Integer.toHexString(b[n]);
sb.append((stmp.length() == 1) ? "0" + stmp : stmp);
sb.append(" ");
}
return sb.toString().toUpperCase().trim();
}

/**
* 利用正则表达式判断字符串是否是数字
*
* @param str
* @return
*/
public static boolean isNumeric(String str) {
Pattern pattern = Pattern.compile("[0-9]*");
Matcher isNum = pattern.matcher(str);
if (!isNum.matches()) {
return false;
}
return true;
}

public static boolean isNumber(char str) {
return CharsUtils.isNumeric(String.valueOf(str));
}

/**
* bytes转换成字符串
*/
public static String byte2Str(byte[] b) {
return hexStr2Str(byte2HexStr(b).replace(" ", ""));
}

public static String convertHexToString(String hex) {

StringBuilder sb = new StringBuilder();
StringBuilder temp = new StringBuilder();

//49204c6f7665204a617661 split into two characters 49, 20, 4c...
for (int i = 0; i < hex.length() - 1; i += 2) {

//grab the hex in pairs
String output = hex.substring(i, (i + 2));
//convert hex to decimal
int decimal = Integer.parseInt(output, 16);
//convert the decimal to character
sb.append((char) decimal);

temp.append(decimal);
}

return sb.toString();
}

/**
* 求校验和的算法
*
* @param msg 需要求校验和的字节数组
* @return 校验和
*/
public static byte sumCheck(byte[] msg) {
long sum = 0;

/** 逐Byte添加位数和 */
for (byte byteMsg : msg) {
if (byteMsg < 0) {
sum += byteMsg & 0xff;
} else {
sum += byteMsg;
}

if (sum > 0xff) {
sum &= 0xff;
}
}

return (byte) (sum & 0xff);
}

public static String sumCheckToHexStr(byte[] msg) {
byte b = sumCheck(msg);
return byteToHex(b);
}

public static String byteToHex(byte b) {
String hex = Integer.toHexString(b & 0xFF);
if (hex.length() < 2) {
hex = "0" + hex;
}
return hex.toUpperCase().trim();
}

}
54 changes: 34 additions & 20 deletions app/src/main/java/org/sheedon/demo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
import android.view.View;

import org.sheedon.demo.converters.DataConverterFactory;
import org.sheedon.demo.converters.SerialConverterFactory;
import org.sheedon.serial.SerialClient;
import org.sheedon.serial.retrofit.Call;
import org.sheedon.serial.retrofit.Callback;
import org.sheedon.serial.retrofit.Observable;
import org.sheedon.serial.retrofit.Response;
import org.sheedon.serial.retrofit.Retrofit;
import org.sheedon.serial.retrofit.converters.SerialConverterFactory;

public class MainActivity extends AppCompatActivity {

Expand All @@ -23,45 +23,59 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);

SerialClient client = new SerialClient.Builder()
.path("/tty/s4")
.baudRate(9600)
.name("qrcode")
.path("/dev/ttyS1")
.baudRate(115200)
.name("bridge_rfid")
.addConverterFactory(DataConverterFactory.create())
.build();

Retrofit retrofit = new Retrofit.Builder()
.client(client)
.addConverterFactory(SerialConverterFactory.create())
.baseStartBit("7A")
.baseEndBit("")
.baseStartBit("BB")
.baseEndBit("7E")
.build();

final RemoteService remoteService = retrofit.create(RemoteService.class);
Observable<RFIDModel> observable = remoteService.bindRFID();
observable.subscribe(new Callback.Observable<RFIDModel>() {
@Override
public void onResponse(Observable<RFIDModel> call, Response<RFIDModel> response) {
Log.v("SXD", "" + response.body());
}

@Override
public void onFailure(Observable<RFIDModel> call, Throwable t) {

}
});

remoteService.bindCommandBack().subscribe(new Callback.Observable<Void>() {
@Override
public void onResponse(Observable<Void> call, Response<Void> response) {
Log.v("SXD", "" + response.body());
}

@Override
public void onFailure(Observable<Void> call, Throwable t) {

}
});

findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Call<BoxModel> managerList = remoteService.getManagerList("0800", "02", "03", "01");
managerList.publishNotCallback();
Call<Void> observable = remoteService.setSignalStrength();
observable.publishNotCallback();

}
});

findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Observable<BoxModel> managerList = remoteService.getManagerList1();
managerList.subscribe(new Callback.Observable<BoxModel>() {
@Override
public void onResponse(Observable<BoxModel> call, Response<BoxModel> response) {
Log.v("SXD", "" + response.body());
}

@Override
public void onFailure(Observable<BoxModel> call, Throwable t) {
Log.v("SXD", "" + t);
}
});
Call<Void> observable = remoteService.sendContinuousRead();
observable.publishNotCallback();
}
});
}
Expand Down
Loading

0 comments on commit ae13fe5

Please sign in to comment.