|
@@ -1,5 +1,9 @@
|
|
|
package com.js.kbt.socket;
|
|
|
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.FileNotFoundException;
|
|
|
+import java.io.IOException;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.text.ParseException;
|
|
|
import java.text.SimpleDateFormat;
|
|
@@ -10,10 +14,10 @@ import java.util.List;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
|
+import org.apache.commons.io.FileUtils;
|
|
|
import org.apache.log4j.Logger;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
-import org.springframework.util.StringUtils;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.js.kbt.mapper.ChangeIpCommandMapper;
|
|
@@ -38,10 +42,7 @@ import com.js.kbt.model.EquipmentSendCommand;
|
|
|
import com.js.kbt.model.EquipmentSendCommandExample;
|
|
|
import com.js.kbt.model.LonLatRecord;
|
|
|
import com.js.kbt.model.LonLatRecordExample;
|
|
|
-import com.js.kbt.model.MouldDownPacket;
|
|
|
-import com.js.kbt.model.MouldDownPacketExample;
|
|
|
import com.js.kbt.model.MouldHistory;
|
|
|
-import com.js.kbt.model.MouldHistoryExample;
|
|
|
import com.js.kbt.model.MouldHistoryTime;
|
|
|
import com.js.kbt.model.MouldHistoryTimeExample;
|
|
|
import com.js.kbt.model.RecDataLog;
|
|
@@ -51,7 +52,7 @@ import com.js.kbt.model.TbMouldEquipment;
|
|
|
import com.js.kbt.model.TbMouldEquipmentExample;
|
|
|
import com.js.kbt.model.TbMouldExample;
|
|
|
import com.js.kbt.model.TimeCalibrationRecord;
|
|
|
-import com.taobao.api.internal.toplink.embedded.websocket.util.StringUtil;
|
|
|
+import com.js.kbt.util.ByteUtils;
|
|
|
|
|
|
import io.netty.buffer.ByteBuf;
|
|
|
import io.netty.buffer.Unpooled;
|
|
@@ -119,7 +120,9 @@ public class UserHandler extends SimpleChannelInboundHandler<String> {
|
|
|
private String equipmentNo = null;
|
|
|
|
|
|
public static int RET_CMD_LENGTH = "FAAF23000106000000000000000100000000000000000000000000000000000000B2C2".length() ;
|
|
|
-
|
|
|
+ public static String REMOTE_UPDATE_START = "FAAF0D0001";
|
|
|
+ public static String[] ERROR = {"无故障", "异或校验失败", "CRC校验失败", "超时", "包序号不匹配", "访问失败"};
|
|
|
+
|
|
|
@Override
|
|
|
protected void channelRead0(ChannelHandlerContext arg0, String arg1) throws ParseException {
|
|
|
String pack = "FAAF0007001e781e50003C37D5";
|
|
@@ -127,9 +130,81 @@ public class UserHandler extends SimpleChannelInboundHandler<String> {
|
|
|
// sendChangeIp();
|
|
|
logger.info("开始接受数据...");
|
|
|
System.out.println("收到===" + arg1 + "\n");
|
|
|
- //设备回馈的指令:FAAF23000106000000000000000100000000000000000000000000000000000000B2C2
|
|
|
- if (arg1.length() == RET_CMD_LENGTH
|
|
|
- &&arg1.startsWith("FAAF23000106")) {
|
|
|
+
|
|
|
+ if (arg1.startsWith(REMOTE_UPDATE_START)) {//远程升级,设备到云平台的指令
|
|
|
+ String cmdStr = arg1.substring(REMOTE_UPDATE_START.length(), REMOTE_UPDATE_START.length() + 2);
|
|
|
+ if ("01".contentEquals(cmdStr)) {
|
|
|
+ //MCU已准备好,可以更新;云平台下发起始帧
|
|
|
+ File f = new File("E:\\software\\apache-tomcat-9.0.14\\webapps\\yscloud\\files\\Mould_Mnitor.bin");
|
|
|
+ int packSize = 900;
|
|
|
+ int packCount = (int) (f.length()/packSize + (f.length()%packSize==0?0:1));
|
|
|
+ String startCmd = startCmd(packCount);
|
|
|
+ sendMsg(startCmd);
|
|
|
+ receiveMCUProcess(equipmentNo, arg1, "MCU已准备就绪");
|
|
|
+ } else if ("02".contentEquals(cmdStr)) {
|
|
|
+ //状态反馈
|
|
|
+ String errorCode = arg1.substring(REMOTE_UPDATE_START.length() + 2, REMOTE_UPDATE_START.length() + 2 + 2);
|
|
|
+ logger.info("状态反馈="+ERROR[Integer.parseInt(errorCode, 16)]);
|
|
|
+ if ("00".equals(errorCode)) {
|
|
|
+ //反馈成功,获取需要接受的包数,从1开始
|
|
|
+ String recPackPageIndex = arg1.substring(REMOTE_UPDATE_START.length() + 4*2, REMOTE_UPDATE_START.length() + 4*2 + 2*2);//9-10位
|
|
|
+ int packIndex = Integer.parseInt(highInFrontLowInBack(recPackPageIndex), 16);
|
|
|
+ logger.info("接收到packIndex=="+packIndex);
|
|
|
+ File f = new File("E:\\software\\apache-tomcat-9.0.14\\webapps\\yscloud\\files\\Mould_Mnitor.bin");
|
|
|
+
|
|
|
+ try {
|
|
|
+ //读取到全部文件内容
|
|
|
+ byte[] allFileBytes = FileUtils.readFileToByteArray(f);
|
|
|
+ //计算整包CRC校验位
|
|
|
+ String fileCRC = getCRC(allFileBytes);
|
|
|
+ fileCRC = fillZeroLeft(fileCRC, 2);//2位
|
|
|
+ fileCRC = highInFrontLowInBack(fileCRC);//高低位互换
|
|
|
+ int totalByteSize = allFileBytes.length + 2;//加上最后的两位校验位
|
|
|
+ byte[] crcByte = ByteUtils.getByteFromHex(fileCRC);
|
|
|
+ byte[] allBytes = ByteUtils.byteMerger(allFileBytes, crcByte);
|
|
|
+
|
|
|
+ int packSize = 900;
|
|
|
+ int packCount = (int) (totalByteSize/packSize + (totalByteSize%packSize==0?0:1));
|
|
|
+ logger.info("总包数="+packCount);
|
|
|
+
|
|
|
+ byte[] packData = null;
|
|
|
+
|
|
|
+ if (packIndex == packCount) {
|
|
|
+ //这是最后一包,按实际大小
|
|
|
+ int lastPackSize = (int) (totalByteSize - (packIndex -1)*packSize);
|
|
|
+ logger.info("最后一包size="+lastPackSize);
|
|
|
+ packData = new byte[lastPackSize];
|
|
|
+ } else {
|
|
|
+ packData = new byte[900];
|
|
|
+ }
|
|
|
+ //从数据包中提取
|
|
|
+ int start = (packIndex-1)*packSize;
|
|
|
+ System.arraycopy(allBytes, start, packData, 0, packData.length);
|
|
|
+ //组装返回数据
|
|
|
+ String msg = dataCmd(packIndex, packData);
|
|
|
+ sendMsg(msg);
|
|
|
+ } catch (FileNotFoundException e) {
|
|
|
+ // TODO Auto-generated catch block
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (IOException e) {
|
|
|
+ // TODO Auto-generated catch block
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ //过程中出错的需要记录下来
|
|
|
+ receiveMCUProcess(equipmentNo, arg1, ERROR[Integer.parseInt(errorCode, 16)]);
|
|
|
+ }
|
|
|
+
|
|
|
+ } else if ("03".contentEquals(cmdStr)){
|
|
|
+ logger.info("=============更新成功=================");
|
|
|
+ receiveMCUProcess(equipmentNo, arg1, "更新成功");
|
|
|
+ } else if ("04".contentEquals(cmdStr)){
|
|
|
+ logger.info("!!!!!!!!!!!!@@更新失败@@!!!!!!!!");
|
|
|
+ receiveMCUProcess(equipmentNo, arg1, "更新失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ } else if (arg1.length() == RET_CMD_LENGTH
|
|
|
+ &&arg1.startsWith("FAAF23000106")) {//设备回馈的指令:FAAF23000106000000000000000100000000000000000000000000000000000000B2C2
|
|
|
//TODO: 更新设备指令下发的执行状态
|
|
|
logger.info("设备回馈指令");
|
|
|
String cmdExecuteStatusStr = arg1.substring("FAAF23000106".length());
|
|
@@ -172,9 +247,24 @@ public class UserHandler extends SimpleChannelInboundHandler<String> {
|
|
|
"2621远程更新"
|
|
|
};
|
|
|
|
|
|
+ //处理远程更新指令反馈
|
|
|
+ private void receiveMCUProcess(String equipmentNo, String recData, String txt) {
|
|
|
+ EquipmentRecCommand cmd = new EquipmentRecCommand();
|
|
|
+ cmd.setEquipmentNo(equipmentNo);
|
|
|
+ cmd.setCmd(recData);
|
|
|
+ cmd.setCmdTxt(txt);
|
|
|
+ equipmentRecCommandMapper.insertSelective(cmd);
|
|
|
+
|
|
|
+ //主表设置有新状态
|
|
|
+ TbMouldEquipmentExample example = new TbMouldEquipmentExample();
|
|
|
+ example.createCriteria().andEquipmentNoEqualTo(equipmentNo);
|
|
|
+ TbMouldEquipment record = new TbMouldEquipment();
|
|
|
+ record.setHasNewMessage(1);
|
|
|
+ tbMouldEquipmentMapper.updateByExampleSelective(record, example);
|
|
|
+ }
|
|
|
+
|
|
|
//存储设备返回的执行指令
|
|
|
private void receiveCmdStatus(String str) {
|
|
|
- //找到最新的一条命令匹配的记录,更新其状态为已执行。
|
|
|
EquipmentRecCommand cmd = new EquipmentRecCommand();
|
|
|
cmd.setEquipmentNo(equipmentNo);
|
|
|
cmd.setCmd(str);
|
|
@@ -187,21 +277,24 @@ public class UserHandler extends SimpleChannelInboundHandler<String> {
|
|
|
}
|
|
|
}
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
- for (int i=0;i<result.size(); i++) {
|
|
|
- sb.append(result.get(i));
|
|
|
- if (i < result.size() -1) {
|
|
|
- sb.append(",");
|
|
|
+ if (result.size() > 0) {
|
|
|
+ //有回馈的情况
|
|
|
+ for (int i=0;i<result.size(); i++) {
|
|
|
+ sb.append(result.get(i));
|
|
|
+ if (i < result.size() -1) {
|
|
|
+ sb.append(",");
|
|
|
+ }
|
|
|
}
|
|
|
+ cmd.setCmdTxt(sb.toString());
|
|
|
+ equipmentRecCommandMapper.insertSelective(cmd);
|
|
|
+
|
|
|
+ //主表设置有新状态
|
|
|
+ TbMouldEquipmentExample example = new TbMouldEquipmentExample();
|
|
|
+ example.createCriteria().andEquipmentNoEqualTo(equipmentNo);
|
|
|
+ TbMouldEquipment record = new TbMouldEquipment();
|
|
|
+ record.setHasNewMessage(1);
|
|
|
+ tbMouldEquipmentMapper.updateByExampleSelective(record, example);
|
|
|
}
|
|
|
- cmd.setCmdTxt(sb.toString());
|
|
|
- equipmentRecCommandMapper.insertSelective(cmd);
|
|
|
-
|
|
|
- //主表设置有新状态
|
|
|
- TbMouldEquipmentExample example = new TbMouldEquipmentExample();
|
|
|
- example.createCriteria().andEquipmentNoEqualTo(equipmentNo);
|
|
|
- TbMouldEquipment record = new TbMouldEquipment();
|
|
|
- record.setHasNewMessage(1);
|
|
|
- tbMouldEquipmentMapper.updateByExampleSelective(record, example);
|
|
|
}
|
|
|
|
|
|
private String processMsg(String input) throws ParseException {
|
|
@@ -310,36 +403,41 @@ public class UserHandler extends SimpleChannelInboundHandler<String> {
|
|
|
item.setAlarm(Integer.decode("0x" + string.substring(64 * 2, 65 * 2)));
|
|
|
String str = string.substring(65 * 2, 69 * 2);
|
|
|
item.setRunCnt(reverseParseHex(str));
|
|
|
- LonLatRecordExample rExp = new LonLatRecordExample();
|
|
|
- rExp.createCriteria().andMccEqualTo("460").andMncEqualTo("0").andGprsCiEqualTo(item.getGprsCi())
|
|
|
- .andGprsLacEqualTo(item.getGprsLac());
|
|
|
- List<LonLatRecord> lRList = lonLatRecordMapper.selectByExample(rExp);
|
|
|
- if (lRList.size() > 0) {
|
|
|
- LonLatRecord lonLatRecord = lRList.get(0);
|
|
|
- item.setLng(lonLatRecord.getLng());
|
|
|
- item.setLat(lonLatRecord.getLat());
|
|
|
-// System.out.println("直接重用经纬度===");
|
|
|
- } else {
|
|
|
- // 根据基站lac和ci获取经纬度
|
|
|
- String api = "http://api.cellocation.com:81/cell/?mcc=460&mnc=0&lac=" + item.getGprsLac() + "&ci="
|
|
|
- + item.getGprsCi() + "&output=json";
|
|
|
- String resp = com.js.kbt.util.HttpRequest.sendGet(api, null);
|
|
|
- JSONObject json = JSONObject.parseObject(resp);
|
|
|
- if (json != null && json.getInteger("errcode") == 0) {
|
|
|
- item.setLng(json.getDouble("lon") + "");
|
|
|
- item.setLat(json.getDouble("lat") + "");
|
|
|
+ if (!item.getGprsCi().equals("0") && !item.getGprsLac().contentEquals("0")) {
|
|
|
+ LonLatRecordExample rExp = new LonLatRecordExample();
|
|
|
+ rExp.createCriteria().andMccEqualTo("460").andMncEqualTo("0").andGprsCiEqualTo(item.getGprsCi())
|
|
|
+ .andGprsLacEqualTo(item.getGprsLac());
|
|
|
+ logger.info("ci="+item.getGprsCi()+", lac="+item.getGprsLac());
|
|
|
+ List<LonLatRecord> lRList = lonLatRecordMapper.selectByExample(rExp);
|
|
|
+ if (lRList.size() > 0) {
|
|
|
+ LonLatRecord lonLatRecord = lRList.get(0);
|
|
|
+ item.setLng(lonLatRecord.getLng());
|
|
|
+ item.setLat(lonLatRecord.getLat());
|
|
|
+ logger.info("直接重用经纬度===");
|
|
|
} else {
|
|
|
- logger.error("调用基站解析平台出错: " + resp);
|
|
|
+ // 根据基站lac和ci获取经纬度
|
|
|
+ String api = "http://api.cellocation.com:81/cell/?mcc=460&mnc=0&lac=" + item.getGprsLac() + "&ci="
|
|
|
+ + item.getGprsCi() + "&output=json";
|
|
|
+ String resp = com.js.kbt.util.HttpRequest.sendGet(api, null);
|
|
|
+ JSONObject json = JSONObject.parseObject(resp);
|
|
|
+ logger.info("请求经纬度=" +json.toJSONString() );
|
|
|
+ if (json != null && json.getInteger("errcode") == 0) {
|
|
|
+ item.setLng(json.getDouble("lon") + "");
|
|
|
+ item.setLat(json.getDouble("lat") + "");
|
|
|
+ } else {
|
|
|
+ logger.error("调用基站解析平台出错: " + resp);
|
|
|
+ }
|
|
|
+ LonLatRecord latRecord = new LonLatRecord();
|
|
|
+ latRecord.setGprsCi(item.getGprsCi());
|
|
|
+ latRecord.setMcc("460");
|
|
|
+ latRecord.setMnc("0");
|
|
|
+ latRecord.setGprsLac(item.getGprsLac());
|
|
|
+ latRecord.setLat(item.getLat());
|
|
|
+ latRecord.setLng(item.getLng());
|
|
|
+ lonLatRecordMapper.insertSelective(latRecord);
|
|
|
}
|
|
|
- LonLatRecord latRecord = new LonLatRecord();
|
|
|
- latRecord.setGprsCi(item.getGprsCi());
|
|
|
- latRecord.setMcc("460");
|
|
|
- latRecord.setMnc("0");
|
|
|
- latRecord.setGprsLac(item.getGprsLac());
|
|
|
- latRecord.setLat(item.getLat());
|
|
|
- latRecord.setLng(item.getLng());
|
|
|
- lonLatRecordMapper.insertSelective(latRecord);
|
|
|
}
|
|
|
+
|
|
|
String crcStr = string.substring(string.length() - 4);
|
|
|
item.setCrcCode("" + reverseParseHex(crcStr));
|
|
|
// 存入数据库
|
|
@@ -553,7 +651,6 @@ public class UserHandler extends SimpleChannelInboundHandler<String> {
|
|
|
}
|
|
|
|
|
|
public void timeCalibration(String equipmentNo) {
|
|
|
- logger.info("下发校准时间的云模盒equipmentNo="+equipmentNo);
|
|
|
if (equipmentNo.startsWith("FAAF")) {
|
|
|
logger.info("非法云模盒编号,不作下发处理");
|
|
|
return;
|
|
@@ -563,6 +660,7 @@ public class UserHandler extends SimpleChannelInboundHandler<String> {
|
|
|
if (count.size() == 0) {
|
|
|
//新协议
|
|
|
String hexDate = getNowDateHex();
|
|
|
+ logger.info("下发校准时间的云模盒equipmentNo="+equipmentNo);
|
|
|
logger.info("校准系统模块时间转成16进制的字符串==>" + hexDate);
|
|
|
sendMsg(getDownPackage(CMD_TIME_FIX, hexDate));
|
|
|
TimeCalibrationRecord timeCalibrationRecord = new TimeCalibrationRecord();
|
|
@@ -659,7 +757,7 @@ public class UserHandler extends SimpleChannelInboundHandler<String> {
|
|
|
}
|
|
|
me.setCurhillNumber(item.getBattery() + "");
|
|
|
if (org.apache.commons.lang.StringUtils.isNotEmpty(item.getLng())) {
|
|
|
- logger.info("更新经纬度:");
|
|
|
+ logger.info("更新经纬度: lng = " + item.getLng() + ", lat=" + item.getLat());
|
|
|
me.setLng(Double.parseDouble(item.getLng()));
|
|
|
me.setLat(Double.parseDouble(item.getLat()));
|
|
|
} else {
|
|
@@ -907,16 +1005,24 @@ public class UserHandler extends SimpleChannelInboundHandler<String> {
|
|
|
}
|
|
|
|
|
|
public static void main(String[] args) throws ParseException {
|
|
|
- String string = "FAAF23000106000000000000000100000000000000000000000000000000000000B2C2";
|
|
|
- if (string.length() >= 25 * 2) {
|
|
|
- String mobilePart = string.substring(4 * 2, 5 * 2);
|
|
|
- String mobile = getStringFromHexStr(mobilePart);
|
|
|
- // 获取设备编码15-24
|
|
|
- String deviceNumPart = string.substring(5 * 2, 25 * 2);
|
|
|
- logger.info("解析出设备deviceNumPart=" + deviceNumPart);
|
|
|
- String deviceNum = getStringFromHexStr(deviceNumPart);
|
|
|
- logger.info("解析出设备No=" + deviceNum);
|
|
|
- }
|
|
|
+// String string = "FAAF23000106000000000000000100000000000000000000000000000000000000B2C2";
|
|
|
+// if (string.length() >= 25 * 2) {
|
|
|
+// String mobilePart = string.substring(4 * 2, 5 * 2);
|
|
|
+// String mobile = getStringFromHexStr(mobilePart);
|
|
|
+// // 获取设备编码15-24
|
|
|
+// String deviceNumPart = string.substring(5 * 2, 25 * 2);
|
|
|
+// logger.info("解析出设备deviceNumPart=" + deviceNumPart);
|
|
|
+// String deviceNum = getStringFromHexStr(deviceNumPart);
|
|
|
+// logger.info("解析出设备No=" + deviceNum);
|
|
|
+// }
|
|
|
+ File f = new File("C:\\Mould_Mnitor.bin");
|
|
|
+ System.out.println("flength=="+f.length());
|
|
|
+ try {
|
|
|
+ System.out.println("size f="+FileUtils.readFileToByteArray(f).length);
|
|
|
+ } catch (IOException e) {
|
|
|
+ // TODO Auto-generated catch block
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
private String getNowDateHex() {
|
|
@@ -939,4 +1045,91 @@ public class UserHandler extends SimpleChannelInboundHandler<String> {
|
|
|
}
|
|
|
return str;
|
|
|
}
|
|
|
+
|
|
|
+ private static String fillZeroLeft(String str, int bitNum) {
|
|
|
+ int fillNum = bitNum*2-str.length();
|
|
|
+ for (int i=0;i< fillNum; i++) {
|
|
|
+ str = "0" + str;
|
|
|
+ }
|
|
|
+ return str;
|
|
|
+ }
|
|
|
+
|
|
|
+ //起始帧
|
|
|
+ public static byte[] getCRCByStartCmd(String cmd, String hexData) {
|
|
|
+ byte[] crcByte = new byte[11];
|
|
|
+ crcByte[0]= (byte)Integer.parseInt("FA", 16);
|
|
|
+ crcByte[1]= (byte)Integer.parseInt("AF", 16);
|
|
|
+ crcByte[2]= (byte)Integer.parseInt("0D", 16);
|
|
|
+ crcByte[3]= (byte)Integer.parseInt("00", 16);
|
|
|
+ crcByte[4]= (byte)Integer.parseInt("00", 16);//云平台至设备
|
|
|
+ crcByte[5]= (byte)Integer.parseInt(cmd, 16);
|
|
|
+ crcByte[6] = (byte)Integer.parseInt(hexData.substring(0, 2), 16);
|
|
|
+ crcByte[7] = (byte)Integer.parseInt(hexData.substring(2, 4), 16);
|
|
|
+ crcByte[8] = (byte)Integer.parseInt(hexData.substring(4, 6), 16);
|
|
|
+ crcByte[9] = (byte)Integer.parseInt(hexData.substring(6, 8), 16);
|
|
|
+ crcByte[10] = (byte)Integer.parseInt(hexData.substring(8, 10), 16);
|
|
|
+ return crcByte;
|
|
|
+ }
|
|
|
+
|
|
|
+ //组装起始帧命令
|
|
|
+ private static String startCmd(int packCount) {
|
|
|
+ String packCountHex = Integer.toHexString(packCount);
|
|
|
+ packCountHex = fillZeroLeft(packCountHex, 2);
|
|
|
+ System.out.println(packCountHex);
|
|
|
+ packCountHex = highInFrontLowInBack(packCountHex);
|
|
|
+
|
|
|
+ String str = "00 00 00 "+packCountHex;
|
|
|
+ str = str.replaceAll(" ", "");
|
|
|
+ byte[] crcBytes = getCRCByStartCmd("03", str);
|
|
|
+ String crcCode = getCRC(crcBytes);
|
|
|
+ System.out.println("crcCode=="+crcCode);
|
|
|
+ crcCode = highInFrontLowInBack(crcCode);
|
|
|
+
|
|
|
+ return "FAAF0D000003" + str + crcCode;
|
|
|
+ }
|
|
|
+
|
|
|
+ //十六进制转byte
|
|
|
+ public static byte[] getByteFromHex(String hexStr) {
|
|
|
+ byte[] data = new byte[hexStr.length()/2];
|
|
|
+ for (int i=0;i<hexStr.length(); i+=2) {
|
|
|
+ data[i/2] = (byte)Integer.parseInt(hexStr.substring(i, i+2), 16);
|
|
|
+ }
|
|
|
+
|
|
|
+ return data;
|
|
|
+ }
|
|
|
+
|
|
|
+ //组装数据帧命令
|
|
|
+ private static String dataCmd(int packIndex, byte[] data) {
|
|
|
+ String packIndexHex = Integer.toHexString(packIndex);
|
|
|
+ packIndexHex = fillZeroLeft(packIndexHex, 2);
|
|
|
+ System.out.println("dataCmd packIndexHex =="+packIndexHex);
|
|
|
+ packIndexHex = highInFrontLowInBack(packIndexHex);
|
|
|
+ logger.info("dataCmd packIndexHex =="+packIndexHex);
|
|
|
+
|
|
|
+ int dataLength = data.length + 10;
|
|
|
+ String dataLengthHex = Integer.toHexString(dataLength);
|
|
|
+ dataLengthHex = fillZeroLeft(dataLengthHex, 2);
|
|
|
+ dataLengthHex = highInFrontLowInBack(dataLengthHex);
|
|
|
+ logger.info("dataCmd dataLengthHex =="+dataLengthHex);
|
|
|
+
|
|
|
+ String allStr = "FAAF"+dataLengthHex+"0004" + packIndexHex;
|
|
|
+ byte[] byteFromHex = getByteFromHex(allStr);
|
|
|
+ byte[] allBytes = new byte[byteFromHex.length + data.length];
|
|
|
+ for (int i=0;i<byteFromHex.length; i++) {
|
|
|
+ allBytes[i] = byteFromHex[i];
|
|
|
+ }
|
|
|
+ int startIndex = byteFromHex.length;
|
|
|
+ for (int i=0;i<data.length; i++) {
|
|
|
+ allBytes[i+startIndex] = data[i];
|
|
|
+ }
|
|
|
+
|
|
|
+ String crcCode = getCRC(allBytes);
|
|
|
+ logger.info("crcCode=="+crcCode);
|
|
|
+ crcCode = fillZeroLeft(crcCode, 2);
|
|
|
+ crcCode = highInFrontLowInBack(crcCode);
|
|
|
+ //数据转16进制
|
|
|
+ String str = BinaryToHexString(data).replaceAll(" ", "");
|
|
|
+
|
|
|
+ return "FAAF"+dataLengthHex+"0004" + packIndexHex + str + crcCode;
|
|
|
+ }
|
|
|
}
|