|  | @ -1,7 +1,11 @@
 | 
	
		
			
				|  |  | package com.yihu.wlyy.service.third.iot;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import com.alibaba.fastjson.JSONArray;
 | 
	
		
			
				|  |  | import com.alibaba.fastjson.JSONObject;
 | 
	
		
			
				|  |  | import com.yihu.device.entity.DevicePatientHealthIndex;
 | 
	
		
			
				|  |  | import com.yihu.wlyy.entity.patient.Patient;
 | 
	
		
			
				|  |  | import com.yihu.wlyy.util.HttpClientUtil;
 | 
	
		
			
				|  |  | import org.apache.commons.lang.StringUtils;
 | 
	
		
			
				|  |  | import org.apache.http.NameValuePair;
 | 
	
		
			
				|  |  | import org.apache.http.message.BasicNameValuePair;
 | 
	
		
			
				|  |  | import org.springframework.beans.factory.annotation.Autowired;
 | 
	
	
		
			
				|  | @ -18,13 +22,92 @@ import java.util.List;
 | 
	
		
			
				|  |  | public class IotDeviceService {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | //    @Value("${}")
 | 
	
		
			
				|  |  |     private String baseUrl = "https://iot.xmtyw.cn/";
 | 
	
		
			
				|  |  |     private String grantType = "client_credentials&";
 | 
	
		
			
				|  |  |     private String clientId = "Va5yQRHlA4Fq4eR3LT0vuXV4&";
 | 
	
		
			
				|  |  |     private String baseUrl = "http://192.168.131.24:8080/svr-iot/";
 | 
	
		
			
				|  |  |     private String grantType = "client_credentials";
 | 
	
		
			
				|  |  |     private String clientId = "Va5yQRHlA4Fq4eR3LT0vuXV4";
 | 
	
		
			
				|  |  |     private String clientSecret = "0rDSjzQ20XUj5itV7WRtznPQSzr5pVw2";
 | 
	
		
			
				|  |  |     private String dataSource = "iHealth";
 | 
	
		
			
				|  |  |     private String accessToken = "test";
 | 
	
		
			
				|  |  |     private String bloodSugarUnit = "mmol/L";
 | 
	
		
			
				|  |  |     private String bloodPressureUnit = "mmHg";
 | 
	
		
			
				|  |  |     private String pulseUnit = "bpm";
 | 
	
		
			
				|  |  |     private String heightUnit = "cm";
 | 
	
		
			
				|  |  |     private String weightUnit = "kg";
 | 
	
		
			
				|  |  |     private String bmiUnit = "kg/m^2";
 | 
	
		
			
				|  |  |     private String waistUnit = "cm";
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private HttpClientUtil httpClientUtil;
 | 
	
		
			
				|  |  |     private String accessToken = "";
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 将上传数据转换成标准的json传
 | 
	
		
			
				|  |  |      * @param obj
 | 
	
		
			
				|  |  |      * @param userType
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public DevicePatientHealthIndex upload(DevicePatientHealthIndex obj,String userType,String deviceName,String deviceModel,Patient patient){
 | 
	
		
			
				|  |  |         JSONObject json = new JSONObject();
 | 
	
		
			
				|  |  |         json.put("sn",obj.getDeviceSn());
 | 
	
		
			
				|  |  |         json.put("ext_code",userType);
 | 
	
		
			
				|  |  |         json.put("device_name",deviceName);
 | 
	
		
			
				|  |  |         json.put("device_model",deviceModel);
 | 
	
		
			
				|  |  |         json.put("idcard",patient.getIdcard());
 | 
	
		
			
				|  |  |         json.put("username",patient.getName());
 | 
	
		
			
				|  |  |         json.put("usercode",patient.getCode());
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         JSONArray jsonArray = new JSONArray();
 | 
	
		
			
				|  |  |         JSONObject js = new JSONObject();
 | 
	
		
			
				|  |  |         js.put("measure_time",obj.getRecordDate());
 | 
	
		
			
				|  |  |         switch (obj.getType()){
 | 
	
		
			
				|  |  |             case 1:
 | 
	
		
			
				|  |  |                 //血糖
 | 
	
		
			
				|  |  |                 js.put("blood_sugar",obj.getValue1());
 | 
	
		
			
				|  |  |                 js.put("blood_sugar_unit",bloodSugarUnit);
 | 
	
		
			
				|  |  |                 js.put("blood_sugar_result",obj.getValue2());
 | 
	
		
			
				|  |  |                 break;
 | 
	
		
			
				|  |  |             case 2:
 | 
	
		
			
				|  |  |                 //血压
 | 
	
		
			
				|  |  |                 js.put("systolic",obj.getValue1());
 | 
	
		
			
				|  |  |                 js.put("systolic_unit",bloodPressureUnit);
 | 
	
		
			
				|  |  |                 js.put("diastolic",obj.getValue2());
 | 
	
		
			
				|  |  |                 js.put("diastolic_unit",bloodPressureUnit);
 | 
	
		
			
				|  |  |                 if(StringUtils.isNotBlank(obj.getValue3())){
 | 
	
		
			
				|  |  |                     js.put("pulse",obj.getValue3());
 | 
	
		
			
				|  |  |                     js.put("pulse_unit",pulseUnit);
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |                 break;
 | 
	
		
			
				|  |  |             case 3:
 | 
	
		
			
				|  |  |                 //体重/身高/BMI
 | 
	
		
			
				|  |  |                 js.put("height",obj.getValue1());
 | 
	
		
			
				|  |  |                 js.put("height_unit",heightUnit);
 | 
	
		
			
				|  |  |                 js.put("weight",obj.getValue2());
 | 
	
		
			
				|  |  |                 js.put("weight_unit",weightUnit);
 | 
	
		
			
				|  |  |                 if(StringUtils.isNotBlank(obj.getValue3())){
 | 
	
		
			
				|  |  |                     js.put("bmi",obj.getValue3());
 | 
	
		
			
				|  |  |                     js.put("bmi_unit",bmiUnit);
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |                 break;
 | 
	
		
			
				|  |  |             case 4:
 | 
	
		
			
				|  |  |                 //腰围
 | 
	
		
			
				|  |  |                 js.put("waist",obj.getValue1());
 | 
	
		
			
				|  |  |                 js.put("waist_unit",waistUnit);
 | 
	
		
			
				|  |  |                 break;
 | 
	
		
			
				|  |  |             default:
 | 
	
		
			
				|  |  |                 break;
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         jsonArray.add(js);
 | 
	
		
			
				|  |  |         json.put("data",jsonArray);
 | 
	
		
			
				|  |  |         //上传
 | 
	
		
			
				|  |  |         String response = upload(json);
 | 
	
		
			
				|  |  |         JSONObject re = JSONObject.parseObject(response);
 | 
	
		
			
				|  |  |         String errorMsg = re.getString("errorMsg");//错误信息(请求失败才有错误消息)
 | 
	
		
			
				|  |  |         String successMsg = re.getString("successMsg");//成功信息(请求成功才有成功消息)
 | 
	
		
			
				|  |  |         if(StringUtils.isBlank(errorMsg)){
 | 
	
		
			
				|  |  |             re.getJSONObject("obj").getString("id");
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         return obj;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public String getAccessToken(){
 | 
	
		
			
				|  |  | 
 | 
	
	
		
			
				|  | @ -44,7 +127,8 @@ public class IotDeviceService {
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public String registedevice(JSONObject json){
 | 
	
		
			
				|  |  |         json.put("access_token",accessToken);
 | 
	
		
			
				|  |  |         String url = baseUrl+"/registedevice";
 | 
	
		
			
				|  |  |         json.put("data_source",dataSource);
 | 
	
		
			
				|  |  |         String url = baseUrl+"/dataInput/userBind";
 | 
	
		
			
				|  |  |         List<NameValuePair> params = new ArrayList<>();
 | 
	
		
			
				|  |  |         params.add(new BasicNameValuePair("json",json.toString()));
 | 
	
		
			
				|  |  |         String response = httpClientUtil.post(url, params, "UTF-8");
 | 
	
	
		
			
				|  | @ -58,11 +142,12 @@ public class IotDeviceService {
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public String upload(JSONObject json){
 | 
	
		
			
				|  |  |         json.put("access_token",accessToken);
 | 
	
		
			
				|  |  |         String url = baseUrl+"/upload";
 | 
	
		
			
				|  |  |         List<NameValuePair> params = new ArrayList<>();
 | 
	
		
			
				|  |  |         params.add(new BasicNameValuePair("json",json.toString()));
 | 
	
		
			
				|  |  |         String response = httpClientUtil.post(url, params, "UTF-8");
 | 
	
		
			
				|  |  |         return null;
 | 
	
		
			
				|  |  |         json.put("data_source",dataSource);
 | 
	
		
			
				|  |  |         String url = baseUrl+"/dataInput/input";
 | 
	
		
			
				|  |  |         org.json.JSONObject params = new org.json.JSONObject();
 | 
	
		
			
				|  |  |         params.put("json_data",json.toString());
 | 
	
		
			
				|  |  |         String response = httpClientUtil.postBody(url, params);
 | 
	
		
			
				|  |  |         return response;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 |