LiTaohong 7 years ago
parent
commit
53c88df4ed

+ 4 - 14
base/common-data-es/src/main/java/com/yihu/base/es/config/ElastricSearchHelper.java

@ -166,26 +166,16 @@ public class ElastricSearchHelper {
        JestResult result = null;
        try {
            jestClient = elasticFactory.getJestClient();
            //根据id批量删除
            Bulk.Builder bulk = new Bulk.Builder().defaultIndex(index).defaultType(type);
//            for (SaveModel obj : saveModels) {
//                Search indexObj = new Search.Builder(obj.getId()).build();
//                bulk.addAction(indexObj);
//            }
            BulkResult br = jestClient.execute(bulk.build());
            Search search = (Search) new Search.Builder(queryStr)
            Search search = new Search.Builder(queryStr)
                    // multiple index or types can be added.
                    .addIndex("articles")
                    .addType("article")
                    .addIndex(index)
                    .addType(type)
                    .build();
            result = jestClient.execute(search);
            JSONObject resultJsonObject = (JSONObject)JSONObject.parse(result.getJsonString());
            JSONObject jsonObject = (JSONObject)resultJsonObject.get("hits");
            logger.info("delete data count:" + jsonObject.get("total"));
            logger.info("delete flag:" + br.isSucceeded());
            logger.info("search data count:" + jsonObject.get("total"));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

+ 32 - 0
base/common-data-hbase/src/main/java/com/yihu/base/hbase/HBaseHelper.java

@ -216,6 +216,38 @@ public class HBaseHelper extends AbstractHBaseClient {
        });
    }
    /**
     * 批量新增行
     */
    public void addBulk(String tableName , List<String> rowkeyList, List<Map<String,Map<String,String>>> familyList) throws Exception
    {
        hbaseTemplate.execute(tableName, new TableCallback<String>() {
            public String doInTable(HTableInterface table) throws Throwable {
                List<Put> list = new ArrayList<>();
                for(int i = 0; i < rowkeyList.size();i++){
                    Put p = new Put(rowkeyList.get(i).getBytes());
                    Map<String,Map<String,String>> family = familyList.get(i);
                    for(String familyName : family.keySet())
                    {
                        Map<String,String> map = family.get(familyName);
                        for (String qualifier : map.keySet())
                        {
                            String value = map.get(qualifier);
                            if(value == null){
                                continue;
                            }
                            p.add(familyName.getBytes(), qualifier.getBytes(), value.getBytes());
                        }
                    }
                    list.add(p);
                }
                table.put(list);
                return null;
            }
        });
    }
    /**
     * 新增数据
     */

+ 18 - 2
common/common-request-mapping/src/main/java/com/yihu/jw/rm/iot/DataRequestMapping.java

@ -22,10 +22,26 @@ public class DataRequestMapping {
    public static class DataInput{
        public static final String api_data_input = "input";//数据上传
        public static final String api_user_bind = "userBind";//设备注册绑定
        public static final String api_update_record = "updateRecord";//更新体征状态标识
        public static final String message_success = "上传成功";
        public static final String message_fail = "上传失败";
        public static final String message_success = "upload success";
        public static final String message_fail = "upload fail";
        public static final String message_fail_jsonData_is_null = "jsonData is null";
    }
    /**
     * 数据查询
     */
    public static class DataSearch{
        public static final String api_data_search_one = "getById";
        public static final String api_user_search_list = "searchList";
        public static final String api_user_search_list_page = "listPage";
        public static final String api_user_search_recent5 = "recent5";
        public static final String api_user_abnormal_times_a_week = "abnormalTimes";
        public static final String message_success = "search success";
        public static final String message_fail = "search fail";
        public static final String message_fail_jsonData_is_null = "param is null";
    }
}

+ 21 - 2
svr/svr-iot/src/main/java/com/yihu/iot/datainput/controller/DataInputController.java

