</parent> | </parent> | ||||
<groupId>com.inet</groupId> | <groupId>com.inet</groupId> | ||||
<artifactId>ailink-receiver</artifactId> | <artifactId>ailink-receiver</artifactId> | ||||
<version>1.1-20210826</version> | |||||
<version>1.2-20250226</version> | |||||
<name>ailink-receiver</name> | <name>ailink-receiver</name> | ||||
<description>Demo project for Spring Boot</description> | <description>Demo project for Spring Boot</description> | ||||
<properties> | <properties> | ||||
<artifactId>spring-boot-starter-test</artifactId> | <artifactId>spring-boot-starter-test</artifactId> | ||||
<scope>test</scope> | <scope>test</scope> | ||||
</dependency> | </dependency> | ||||
<!-- 以下是后来增加的库--> | |||||
<!-- <dependency>--> | |||||
<!-- <groupId>com.inet</groupId>--> | |||||
<!-- <artifactId>inet-base</artifactId>--> | |||||
<!-- <version>1.0</version>--> | |||||
<!-- </dependency>--> | |||||
<dependency> | <dependency> | ||||
<groupId>commons-lang</groupId> | <groupId>commons-lang</groupId> | ||||
<artifactId>commons-lang</artifactId> | <artifactId>commons-lang</artifactId> |
SUCCESS("1","success"), | SUCCESS("1","success"), | ||||
FAIL("0","faile"), | FAIL("0","faile"), | ||||
PARAM_EMPTY("9901","params is null"), | PARAM_EMPTY("9901","params is null"), | ||||
DAO_ERROR("9902","数据库操作失败"), | |||||
USER_NOT_EXIST("9903","用户不存在"), | |||||
USER_PASSWORD_ERROR("9904","用户密码错误"), | |||||
USER_EXIST("9905","用户已经存在"), | |||||
PASSWORD_INCONSIST("9906","密码不一致"), | |||||
VERIFY_CODE_ERROR("9907","验证码错误"), | |||||
VERIFY_CODE_SEND_OUT_MAX_NUMBER("9908","验证码发送超过当天最大次数"), | |||||
VERIFY_CODE_NOT_EXIST("9909","不存在可用的验证码"), | |||||
PARAM_UNDEFINE("9910","参数未定义"), | |||||
PARAM_ERROR_REGION_PUT("9911","解析区域投放参数错误"), | |||||
PARAM_INVALID("9912","params is invalid"), | |||||
USER_NO_PERMISSIONS("9913","用户无权限"), | |||||
DEVICE_NOT_EXIST("9914","设备不存在"), | |||||
COMPANY_NOT_EXIST("9915","参数缺少APPId"), | |||||
NOT_PERMISSIONS("9916","没有权限"), | |||||
IN_USE("9917","使用中,不能删除"), | |||||
CANT_MODIFY("9929","操作冲突,刷新重试"), | |||||
PARAM_CHECK_ERROR("9995","参数检查错误"), | |||||
USER_UNLOGIN("9997","用户未登录"), | |||||
QUERY_EMPTY("9998","查询结果为空"), | |||||
FILE_TOO_BIG("9920","图片文件不能超过10M,视频文件不能超过30M"), | |||||
DAO_UPDATE_COUNT_ZERO_EXCEPTION("9921","更新失败"), | |||||
ACCOUNT_MEMBER_UPDATE_FAIL("9922","账户修改失败"), | |||||
UNKNOW("9999","未知异常"), | |||||
APP_ID_EMPTY("9923","appID数据为空"), | |||||
APP_TOKEN_EMPTY("9924","登录标识为空"), | |||||
ACCOUNT_ID_EMPTY("9925","UUID为空"), | |||||
ACCOUNT_SIGNATURE_EMPTY("9926","签名为空"), | |||||
ACCOUNT_SIGNATURE_ERROR("9927","签名错误"), | |||||
APP_EMPTY("6001","app不存在"), | |||||
APP_DELSTATUS("6002","app已经不存在"), | |||||
APP_STATUS("6003","app状态不正常"), | |||||
PARAMS_JSON_ERROR("2","参数不是json格式"), | PARAMS_JSON_ERROR("2","参数不是json格式"), | ||||
PARAMS_JSON_KEY_ERROR("3","参数关键key错误"), | PARAMS_JSON_KEY_ERROR("3","参数关键key错误"), | ||||
DEVICE_OTA_NO_NEW("6","没有更新版本的升级包"), | DEVICE_OTA_NO_NEW("6","没有更新版本的升级包"), | ||||
DEVICE_SERVER_URL_NO_SET("7","该类型的设备未设置服务器url"), | DEVICE_SERVER_URL_NO_SET("7","该类型的设备未设置服务器url"), | ||||
DEVICE_NOT_EXIT("8","设备不存在"), | DEVICE_NOT_EXIT("8","设备不存在"), | ||||
PARAMS_EMPTY_URL_CID_VID_PID("9","URL关键参数为空"), | |||||
DEVICE_UN_SUPPORT("10","不支持的设备"), | |||||
; | ; | ||||
private String code=""; | private String code=""; |
package com.inet.ailink.receiver.common.utils; | |||||
import java.io.UnsupportedEncodingException; | |||||
public class Base64TeaUitls { | |||||
//加密 | |||||
public static String encrypt(String dataValue){ | |||||
byte[] old = BytesUtils.getBytes(Integer.parseInt(dataValue)); | |||||
String result = ""; | |||||
try { | |||||
//tea加密 | |||||
byte[] teaDscrypt =TeaUtils.encrypt(old,null); | |||||
//base编码 | |||||
result= Base64Util.encoderString(teaDscrypt); | |||||
} catch (UnsupportedEncodingException e) { | |||||
return result; | |||||
} | |||||
return result; | |||||
} | |||||
//解密 | |||||
public static byte[] decrypt(String base64Text){ | |||||
//base解码 | |||||
String base64TextEn; | |||||
try { | |||||
base64TextEn = Base64Util.decoderString(base64Text); | |||||
//解码数据转换为byte数组 | |||||
byte[] base64TeaByteOld = Base64Util.hexStrToByteArray(base64TextEn); | |||||
//tea解密byte数组 | |||||
byte[] base64TeaByte =TeaUtils.decrypt(base64TeaByteOld,null); | |||||
//将负数转换为整数 | |||||
/*if(base64TeaByte != null){ | |||||
for(int i=0;i<base64TeaByte.length;i++){ | |||||
if(base64TeaByte[i]<0){ | |||||
base64TeaByte[i] = (byte) (256 + base64TeaByte[i]); | |||||
} | |||||
} | |||||
}*/ | |||||
return base64TeaByte; | |||||
} catch (UnsupportedEncodingException e) { | |||||
return null; | |||||
} | |||||
} | |||||
/** | |||||
* 十进制转16进制 | |||||
* @param n | |||||
* @return | |||||
*/ | |||||
private static String intToHex(int n) { | |||||
StringBuilder sb = new StringBuilder(8); | |||||
String a; | |||||
char []b = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; | |||||
if(n==0){ | |||||
return "00"; | |||||
} | |||||
while(n != 0){ | |||||
if(n<0){ | |||||
n= 256 +n; | |||||
} | |||||
sb = sb.append(b[n%16]); | |||||
n = n/16; | |||||
} | |||||
a = sb.reverse().toString(); | |||||
if(a.length() == 1){ | |||||
a= "0"+a; | |||||
} | |||||
return a; | |||||
} | |||||
/** | |||||
* 获取开始下标到结束下标的16进制的值 | |||||
* @param start | |||||
* @param end | |||||
* @param params | |||||
* @return | |||||
*/ | |||||
public static String getStartToEndForHex(int start,int end,byte[] params){ | |||||
String result = ""; | |||||
for(int i=start;i<=end;i++){ | |||||
if(i==end){ | |||||
result = result+intToHex(params[i]); | |||||
}else{ | |||||
result = result+intToHex(params[i])+":"; | |||||
} | |||||
// System.out.println(params[i]+":"+intToHex(params[i])); | |||||
} | |||||
return result; | |||||
} | |||||
/** | |||||
* 获取开始下标到结束下标的10进制的值 | |||||
* @param start | |||||
* @param end | |||||
* @param params | |||||
* @return | |||||
*/ | |||||
public static String getStartToEndForInt(int start,int end,byte[] params){ | |||||
String result = ""; | |||||
for(int i=start;i<=end;i++){ | |||||
int temp = params[i]; | |||||
if(temp < 0){ | |||||
temp = 256 + temp; | |||||
} | |||||
if(i==end){ | |||||
result = result+temp; | |||||
}else{ | |||||
result = result+temp+":"; | |||||
} | |||||
} | |||||
return result; | |||||
} | |||||
/*public static String getLowToHighEndForInt(int indexLow,int indexHigh,byte[] params){ | |||||
String result = ""; | |||||
int tempLow = params[indexLow]; | |||||
if(tempLow < 0){ | |||||
tempLow = 256 + tempLow; | |||||
} | |||||
int tempHigh = params[indexHigh]; | |||||
if(tempHigh < 0){ | |||||
tempHigh = 256 + tempHigh; | |||||
} | |||||
int tempResult = (tempLow << 8) | tempHigh; | |||||
return result+tempResult; | |||||
}*/ | |||||
/** | |||||
* 获取低位到高位的移位+或运算的值 | |||||
* @param indexLow | |||||
* @param indexHigh | |||||
* @param params | |||||
* @return | |||||
*/ | |||||
public static String getLowConactHighEndForInt(int indexLow,int indexHigh,byte[] params){ | |||||
String result = ""; | |||||
int tempResult = 0; | |||||
for(int i = indexLow;i<=indexHigh;i++){ | |||||
int temp = (params[i] < 0 ? 256 + params[i] : params[i]); | |||||
int tempmov = (indexHigh-i)+2; | |||||
tempmov = (i == indexHigh ? 0 : (int) Math.pow(2, tempmov)); | |||||
tempResult = (temp << tempmov) | tempResult; | |||||
} | |||||
return result+tempResult; | |||||
} | |||||
public static String byteToHex(byte b){ | |||||
String hex = Integer.toHexString(b & 0xFF); | |||||
if(hex.length() < 2){ | |||||
hex = "0" + hex; | |||||
} | |||||
return hex; | |||||
} | |||||
/** | |||||
* 获取开始下标到结束下标的ASIII对应的值 | |||||
* @param start | |||||
* @param end | |||||
* @param params | |||||
* @return | |||||
*/ | |||||
public static String getStartToEndForASIII(int start,int end,byte[] params){ | |||||
String result = ""; | |||||
for(int i=start;i<=end;i++){ | |||||
if(i==end){ | |||||
result = result+asciiToString(params[i]+""); | |||||
}else{ | |||||
result = result+asciiToString(params[i]+"")+":"; | |||||
} | |||||
} | |||||
return result; | |||||
} | |||||
/** | |||||
* ASCII转字符 | |||||
* @param value | |||||
* @return | |||||
*/ | |||||
public static String asciiToString(String value) | |||||
{ | |||||
value = value.replaceAll(":", ""); | |||||
char c= (char) Integer.parseInt(value); | |||||
//System.out.println(value+":"+c); | |||||
return c+""; | |||||
} | |||||
public static void main(String args[]) throws UnsupportedEncodingException | |||||
{ | |||||
// String str = "H4CpBA/xguYaSidJuBnCEydjVVdwDBKAfgvuaUpSEuU="; | |||||
// byte[] paramsByte = Base64TeaUitls.decrypt(str); | |||||
// | |||||
// for(byte i : paramsByte) | |||||
// System.out.print(byteToHex(i) + " "); | |||||
// System.out.println(); | |||||
//01 01 16 b7 28 32 18 4a 88 00 0e 00 00 00 00 42 4d 10 01 0a 00 13 05 07 e9 | |||||
//注册设备数据 | |||||
// byte[] old =new byte[]{ | |||||
// (byte)0x01,(byte)0x01,(byte)0x08 ,(byte)0x16 ,(byte)0xb7 ,(byte)0x28 //mac地址 | |||||
// ,(byte)0x00 ,(byte)0x00 //cid | |||||
// ,(byte)0x00 ,(byte)0x01 //pid | |||||
// ,(byte)0x00 ,(byte)0x02 //vid | |||||
// ,(byte)0x42 ,(byte)0x4d //产品名称 | |||||
// ,(byte)0x01 //产品型号 | |||||
// ,(byte)0x01 //硬件版本 | |||||
// ,(byte)0x42 //软件版本 | |||||
// ,(byte)0x4d //用户版本 | |||||
// ,(byte)0x13 //年 | |||||
// ,(byte)0x0c //月 | |||||
// ,(byte)0x12 //日 | |||||
// ,(byte)0x06,(byte)0x06,(byte)0x06 ,(byte)0x06 ,(byte)0x06 ,(byte)0x06 //芯片id | |||||
// ,(byte)0x09 //设备单位 | |||||
// ,(byte)0x03 //设备IMEI长度 | |||||
// ,(byte)0x01,(byte)0x02,(byte)0x20 }; //设备IMEI | |||||
//上报称重数据(原始数据) | |||||
//5781 | |||||
// byte[] old =new byte[]{ | |||||
// (byte)0x00,(byte)0x00 ,(byte)0x0f,(byte)0x71 //设备SN号 | |||||
// ,(byte)0x15 //年 | |||||
// ,(byte)0x03 //月 | |||||
// ,(byte)0x04 //日 | |||||
// ,(byte)0x0c //时 | |||||
// ,(byte)0x00 //分 | |||||
// ,(byte)0x00 //秒 | |||||
// ,(byte)0x00 //是否离线记录 | |||||
// ,(byte)0x00 //体重数据高 | |||||
// ,(byte)0x00 //体重数据中 | |||||
// ,(byte)0x3C //体重数据低 | |||||
// ,(byte)0x00 //体重单位 | |||||
// ,(byte)0x00 //体重精度 | |||||
// ,(byte)0x01 //阻抗高 | |||||
// ,(byte)0xff //阻抗低 | |||||
// ,(byte)0x5f //心率 | |||||
// ,(byte)0x01 //算法 | |||||
// ,(byte)0x00,(byte)0x00,(byte)0x03,(byte)0x97}; //用户id | |||||
//最新OTA升级地址 | |||||
// byte[] old =new byte[]{ | |||||
// (byte)0x01,(byte)0x01,(byte)0x08 ,(byte)0x16 ,(byte)0xb7 ,(byte)0x28 //mac地址 | |||||
// ,(byte)0x00 ,(byte)0x00 //cid | |||||
// ,(byte)0x00 ,(byte)0x01 //pid | |||||
// ,(byte)0x00 ,(byte)0x02 //vid | |||||
// ,(byte)0x42 ,(byte)0x4d //产品名称 | |||||
// ,(byte)0x01 //产品型号 | |||||
// ,(byte)0x01 //硬件版本 | |||||
// ,(byte)0x09 //软件版本 | |||||
// ,(byte)0x06,(byte)0x06,(byte)0x06 ,(byte)0x06 ,(byte)0x06 ,(byte)0x06 //芯片id | |||||
// ,(byte)0x00,(byte)0x00 ,(byte)0x07,(byte)0xBF //设备id | |||||
// ,(byte)0x00 //客户定制版本 | |||||
// ,(byte)0x00 ,(byte)0x00,(byte)0x00,(byte)0x00 //保留位 | |||||
// }; | |||||
// //通过cid获取设备访问地址 | |||||
byte[] old =new byte[]{ | |||||
(byte)0x00 ,(byte)0x00,(byte)0x00, //模块名称 | |||||
(byte)0x01,(byte)0x01,(byte)0x08 ,(byte)0x16 ,(byte)0xb7 ,(byte)0x28, //mac地址 | |||||
(byte)0x01,(byte)0x01,(byte)0x08 ,(byte)0x16 ,(byte)0xb7 ,(byte)0x28, //芯片id | |||||
(byte)0x00 ,(byte)0x11, //cid | |||||
(byte)0x00 ,(byte)0x01, //pid | |||||
(byte)0x00 ,(byte)0x1B, //vid | |||||
(byte)0x00,(byte)0x00 ,(byte)0x00 //保留位 | |||||
}; | |||||
//通过cid,pid,vid获取设备访问地址 | |||||
//57 4d 05 88 4a 18 32 28 a4 02 38 6f a5 dc 12 00 11 00 00 00 00 00 00 00 | |||||
// byte[] old =new byte[]{ | |||||
// (byte)0x57 ,(byte)0x4d,(byte)0x05, //模块名称 | |||||
// (byte)0x88,(byte)0x4a,(byte)0x08 ,(byte)0x32 ,(byte)0x28 ,(byte)0xa4, //mac地址 | |||||
// (byte)0x02,(byte)0x38,(byte)0x6f ,(byte)0xa5 ,(byte)0xdc ,(byte)0x12, //芯片id | |||||
// (byte)0x00 ,(byte)0x0e, //cid | |||||
// (byte)0x00 ,(byte)0x01, //pid | |||||
// (byte)0x00 ,(byte)0x32, //vid | |||||
// (byte)0x00,(byte)0x00 ,(byte)0x00 //保留位 | |||||
// }; | |||||
//刷牙数据上传 | |||||
// byte[] old =new byte[]{ | |||||
// (byte)0x00,(byte)0x00 ,(byte)0x07,(byte)0xBF, //设备SN号 | |||||
// (byte)0x01, //牙刷工作模式 | |||||
// (byte)0x00, // 刷牙时长高字节 | |||||
// (byte)0x03, // 刷牙时长低字节 | |||||
// (byte)0x00, //左边刷牙时长高字节 | |||||
// (byte)0x02, //左边刷牙时长低字节 | |||||
// (byte)0x00, //右边刷牙时长低字节 | |||||
// (byte)0x01, //右边刷牙时长低字节 | |||||
// (byte)0x09, //剩余电量 | |||||
// (byte)0x00,(byte)0x04, //默认刷牙时长 | |||||
// (byte)0x00,(byte)0x00,//保留位 | |||||
// (byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,//保留位 | |||||
// (byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00 //保留位 | |||||
// }; | |||||
//血糖数据 | |||||
// byte[] old =new byte[]{ | |||||
// (byte)0x00,(byte)0x00 ,(byte)0x07,(byte)0xBF, //设备SN号 | |||||
// (byte)0x00, //血糖数据高字节 | |||||
// (byte)0x00, //血糖数据中字节 | |||||
// (byte)0x12, //血糖数据低字节 | |||||
// (byte)0x01, //血糖单位 | |||||
// (byte)0x01, //小数点 | |||||
// (byte)0x00, //状态码 | |||||
// (byte)0x10, //剩余电量 | |||||
// (byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,//保留位 | |||||
// (byte)0x00 //保留位 | |||||
// }; | |||||
/*int a= 1846; | |||||
System.out.println("原始数字:"+a); | |||||
byte[] old = BytesUtils.getBytes(a);*/ | |||||
//通过设备id,获取设备单位 | |||||
// byte[] old =new byte[]{ | |||||
// (byte)0x00,(byte)0x00 ,(byte)0x0F,(byte)0x72, //设备id | |||||
// (byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,//保留位 | |||||
// }; | |||||
//通过设备id,修改设备单位 | |||||
// byte[] old =new byte[]{ | |||||
// (byte)0x00,(byte)0x00 ,(byte)0x0B,(byte)0x31, //设备id | |||||
// (byte)0x01 //设备单位 | |||||
// }; | |||||
//注册设备数据 | |||||
// byte[] old =new byte[]{ | |||||
// (byte)0x00, (byte)0x00 , (byte)0x00 , (byte)0x00 , (byte)0x00, (byte)0x00 //设备MAC | |||||
// , (byte)0x00, (byte)0x2c//CID | |||||
// , (byte)0x00 , (byte)0x00//PID | |||||
// , (byte)0x00 , (byte)0x00//VID | |||||
// , (byte)0x4c//模块名称 | |||||
// , (byte)0x4d | |||||
// , (byte)0x09 | |||||
// , (byte)0x00 | |||||
// , (byte)0x0a | |||||
// , (byte)0x00 , (byte)0x15 , (byte)0x04 , (byte)0x07 , (byte)0x00 , (byte)0x00 | |||||
// , (byte)0x00 , (byte)0x00 , (byte)0x00 , (byte)0x00 , (byte)0x00 | |||||
// , (byte)0x0f //设备IMEI长度 | |||||
// , (byte)0x08 , (byte)0x06 , (byte)0x09 , (byte)0x03 , (byte)0x02 , (byte)0x04 , (byte)0x00 , (byte)0x05 //设备IMEI 86932405 | |||||
// , (byte)0x00 , (byte)0x00 , (byte)0x00 , (byte)0x02 , (byte)0x07 , (byte)0x06 , (byte)0x09 //设备IMEI 0002769 | |||||
// , (byte)0x00, (byte)0x00 , (byte)0x00 , (byte)0x00 }; //备用字段 | |||||
//原始数据 | |||||
System.out.println( "原始数据:"); | |||||
for(byte i : old) | |||||
System.out.print(byteToHex(i) + " "); | |||||
System.out.println(); | |||||
//tea加密 | |||||
byte[] teaDscrypt =TeaUtils.encrypt(old,null); | |||||
System.out.println( "tea加密:"); | |||||
for(byte i : teaDscrypt) | |||||
System.out.print(byteToHex(i) + " "); | |||||
System.out.println(); | |||||
//base编码 | |||||
String base64Text = Base64Util.encoderString(teaDscrypt); | |||||
base64Text = "rvXiSvrWrR+/p1ZhAPlXr7x1gxbvrurA"; | |||||
System.out.println("base64编码后的数据长度:"+Base64Util.getLength(base64Text)+":"+base64Text); | |||||
//base解码 | |||||
String base64TextEn= Base64Util.decoderString(base64Text); | |||||
//String base64TextEn= Base64Util.decoderString("BCc0vnfr9kd5"); | |||||
// System.out.println("base64解码后的数据长度:"+Base64Util.getLength(base64TextEn)+":"+base64TextEn); | |||||
/*//转称byte数组(每两位一个16进制byte) | |||||
List<String> base64TeaByteOldList = new ArrayList<String>(); | |||||
for(int i=0;i<base64TextEn.length();i++){ | |||||
if(i%2 == 0){ | |||||
base64TeaByteOldList.add("0x"+base64TextEn.charAt(i)+base64TextEn.charAt(i+1)); | |||||
} | |||||
} | |||||
//解码后转换得到的list | |||||
System.out.println("解码后转换得到的list:"); | |||||
for(String byte16:base64TeaByteOldList){ | |||||
System.out.print(byte16+" "); | |||||
} | |||||
System.out.println();*/ | |||||
//解码数据转换为byte数组 | |||||
byte[] base64TeaByteOld = Base64Util.hexStrToByteArray(base64TextEn); | |||||
System.out.println( "解码后得到的tea加密的byte数组:"); | |||||
for(byte i : base64TeaByteOld) | |||||
System.out.print(byteToHex(i) + " "); | |||||
System.out.println(); | |||||
byte[] base64TeaByte =TeaUtils.decrypt(base64TeaByteOld,null); | |||||
System.out.println( "tea解密:"); | |||||
for(byte i : base64TeaByte) | |||||
System.out.print(byteToHex(i) + " "); | |||||
System.out.println(); | |||||
//int imeiLength = base64TeaByte[28] & 0xFF; | |||||
int imeiLength = Integer.parseInt(Base64TeaUitls.getStartToEndForInt(28, 28, base64TeaByte)); | |||||
System.out.println("imeiLength:"+imeiLength); | |||||
System.out.println("IMEI:"+ Base64TeaUitls.getStartToEndForHex(28+1, 28+imeiLength, base64TeaByte)); | |||||
System.out.println("IMEI:"+ Base64TeaUitls.getStartToEndForInt(28+1, 28+imeiLength, base64TeaByte)); | |||||
System.out.println("IMEI:"+ Base64TeaUitls.getStartToEndForASIII(28+1, 28+imeiLength, base64TeaByte)); | |||||
} | |||||
} | |||||
package com.inet.ailink.receiver.common.utils; | |||||
import java.io.UnsupportedEncodingException; | |||||
public class Base64TeaUitls { | |||||
//加密 | |||||
public static String encrypt(String dataValue){ | |||||
byte[] old = BytesUtils.getBytes(Integer.parseInt(dataValue)); | |||||
String result = ""; | |||||
try { | |||||
//tea加密 | |||||
byte[] teaDscrypt =TeaUtils.encrypt(old,null); | |||||
//base编码 | |||||
result= Base64Util.encoderString(teaDscrypt); | |||||
} catch (UnsupportedEncodingException e) { | |||||
return result; | |||||
} | |||||
return result; | |||||
} | |||||
//解密 | |||||
public static byte[] decrypt(String base64Text){ | |||||
//base解码 | |||||
String base64TextEn; | |||||
try { | |||||
base64TextEn = Base64Util.decoderString(base64Text); | |||||
//解码数据转换为byte数组 | |||||
byte[] base64TeaByteOld = Base64Util.hexStrToByteArray(base64TextEn); | |||||
//tea解密byte数组 | |||||
byte[] base64TeaByte =TeaUtils.decrypt(base64TeaByteOld,null); | |||||
//将负数转换为整数 | |||||
/*if(base64TeaByte != null){ | |||||
for(int i=0;i<base64TeaByte.length;i++){ | |||||
if(base64TeaByte[i]<0){ | |||||
base64TeaByte[i] = (byte) (256 + base64TeaByte[i]); | |||||
} | |||||
} | |||||
}*/ | |||||
return base64TeaByte; | |||||
} catch (UnsupportedEncodingException e) { | |||||
return null; | |||||
} | |||||
} | |||||
/** | |||||
* 十进制转16进制 | |||||
* @param n | |||||
* @return | |||||
*/ | |||||
private static String intToHex(int n) { | |||||
StringBuilder sb = new StringBuilder(8); | |||||
String a; | |||||
char []b = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; | |||||
if(n==0){ | |||||
return "00"; | |||||
} | |||||
while(n != 0){ | |||||
if(n<0){ | |||||
n= 256 +n; | |||||
} | |||||
sb = sb.append(b[n%16]); | |||||
n = n/16; | |||||
} | |||||
a = sb.reverse().toString(); | |||||
if(a.length() == 1){ | |||||
a= "0"+a; | |||||
} | |||||
return a; | |||||
} | |||||
/** | |||||
* 获取开始下标到结束下标的16进制的值 | |||||
* @param start | |||||
* @param end | |||||
* @param params | |||||
* @return | |||||
*/ | |||||
public static String getStartToEndForHex(int start,int end,byte[] params){ | |||||
String result = ""; | |||||
for(int i=start;i<=end;i++){ | |||||
if(i==end){ | |||||
result = result+intToHex(params[i]); | |||||
}else{ | |||||
result = result+intToHex(params[i])+":"; | |||||
} | |||||
// System.out.println(params[i]+":"+intToHex(params[i])); | |||||
} | |||||
return result; | |||||
} | |||||
/** | |||||
* 获取开始下标到结束下标的10进制的值 | |||||
* @param start | |||||
* @param end | |||||
* @param params | |||||
* @return | |||||
*/ | |||||
public static String getStartToEndForInt(int start,int end,byte[] params){ | |||||
String result = ""; | |||||
for(int i=start;i<=end;i++){ | |||||
int temp = params[i]; | |||||
if(temp < 0){ | |||||
temp = 256 + temp; | |||||
} | |||||
if(i==end){ | |||||
result = result+temp; | |||||
}else{ | |||||
result = result+temp+":"; | |||||
} | |||||
} | |||||
return result; | |||||
} | |||||
/*public static String getLowToHighEndForInt(int indexLow,int indexHigh,byte[] params){ | |||||
String result = ""; | |||||
int tempLow = params[indexLow]; | |||||
if(tempLow < 0){ | |||||
tempLow = 256 + tempLow; | |||||
} | |||||
int tempHigh = params[indexHigh]; | |||||
if(tempHigh < 0){ | |||||
tempHigh = 256 + tempHigh; | |||||
} | |||||
int tempResult = (tempLow << 8) | tempHigh; | |||||
return result+tempResult; | |||||
}*/ | |||||
/** | |||||
* 获取低位到高位的移位+或运算的值 | |||||
* @param indexLow | |||||
* @param indexHigh | |||||
* @param params | |||||
* @return | |||||
*/ | |||||
public static String getLowConactHighEndForInt(int indexLow,int indexHigh,byte[] params){ | |||||
String result = ""; | |||||
int tempResult = 0; | |||||
for(int i = indexLow;i<=indexHigh;i++){ | |||||
int temp = (params[i] < 0 ? 256 + params[i] : params[i]); | |||||
int tempmov = (indexHigh-i)+2; | |||||
tempmov = (i == indexHigh ? 0 : (int) Math.pow(2, tempmov)); | |||||
tempResult = (temp << tempmov) | tempResult; | |||||
} | |||||
return result+tempResult; | |||||
} | |||||
public static String byteToHex(byte b){ | |||||
String hex = Integer.toHexString(b & 0xFF); | |||||
if(hex.length() < 2){ | |||||
hex = "0" + hex; | |||||
} | |||||
return hex; | |||||
} | |||||
/** | |||||
* 获取开始下标到结束下标的ASIII对应的值 | |||||
* @param start | |||||
* @param end | |||||
* @param params | |||||
* @return | |||||
*/ | |||||
public static String getStartToEndForASIII(int start,int end,byte[] params){ | |||||
String result = ""; | |||||
for(int i=start;i<=end;i++){ | |||||
if(i==end){ | |||||
result = result+asciiToString(params[i]+""); | |||||
}else{ | |||||
result = result+asciiToString(params[i]+"")+":"; | |||||
} | |||||
} | |||||
return result; | |||||
} | |||||
/** | |||||
* ASCII转字符 | |||||
* @param value | |||||
* @return | |||||
*/ | |||||
public static String asciiToString(String value) | |||||
{ | |||||
value = value.replaceAll(":", ""); | |||||
char c= (char) Integer.parseInt(value); | |||||
//System.out.println(value+":"+c); | |||||
return c+""; | |||||
} | |||||
/** | |||||
* 将byte数组转为long类型,数组下标起止的差最大为8 | |||||
* @param start | |||||
* @param end | |||||
* @param byteParams | |||||
* @return | |||||
*/ | |||||
public static long getLowConactHighEndForLong(int start,int end,byte[] byteParams) { | |||||
byte[] bs = new byte[end-start+1]; | |||||
System.arraycopy(byteParams,start,bs,0,bs.length); | |||||
int bytes = bs.length; | |||||
switch(bytes) { | |||||
case 0: | |||||
return 0; | |||||
case 1: | |||||
return (long)((bs[0] & 0xff)); | |||||
case 2: | |||||
return (long)((bs[0] & 0xff) <<8 | (bs[1] & 0xff)); | |||||
case 3: | |||||
return (long)((bs[0] & 0xff) <<16 | (bs[1] & 0xff) <<8 | (bs[2] & 0xff)); | |||||
case 4: | |||||
return (long)((bs[0] & 0xffL) <<24 | (bs[1] & 0xffL) << 16 | (bs[2] & 0xffL) <<8 | (bs[3] & 0xffL)); | |||||
case 5: | |||||
return (long)((bs[0] & 0xffL) <<32 | (bs[1] & 0xffL) <<24 | (bs[2] & 0xffL) << 16 | (bs[3] & 0xffL) <<8 | (bs[4] & 0xffL)); | |||||
case 6: | |||||
return (long)((bs[0] & 0xffL) <<40 | (bs[1] & 0xffL) <<32 | (bs[2] & 0xffL) <<24 | (bs[3] & 0xffL) << 16 | (bs[4] & 0xffL) <<8 | (bs[5] & 0xffL)); | |||||
case 7: | |||||
return (long)((bs[0] & 0xffL) <<48 | (bs[1] & 0xffL) <<40 | (bs[2] & 0xffL) <<32 | (bs[3] & 0xffL) <<24 | (bs[4] & 0xffL) << 16 | (bs[5] & 0xffL) <<8 | (bs[6] & 0xffL)); | |||||
case 8: | |||||
return (long)((bs[0] & 0xffL) <<56 | (bs[1] & 0xffL) << 48 | (bs[2] & 0xffL) <<40 | (bs[3] & 0xffL)<<32 | | |||||
(bs[4] & 0xffL) <<24 | (bs[5] & 0xffL) << 16 | (bs[6] & 0xffL) <<8 | (bs[7] & 0xffL)); | |||||
default: | |||||
return 0; | |||||
} | |||||
} | |||||
public static void main(String args[]) throws UnsupportedEncodingException | |||||
{ | |||||
// | |||||
} | |||||
} |
package com.inet.ailink.receiver.controller; | |||||
import com.inet.ailink.receiver.common.enums.StatusCode; | |||||
import com.inet.ailink.receiver.common.exception.BizException; | |||||
import com.inet.ailink.receiver.common.utils.Base64TeaUitls; | |||||
import com.inet.ailink.receiver.common.utils.JsonUtils; | |||||
import com.inet.ailink.receiver.common.vo.Response; | |||||
import com.inet.ailink.receiver.service.impl.SysAccoountServiceImpl; | |||||
import org.slf4j.Logger; | |||||
import org.slf4j.LoggerFactory; | |||||
import org.springframework.beans.factory.annotation.Autowired; | |||||
import org.springframework.validation.BindingResult; | |||||
import org.springframework.validation.FieldError; | |||||
import javax.servlet.http.HttpServletRequest; | |||||
import javax.servlet.http.HttpServletResponse; | |||||
import java.io.IOException; | |||||
import java.io.PrintWriter; | |||||
import java.util.HashMap; | |||||
import java.util.List; | |||||
import java.util.Map; | |||||
//import com.google.common.primitives.Bytes; | |||||
public abstract class BaseController extends java.beans.PropertyEditorSupport | |||||
{ | |||||
/** | |||||
* 子类可以用这个Logger Log4J Logger for this class | |||||
*/ | |||||
protected final Logger log = LoggerFactory.getLogger(getClass()); | |||||
//系统统一的提示没有权限访问的界面 | |||||
protected static final String noRight_page="noRight"; | |||||
public final static String KEY_USER = "user"; | |||||
@Autowired | |||||
private SysAccoountServiceImpl sysAccoountServiceImpl; | |||||
// /*@Autowired | |||||
// public RedisClient redisClient; | |||||
// | |||||
// @Autowired | |||||
// public RedisCacheBean redis; | |||||
// | |||||
// @Value("${redis.cacheEnable}") | |||||
// private Boolean cacheEnable;*/ | |||||
// | |||||
// /** | |||||
// * 功能:检查参数的结果处理 | |||||
// * @param result | |||||
// * @throws BizException | |||||
// */ | |||||
// protected void checkParmResultHandler(BindingResult result) throws BizException { | |||||
// if(result.hasErrors()){ | |||||
// StringBuffer sb = new StringBuffer(); | |||||
// List<FieldError> fieldErrorList = result.getFieldErrors(); | |||||
// for(FieldError fieldError : fieldErrorList){ | |||||
// sb.append(fieldError.getField()).append(":").append(fieldError.getDefaultMessage()).append("; "); | |||||
// } | |||||
// throw new BizException(StatusCode.PARAM_CHECK_ERROR.getCode(),sb.toString()); | |||||
// } | |||||
// | |||||
// } | |||||
// | |||||
// /** | |||||
// * 校验消息体签名 | |||||
// * @param request | |||||
// * @param obj | |||||
// * @throws Exception | |||||
// */ | |||||
// protected void checkMsgSignature(HttpServletRequest request, Object obj) throws BizException { | |||||
// WebContext user = WebContext.getWebContext(); | |||||
// String serverMsgSignature = MD5Util.desEncrypt(JsonUtils.toJson(obj), user.getAccountSecret()); | |||||
// if(!serverMsgSignature.substring(0,32).equals(user.getMsgSignature())){ | |||||
// throw new BizException(StatusCode.ACCOUNT_SIGNATURE_ERROR.getCode()); | |||||
// } | |||||
// } | |||||
// | |||||
// /** | |||||
// * @Description 通过request请求中获取用户信息 | |||||
// * @param request | |||||
// * @return | |||||
// * @throws BizException | |||||
// * @throws NumberFormatException | |||||
// */ | |||||
// protected SysAccount getUser(HttpServletRequest request) throws NumberFormatException, BizException { | |||||
// | |||||
// SysAccount account = new SysAccount(); | |||||
// if (StringUtils.isNotBlank(request.getHeader("token"))) { | |||||
// account = sysAccoountServiceImpl.get(Long.parseLong(request.getHeader("token"))); | |||||
// } | |||||
// return account; | |||||
// } | |||||
// | |||||
// protected Long getAppId(HttpServletRequest request) throws BizException{ | |||||
// if (StringUtils.isNotBlank(request.getHeader("appId"))) { | |||||
// return Long.parseLong(request.getHeader("appId")); | |||||
// }else{ | |||||
// throw new BizException(StatusCode.COMPANY_NOT_EXIST); | |||||
// } | |||||
// } | |||||
/** | |||||
* 写回加密数据 | |||||
* @param response | |||||
* @param result | |||||
* @throws IOException | |||||
*/ | |||||
public void writeOutResponse(HttpServletResponse response, Response<Object> result) throws IOException{ | |||||
response.setHeader("Access-Control-Allow-Origin",""); | |||||
response.setHeader("Access-Control-Allow-Credentials",""); | |||||
response.setHeader("Access-Control-Allow-Methods",""); | |||||
response.setHeader("Access-Control-Max-Age",""); | |||||
response.setHeader("Access-Control-Allow-Headers",""); | |||||
response.setHeader("X-Application-Context",""); | |||||
response.setHeader("Transfer-Encoding", ""); | |||||
PrintWriter out = response.getWriter(); | |||||
if(result.getData() != null){ | |||||
String dataValue = result.getData().toString(); | |||||
result.setData(Base64TeaUitls.encrypt(dataValue)); | |||||
} | |||||
String resultJson = JsonUtils.toJson(result); | |||||
response.setHeader("ResponseBodySize", resultJson.length()+""); | |||||
out.write(resultJson); | |||||
out.flush(); | |||||
out.close(); | |||||
} | |||||
/** | |||||
* 写回正常数据 | |||||
* @param response | |||||
* @param result | |||||
* @throws IOException | |||||
*/ | |||||
public void writeOutResponseNoEncrypt(HttpServletResponse response, Response<Object> result) throws IOException{ | |||||
response.setHeader("Access-Control-Allow-Origin",""); | |||||
response.setHeader("Access-Control-Allow-Credentials",""); | |||||
response.setHeader("Access-Control-Allow-Methods",""); | |||||
response.setHeader("Access-Control-Max-Age",""); | |||||
response.setHeader("Access-Control-Allow-Headers",""); | |||||
response.setHeader("X-Application-Context",""); | |||||
// response.setHeader("Transfer-Encoding", "chunked"); | |||||
//response.setBufferSize(result.toString().length()); | |||||
String resultJson = JsonUtils.toJson(result); | |||||
response.setHeader("ResponseBodySize", resultJson.length()+""); | |||||
PrintWriter out = response.getWriter(); | |||||
out.write(resultJson); | |||||
out.flush(); | |||||
out.close(); | |||||
} | |||||
/** | |||||
* 写回任意原始数据 | |||||
* @param response | |||||
* @param result | |||||
* @throws IOException | |||||
*/ | |||||
public void writeOutResponseOld(HttpServletResponse response, String result) throws IOException{ | |||||
response.setHeader("Access-Control-Allow-Origin",""); | |||||
response.setHeader("Access-Control-Allow-Credentials",""); | |||||
response.setHeader("Access-Control-Allow-Methods",""); | |||||
response.setHeader("Access-Control-Max-Age",""); | |||||
response.setHeader("Access-Control-Allow-Headers",""); | |||||
response.setHeader("X-Application-Context",""); | |||||
response.setHeader("Transfer-Encoding", ""); | |||||
response.setHeader("ResponseBodySize", result.length()+""); | |||||
PrintWriter out = response.getWriter(); | |||||
out.write(result); | |||||
out.flush(); | |||||
out.close(); | |||||
} | |||||
/** | |||||
* 写回数据解析失败数据 | |||||
* @param response | |||||
* @param result | |||||
* @throws IOException | |||||
*/ | |||||
public void writeOutResponse(HttpServletResponse response, byte[] paramsByte) throws IOException { | |||||
Response<Object> result = new Response<Object>(); | |||||
result.setStatus(paramsByte[0]+""); | |||||
response.setHeader("Access-Control-Allow-Origin",""); | |||||
response.setHeader("Access-Control-Allow-Credentials",""); | |||||
response.setHeader("Access-Control-Allow-Methods",""); | |||||
response.setHeader("Access-Control-Max-Age",""); | |||||
response.setHeader("Access-Control-Allow-Headers",""); | |||||
response.setHeader("X-Application-Context",""); | |||||
response.setHeader("Transfer-Encoding", ""); | |||||
String resultJson = JsonUtils.toJson(result); | |||||
response.setHeader("ResponseBodySize", resultJson.length()+""); | |||||
PrintWriter out = response.getWriter(); | |||||
out.write(resultJson); | |||||
out.flush(); | |||||
out.close(); | |||||
} | |||||
/** | |||||
* 校验参数是否为空 | |||||
* @param response | |||||
* @param params | |||||
* @throws IOException | |||||
*/ | |||||
public void checkParams(HttpServletResponse response, String params) throws IOException{ | |||||
Response<Object> result = new Response<Object>(); | |||||
if(params == null || params.isEmpty() || params.equals("")){ | |||||
result.setStatus(StatusCode.PARAM_EMPTY.getCode()); | |||||
writeOutResponse(response, result); | |||||
} | |||||
} | |||||
/** | |||||
* 解密数据 | |||||
* @param params | |||||
* @return | |||||
* @throws IOException | |||||
*/ | |||||
public byte[] decryptParams(HttpServletResponse response, String params) throws IOException{ | |||||
//解密数据失败 | |||||
byte[] paramsByte = new byte[]{}; | |||||
//数据格式不正确 | |||||
Map<String,String> paramsMap = new HashMap<String, String>(); | |||||
try { | |||||
paramsMap = JsonUtils.fromJson(params, Map.class); | |||||
// System.out.println("decryptParams() paramsMap: " + paramsMap); | |||||
} catch (Exception e) { | |||||
paramsByte = new byte[]{(byte) Integer.parseInt(StatusCode.PARAMS_JSON_ERROR.getCode())}; | |||||
// System.out.println("decryptParams() Exception: " + e); | |||||
return paramsByte; | |||||
} | |||||
//关键参数字段获取失败 | |||||
String paramsValue = paramsMap.get("params"); | |||||
// System.out.println("decryptParams() paramsValue: " + paramsValue); | |||||
if(paramsValue == null || paramsValue.equals("")){ | |||||
paramsByte = new byte[]{(byte) Integer.parseInt(StatusCode.PARAMS_JSON_KEY_ERROR.getCode())}; | |||||
return paramsByte; | |||||
} | |||||
try { | |||||
paramsByte = Base64TeaUitls.decrypt(paramsValue); | |||||
// System.out.println("decryptParams() paramsByte: " + paramsByte); | |||||
} catch (Exception e) { | |||||
paramsByte = new byte[]{(byte) Integer.parseInt(StatusCode.PARAMS_dECRYPT_ERROR.getCode())}; | |||||
// System.out.println("decryptParams() paramsByte2: " + paramsByte); | |||||
return paramsByte; | |||||
} | |||||
return paramsByte; | |||||
} | |||||
} | |||||
package com.inet.ailink.receiver.controller; | |||||
import com.inet.ailink.receiver.common.enums.StatusCode; | |||||
import com.inet.ailink.receiver.common.exception.BizException; | |||||
import com.inet.ailink.receiver.common.utils.Base64TeaUitls; | |||||
import com.inet.ailink.receiver.common.utils.JsonUtils; | |||||
import com.inet.ailink.receiver.common.vo.Response; | |||||
import com.inet.ailink.receiver.service.impl.SysAccoountServiceImpl; | |||||
import org.slf4j.Logger; | |||||
import org.slf4j.LoggerFactory; | |||||
import org.springframework.beans.factory.annotation.Autowired; | |||||
import org.springframework.validation.BindingResult; | |||||
import org.springframework.validation.FieldError; | |||||
import javax.servlet.http.HttpServletRequest; | |||||
import javax.servlet.http.HttpServletResponse; | |||||
import java.io.IOException; | |||||
import java.io.PrintWriter; | |||||
import java.util.HashMap; | |||||
import java.util.List; | |||||
import java.util.Map; | |||||
//import com.google.common.primitives.Bytes; | |||||
public abstract class BaseController extends java.beans.PropertyEditorSupport | |||||
{ | |||||
/** | |||||
* 子类可以用这个Logger Log4J Logger for this class | |||||
*/ | |||||
protected final Logger log = LoggerFactory.getLogger(getClass()); | |||||
//系统统一的提示没有权限访问的界面 | |||||
protected static final String noRight_page="noRight"; | |||||
public final static String KEY_USER = "user"; | |||||
/** | |||||
* 写回加密数据 | |||||
* @param response | |||||
* @param result | |||||
* @throws IOException | |||||
*/ | |||||
public void writeOutResponse(HttpServletResponse response, Response<Object> result) throws IOException{ | |||||
response.setHeader("Access-Control-Allow-Origin",""); | |||||
response.setHeader("Access-Control-Allow-Credentials",""); | |||||
response.setHeader("Access-Control-Allow-Methods",""); | |||||
response.setHeader("Access-Control-Max-Age",""); | |||||
response.setHeader("Access-Control-Allow-Headers",""); | |||||
response.setHeader("X-Application-Context",""); | |||||
response.setHeader("Transfer-Encoding", ""); | |||||
PrintWriter out = response.getWriter(); | |||||
if(result.getData() != null){ | |||||
String dataValue = result.getData().toString(); | |||||
result.setData(Base64TeaUitls.encrypt(dataValue)); | |||||
} | |||||
String resultJson = JsonUtils.toJson(result); | |||||
response.setHeader("ResponseBodySize", resultJson.length()+""); | |||||
out.write(resultJson); | |||||
out.flush(); | |||||
out.close(); | |||||
} | |||||
/** | |||||
* 写回正常数据 | |||||
* @param response | |||||
* @param result | |||||
* @throws IOException | |||||
*/ | |||||
public void writeOutResponseNoEncrypt(HttpServletResponse response, Response<Object> result) throws IOException{ | |||||
response.setHeader("Access-Control-Allow-Origin",""); | |||||
response.setHeader("Access-Control-Allow-Credentials",""); | |||||
response.setHeader("Access-Control-Allow-Methods",""); | |||||
response.setHeader("Access-Control-Max-Age",""); | |||||
response.setHeader("Access-Control-Allow-Headers",""); | |||||
response.setHeader("X-Application-Context",""); | |||||
// response.setHeader("Transfer-Encoding", "chunked"); | |||||
//response.setBufferSize(result.toString().length()); | |||||
String resultJson = JsonUtils.toJson(result); | |||||
response.setHeader("ResponseBodySize", resultJson.length()+""); | |||||
PrintWriter out = response.getWriter(); | |||||
out.write(resultJson); | |||||
out.flush(); | |||||
out.close(); | |||||
} | |||||
/** | |||||
* 写回任意原始数据 | |||||
* @param response | |||||
* @param result | |||||
* @throws IOException | |||||
*/ | |||||
public void writeOutResponseOld(HttpServletResponse response, String result) throws IOException{ | |||||
response.setHeader("Access-Control-Allow-Origin",""); | |||||
response.setHeader("Access-Control-Allow-Credentials",""); | |||||
response.setHeader("Access-Control-Allow-Methods",""); | |||||
response.setHeader("Access-Control-Max-Age",""); | |||||
response.setHeader("Access-Control-Allow-Headers",""); | |||||
response.setHeader("X-Application-Context",""); | |||||
response.setHeader("Transfer-Encoding", ""); | |||||
response.setHeader("ResponseBodySize", result.length()+""); | |||||
PrintWriter out = response.getWriter(); | |||||
out.write(result); | |||||
out.flush(); | |||||
out.close(); | |||||
} | |||||
/** | |||||
* 写回数据解析失败数据 | |||||
* @param response | |||||
* @param result | |||||
* @throws IOException | |||||
*/ | |||||
public void writeOutResponse(HttpServletResponse response, byte[] paramsByte) throws IOException { | |||||
Response<Object> result = new Response<Object>(); | |||||
result.setStatus(paramsByte[0]+""); | |||||
response.setHeader("Access-Control-Allow-Origin",""); | |||||
response.setHeader("Access-Control-Allow-Credentials",""); | |||||
response.setHeader("Access-Control-Allow-Methods",""); | |||||
response.setHeader("Access-Control-Max-Age",""); | |||||
response.setHeader("Access-Control-Allow-Headers",""); | |||||
response.setHeader("X-Application-Context",""); | |||||
response.setHeader("Transfer-Encoding", ""); | |||||
String resultJson = JsonUtils.toJson(result); | |||||
response.setHeader("ResponseBodySize", resultJson.length()+""); | |||||
PrintWriter out = response.getWriter(); | |||||
out.write(resultJson); | |||||
out.flush(); | |||||
out.close(); | |||||
} | |||||
/** | |||||
* 校验参数是否为空 | |||||
* @param response | |||||
* @param params | |||||
* @throws IOException | |||||
*/ | |||||
public void checkParams(HttpServletResponse response, String params) throws IOException{ | |||||
Response<Object> result = new Response<Object>(); | |||||
if(params == null || params.isEmpty() || params.equals("")){ | |||||
result.setStatus(StatusCode.PARAM_EMPTY.getCode()); | |||||
writeOutResponse(response, result); | |||||
} | |||||
} | |||||
/** | |||||
* 校验参数是否为空 | |||||
* @param response | |||||
* @param params | |||||
* @throws IOException | |||||
*/ | |||||
public void checkParams(HttpServletResponse response,String params,Integer cid,Integer vid,Integer pid) throws IOException{ | |||||
Response<Object> result = new Response<Object>(); | |||||
if(cid == null || vid == null || pid == null){ | |||||
result.setStatus(StatusCode.PARAMS_EMPTY_URL_CID_VID_PID.getCode()); | |||||
writeOutResponse(response, result); | |||||
} | |||||
if(params == null || params.isEmpty() || params.equals("")){ | |||||
result.setStatus(StatusCode.PARAMS_EMPTY.getCode()); | |||||
writeOutResponse(response, result); | |||||
} | |||||
} | |||||
/** | |||||
* 解密数据 | |||||
* @param params | |||||
* @return | |||||
* @throws IOException | |||||
*/ | |||||
public byte[] decryptParams(HttpServletResponse response, String params) throws IOException{ | |||||
//解密数据失败 | |||||
byte[] paramsByte = new byte[]{}; | |||||
//数据格式不正确 | |||||
Map<String,String> paramsMap = new HashMap<String, String>(); | |||||
try { | |||||
paramsMap = JsonUtils.fromJson(params, Map.class); | |||||
// System.out.println("decryptParams() paramsMap: " + paramsMap); | |||||
} catch (Exception e) { | |||||
paramsByte = new byte[]{(byte) Integer.parseInt(StatusCode.PARAMS_JSON_ERROR.getCode())}; | |||||
// System.out.println("decryptParams() Exception: " + e); | |||||
return paramsByte; | |||||
} | |||||
//关键参数字段获取失败 | |||||
String paramsValue = paramsMap.get("params"); | |||||
// System.out.println("decryptParams() paramsValue: " + paramsValue); | |||||
if(paramsValue == null || paramsValue.equals("")){ | |||||
paramsByte = new byte[]{(byte) Integer.parseInt(StatusCode.PARAMS_JSON_KEY_ERROR.getCode())}; | |||||
return paramsByte; | |||||
} | |||||
try { | |||||
paramsByte = Base64TeaUitls.decrypt(paramsValue); | |||||
// System.out.println("decryptParams() paramsByte: " + paramsByte); | |||||
} catch (Exception e) { | |||||
paramsByte = new byte[]{(byte) Integer.parseInt(StatusCode.PARAMS_dECRYPT_ERROR.getCode())}; | |||||
// System.out.println("decryptParams() paramsByte2: " + paramsByte); | |||||
return paramsByte; | |||||
} | |||||
return paramsByte; | |||||
} | |||||
} | |||||
package com.inet.ailink.receiver.controller; | |||||
import com.inet.ailink.receiver.common.enums.StatusCode; | |||||
import com.inet.ailink.receiver.common.vo.Response; | |||||
import com.inet.ailink.receiver.service.impl.BloodPressureServiceImpl; | |||||
import com.inet.ailink.receiver.service.impl.BodyFatServiceImpl; | |||||
import org.springframework.beans.factory.annotation.Autowired; | |||||
import org.springframework.web.bind.annotation.*; | |||||
import javax.servlet.http.HttpServletRequest; | |||||
import javax.servlet.http.HttpServletResponse; | |||||
import java.io.IOException; | |||||
@RestController | |||||
@RequestMapping(value="deData",produces={"application/json;charset=UTF-8"}) | |||||
public class DeDataBaseController extends BaseController { | |||||
@Autowired | |||||
private BodyFatServiceImpl bodyFatServiceImpl; | |||||
@Autowired | |||||
private BloodPressureServiceImpl bloodPressureServiceImpl; | |||||
/** | |||||
* 4G/WIFI+BLE设备测量结果数据 | |||||
* @param params | |||||
* @param request | |||||
* @param response | |||||
* @throws IOException | |||||
*/ | |||||
@RequestMapping(value="base_{cid}_{vid}_{pid}") | |||||
@ResponseBody | |||||
public void saveDataBase(@RequestBody String params,@PathVariable Integer cid,@PathVariable Integer vid,@PathVariable Integer pid,HttpServletRequest request,HttpServletResponse response) throws IOException { | |||||
checkParams(response, params,cid,vid,pid); | |||||
byte[] paramsByte = decryptParams(response,params); | |||||
if(paramsByte.length >1){ | |||||
//WIFI_BLE体脂秤 cid = 17 ,WIFI体脂秤 cid = 25 ,4G体脂秤 cid = 57 | |||||
if(cid == 17 || cid == 25 || cid == 57){ | |||||
writeOutResponse(response, bodyFatServiceImpl.saveWeighDataByAdc(paramsByte, request)); | |||||
} | |||||
//4G血压仪 cid = 48,WIFI+BLE血压仪 cid = 56 | |||||
else if(cid == 48 || cid == 56){ | |||||
writeOutResponse(response, bloodPressureServiceImpl.saveOrUpdateBloodPressure(paramsByte, request)); | |||||
}else{ | |||||
Response<Object> result = new Response<Object>(); | |||||
result.setStatus(StatusCode.DEVICE_UN_SUPPORT.getCode()); | |||||
writeOutResponse(response, result); | |||||
} | |||||
}else{ | |||||
writeOutResponse(response,paramsByte); | |||||
} | |||||
} | |||||
} |
/** | |||||
* Copyright (c) 2020-2020 yann | |||||
* Licensed under the Apache License, Version 1.0 (the "License"); | |||||
*/ | |||||
package com.inet.ailink.receiver.entity; | |||||
import java.util.Date; | |||||
@SuppressWarnings("serial") | |||||
public class BloodPressure extends BaseEntity { | |||||
//alias | |||||
public static final String TABLE_ALIAS = "BloodPressure"; | |||||
public static final String ALIAS_APP_USER_ID = "appUserId"; | |||||
public static final String ALIAS_SUB_USER_ID = "subUserId"; | |||||
public static final String ALIAS_DEVICE_ID = "deviceId"; | |||||
public static final String ALIAS_DIA = "dia"; | |||||
public static final String ALIAS_UNIT = "unit"; | |||||
public static final String ALIAS_POINT = "point"; | |||||
public static final String ALIAS_SYS = "sys"; | |||||
public static final String ALIAS_PUL = "pul"; | |||||
public static final String ALIAS_UPLOAD_TIME = "uploadTime"; | |||||
public static final String ALIAS_MEDICINE = "medicine"; | |||||
public static final String ALIAS_FEEL = "feel"; | |||||
public static final String ALIAS_CONFIRM_STATUS = "确认状态"; | |||||
//columns START | |||||
private Integer appUserId; | |||||
private Integer subUserId; | |||||
private Integer deviceId; | |||||
private String dia; | |||||
private Integer unit; | |||||
private Integer point; | |||||
private String sys; | |||||
private String pul; | |||||
private Date uploadTime; | |||||
private Integer medicine; | |||||
private Integer feel; | |||||
private Integer confirmStatus; | |||||
private Long createTimeLong; | |||||
//columns END | |||||
public BloodPressure(){ | |||||
} | |||||
public Integer getAppUserId() { | |||||
return appUserId; | |||||
} | |||||
public void setAppUserId(Integer appUserId) { | |||||
this.appUserId = appUserId; | |||||
} | |||||
public Integer getSubUserId() { | |||||
return subUserId; | |||||
} | |||||
public void setSubUserId(Integer subUserId) { | |||||
this.subUserId = subUserId; | |||||
} | |||||
public Integer getDeviceId() { | |||||
return deviceId; | |||||
} | |||||
public void setDeviceId(Integer deviceId) { | |||||
this.deviceId = deviceId; | |||||
} | |||||
public void setDia(String value) { | |||||
this.dia = value; | |||||
} | |||||
public String getDia() { | |||||
return this.dia; | |||||
} | |||||
public void setUnit(Integer value) { | |||||
this.unit = value; | |||||
} | |||||
public Integer getUnit() { | |||||
return this.unit; | |||||
} | |||||
public void setPoint(Integer value) { | |||||
this.point = value; | |||||
} | |||||
public Integer getPoint() { | |||||
return this.point; | |||||
} | |||||
public void setSys(String value) { | |||||
this.sys = value; | |||||
} | |||||
public String getSys() { | |||||
return this.sys; | |||||
} | |||||
public void setPul(String value) { | |||||
this.pul = value; | |||||
} | |||||
public String getPul() { | |||||
return this.pul; | |||||
} | |||||
public void setUploadTime(Date value) { | |||||
this.uploadTime = value; | |||||
} | |||||
public Date getUploadTime() { | |||||
return this.uploadTime; | |||||
} | |||||
public void setMedicine(Integer value) { | |||||
this.medicine = value; | |||||
} | |||||
public Integer getMedicine() { | |||||
return this.medicine; | |||||
} | |||||
public void setFeel(Integer value) { | |||||
this.feel = value; | |||||
} | |||||
public Integer getFeel() { | |||||
return this.feel; | |||||
} | |||||
public void setConfirmStatus(Integer value) { | |||||
this.confirmStatus = value; | |||||
} | |||||
public Integer getConfirmStatus() { | |||||
return this.confirmStatus; | |||||
} | |||||
public Long getCreateTimeLong() { | |||||
return createTimeLong; | |||||
} | |||||
public void setCreateTimeLong(Long createTimeLong) { | |||||
this.createTimeLong = createTimeLong; | |||||
} | |||||
} | |||||
/**/ |
/** | |||||
* Copyright (c) 2020-2020 yann | |||||
* Licensed under the Apache License, Version 1.0 (the "License"); | |||||
*/ | |||||
package com.inet.ailink.receiver.mapper; | |||||
import com.inet.ailink.receiver.entity.BloodPressure; | |||||
/** | |||||
* @author Yann group | |||||
* @version 1.0 | |||||
* @since 1.0 | |||||
*/ | |||||
public interface IBloodPressureDao extends BaseDao<BloodPressure>{ | |||||
} |
/** | |||||
* Copyright (c) 2020-2020 yann | |||||
* Licensed under the Apache License, Version 1.0 (the "License"); | |||||
*/ | |||||
package com.inet.ailink.receiver.service.impl; | |||||
import com.inet.ailink.receiver.common.enums.StatusCode; | |||||
import com.inet.ailink.receiver.common.utils.Base64TeaUitls; | |||||
import com.inet.ailink.receiver.common.vo.Response; | |||||
import com.inet.ailink.receiver.entity.BloodPressure; | |||||
import com.inet.ailink.receiver.entity.Device; | |||||
import com.inet.ailink.receiver.mapper.IBloodPressureDao; | |||||
import com.inet.ailink.receiver.mapper.ISysDeviceInfoDao; | |||||
import org.springframework.beans.factory.annotation.Autowired; | |||||
import org.springframework.stereotype.Service; | |||||
import org.springframework.transaction.annotation.Transactional; | |||||
import javax.servlet.http.HttpServletRequest; | |||||
import java.util.ArrayList; | |||||
import java.util.Date; | |||||
import java.util.List; | |||||
@Service | |||||
@Transactional(rollbackFor=Exception.class) | |||||
public class BloodPressureServiceImpl extends BaseServiceImpl<IBloodPressureDao,BloodPressure> | |||||
{ | |||||
@Autowired | |||||
IBloodPressureDao bloodPressureDao; | |||||
@Autowired | |||||
ISysDeviceInfoDao deviceInfoDao; | |||||
@Override | |||||
protected IBloodPressureDao getEntityDao() { | |||||
// TODO Auto-generated method stub | |||||
return bloodPressureDao; | |||||
} | |||||
public Response<Object> saveOrUpdateBloodPressure(byte[] paramsByte,HttpServletRequest request) { | |||||
Response<Object> result = new Response<Object>(); | |||||
try { | |||||
//解析数据 | |||||
//设备ID【0-3】 | |||||
Integer deviceId = Integer.parseInt(Base64TeaUitls.getLowConactHighEndForLong(0, 3, paramsByte)+""); | |||||
String deviceId2 = Base64TeaUitls.getStartToEndForHex(0, 3, paramsByte); | |||||
//舒张压【4-5】 | |||||
String dia = Base64TeaUitls.getLowConactHighEndForInt(4, 5, paramsByte); | |||||
//收缩压【6-7】 | |||||
String sys = Base64TeaUitls.getLowConactHighEndForInt(6, 7, paramsByte); | |||||
//心率【8】 | |||||
String pul = Base64TeaUitls.getStartToEndForInt(8, 8, paramsByte); | |||||
//单位【9】;0:mmhg;1:kPa | |||||
String unit = Base64TeaUitls.getStartToEndForInt(9, 9, paramsByte); | |||||
//小数点【10】:0:无小数点;1:1位小数点;2:2位小数点;N:N位小数点 | |||||
String point = Base64TeaUitls.getStartToEndForInt(10, 10, paramsByte); | |||||
BloodPressure bloodPressure = new BloodPressure(); | |||||
//设置基础数据 | |||||
bloodPressure.setDeviceId(deviceId); | |||||
bloodPressure.setDia(dia); | |||||
bloodPressure.setSys(sys); | |||||
bloodPressure.setPul(pul); | |||||
bloodPressure.setUnit(Integer.parseInt(unit)); | |||||
bloodPressure.setPoint(Integer.parseInt(point)); | |||||
bloodPressure.setConfirmStatus(0); | |||||
//设置默认值;和蓝牙统一 | |||||
bloodPressure.setMedicine(-1); | |||||
bloodPressure.setFeel(-1); | |||||
//设备未上传时间,以本地时间为准 | |||||
bloodPressure.setCreateTimeLong(new Date().getTime()); | |||||
bloodPressure.setUploadTime(new Date()); | |||||
//查询该设备是否有绑定用户,具体实现根据业务实现 | |||||
List<Integer> appUserIds = new ArrayList<Integer>(); | |||||
//设置其他值 | |||||
if(appUserIds != null && appUserIds.size()>0){ | |||||
for(Integer appUserId:appUserIds){ | |||||
bloodPressure.setAppUserId(appUserId); | |||||
//保存数据 | |||||
if(bloodPressureDao.insert(bloodPressure) > 0){ | |||||
result.setStatus(StatusCode.SUCCESS.getCode()); | |||||
result.setData(bloodPressure.getId()); | |||||
//推送给APP | |||||
//aliyunPushService.pushForBloodPressure(bloodPressure); | |||||
}else{ | |||||
result.setStatus(StatusCode.FAIL.getCode()); | |||||
} | |||||
} | |||||
}else{ | |||||
bloodPressure.setAppUserId(0); | |||||
bloodPressure.setRemark("没有绑定用户"); | |||||
bloodPressureDao.insert(bloodPressure); | |||||
result.setStatus(StatusCode.SUCCESS.getCode()); | |||||
} | |||||
} catch (Exception e) { | |||||
e.printStackTrace(); | |||||
result.setStatus(StatusCode.PARAMS_dECRYPT_ERROR.getCode()); | |||||
} | |||||
return result; | |||||
} | |||||
} |