0、背景
本协议包参考车联网TCP 组包格式,Java写测试客户端组包函数。
1、代码
java组包代码,支持http 的终端可以通过此接口发送消息,统一组包到消息队列。
private Integer postDataToMq(String tsid, String message_type, Float temper, Float humidity){
String head = strTo16("##");
String cmd = "";
cmd += message_type;//2 命令标识 01 登录 02 实时信息上报
cmd += "FE";//3 应答标志
cmd += strTo16(tsid);
cmd += "01";//21 数据单元加密方式 01 不加密
String dataLength = "";
String data = "";
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
Date dtNow = new Date();
String nowStr = df.format(new Date());
String year = nowStr;
//System.out.println(year.substring(2, 4));
data += padLeft(Integer.toHexString(Integer.parseInt(year.substring(2, 4))), 2, '0');// nowStr.substring(2, 2);//年
//System.out.println(year.substring(2, 4));
data += padLeft(Integer.toHexString(Integer.parseInt(nowStr.substring(4,6))), 2, '0');// nowStr.substring(4,2);//月
//System.out.println(nowStr.substring(4,6));
data += padLeft(Integer.toHexString(Integer.parseInt(nowStr.substring(6,8))), 2, '0');// nowStr.substring(6,2);//日
//System.out.println(nowStr.substring(6,8));
data += padLeft(Integer.toHexString(Integer.parseInt(nowStr.substring(8, 10))), 2, '0');// strTo16((nowStr.substring(8, 2)));//时
//System.out.println(nowStr.substring(8, 10));
data += padLeft(Integer.toHexString(Integer.parseInt(nowStr.substring(10,12))), 2, '0');// strTo16(nowStr.substring(10,2));//分
//System.out.println(nowStr.substring(10,12));
data += padLeft(Integer.toHexString(dtNow.getSeconds()), 2, '0');// strTo16(nowStr.substring(12, 2));//秒
//System.out.println(dtNow.getSeconds());
if(message_type.equals("02")) {
data += "01";
DecimalFormat decimalFormat = new DecimalFormat(".0");//构造方法的字符格式这里如果小数不足2位,会以0补足.
String p = decimalFormat.format(temper);//format 返回的是字符串
System.out.println(padLeft(p.replace('.', '0'), 4, '0'));
data += padLeft(p.replace('.', '0'), 4, '0');
data += "02";
String h = decimalFormat.format(humidity);
data += padLeft(h.replace('.', '0'), 4, '0');
}else {
data += padLeft(Integer.toHexString(2001), 4, '0');//登录流水号
data += strTo16("898600MXKENJIXXXXX03");//20位 SSID
data += strTo16("1");//温度
data += strTo16("1");//湿度
data += strTo16("0");//烟雾
data += strTo16("0");//水浸
data += strTo16("0");//电力
data += strTo16("0");//入侵
}
dataLength = Integer.toHexString(data.length());
dataLength = padLeft(dataLength, 4, '0');
byte[] checkArray = (cmd + dataLength + data).getBytes();
String check = getBCC(checkArray);
String result = head + cmd + dataLength + data + check;
//rabbitTemplate.convertAndSend("System.Byte[], mscorlib","upload", result.getBytes());
//Message msg = new Message();
//msg.setMsg(result);
String routingKey = "upload";
if(message_type.equals("01")){
routingKey = "login";
}
rabbitTemplate.convertAndSend("IEMS",routingKey, result);
System.out.println(result);
return 1;
}
public static String getBCC(byte[] data) {
String ret = "";
byte BCC[]= new byte[1];
for(int i=0;i<data.length;i++)
{
BCC[0]=(byte) (BCC[0] ^ data[i]);
}
String hex = Integer.toHexString(BCC[0] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
ret += hex.toUpperCase();
return ret;
}
/**
* String左对齐
*/
public static String padLeft(String src, int len, char ch) {
int diff = len - src.length();
if (diff <= 0) {
return src;
}
char[] charr = new char[len];
System.arraycopy(src.toCharArray(), 0, charr, 0, src.length());
for (int i = src.length(); i < len; i++) {
charr[i] = ch;
}
return new String(charr);
}
/**
* 字符串转化成为16进制字符串
* @param s
* @return
*/
public static String strTo16(String s) {
String str = "";
for (int i = 0; i < s.length(); i++) {
int ch = (int) s.charAt(i);
String s4 = Integer.toHexString(ch);
str = str + s4;
}
return str;
}
标签:strTo16,Java,String,包之,联网,substring,Integer,nowStr,data
From: https://blog.51cto.com/kenji/7578229