差分数据解析
package home.ble;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class InitXK {
static HashMap map = new HashMap<>();
static HashMap pulseMap = new HashMap<>();
static Socket socket = null;
// static String ip = "127.0.0.1";
static String ip = "192.168.0.115";
static int preType = 0;
static {
Thread listenerThread = new Thread(() -> {
while (true) {
try {
if (socket != null && socket.isConnected()) {
InputStream inputStream = socket.getInputStream();
byte[] buffer = new byte[300];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
System.out.println("read = " + bytesRead);
byte[] guidBytes = new byte[17];
System.arraycopy(buffer, 0, guidBytes, 0, 17);
String guid = new String(guidBytes);
byte[] data = new byte[bytesRead - 17];
System.arraycopy(buffer, 17, data, 0, bytesRead - 17);
dataSet0(guid, data);
}
}
Thread.sleep(300);
} catch (IOException e2) {
e2.printStackTrace();
if (socket.isClosed()) {
try {
socket = new Socket(ip, 7776);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
});
// listenerThread.start();
}
public static boolean initDevice(String guid) {
if (socket == null || socket.isClosed()) {
try {
socket = new Socket(ip, 7776);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
socket.getOutputStream().write(guid.getBytes());
System.out.println("send xk");
return true;
} catch (IOException e) {
e.printStackTrace();
try {
socket.close();
} catch (UnknownHostException e2) {
e2.printStackTrace();
} catch (IOException e3) {
e3.printStackTrace();
}
}
return false;
}
static void dataSet0(String guid, byte[] data) {
// byte[] sysData = new byte[4];
// System.arraycopy(data, 12, sysData, 0, 4);
//
// int pulse = extractIntFromBits(sysData, 7, 15);
// System.out.println("pulse" + pulse);
//
// List byteList = new ArrayList<>();
// for (int i = 0; i < data.length; i++) {
// byteList.add(data[i]);
// }
//
// parseXD(pulse, byteList);
int b1 = data[0] & 0xff;
int b_1 = data[data.length - 1] & 0xff;
int b_xd = data[3] & 0xff;
if (b1 == 0x7f && b_1 == 0xf7) {
System.out.println("all data");
if (b_xd != 0x80) {
return;
}
byte[] sysData = new byte[4];
System.arraycopy(data, 12, sysData, 0, 4);
int pulse = extractIntFromBits(sysData, 7, 15);
System.out.println("pulse = " + pulse);
// int cmdcontentLen = (data[2] & 0xFF) << 8 | (data[1] & 0xFF);
// int xdLen = cmdcontentLen - 44;
// System.out.println("cmdcontentLen = " + cmdcontentLen);
//
// List fByteList = new ArrayList<>();
// for (int i = 16; i < 16 + 32; i++) {
// fByteList.add(data[i]);
// }
//
// List xdByteList = new ArrayList<>();
// for (int i = 48; i < 48 + xdLen; i++) {
// xdByteList.add(data[i]);
// }
//
// List xdIntList = extractByList(fByteList, xdByteList);
// PaintThread paintThread = PaintThread.getIns();
// paintThread.addXDData(xdIntList, pulse);
List integers = new ArrayList<>();
for (int i = 0; i < data.length; i++) {
integers.add(data[i]);
}
parse(guid, integers);
}
if (b1 == 0x7f && b_1 != 0xf7) {
System.out.println("未完待续");
preType = b_xd;
byte[] sysData = new byte[4];
System.arraycopy(data, 12, sysData, 0, 4);
int pulse = extractIntFromBits(sysData, 7, 15);
System.out.println("pulse" + pulse);
pulseMap.put(guid, pulse);
// int cmdcontentLen = (data[2] & 0xFF) << 8 | (data[1] & 0xFF);
// System.out.println("contentLen len = " + cmdcontentLen);
// int xdLen = cmdcontentLen - 44;
// byte[] xdData = new byte[xdLen];
// System.arraycopy(data, 48, xdData, 0, xdLen);
// map.put(guid, xdData);
map.put(guid, data);
}
if (b1 != 0x7f && b_1 == 0xf7) {
System.out.println("第二节");
if (preType != 0x80) {
return;
}
byte[] data1 = map.get(guid);
if (data1 != null) {
List integers = new ArrayList<>();
for (int i = 0; i < data1.length; i++) {
integers.add(data1[i]);
}
for (int i = 0; i < data.length; i++) {
integers.add(data[i]);
}
parse(guid, integers);
}
}
}
static void parse(String guid, List pack) {
int b1 = pack.get(0) & 0xff;
int b_1 = pack.get(pack.size() - 1) & 0xff;
int b_xd = pack.get(3) & 0xff;
if (b1 != 0x7F || b_1 != 0xF7) {
// for (Byte byte1 : pack) {
// System.out.print(String.format("%02x", byte1));
// System.out.print(" ");
// }
System.out.println("parse error");
} else {
if (b_xd == 0x80) {
int contentLen = (pack.get(2) & 0xFF) << 8 | (pack.get(1) & 0xFF);
System.out.println("contentLen len = " + contentLen);
List flagList = new ArrayList<>();
List ecgList = new ArrayList<>();
for (int i = 16; i < 16 + 32; i++) {
flagList.add(pack.get(i));
}
for (int i = 48; i < 48 + (contentLen - 44); i++) {
try {
ecgList.add(pack.get(i));
} catch (Exception e) {
e.printStackTrace();
System.out.println(" i = " + i + ", content len = " + contentLen + ", all len = ");
}
}
List shorts = extractByList(flagList, ecgList);
System.out.println("");
for (Integer s : shorts) {
System.out.print(s);
System.out.print(" ");
if (s < 0) {
// System.out.println("error");
}
}
System.out.println("");
System.out.println("parse end");
PaintThread paintThread = PaintThread.getIns();
paintThread.addXDData(shorts, pulseMap.get(guid));
}
}
}
static void parseXD(int pulse, List pack) {
int b1 = pack.get(0) & 0xff;
int b_1 = pack.get(pack.size() - 1) & 0xff;
int b_xd = pack.get(3) & 0xff;
if (b1 != 0x7F || b_1 != 0xF7) {
// for (Byte byte1 : pack) {
// System.out.print(String.format("%02x", byte1));
// System.out.print(" ");
// }
System.out.println("parse error");
} else {
if (b_xd == 0x80) {
int contentLen = (pack.get(2) & 0xFF) << 8 | (pack.get(1) & 0xFF);
System.out.println("contentLen len = " + contentLen);
List flagList = new ArrayList<>();
List ecgList = new ArrayList<>();
for (int i = 16; i < 16 + 32; i++) {
flagList.add(pack.get(i));
}
for (int i = 48; i < 48 + (contentLen - 44); i++) {
try {
ecgList.add(pack.get(i));
} catch (Exception e) {
e.printStackTrace();
System.out.println(" i = " + i + ", content len = " + contentLen + ", all len = ");
}
}
// List shorts = extractByList(flagList, ecgList);
List shorts = extractByList(flagList, ecgList);
System.out.println("");
for (Integer s : shorts) {
System.out.print(s);
System.out.print(" ");
}
System.out.println("");
System.out.println("parse end");
PaintThread paintThread = PaintThread.getIns();
paintThread.addXDData(shorts, pulse);
}
}
}
static List extract(byte[] flagData, byte[] ecgData) {
List ecgShortValues = new ArrayList<>();
// Check the validity of flagData and ecgData lengths
if (flagData.length != 32 || ecgData.length < 251 || ecgData.length > 500) {
throw new IllegalArgumentException("Invalid flagData or ecgData lengths");
}
int dataIndex = 0;
// Process each bit in flagData to determine the byte size for each of the 250
// ECG data points
for (int i = 0; i < 250; i++) {
int sh = (i % 8);
if (sh < 0) {
sh = 0;
}
if (i == 0) {
System.out.println("[i:0] bit=" + ((flagData[i / 8] >> sh) & 0x01));
}
boolean isTwoBytes = ((flagData[i / 8] >> sh) & 0x01) == 0x01;
if (isTwoBytes) {
// Extract a short (two bytes) from ecgData
short value = (short) ((ecgData[dataIndex + 1] & 0xFF) << 8 | (ecgData[dataIndex] & 0xFF));
ecgShortValues.add(value);
dataIndex += 2;
} else {
// Extract a byte (one byte) from ecgData
short value = (short) (ecgData[dataIndex] & 0xFF);
ecgShortValues.add(value);
dataIndex += 1;
}
System.out.print(" ");
}
System.out.println("dataIndex = " + dataIndex);
return ecgShortValues;
}
static List extractByList(List flagData, List ecgData) {
List ecgShortValues = new ArrayList<>();
// ByteBuffer buffer = ByteBuffer.allocate(ecgData.size());
//
// // 遍历ecgData,将每个字节添加到ByteBuffer中
// for (Byte b : ecgData) {
// buffer.put(b);
// }
//
// // 完成数据添加后,需要将ByteBuffer切换为读模式
// buffer.flip();
// Check the validity of flagData and ecgData lengths
if (flagData.size() != 32 || ecgData.size() < 251 || ecgData.size() > 500) {
throw new IllegalArgumentException("Invalid flagData or ecgData lengths");
}
// int dataIndex = 0;
// for (int i = 0; i < 250; i++) {
// int sh = (i % 8);
// if (sh < 0) {
// sh = 0;
// }
//
// if (i == 0) {
// System.out.println("[i:0] bit=" + ((flagData.get(i / 8) >> sh) & 0x01));
// }
//
// boolean isTwoBytes = ((flagData.get(i / 8) >> sh) & 0x01) == 0x01;
//
// if (isTwoBytes) {
// short value = (short) ((ecgData.get(dataIndex + 1) & 0xFF) << 8 | (ecgData.get(dataIndex) & 0xFF));
//
// int v2 = value;
// ecgShortValues.add(v2);
// dataIndex += 2;
//
// } else {
// // Extract a byte (one byte) from ecgData
// byte value = (byte) (ecgData.get(dataIndex) & 0xFF);
// int v2 = value + ecgShortValues.get(ecgShortValues.size()-1).intValue();
// ecgShortValues.add(v2);
// dataIndex += 1;
//
// }
//
// System.out.print(" ");
// }
int k = 0;
int dataIndex = 0;
int[] ecgsList = new int[ecgData.size()];
for(int i = 0; i < 32; ++i) {
if (dataIndex >= ecgData.size()) {
break;
}
byte rawByte = flagData.get(i);
for(int j = 0; j < 8 && k < ecgData.size(); ++k) {
if ((rawByte >>> j & 1) == 1) {
short value = (short) ((ecgData.get(dataIndex + 1) & 0xFF) << 8 | (ecgData.get(dataIndex) & 0xFF));
// ecgsList[k] = buffer.getShort();
ecgsList[k] = value;
dataIndex+=2;
} else {
if (dataIndex >= ecgData.size()) {
break;
}
try {
// ecgsList[k] = ecgsList[k - 1] + buffer.get();
ecgsList[k] = ecgsList[k - 1] + ecgData.get(dataIndex);
} catch (Exception e) {
// e.printStackTrace();
System.out.println("error : " + k + ", ecgdata len = " + ecgData.size() +", dataIndex= " + dataIndex);
}
dataIndex+=1;
}
++j;
}
}
for (int i = 0; i < k ; i++) {
ecgShortValues.add(ecgsList[i]);
}
return ecgShortValues;
}
public static byte[] convertHexStringToByteArray(String hexString) {
String[] hexValues = null;
if (hexString.contains("-")) {
hexValues = hexString.split("-");
} else {
hexValues = hexString.split(" ");
}
byte[] byteArray = new byte[hexValues.length];
for (int i = 0; i < hexValues.length; i++) {
byteArray[i] = (byte) Integer.parseInt(hexValues[i], 16);
}
return byteArray;
}
public static int extractIntFromBits(byte[] data, int startBit, int endBit) {
if (data.length < 4) {
throw new IllegalArgumentException("Input byte array must have at least 4 bytes");
}
int extractedValue = 0;
for (int i = 0; i < 4; i++) {
extractedValue |= (data[i] & 0xFF) << (8 * i);
}
// Shift to the right to discard lower bits and align the extracted bits to the
// rightmost position
extractedValue = extractedValue >> startBit;
// Mask to keep only the bits of interest
int mask = (1 << (endBit - startBit + 1)) - 1;
extractedValue &= mask;
return extractedValue;
}
public static void main(String[] args) {
String data = "7f 2c 01 80 bc 3b 01 00 6f 45 " + //
"7f 07 a1 25 e9 00 01 00 00 00 " + //
"00 00 00 00 00 00 00 00 98 03 " + //
"00 00 " + //
"00 00 00 00 " + //
"00 00 00 00 " + //
"00 00 00 00 " + //
"00 00 00 00 " + //
"e6 ff 00 00 " + //
"fd 00 00 00 " + //
"03 00 00 fd " + //
"03 00 00 fd " + //
"00 00 00 00 " + //
"00 fd 00 03 " + //
"00 00 fd 00 " + //
"00 00 00 00 " + //
"00 fc 00 00 " + //
"04 00 00 00 " + //
"fc fd 03 00 " + //
"04 00 00 00 " + //
"00 00 00 fc " + //
"00 00 00 00 " + //
"00 00 00 00 " + //
"00 00 fd 00 " + //
"fd 00 03 03 " + //
"04 00 00 03 " + //
"03 03 00 00 " + //
"fa fd fc fd " + //
"00 00 03 00 " + //
"fd fd 00 00 " + //
"03 00 00 00 " + //
"00 03 00 04 " + //
"03 0d 30 5d " + //
"0a 01 9f 01 " + //
"55 d6 22 01 " + //
"45 00 9e ff " + //
"be 1d 38 28 " + //
"0d 03 04 03 " + //
"00 00 fd 03 " + //
"03 04 00 fc " + //
"00 04 06 03 " + //
"fd 00 03 03 " + //
"03 00 00 03 " + //
"04 03 03 04 " + //
"03 06 04 00 " + //
"00 06 07 03 " + //
"07 03 03 0a " + //
"07 03 07 0a " + //
"0a 06 03 07 " + //
"07 06 07 00 " + //
"00 03 00 fd " + //
"f9 f6 f6 f7 " + //
"f2 f0 ef f3 " + //
"f3 f6 f6 f6 " + //
"f7 fd fa fc " + //
"fd 00 fd 00 " + //
"fc fd 00 fd " + //
"00 fd 00 00 " + //
"03 00 00 fd " + //
"00 00 00 00 " + //
"00 03 00 00 " + //
"00 00 00 03 " + //
"00 00 00 00 " + //
"03 00 fd fd " + //
"00 03 00 fd " + //
"00 00 00 00 " + //
"00 00 00 00 " + //
"00 00 00 00 " + //
"00 00 fd 00 " + //
"00 00 00 00 " + //
"00 fc 04 00 " + //
"fc 00 00 04 " + //
"96 f7";
byte[] originData = convertHexStringToByteArray(data);
System.out.println("total len = " + originData.length);
int len = (originData[2] & 0xFF) << 8 | (originData[1] & 0xFF);
System.out.println("len = " + len);
int pulse = 0;
byte[] flagData = new byte[32];
byte[] sysData = new byte[4];
byte[] ecgData = new byte[len - 44];
System.arraycopy(originData, 16, flagData, 0, 32);
System.arraycopy(originData, 12, sysData, 0, 4);
System.arraycopy(originData, 48, ecgData, 0, len - 44);
int extractedValue = extractIntFromBits(sysData, 7, 15);
System.out.println("pulse = " + extractedValue);
for (byte b : ecgData) {
System.out.print(String.format("%02x", b));
System.out.print(" ");
}
System.out.println(" ");
List shorts = extract(flagData, ecgData);
for (Short short1 : shorts) {
System.out.print(short1);
System.out.print(" ");
}
System.out.println(" ");
byte t = (byte) 0x80;
System.out.println(0 & 1);
System.out.println(1 & 1);
System.out.println((0x80 >> 8) & 0x01);
System.out.println((0x80 >> 7) & 0x01);
}
}
加入收藏