|  | @ -4,8 +4,10 @@ import com.alibaba.fastjson.JSONArray;
 | 
	
		
			
				|  |  | import com.alibaba.fastjson.JSONObject;
 | 
	
		
			
				|  |  | import com.yihu.base.es.config.ElastricSearchHelper;
 | 
	
		
			
				|  |  | import com.yihu.base.es.config.model.SaveModel;
 | 
	
		
			
				|  |  | import com.yihu.base.hbase.HBaseAdmin;
 | 
	
		
			
				|  |  | import com.yihu.base.hbase.HBaseHelper;
 | 
	
		
			
				|  |  | import com.yihu.iot.datainput.enums.DataOperationTypeEnum;
 | 
	
		
			
				|  |  | import com.yihu.iot.datainput.util.ConstantUtils;
 | 
	
		
			
				|  |  | import com.yihu.iot.datainput.util.RowKeyUtils;
 | 
	
		
			
				|  |  | import com.yihu.iot.service.device.IotDeviceService;
 | 
	
		
			
				|  |  | import com.yihu.jw.iot.device.IotDeviceDO;
 | 
	
	
		
			
				|  | @ -40,22 +42,16 @@ public class DataInputService {
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private HBaseHelper hBaseHelper;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     private String esIndex = "body_health_data";
 | 
	
		
			
				|  |  |     private String esType = "signs_data";
 | 
	
		
			
				|  |  |     private String tableName = "body_health_data";
 | 
	
		
			
				|  |  |     private String familyA = "column_signs_header";
 | 
	
		
			
				|  |  |     private String familyB = "column_signs_data";
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private HBaseAdmin hBaseAdmin;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 居民设备注册及绑定
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public String bindUser(String json){
 | 
	
		
			
				|  |  |         List<IotDeviceDO> deviceDOList = new ArrayList<>();
 | 
	
		
			
				|  |  |         JSONObject jsonObject = JSONObject.parseObject(json);
 | 
	
		
			
				|  |  |         String data_source = jsonObject.getString("data_source");
 | 
	
		
			
				|  |  |         List<IotDeviceDO> deviceDOList = new ArrayList<>();
 | 
	
		
			
				|  |  |         JSONArray jsonArray = jsonObject.getJSONArray("data");
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             if(null != jsonArray){
 | 
	
	
		
			
				|  | @ -68,7 +64,7 @@ public class DataInputService {
 | 
	
		
			
				|  |  |                         continue; //表示设备已经绑定过
 | 
	
		
			
				|  |  |                     }
 | 
	
		
			
				|  |  |                     iotDeviceDO.setDeviceSn(sn);
 | 
	
		
			
				|  |  |                     iotDeviceDO.setCode(dataJson.getString("ext_code"));
 | 
	
		
			
				|  |  | //                    iotDeviceDO.setCode(dataJson.getString("ext_code"));
 | 
	
		
			
				|  |  |                     iotDeviceDO.setName(dataJson.getString("device_name"));
 | 
	
		
			
				|  |  |                     iotDeviceDO.setDeviceModel(dataJson.getString("device_model"));
 | 
	
		
			
				|  |  |                     iotDeviceDO.setDeviceSource("2"); //设备来源为居民绑定
 | 
	
	
		
			
				|  | @ -90,7 +86,6 @@ public class DataInputService {
 | 
	
		
			
				|  |  |                 iotDeviceService.bindUser(deviceDOList);
 | 
	
		
			
				|  |  |                 //保存日志
 | 
	
		
			
				|  |  |                 dataProcessLogService.saveLog("","",data_source,"", DateUtils.formatDate(new Date(), DateUtil.yyyy_MM_dd_HH_mm_ss),"1","4","com.yihu.iot.datainput.service.DataInputService.bindUser",DataOperationTypeEnum.bindUser.getName(),0);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         }catch (Exception e){
 | 
	
		
			
				|  |  |             logger.error("注册绑定失败");
 | 
	
	
		
			
				|  | @ -109,6 +104,12 @@ public class DataInputService {
 | 
	
		
			
				|  |  |         if(null != iotDeviceDO){
 | 
	
		
			
				|  |  |             iotDeviceDO.setUpdateUser(idcard);
 | 
	
		
			
				|  |  |             iotDeviceDO.setUpdateUserName(username);
 | 
	
		
			
				|  |  |         }else{
 | 
	
		
			
				|  |  |             iotDeviceDO = new IotDeviceDO();
 | 
	
		
			
				|  |  |             iotDeviceDO.setDeviceSource(data_source);
 | 
	
		
			
				|  |  |             iotDeviceDO.setDeviceSn(deviveSn);
 | 
	
		
			
				|  |  |             iotDeviceDO.setCreateUser(idcard);
 | 
	
		
			
				|  |  |             iotDeviceDO.setCreateUserName(username);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         iotDeviceService.save(iotDeviceDO);
 | 
	
		
			
				|  |  |         //保存日志
 | 
	
	
		
			
				|  | @ -125,16 +126,13 @@ public class DataInputService {
 | 
	
		
			
				|  |  |         String fileName = "";
 | 
	
		
			
				|  |  |         String fileAbsPath = "";
 | 
	
		
			
				|  |  |         String rowkey = "";
 | 
	
		
			
				|  |  |         //提取json各项值
 | 
	
		
			
				|  |  |         //提取json某些项值
 | 
	
		
			
				|  |  |         JSONObject jsonObject = JSONObject.parseObject(json);
 | 
	
		
			
				|  |  |         String accessToken= jsonObject.getString("access_token");
 | 
	
		
			
				|  |  |         String dataSource = jsonObject.getString("data_source");
 | 
	
		
			
				|  |  |         String deviceSn = jsonObject.getString("sn");
 | 
	
		
			
				|  |  |         String extCode = jsonObject.getString("ext_code");
 | 
	
		
			
				|  |  |         String measuretime = jsonObject.getString("measure_time");
 | 
	
		
			
				|  |  |         if(null == measuretime){
 | 
	
		
			
				|  |  |             measuretime = DateUtils.formatDate(new Date(), DateUtil.yyyy_MM_dd_HH_mm_ss);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         //包含居民身份的数据,对设备数据进行校验绑定,此处包含的信息只有身份证号和用户名以及设备序列号,如果设备库中存在该序号的设备,则对绑定居民进行修改,改为当前居民,如果没有则跳过
 | 
	
		
			
				|  |  |         if(jsonObject.containsKey("idcard") && jsonObject.containsKey("username")){
 | 
	
		
			
				|  |  |             String idcard = jsonObject.getString("idcard");
 | 
	
	
		
			
				|  | @ -142,54 +140,60 @@ public class DataInputService {
 | 
	
		
			
				|  |  |             updateBindUser(dataSource,deviceSn,idcard,username);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             rowkey = RowKeyUtils.makeRowKey(accessToken, deviceSn, extCode, DateUtil.dateTimeParse(measuretime).getTime());
 | 
	
		
			
				|  |  |         } catch (Exception e) {
 | 
	
		
			
				|  |  |             e.printStackTrace();
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         //将数据存入es
 | 
	
		
			
				|  |  |         jsonObject.put("_id", new SaveModel().getId());//es的id继承至jestId
 | 
	
		
			
				|  |  |         jsonObject.put("id", rowkey);//hbase的rowkey
 | 
	
		
			
				|  |  |         elastricSearchHelper.save(esIndex, esType, jsonObject.toJSONString());
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         Map<String, Map<String, String>> family = new HashMap<>();
 | 
	
		
			
				|  |  |         Map<String, String> columnsA = new HashMap<>();
 | 
	
		
			
				|  |  |         Map<String, String> columnsB = new HashMap<>();
 | 
	
		
			
				|  |  |         //组装A列
 | 
	
		
			
				|  |  |         columnsA.put("access_token",accessToken);
 | 
	
		
			
				|  |  |         columnsA.put("data_source",dataSource);
 | 
	
		
			
				|  |  |         columnsA.put("sn",deviceSn);
 | 
	
		
			
				|  |  |         columnsA.put("ext_code",extCode);
 | 
	
		
			
				|  |  |         columnsA.put("device_name",jsonObject.getString("device_name"));
 | 
	
		
			
				|  |  |         columnsA.put("device_model",jsonObject.getString("device_model"));
 | 
	
		
			
				|  |  |         family.put(familyA,columnsA);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         JSONArray jsonArray = jsonObject.getJSONArray("data");
 | 
	
		
			
				|  |  |         if(null == jsonArray || jsonArray.size() == 0){
 | 
	
		
			
				|  |  |             return "json no data";
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         //组装B列
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         List<String> rowkeyList = new ArrayList<>();
 | 
	
		
			
				|  |  |         List<Map<String,Map<String,String>>> familyList = new ArrayList<>();
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         //循环数据,一组数据存一行,生成一个rowkey,并将该rowkey存到es中
 | 
	
		
			
				|  |  |         for(Object obj:jsonArray){
 | 
	
		
			
				|  |  |             JSONObject data = (JSONObject)obj;
 | 
	
		
			
				|  |  |            for(String key:data.keySet()){
 | 
	
		
			
				|  |  |                columnsB.put(key,data.getString(key));
 | 
	
		
			
				|  |  |            }
 | 
	
		
			
				|  |  |            if(data.containsKey("ecg")){
 | 
	
		
			
				|  |  |                fileName = data.getString("fileName");
 | 
	
		
			
				|  |  |                fileAbsPath = data.getString("filepath");
 | 
	
		
			
				|  |  |            }
 | 
	
		
			
				|  |  |             data.put("del","1"); //添加删除标记
 | 
	
		
			
				|  |  |             try {
 | 
	
		
			
				|  |  |                 String measuretime = jsonObject.getString("measure_time");
 | 
	
		
			
				|  |  |                 if(null == measuretime){
 | 
	
		
			
				|  |  |                     measuretime = DateUtils.formatDate(new Date(), DateUtil.yyyy_MM_dd_HH_mm_ss);
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |                 //生成一份json数据的rowkey
 | 
	
		
			
				|  |  |                 rowkey = RowKeyUtils.makeRowKey(accessToken,deviceSn, extCode, DateUtil.dateTimeParse(measuretime).getTime());
 | 
	
		
			
				|  |  |                 data.put("rid",rowkey);//hbase的rowkey
 | 
	
		
			
				|  |  |                 rowkeyList.add(rowkey);
 | 
	
		
			
				|  |  |             } catch (Exception e) {
 | 
	
		
			
				|  |  |                 e.printStackTrace();
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             //组装B列
 | 
	
		
			
				|  |  |             Map<String, Map<String, String>> family = new HashMap<>();
 | 
	
		
			
				|  |  |             Map<String, String> columnsB = new HashMap<>();
 | 
	
		
			
				|  |  |             for(String key:data.keySet()){
 | 
	
		
			
				|  |  |                 columnsB.put(key,data.getString(key));
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             if(data.containsKey("ecg")){
 | 
	
		
			
				|  |  |                 fileName = data.getString("fileName");
 | 
	
		
			
				|  |  |                 fileAbsPath = data.getString("filepath");
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             family.put(ConstantUtils.tableName,columnsB);
 | 
	
		
			
				|  |  |             familyList.add(family);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         family.put(familyB,columnsB);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         //将数据存入es
 | 
	
		
			
				|  |  |         elastricSearchHelper.save(ConstantUtils.esIndex, ConstantUtils.esType, jsonObject.toJSONString());
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             hBaseHelper.add(tableName,rowkey,family);
 | 
	
		
			
				|  |  |             boolean tableExists = hBaseAdmin.isTableExists(ConstantUtils.tableName);
 | 
	
		
			
				|  |  |             if (!tableExists) {
 | 
	
		
			
				|  |  |                 hBaseAdmin.createTable(ConstantUtils.tableName,ConstantUtils.familyB);
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             hBaseHelper.addBulk(ConstantUtils.tableName, rowkeyList, familyList);
 | 
	
		
			
				|  |  |         } catch (Exception e) {
 | 
	
		
			
				|  |  |             e.printStackTrace();
 | 
	
		
			
				|  |  |             //保存日志
 | 
	
		
			
				|  |  |             dataProcessLogService.saveLog(fileName,fileAbsPath,dataSource,"", DateUtils.formatDate(new Date(), DateUtil.yyyy_MM_dd_HH_mm_ss),"1","3","com.yihu.iot.datainput.service.DataInputService.uploadData", DataOperationTypeEnum.upload1.getName(),1);
 | 
	
		
			
				|  |  |             dataProcessLogService.saveLog(fileName, fileAbsPath, dataSource, "", DateUtils.formatDate(new Date(), DateUtil.yyyy_MM_dd_HH_mm_ss), "1", "3", "com.yihu.iot.datainput.service.DataInputService.uploadData", DataOperationTypeEnum.upload1.getName(), 1);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         //保存日志
 | 
	
		
			
				|  |  |         dataProcessLogService.saveLog(fileName,fileAbsPath,dataSource,"", DateUtils.formatDate(new Date(), DateUtil.yyyy_MM_dd_HH_mm_ss),"1","4","com.yihu.iot.datainput.service.DataInputService.uploadData",DataOperationTypeEnum.upload1.getName(),0);
 | 
	
		
			
				|  |  |         dataProcessLogService.saveLog(fileName, fileAbsPath, dataSource, "", DateUtils.formatDate(new Date(), DateUtil.yyyy_MM_dd_HH_mm_ss), "1", "4", "com.yihu.iot.datainput.service.DataInputService.uploadData", DataOperationTypeEnum.upload1.getName(), 0);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         return "success";
 | 
	
		
			
				|  |  |     }
 |