@ -9,6 +9,7 @@ import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
@ -34,11 +35,29 @@ public class DataInputController {
    @PostMapping(value = DataRequestMapping.DataInput.api_data_input, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "数据上传", notes = "数据上传入库")
    public Envelop uoloadData(@ApiParam(name = "json_data", value = "", defaultValue = "") @RequestBody String jsonData){
    public Envelop uploadData(@ApiParam(name = "json_data", value = "", defaultValue = "") @RequestBody String jsonData) {
        String str = "";
        try {
            return Envelop.getSuccess(DataRequestMapping.DataInput.message_success, dataInputService.uploadData(jsonData));
            str = dataInputService.uploadData(jsonData);
            if (str.equals("fail")) {
                return Envelop.getError(DataRequestMapping.DataInput.message_fail, 0);
            }
            if (str.equals("json no data")) {
                return Envelop.getError(DataRequestMapping.DataInput.message_fail_jsonData_is_null, 1);
            }
        } catch (ApiException e) {
            return Envelop.getError(e.getMessage(), e.getErrorCode());
        }
        return Envelop.getSuccess(DataRequestMapping.DataInput.message_success, str);
    }
    @PostMapping(value = DataRequestMapping.DataInput.api_update_record, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "用户体征状态修改", notes = "用户体征状态修改,0-标准,1-异常")
    public Envelop updateRecord(@ApiParam(name = "json_data", value = "", defaultValue = "") @RequestBody String jsonData){
        try{
            return Envelop.getSuccess(DataRequestMapping.DataInput.message_success,dataInputService.bindUser(jsonData));
        } catch (ApiException e){
            return Envelop.getError(e.getMessage(), e.getErrorCode());
        }
    }
}

+ 102 - 0
svr/svr-iot/src/main/java/com/yihu/iot/datainput/controller/DataSearchController.java

@ -1,4 +1,106 @@
package com.yihu.iot.datainput.controller;
import com.alibaba.fastjson.JSONObject;
import com.yihu.iot.datainput.service.DataSearchService;
import com.yihu.jw.exception.ApiException;
import com.yihu.jw.restmodel.common.Envelop;
import com.yihu.jw.rm.iot.DataRequestMapping;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(DataRequestMapping.api_iot_common)
@Api(value = "数据查询操作", description = "数据查询操作")
public class DataSearchController {
    @Autowired
    private DataSearchService dataSearchService;
    @PostMapping(value = DataRequestMapping.DataSearch.api_data_search_one, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "查询单条数据", notes = "根据id查询单条数据")
    public Envelop getOne(@ApiParam(name = "id", value = "", defaultValue = "") @RequestBody String id){
        try{
            String jsonData = "{\"id\":" + id + "}";
            return Envelop.getSuccess(DataRequestMapping.DataInput.message_success,dataSearchService.getData(jsonData));
        } catch (ApiException e){
            return Envelop.getError(e.getMessage(), e.getErrorCode());
        }
    }
    @PostMapping(value = DataRequestMapping.DataSearch.api_user_search_list, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "查询数据", notes = "根据条件查询数据")
    public Envelop getList(@ApiParam(name = "json_data", value = "", defaultValue = "") @RequestBody String jsonData){
        try{
            return Envelop.getSuccess(DataRequestMapping.DataInput.message_success,dataSearchService.getData(jsonData));
        } catch (ApiException e){
            return Envelop.getError(e.getMessage(), e.getErrorCode());
        }
    }
    @PostMapping(value = DataRequestMapping.DataSearch.api_user_search_list_page, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "查询数据,分页", notes = "根据条件查询数据,分页")
    public Envelop getListPage(@ApiParam(name = "json_data", value = "", defaultValue = "") @RequestBody String jsonData){
        try{
            return Envelop.getSuccess(DataRequestMapping.DataInput.message_success,dataSearchService.getData(jsonData));
        } catch (ApiException e){
            return Envelop.getError(e.getMessage(), e.getErrorCode());
        }
    }
    @PostMapping(value = DataRequestMapping.DataSearch.api_user_search_recent5, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "获取最近5条数据", notes = "根据居民的体征类型,测量时间获取")
    public Envelop getRecent5(@ApiParam(name = "json_data", value = "", defaultValue = "") @RequestBody String jsonData){
        try{
            return Envelop.getSuccess(DataRequestMapping.DataInput.message_success,dataSearchService.getData(jsonData));
        } catch (ApiException e){
            return Envelop.getError(e.getMessage(), e.getErrorCode());
        }
    }
    @PostMapping(value = DataRequestMapping.DataSearch.api_user_abnormal_times_a_week, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "获取居民一周内体征数据异常次数", notes = "血糖或血压体征数据")
    public Envelop getAbnormalTimesAWeek(@ApiParam(name = "json_data", value = "", defaultValue = "") @RequestBody String jsonData){
        try{
            return Envelop.getSuccess(DataRequestMapping.DataInput.message_success,dataSearchService.getData(jsonData));
        } catch (ApiException e){
            return Envelop.getError(e.getMessage(), e.getErrorCode());
        }
    }
    @PostMapping(value = DataRequestMapping.DataSearch.api_user_abnormal_times_a_week, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "查询体征数据", notes = "根据居民code和删除标识获取最近一次体征数据")
    public Envelop getOneByCodeAndDel(@ApiParam(name = "userCode", value = "", defaultValue = "") @RequestBody String userCode,
                                      @ApiParam(name = "del", value = "", defaultValue = "") @RequestBody String del) {
        try {
            String jsonData = "{\"userCode\":" + userCode + ",\"del\":" + del + "}";
            return Envelop.getSuccess(DataRequestMapping.DataInput.message_success, dataSearchService.getData(jsonData));
        } catch (ApiException e) {
            return Envelop.getError(e.getMessage(), e.getErrorCode());
        }
    }
    @PostMapping(value = DataRequestMapping.DataSearch.api_user_abnormal_times_a_week, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "查询体征数据", notes = "根据居民code和删除标识获取所有体征数据,时间倒序")
    public Envelop getListByCodeAndDel(
            @ApiParam(name = "userCode", value = "", defaultValue = "") @RequestBody String userCode,
            @ApiParam(name = "del", value = "", defaultValue = "") @RequestBody String del){
        try{
            String jsonData = "{\"userCode\":"+userCode+",\"del\":"+ del +"}";
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            searchSourceBuilder.query(QueryBuilders.matchQuery("user", "kimchy"));
//            searchSourceBuilder.query(JSONObject.parseObject(jsonData).);
            return Envelop.getSuccess(DataRequestMapping.DataInput.message_success,dataSearchService.getData(jsonData));
        } catch (ApiException e){
            return Envelop.getError(e.getMessage(), e.getErrorCode());
        }
    }
}

+ 38 - 44
svr/svr-iot/src/main/java/com/yihu/iot/datainput/service/DataInputService.java

@ -45,9 +45,6 @@ public class DataInputService {
    @Autowired
    private HBaseAdmin hBaseAdmin;
    /**
     * 居民设备注册及绑定
     */
@ -129,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");
@ -146,53 +140,53 @@ 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(ConstantUtils.esIndex, ConstantUtils.esType, jsonObject.toJSONString());
        Map<String, Map<String, String>> family = new HashMap<>();
//        List<Map<String, String>> columnsA = new ArrayList<>();
        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(ConstantUtils.tableName,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");
           }
//            columnsA.add(columnsB);
            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(ConstantUtils.familyB, columnsB);
        //将数据存入es
        elastricSearchHelper.save(ConstantUtils.esIndex, ConstantUtils.esType, jsonObject.toJSONString());
        try {
            boolean tableExists = hBaseAdmin.isTableExists(ConstantUtils.tableName);
            if (!tableExists) {
                hBaseAdmin.createTable(ConstantUtils.tableName,ConstantUtils.familyA,ConstantUtils.familyB);
                hBaseAdmin.createTable(ConstantUtils.tableName,ConstantUtils.familyB);
            }
            hBaseHelper.add(ConstantUtils.tableName, rowkey, family);
            hBaseHelper.addBulk(ConstantUtils.tableName, rowkeyList, familyList);
        } catch (Exception e) {
            e.printStackTrace();
            //保存日志

+ 2 - 2
svr/svr-iot/src/main/java/com/yihu/iot/datainput/service/DataSearchService.java

@ -24,12 +24,12 @@ public class DataSearchService {
     * @param json
     * @return
     */
  /*  public String getData(String json){
    public String getData(String json){
        String result = elastricSearchHelper.search(ConstantUtils.esIndex,ConstantUtils.esType,json);
        if(null == result){
            return "no data";
        }
        return result;
    }*/
    }
}

+ 88 - 5
svr/svr-iot/src/main/java/com/yihu/iot/datainput/util/RowKeyUtils.java

@ -1,17 +1,100 @@
package com.yihu.iot.datainput.util;
import org.apache.commons.lang3.StringUtils;
import org.bouncycastle.jcajce.provider.symmetric.DES;
import org.springframework.util.Base64Utils;
import org.springframework.util.DigestUtils;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.io.UnsupportedEncodingException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;
public class RowKeyUtils {
    public static String makeRowKey(String accessToken,String deviceSn,String extCode,long measureTime) throws NoSuchAlgorithmException, UnsupportedEncodingException {
    private static EncryptUtil encryptUtil = EncryptUtil.getInstance();
    public static String makeRowKey(String accessToken,String deviceSn,String extCode,long measureTime) throws Exception {
        StringBuilder sb = new StringBuilder();
        sb.append(accessToken+"_");
        sb.append(deviceSn+"_");
        sb.append(extCode+"_");
        sb.append(accessToken+",");
        sb.append(deviceSn+",");
        sb.append(extCode+",");
        sb.append(measureTime);
        return DigestUtils.md5DigestAsHex(sb.toString().getBytes("utf-8"));
        return encryptUtil.encode(sb.toString());
    }
    /**
     * 将rowkey里的信息还原回去
     */
    public static String getMessageFromRowKey(String rowkey) throws Exception {
        return encryptUtil.decode(rowkey);
    }
    static class EncryptUtil {
        private final byte[] DESIV = new byte[] { 0x12, 0x34, 0x56, 120, (byte) 0x90, (byte) 0xab, (byte) 0xcd, (byte) 0xef };// 向量
        private AlgorithmParameterSpec iv = null;// 加密算法的参数接口
        private Key key = null;
        private static String charset = "utf-8";
        private static String deskey = "9ba45bfd500642328ec03ad8ef1b6e75";// 自定义密钥
        private static EncryptUtil encryptUtils = null;
        public static synchronized EncryptUtil getInstance()  {
            try {
                if(null == encryptUtils){
                    encryptUtils = new EncryptUtil(deskey,charset);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            return encryptUtils;
        }
        /**
         * 初始化
         * @param deSkey    密钥
         * @throws Exception
         */
        private EncryptUtil(String deSkey, String charset) throws Exception {
            if (StringUtils.isNotBlank(charset)) {
                this.charset = charset;
            }
            DESKeySpec keySpec = new DESKeySpec(deSkey.getBytes(this.charset));// 设置密钥参数
            iv = new IvParameterSpec(DESIV);// 设置向量
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");// 获得密钥工厂
            key = keyFactory.generateSecret(keySpec);// 得到密钥对象
        }
        /**
         * 加密
         */
        public String encode(String data) throws Exception {
            Cipher enCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");// 得到加密对象Cipher
            enCipher.init(Cipher.ENCRYPT_MODE, key, iv);// 设置工作模式为加密模式,给出密钥和向量
            byte[] pasByte = enCipher.doFinal(data.getBytes("utf-8"));
            BASE64Encoder base64Encoder = new BASE64Encoder();
            return base64Encoder.encode(pasByte);
        }
        /**
         * 解密
         */
        public String decode(String data) throws Exception {
            Cipher deCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            deCipher.init(Cipher.DECRYPT_MODE, key, iv);
            BASE64Decoder base64Decoder = new BASE64Decoder();
            byte[] pasByte = deCipher.doFinal(base64Decoder.decodeBuffer(data));
            return new String(pasByte, "UTF-8");
        }
    }
}