diff --git a/casic-common/pom.xml b/casic-common/pom.xml index 27e6a55..a47c616 100644 --- a/casic-common/pom.xml +++ b/casic-common/pom.xml @@ -46,6 +46,11 @@ mina-core 2.0.4 + + com.casic + casic-data-es + 1.0.1.alpha + diff --git a/casic-common/pom.xml b/casic-common/pom.xml index 27e6a55..a47c616 100644 --- a/casic-common/pom.xml +++ b/casic-common/pom.xml @@ -46,6 +46,11 @@ mina-core 2.0.4 + + com.casic + casic-data-es + 1.0.1.alpha + diff --git a/casic-common/src/main/java/com/casic/missiles/es/DataGasEs.java b/casic-common/src/main/java/com/casic/missiles/es/DataGasEs.java new file mode 100644 index 0000000..3b1f3fe --- /dev/null +++ b/casic-common/src/main/java/com/casic/missiles/es/DataGasEs.java @@ -0,0 +1,18 @@ +package com.casic.missiles.es; + +import lombok.Data; + +@Data +public class DataGasEs { + + private Long stationId; + private String stationName; + private Long monitorId; + private String monitorName; + private String devcode; + private Double direction; + private Double pitch; + private Double concentration; + private Double threshold; + private String logTime; +} diff --git a/casic-common/pom.xml b/casic-common/pom.xml index 27e6a55..a47c616 100644 --- a/casic-common/pom.xml +++ b/casic-common/pom.xml @@ -46,6 +46,11 @@ mina-core 2.0.4 + + com.casic + casic-data-es + 1.0.1.alpha + diff --git a/casic-common/src/main/java/com/casic/missiles/es/DataGasEs.java b/casic-common/src/main/java/com/casic/missiles/es/DataGasEs.java new file mode 100644 index 0000000..3b1f3fe --- /dev/null +++ b/casic-common/src/main/java/com/casic/missiles/es/DataGasEs.java @@ -0,0 +1,18 @@ +package com.casic.missiles.es; + +import lombok.Data; + +@Data +public class DataGasEs { + + private Long stationId; + private String stationName; + private Long monitorId; + private String monitorName; + private String devcode; + private Double direction; + private Double pitch; + private Double concentration; + private Double threshold; + private String logTime; +} diff --git a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java new file mode 100644 index 0000000..aeef63d --- /dev/null +++ b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java @@ -0,0 +1,257 @@ +package com.casic.missiles.es; + +import cn.hutool.core.date.DateUtil; +import cn.hutool.core.util.ObjectUtil; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +/** + * ES查询请求体 + * + * @author zhangyingjie123 + * @since 2020-11-30 + */ +public class ElasticSearchQuery { + private JSONObject body; + private JSONObject query; + private JSONObject bool; + private JSONArray boolMust; + private JSONObject filter; + private JSONObject filterBool; + private JSONArray filterBoolMust; + private JSONArray filterBoolMustnot; + + public static final String MUST_FLAG = "1"; + public static final String MUST_NOT_FLAG = "0"; + + public static final String QUERY = "query"; + public static final String BOOL = "bool"; + public static final String MUST = "must"; + public static final String MUST_NOT = "must_not"; + public static final String FILTER = "filter"; + + + /** + * 生成查询请求体基础结构: + * { + * "query": { + * "bool": { + * "must" [], + * "filter": { + * "bool": { + * "must": [], + * "must_not": [] + * } + * } + * } + * } + * } + */ + public ElasticSearchQuery() { + this.body = new JSONObject(); + this.query = this.query(); + this.bool = this.bool(); + this.boolMust = this.boolMust(); + this.filter = this.filter(); + this.filterBool = this.filterBool(); + this.filterBoolMust = this.filterBoolMust(); + this.filterBoolMustnot = this.filterBoolMustnot(); + } + + public ElasticSearchQuery(JSONObject body){ + this.body = body; + } + + public JSONObject getBody() { + return body; + } + + private JSONObject query(){ + if(ObjectUtil.isEmpty(this.body.get(QUERY))){ + this.body.put("query",new JSONObject()); + } + return this.body.getJSONObject(QUERY); + + } + + private JSONObject bool() { + JSONObject query = this.query; + if(ObjectUtil.isEmpty(query.get(BOOL))){ + query.put(BOOL,new JSONObject()); + } + return query.getJSONObject(BOOL); + } + + private JSONArray boolMust(){ + JSONObject bool = this.bool; + if(ObjectUtil.isEmpty(bool.get(MUST))){ + bool.put(MUST,new JSONArray()); + } + return bool.getJSONArray(MUST); + } + + private JSONObject filter(){ + JSONObject bool = this.bool; + if(ObjectUtil.isEmpty(bool.get(FILTER))){ + bool.put(FILTER,new JSONObject()); + } + return bool.getJSONObject(FILTER); + } + + private JSONObject filterBool(){ + JSONObject filter = this.filter; + if(ObjectUtil.isEmpty(filter.get(BOOL))){ + filter.put(BOOL,new JSONObject()); + } + return filter.getJSONObject(BOOL); + } + + private JSONArray filterBoolMust(){ + JSONObject bool = this.filterBool; + if(ObjectUtil.isEmpty(bool.get(MUST))){ + bool.put(MUST,new JSONArray()); + } + return bool.getJSONArray(MUST); + } + + private JSONArray filterBoolMustnot(){ + JSONObject bool = this.filterBool; + if(ObjectUtil.isEmpty(bool.get(MUST_NOT))){ + bool.put(MUST_NOT,new JSONArray()); + } + return bool.getJSONArray(MUST_NOT); + } + + public void match(String filed, Serializable value){ + JSONArray boolMust = this.boolMust; + JSONObject match = new JSONObject(); + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,value); + match.put("match",filedJson); + boolMust.add(match); + } + + public void matchPhrase(String filed, Serializable value){ + JSONArray boolMust = this.boolMust; + JSONObject matchPhrase = new JSONObject(); + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,value); + matchPhrase.put("match_phrase",filedJson); + boolMust.add(matchPhrase); + } + + public void term(String filed, Serializable value){ + this.term(filed,value,MUST_FLAG); + } + + public void term(String filed, Serializable value,String mustFlag){ + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,value); + JSONObject term = new JSONObject(); + term.put("term",filedJson); + if(MUST_FLAG.equals(mustFlag)){ + this.filterBoolMust.add(term); + } + if(MUST_NOT_FLAG.equals(mustFlag)){ + this.filterBoolMustnot.add(term); + } + } + + public void terms(String filed,List value){ + this.terms(filed,value,MUST_FLAG); + } + + public void terms(String filed, List value, String mustFlag){ + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,value); + JSONObject terms = new JSONObject(); + terms.put("terms",filedJson); + if(MUST_FLAG.equals(mustFlag)){ + this.filterBoolMust.add(terms); + } + if(MUST_NOT_FLAG.equals(mustFlag)){ + this.filterBoolMustnot.add(terms); + } + } + + public void range(String filed, Serializable from, Serializable to){ + this.range(filed,from,to,MUST_FLAG); + } + + public void range(String filed, Serializable from, Serializable to, String mustFlag){ + JSONObject rangeJson = new JSONObject(); + if(ObjectUtil.isNotEmpty(from)){ + rangeJson.put("from",from); + } + if(ObjectUtil.isNotEmpty(to)){ + rangeJson.put("to",to); + } + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,rangeJson); + JSONObject range = new JSONObject(); + range.put("range",filedJson); + if(MUST_FLAG.equals(mustFlag)){ + this.filterBoolMust.add(range); + } + if(MUST_NOT_FLAG.equals(mustFlag)){ + this.filterBoolMustnot.add(range); + } + } + + public void size(int size){ + this.body.put("size",size); + } + + public void from(int from) { + this.body.put("from",from); + } + + public void sort(String sort, String order){ + JSONObject sortJson = new JSONObject(); + JSONObject orderJson = new JSONObject(); + orderJson.put("order",order); + sortJson.put(sort,orderJson); + this.body.put("sort",sortJson); + } + + public void aggs(String aggsName, String filed){ + JSONObject terms = new JSONObject(); + terms.put("field",filed); + JSONObject aggsTerms = new JSONObject(); + aggsTerms.put("terms",terms); + JSONObject aggsBody = new JSONObject(); + aggsBody.put(aggsName,aggsTerms); + this.body.put("aggs",aggsBody); + } + + public void aggs(JSONObject aggBody){ + this.body.put("aggs",aggBody); + } + + + + public static void main(String[] args) { + ElasticSearchQuery query = new ElasticSearchQuery(); + query.matchPhrase("name","张三"); + query.term("sex","2"); + List deptIds = new ArrayList<>(); + deptIds.add(0L); + deptIds.add(24L); + query.terms("deptId", deptIds); + Date date1 = DateUtil.parse("2020-01-01","yyyy-MM-dd"); + Date date2 = DateUtil.parse("2020-04-01","yyyy-MM-dd"); + query.range("collTime",date1.getTime(),date2.getTime()); + query.term("collState","3",MUST_NOT_FLAG); + query.size(10); + query.from(0); + query.sort("irId","asc"); + System.out.println(query.getBody().toJSONString()); + } + + +} diff --git a/casic-common/pom.xml b/casic-common/pom.xml index 27e6a55..a47c616 100644 --- a/casic-common/pom.xml +++ b/casic-common/pom.xml @@ -46,6 +46,11 @@ mina-core 2.0.4 + + com.casic + casic-data-es + 1.0.1.alpha + diff --git a/casic-common/src/main/java/com/casic/missiles/es/DataGasEs.java b/casic-common/src/main/java/com/casic/missiles/es/DataGasEs.java new file mode 100644 index 0000000..3b1f3fe --- /dev/null +++ b/casic-common/src/main/java/com/casic/missiles/es/DataGasEs.java @@ -0,0 +1,18 @@ +package com.casic.missiles.es; + +import lombok.Data; + +@Data +public class DataGasEs { + + private Long stationId; + private String stationName; + private Long monitorId; + private String monitorName; + private String devcode; + private Double direction; + private Double pitch; + private Double concentration; + private Double threshold; + private String logTime; +} diff --git a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java new file mode 100644 index 0000000..aeef63d --- /dev/null +++ b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java @@ -0,0 +1,257 @@ +package com.casic.missiles.es; + +import cn.hutool.core.date.DateUtil; +import cn.hutool.core.util.ObjectUtil; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +/** + * ES查询请求体 + * + * @author zhangyingjie123 + * @since 2020-11-30 + */ +public class ElasticSearchQuery { + private JSONObject body; + private JSONObject query; + private JSONObject bool; + private JSONArray boolMust; + private JSONObject filter; + private JSONObject filterBool; + private JSONArray filterBoolMust; + private JSONArray filterBoolMustnot; + + public static final String MUST_FLAG = "1"; + public static final String MUST_NOT_FLAG = "0"; + + public static final String QUERY = "query"; + public static final String BOOL = "bool"; + public static final String MUST = "must"; + public static final String MUST_NOT = "must_not"; + public static final String FILTER = "filter"; + + + /** + * 生成查询请求体基础结构: + * { + * "query": { + * "bool": { + * "must" [], + * "filter": { + * "bool": { + * "must": [], + * "must_not": [] + * } + * } + * } + * } + * } + */ + public ElasticSearchQuery() { + this.body = new JSONObject(); + this.query = this.query(); + this.bool = this.bool(); + this.boolMust = this.boolMust(); + this.filter = this.filter(); + this.filterBool = this.filterBool(); + this.filterBoolMust = this.filterBoolMust(); + this.filterBoolMustnot = this.filterBoolMustnot(); + } + + public ElasticSearchQuery(JSONObject body){ + this.body = body; + } + + public JSONObject getBody() { + return body; + } + + private JSONObject query(){ + if(ObjectUtil.isEmpty(this.body.get(QUERY))){ + this.body.put("query",new JSONObject()); + } + return this.body.getJSONObject(QUERY); + + } + + private JSONObject bool() { + JSONObject query = this.query; + if(ObjectUtil.isEmpty(query.get(BOOL))){ + query.put(BOOL,new JSONObject()); + } + return query.getJSONObject(BOOL); + } + + private JSONArray boolMust(){ + JSONObject bool = this.bool; + if(ObjectUtil.isEmpty(bool.get(MUST))){ + bool.put(MUST,new JSONArray()); + } + return bool.getJSONArray(MUST); + } + + private JSONObject filter(){ + JSONObject bool = this.bool; + if(ObjectUtil.isEmpty(bool.get(FILTER))){ + bool.put(FILTER,new JSONObject()); + } + return bool.getJSONObject(FILTER); + } + + private JSONObject filterBool(){ + JSONObject filter = this.filter; + if(ObjectUtil.isEmpty(filter.get(BOOL))){ + filter.put(BOOL,new JSONObject()); + } + return filter.getJSONObject(BOOL); + } + + private JSONArray filterBoolMust(){ + JSONObject bool = this.filterBool; + if(ObjectUtil.isEmpty(bool.get(MUST))){ + bool.put(MUST,new JSONArray()); + } + return bool.getJSONArray(MUST); + } + + private JSONArray filterBoolMustnot(){ + JSONObject bool = this.filterBool; + if(ObjectUtil.isEmpty(bool.get(MUST_NOT))){ + bool.put(MUST_NOT,new JSONArray()); + } + return bool.getJSONArray(MUST_NOT); + } + + public void match(String filed, Serializable value){ + JSONArray boolMust = this.boolMust; + JSONObject match = new JSONObject(); + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,value); + match.put("match",filedJson); + boolMust.add(match); + } + + public void matchPhrase(String filed, Serializable value){ + JSONArray boolMust = this.boolMust; + JSONObject matchPhrase = new JSONObject(); + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,value); + matchPhrase.put("match_phrase",filedJson); + boolMust.add(matchPhrase); + } + + public void term(String filed, Serializable value){ + this.term(filed,value,MUST_FLAG); + } + + public void term(String filed, Serializable value,String mustFlag){ + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,value); + JSONObject term = new JSONObject(); + term.put("term",filedJson); + if(MUST_FLAG.equals(mustFlag)){ + this.filterBoolMust.add(term); + } + if(MUST_NOT_FLAG.equals(mustFlag)){ + this.filterBoolMustnot.add(term); + } + } + + public void terms(String filed,List value){ + this.terms(filed,value,MUST_FLAG); + } + + public void terms(String filed, List value, String mustFlag){ + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,value); + JSONObject terms = new JSONObject(); + terms.put("terms",filedJson); + if(MUST_FLAG.equals(mustFlag)){ + this.filterBoolMust.add(terms); + } + if(MUST_NOT_FLAG.equals(mustFlag)){ + this.filterBoolMustnot.add(terms); + } + } + + public void range(String filed, Serializable from, Serializable to){ + this.range(filed,from,to,MUST_FLAG); + } + + public void range(String filed, Serializable from, Serializable to, String mustFlag){ + JSONObject rangeJson = new JSONObject(); + if(ObjectUtil.isNotEmpty(from)){ + rangeJson.put("from",from); + } + if(ObjectUtil.isNotEmpty(to)){ + rangeJson.put("to",to); + } + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,rangeJson); + JSONObject range = new JSONObject(); + range.put("range",filedJson); + if(MUST_FLAG.equals(mustFlag)){ + this.filterBoolMust.add(range); + } + if(MUST_NOT_FLAG.equals(mustFlag)){ + this.filterBoolMustnot.add(range); + } + } + + public void size(int size){ + this.body.put("size",size); + } + + public void from(int from) { + this.body.put("from",from); + } + + public void sort(String sort, String order){ + JSONObject sortJson = new JSONObject(); + JSONObject orderJson = new JSONObject(); + orderJson.put("order",order); + sortJson.put(sort,orderJson); + this.body.put("sort",sortJson); + } + + public void aggs(String aggsName, String filed){ + JSONObject terms = new JSONObject(); + terms.put("field",filed); + JSONObject aggsTerms = new JSONObject(); + aggsTerms.put("terms",terms); + JSONObject aggsBody = new JSONObject(); + aggsBody.put(aggsName,aggsTerms); + this.body.put("aggs",aggsBody); + } + + public void aggs(JSONObject aggBody){ + this.body.put("aggs",aggBody); + } + + + + public static void main(String[] args) { + ElasticSearchQuery query = new ElasticSearchQuery(); + query.matchPhrase("name","张三"); + query.term("sex","2"); + List deptIds = new ArrayList<>(); + deptIds.add(0L); + deptIds.add(24L); + query.terms("deptId", deptIds); + Date date1 = DateUtil.parse("2020-01-01","yyyy-MM-dd"); + Date date2 = DateUtil.parse("2020-04-01","yyyy-MM-dd"); + query.range("collTime",date1.getTime(),date2.getTime()); + query.term("collState","3",MUST_NOT_FLAG); + query.size(10); + query.from(0); + query.sort("irId","asc"); + System.out.println(query.getBody().toJSONString()); + } + + +} diff --git a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java new file mode 100644 index 0000000..dc9a968 --- /dev/null +++ b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java @@ -0,0 +1,172 @@ +package com.casic.missiles.es; + +import cn.hutool.core.util.ObjectUtil; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.casic.missiles.core.es.EsResponse; +import org.apache.http.nio.entity.NStringEntity; +import org.apache.http.util.EntityUtils; +import org.elasticsearch.client.Request; +import org.elasticsearch.client.RequestOptions; +import org.elasticsearch.client.Response; +import org.elasticsearch.client.RestClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Base64; +import java.util.List; + +/** + * ES工具类 + * + * @author zhangyingjie123 + * @since 2020-11-30 + */ +@Component +public class ElasticSearchUtil { + private static final Logger logger = LoggerFactory.getLogger(ElasticSearchUtil.class); + + @Autowired + private RestClient clientMapping; + @Autowired + private Environment env; + + private static String username; + private static String password; + + private static RestClient client; + + @PostConstruct + public void init(){ + client = clientMapping; + username = env.getProperty("casic.data.es.username"); + password = env.getProperty("casic.data.es.password"); + } + + public static JSONObject selectDocumentById(String index, String type, String id) throws IOException{ + String method = "GET"; + String endPoint = index.concat("/").concat(type).concat("/").concat(id); + Response response = performRequest(method,endPoint,null); + JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); + return result.getJSONObject("_source"); + } + + public static Response addDocumentById(String index, String type, String id, Object entity) throws IOException{ + String method = "POST"; + String endPoint = index.concat("/").concat(type).concat("/"); + if(ObjectUtil.isNotEmpty(id)){ + endPoint.concat(id); + } + endPoint.concat("?refresh=true"); + logger.debug(entity.toString()); + return performRequest(method,endPoint,JSON.toJSONString(entity)); + } + + public static Response deleteDocumentById(String index, String type, String id) throws IOException{ + String method = "DELETE"; + String endPoint = index.concat("/").concat(type).concat("/").concat(id).concat("?refresh=true"); + return performRequest(method,endPoint,null); + } + + public static Response updateDocumentById(String index, String type, String id, Object entity) throws IOException{ + String method = "POST"; + String endPoint = index.concat("/").concat(type).concat("/").concat(id).concat("?refresh=true"); + logger.debug(entity.toString()); + return performRequest(method,endPoint,JSON.toJSONString(entity)); + } + + public static EsResponse searchQuery(String index, String type, ElasticSearchQuery query) throws IOException{ + String method = "POST"; + String entPoint = index.concat("/").concat(type).concat("/").concat("_search"); + logger.debug(query.getBody().toString()); + Response response = performRequest(method, entPoint, query.getBody().toString()); + // 获取response body,转换为json对象 + JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); + return result.toJavaObject(EsResponse.class); + } + + public static Response aggsQuery(String index, String type, ElasticSearchQuery query) throws IOException{ + String method = "POST"; + String entPoint = index.concat("/").concat(type).concat("/").concat("_search"); + logger.debug(query.getBody().toString()); + Response response = performRequest(method, entPoint, query.getBody().toString()); + return response; + } + + public static List searchQueryScroll(String index, String type, ElasticSearchQuery query) throws IOException{ + // 首次查询,提交查询条件,endpoint增加‘?scroll=1m’ + List results = new ArrayList<>(); + String method = "POST"; + String entPoint = index.concat("/").concat(type).concat("/").concat("_search").concat("?scroll=1m"); + logger.debug(query.getBody().toString()); + Response response = performRequest(method, entPoint, query.getBody().toString()); + JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); + EsResponse esResponse = result.toJavaObject(EsResponse.class); + String scrollId = result.getString("_scroll_id"); + if(ObjectUtil.isNotEmpty(esResponse.getDatas())){ + results.addAll(esResponse.getDatas()); + } + // 循环发送scroll请求,直到返回结果为空 + entPoint = "_search/scroll"; + JSONObject scrollBody = new JSONObject(); + scrollBody.put("scroll","1m"); + scrollBody.put("scroll_id",scrollId); + while (esResponse.getDatas().size() > 0){ + method = "GET"; + response = performRequest(method, entPoint, scrollBody.toString()); + result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); + esResponse = result.toJavaObject(EsResponse.class); + if(ObjectUtil.isNotEmpty(esResponse.getDatas())){ + results.addAll(esResponse.getDatas()); + } + } + // 查询完成后,及时删除scroll,释放资源 + scrollBody.remove("scroll"); + method = "DELETE"; + performRequest(method, entPoint, scrollBody.toString()); + return results; + } + + public static Response bulk(String index,String type,String entity) throws IOException{ + String method = "POST"; + StringBuilder endPoint = new StringBuilder(); + if(ObjectUtil.isNotEmpty(index)){ + endPoint.append(index).append("/"); + } + if(ObjectUtil.isNotEmpty(type)){ + endPoint.append(type).append("/"); + } + endPoint.append("_bulk"); + endPoint.append("?refresh=true"); + logger.debug(entity); + return performRequest(method,endPoint.toString(),entity); + } + + private static Response performRequest(String method, String endpoint, String entity) throws IOException { + //logger.info(method + " " + endpoint); + Request request = new Request(method,endpoint); + if(ObjectUtil.isNotEmpty(entity)){ + request.setEntity(new NStringEntity(entity,"utf-8")); + } + + if(ObjectUtil.isNotEmpty(username) && ObjectUtil.isNotEmpty(password)){ + RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder(); + String token = username.concat(":").concat(password); + String base64encode = Base64.getEncoder().encodeToString(token.getBytes("utf-8")); + builder.addHeader("Authorization", "Basic " + base64encode ); + request.setOptions(builder.build()); + } + + + Response response = client.performRequest(request); + logger.debug(response.toString()); + return response; + } + +} diff --git a/casic-common/pom.xml b/casic-common/pom.xml index 27e6a55..a47c616 100644 --- a/casic-common/pom.xml +++ b/casic-common/pom.xml @@ -46,6 +46,11 @@ mina-core 2.0.4 + + com.casic + casic-data-es + 1.0.1.alpha + diff --git a/casic-common/src/main/java/com/casic/missiles/es/DataGasEs.java b/casic-common/src/main/java/com/casic/missiles/es/DataGasEs.java new file mode 100644 index 0000000..3b1f3fe --- /dev/null +++ b/casic-common/src/main/java/com/casic/missiles/es/DataGasEs.java @@ -0,0 +1,18 @@ +package com.casic.missiles.es; + +import lombok.Data; + +@Data +public class DataGasEs { + + private Long stationId; + private String stationName; + private Long monitorId; + private String monitorName; + private String devcode; + private Double direction; + private Double pitch; + private Double concentration; + private Double threshold; + private String logTime; +} diff --git a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java new file mode 100644 index 0000000..aeef63d --- /dev/null +++ b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java @@ -0,0 +1,257 @@ +package com.casic.missiles.es; + +import cn.hutool.core.date.DateUtil; +import cn.hutool.core.util.ObjectUtil; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +/** + * ES查询请求体 + * + * @author zhangyingjie123 + * @since 2020-11-30 + */ +public class ElasticSearchQuery { + private JSONObject body; + private JSONObject query; + private JSONObject bool; + private JSONArray boolMust; + private JSONObject filter; + private JSONObject filterBool; + private JSONArray filterBoolMust; + private JSONArray filterBoolMustnot; + + public static final String MUST_FLAG = "1"; + public static final String MUST_NOT_FLAG = "0"; + + public static final String QUERY = "query"; + public static final String BOOL = "bool"; + public static final String MUST = "must"; + public static final String MUST_NOT = "must_not"; + public static final String FILTER = "filter"; + + + /** + * 生成查询请求体基础结构: + * { + * "query": { + * "bool": { + * "must" [], + * "filter": { + * "bool": { + * "must": [], + * "must_not": [] + * } + * } + * } + * } + * } + */ + public ElasticSearchQuery() { + this.body = new JSONObject(); + this.query = this.query(); + this.bool = this.bool(); + this.boolMust = this.boolMust(); + this.filter = this.filter(); + this.filterBool = this.filterBool(); + this.filterBoolMust = this.filterBoolMust(); + this.filterBoolMustnot = this.filterBoolMustnot(); + } + + public ElasticSearchQuery(JSONObject body){ + this.body = body; + } + + public JSONObject getBody() { + return body; + } + + private JSONObject query(){ + if(ObjectUtil.isEmpty(this.body.get(QUERY))){ + this.body.put("query",new JSONObject()); + } + return this.body.getJSONObject(QUERY); + + } + + private JSONObject bool() { + JSONObject query = this.query; + if(ObjectUtil.isEmpty(query.get(BOOL))){ + query.put(BOOL,new JSONObject()); + } + return query.getJSONObject(BOOL); + } + + private JSONArray boolMust(){ + JSONObject bool = this.bool; + if(ObjectUtil.isEmpty(bool.get(MUST))){ + bool.put(MUST,new JSONArray()); + } + return bool.getJSONArray(MUST); + } + + private JSONObject filter(){ + JSONObject bool = this.bool; + if(ObjectUtil.isEmpty(bool.get(FILTER))){ + bool.put(FILTER,new JSONObject()); + } + return bool.getJSONObject(FILTER); + } + + private JSONObject filterBool(){ + JSONObject filter = this.filter; + if(ObjectUtil.isEmpty(filter.get(BOOL))){ + filter.put(BOOL,new JSONObject()); + } + return filter.getJSONObject(BOOL); + } + + private JSONArray filterBoolMust(){ + JSONObject bool = this.filterBool; + if(ObjectUtil.isEmpty(bool.get(MUST))){ + bool.put(MUST,new JSONArray()); + } + return bool.getJSONArray(MUST); + } + + private JSONArray filterBoolMustnot(){ + JSONObject bool = this.filterBool; + if(ObjectUtil.isEmpty(bool.get(MUST_NOT))){ + bool.put(MUST_NOT,new JSONArray()); + } + return bool.getJSONArray(MUST_NOT); + } + + public void match(String filed, Serializable value){ + JSONArray boolMust = this.boolMust; + JSONObject match = new JSONObject(); + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,value); + match.put("match",filedJson); + boolMust.add(match); + } + + public void matchPhrase(String filed, Serializable value){ + JSONArray boolMust = this.boolMust; + JSONObject matchPhrase = new JSONObject(); + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,value); + matchPhrase.put("match_phrase",filedJson); + boolMust.add(matchPhrase); + } + + public void term(String filed, Serializable value){ + this.term(filed,value,MUST_FLAG); + } + + public void term(String filed, Serializable value,String mustFlag){ + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,value); + JSONObject term = new JSONObject(); + term.put("term",filedJson); + if(MUST_FLAG.equals(mustFlag)){ + this.filterBoolMust.add(term); + } + if(MUST_NOT_FLAG.equals(mustFlag)){ + this.filterBoolMustnot.add(term); + } + } + + public void terms(String filed,List value){ + this.terms(filed,value,MUST_FLAG); + } + + public void terms(String filed, List value, String mustFlag){ + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,value); + JSONObject terms = new JSONObject(); + terms.put("terms",filedJson); + if(MUST_FLAG.equals(mustFlag)){ + this.filterBoolMust.add(terms); + } + if(MUST_NOT_FLAG.equals(mustFlag)){ + this.filterBoolMustnot.add(terms); + } + } + + public void range(String filed, Serializable from, Serializable to){ + this.range(filed,from,to,MUST_FLAG); + } + + public void range(String filed, Serializable from, Serializable to, String mustFlag){ + JSONObject rangeJson = new JSONObject(); + if(ObjectUtil.isNotEmpty(from)){ + rangeJson.put("from",from); + } + if(ObjectUtil.isNotEmpty(to)){ + rangeJson.put("to",to); + } + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,rangeJson); + JSONObject range = new JSONObject(); + range.put("range",filedJson); + if(MUST_FLAG.equals(mustFlag)){ + this.filterBoolMust.add(range); + } + if(MUST_NOT_FLAG.equals(mustFlag)){ + this.filterBoolMustnot.add(range); + } + } + + public void size(int size){ + this.body.put("size",size); + } + + public void from(int from) { + this.body.put("from",from); + } + + public void sort(String sort, String order){ + JSONObject sortJson = new JSONObject(); + JSONObject orderJson = new JSONObject(); + orderJson.put("order",order); + sortJson.put(sort,orderJson); + this.body.put("sort",sortJson); + } + + public void aggs(String aggsName, String filed){ + JSONObject terms = new JSONObject(); + terms.put("field",filed); + JSONObject aggsTerms = new JSONObject(); + aggsTerms.put("terms",terms); + JSONObject aggsBody = new JSONObject(); + aggsBody.put(aggsName,aggsTerms); + this.body.put("aggs",aggsBody); + } + + public void aggs(JSONObject aggBody){ + this.body.put("aggs",aggBody); + } + + + + public static void main(String[] args) { + ElasticSearchQuery query = new ElasticSearchQuery(); + query.matchPhrase("name","张三"); + query.term("sex","2"); + List deptIds = new ArrayList<>(); + deptIds.add(0L); + deptIds.add(24L); + query.terms("deptId", deptIds); + Date date1 = DateUtil.parse("2020-01-01","yyyy-MM-dd"); + Date date2 = DateUtil.parse("2020-04-01","yyyy-MM-dd"); + query.range("collTime",date1.getTime(),date2.getTime()); + query.term("collState","3",MUST_NOT_FLAG); + query.size(10); + query.from(0); + query.sort("irId","asc"); + System.out.println(query.getBody().toJSONString()); + } + + +} diff --git a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java new file mode 100644 index 0000000..dc9a968 --- /dev/null +++ b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java @@ -0,0 +1,172 @@ +package com.casic.missiles.es; + +import cn.hutool.core.util.ObjectUtil; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.casic.missiles.core.es.EsResponse; +import org.apache.http.nio.entity.NStringEntity; +import org.apache.http.util.EntityUtils; +import org.elasticsearch.client.Request; +import org.elasticsearch.client.RequestOptions; +import org.elasticsearch.client.Response; +import org.elasticsearch.client.RestClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Base64; +import java.util.List; + +/** + * ES工具类 + * + * @author zhangyingjie123 + * @since 2020-11-30 + */ +@Component +public class ElasticSearchUtil { + private static final Logger logger = LoggerFactory.getLogger(ElasticSearchUtil.class); + + @Autowired + private RestClient clientMapping; + @Autowired + private Environment env; + + private static String username; + private static String password; + + private static RestClient client; + + @PostConstruct + public void init(){ + client = clientMapping; + username = env.getProperty("casic.data.es.username"); + password = env.getProperty("casic.data.es.password"); + } + + public static JSONObject selectDocumentById(String index, String type, String id) throws IOException{ + String method = "GET"; + String endPoint = index.concat("/").concat(type).concat("/").concat(id); + Response response = performRequest(method,endPoint,null); + JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); + return result.getJSONObject("_source"); + } + + public static Response addDocumentById(String index, String type, String id, Object entity) throws IOException{ + String method = "POST"; + String endPoint = index.concat("/").concat(type).concat("/"); + if(ObjectUtil.isNotEmpty(id)){ + endPoint.concat(id); + } + endPoint.concat("?refresh=true"); + logger.debug(entity.toString()); + return performRequest(method,endPoint,JSON.toJSONString(entity)); + } + + public static Response deleteDocumentById(String index, String type, String id) throws IOException{ + String method = "DELETE"; + String endPoint = index.concat("/").concat(type).concat("/").concat(id).concat("?refresh=true"); + return performRequest(method,endPoint,null); + } + + public static Response updateDocumentById(String index, String type, String id, Object entity) throws IOException{ + String method = "POST"; + String endPoint = index.concat("/").concat(type).concat("/").concat(id).concat("?refresh=true"); + logger.debug(entity.toString()); + return performRequest(method,endPoint,JSON.toJSONString(entity)); + } + + public static EsResponse searchQuery(String index, String type, ElasticSearchQuery query) throws IOException{ + String method = "POST"; + String entPoint = index.concat("/").concat(type).concat("/").concat("_search"); + logger.debug(query.getBody().toString()); + Response response = performRequest(method, entPoint, query.getBody().toString()); + // 获取response body,转换为json对象 + JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); + return result.toJavaObject(EsResponse.class); + } + + public static Response aggsQuery(String index, String type, ElasticSearchQuery query) throws IOException{ + String method = "POST"; + String entPoint = index.concat("/").concat(type).concat("/").concat("_search"); + logger.debug(query.getBody().toString()); + Response response = performRequest(method, entPoint, query.getBody().toString()); + return response; + } + + public static List searchQueryScroll(String index, String type, ElasticSearchQuery query) throws IOException{ + // 首次查询,提交查询条件,endpoint增加‘?scroll=1m’ + List results = new ArrayList<>(); + String method = "POST"; + String entPoint = index.concat("/").concat(type).concat("/").concat("_search").concat("?scroll=1m"); + logger.debug(query.getBody().toString()); + Response response = performRequest(method, entPoint, query.getBody().toString()); + JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); + EsResponse esResponse = result.toJavaObject(EsResponse.class); + String scrollId = result.getString("_scroll_id"); + if(ObjectUtil.isNotEmpty(esResponse.getDatas())){ + results.addAll(esResponse.getDatas()); + } + // 循环发送scroll请求,直到返回结果为空 + entPoint = "_search/scroll"; + JSONObject scrollBody = new JSONObject(); + scrollBody.put("scroll","1m"); + scrollBody.put("scroll_id",scrollId); + while (esResponse.getDatas().size() > 0){ + method = "GET"; + response = performRequest(method, entPoint, scrollBody.toString()); + result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); + esResponse = result.toJavaObject(EsResponse.class); + if(ObjectUtil.isNotEmpty(esResponse.getDatas())){ + results.addAll(esResponse.getDatas()); + } + } + // 查询完成后,及时删除scroll,释放资源 + scrollBody.remove("scroll"); + method = "DELETE"; + performRequest(method, entPoint, scrollBody.toString()); + return results; + } + + public static Response bulk(String index,String type,String entity) throws IOException{ + String method = "POST"; + StringBuilder endPoint = new StringBuilder(); + if(ObjectUtil.isNotEmpty(index)){ + endPoint.append(index).append("/"); + } + if(ObjectUtil.isNotEmpty(type)){ + endPoint.append(type).append("/"); + } + endPoint.append("_bulk"); + endPoint.append("?refresh=true"); + logger.debug(entity); + return performRequest(method,endPoint.toString(),entity); + } + + private static Response performRequest(String method, String endpoint, String entity) throws IOException { + //logger.info(method + " " + endpoint); + Request request = new Request(method,endpoint); + if(ObjectUtil.isNotEmpty(entity)){ + request.setEntity(new NStringEntity(entity,"utf-8")); + } + + if(ObjectUtil.isNotEmpty(username) && ObjectUtil.isNotEmpty(password)){ + RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder(); + String token = username.concat(":").concat(password); + String base64encode = Base64.getEncoder().encodeToString(token.getBytes("utf-8")); + builder.addHeader("Authorization", "Basic " + base64encode ); + request.setOptions(builder.build()); + } + + + Response response = client.performRequest(request); + logger.debug(response.toString()); + return response; + } + +} diff --git a/casic-common/src/main/java/com/casic/missiles/weigeng/CommDoorUtil.java b/casic-common/src/main/java/com/casic/missiles/weigeng/CommDoorUtil.java index bed1361..7457109 100644 --- a/casic-common/src/main/java/com/casic/missiles/weigeng/CommDoorUtil.java +++ b/casic-common/src/main/java/com/casic/missiles/weigeng/CommDoorUtil.java @@ -118,15 +118,14 @@ } //重启设备 - public static boolean restart(String monitorId) { + public static boolean restart(String controllerIP, String controllerSN) { try { - String doors = ObjectUtil.isNotEmpty(map.get(monitorId)) ? map.get(monitorId).toString() : ""; - if (ObjectUtil.isNotEmpty(doors)) - if (DoorShortUtil.remoteControl(doors.split(",")[1], Long.valueOf(doors.split(",")[0]), 2, 1)) { + if (ObjectUtil.isAllNotEmpty(controllerIP, controllerSN)) + if (DoorShortUtil.remoteControl(controllerIP, Long.valueOf(controllerSN), 2, 1)) { //重新上电需要间隔20秒 Thread.sleep(Integer.valueOf(map.get("time").toString()) * 1000); //重新关闭开关上电 - return DoorShortUtil.remoteControl(doors.split(",")[1], Long.valueOf(doors.split(",")[0]), 2, 2); + return DoorShortUtil.remoteControl(controllerIP, Long.valueOf(controllerSN), 2, 2); } } catch (Exception e) { e.printStackTrace(); @@ -135,16 +134,14 @@ } //重启设备 - public static boolean interruptStart(String monitorId) { + public static boolean interruptStart(String controllerIP, String controllerSN) { try { - String doors = ObjectUtil.isNotEmpty(map.get(monitorId)) ? map.get(monitorId).toString() : ""; - if (ObjectUtil.isNotEmpty(doors)) - if (DoorShortUtil.remoteControl(doors.split(",")[1], Long.valueOf(doors.split(",")[0]), 2, 1)) { + if (ObjectUtil.isAllNotEmpty(controllerIP,controllerSN)) + if (DoorShortUtil.remoteControl(controllerIP, Long.valueOf(controllerSN), 2, 1)) { //重新上电需要间隔20秒 Thread.sleep(Integer.valueOf(map.get("time").toString()) * 1000); //重新关闭开关上电 - DoorShortUtil.remoteControl(doors.split(",")[1], Long.valueOf(doors.split(",")[0]), 2, 2); - + DoorShortUtil.remoteControl(controllerIP, Long.valueOf(controllerSN), 2, 2); Thread.sleep(2 * 60 * 1000); return true; } diff --git a/casic-common/pom.xml b/casic-common/pom.xml index 27e6a55..a47c616 100644 --- a/casic-common/pom.xml +++ b/casic-common/pom.xml @@ -46,6 +46,11 @@ mina-core 2.0.4 + + com.casic + casic-data-es + 1.0.1.alpha + diff --git a/casic-common/src/main/java/com/casic/missiles/es/DataGasEs.java b/casic-common/src/main/java/com/casic/missiles/es/DataGasEs.java new file mode 100644 index 0000000..3b1f3fe --- /dev/null +++ b/casic-common/src/main/java/com/casic/missiles/es/DataGasEs.java @@ -0,0 +1,18 @@ +package com.casic.missiles.es; + +import lombok.Data; + +@Data +public class DataGasEs { + + private Long stationId; + private String stationName; + private Long monitorId; + private String monitorName; + private String devcode; + private Double direction; + private Double pitch; + private Double concentration; + private Double threshold; + private String logTime; +} diff --git a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java new file mode 100644 index 0000000..aeef63d --- /dev/null +++ b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java @@ -0,0 +1,257 @@ +package com.casic.missiles.es; + +import cn.hutool.core.date.DateUtil; +import cn.hutool.core.util.ObjectUtil; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +/** + * ES查询请求体 + * + * @author zhangyingjie123 + * @since 2020-11-30 + */ +public class ElasticSearchQuery { + private JSONObject body; + private JSONObject query; + private JSONObject bool; + private JSONArray boolMust; + private JSONObject filter; + private JSONObject filterBool; + private JSONArray filterBoolMust; + private JSONArray filterBoolMustnot; + + public static final String MUST_FLAG = "1"; + public static final String MUST_NOT_FLAG = "0"; + + public static final String QUERY = "query"; + public static final String BOOL = "bool"; + public static final String MUST = "must"; + public static final String MUST_NOT = "must_not"; + public static final String FILTER = "filter"; + + + /** + * 生成查询请求体基础结构: + * { + * "query": { + * "bool": { + * "must" [], + * "filter": { + * "bool": { + * "must": [], + * "must_not": [] + * } + * } + * } + * } + * } + */ + public ElasticSearchQuery() { + this.body = new JSONObject(); + this.query = this.query(); + this.bool = this.bool(); + this.boolMust = this.boolMust(); + this.filter = this.filter(); + this.filterBool = this.filterBool(); + this.filterBoolMust = this.filterBoolMust(); + this.filterBoolMustnot = this.filterBoolMustnot(); + } + + public ElasticSearchQuery(JSONObject body){ + this.body = body; + } + + public JSONObject getBody() { + return body; + } + + private JSONObject query(){ + if(ObjectUtil.isEmpty(this.body.get(QUERY))){ + this.body.put("query",new JSONObject()); + } + return this.body.getJSONObject(QUERY); + + } + + private JSONObject bool() { + JSONObject query = this.query; + if(ObjectUtil.isEmpty(query.get(BOOL))){ + query.put(BOOL,new JSONObject()); + } + return query.getJSONObject(BOOL); + } + + private JSONArray boolMust(){ + JSONObject bool = this.bool; + if(ObjectUtil.isEmpty(bool.get(MUST))){ + bool.put(MUST,new JSONArray()); + } + return bool.getJSONArray(MUST); + } + + private JSONObject filter(){ + JSONObject bool = this.bool; + if(ObjectUtil.isEmpty(bool.get(FILTER))){ + bool.put(FILTER,new JSONObject()); + } + return bool.getJSONObject(FILTER); + } + + private JSONObject filterBool(){ + JSONObject filter = this.filter; + if(ObjectUtil.isEmpty(filter.get(BOOL))){ + filter.put(BOOL,new JSONObject()); + } + return filter.getJSONObject(BOOL); + } + + private JSONArray filterBoolMust(){ + JSONObject bool = this.filterBool; + if(ObjectUtil.isEmpty(bool.get(MUST))){ + bool.put(MUST,new JSONArray()); + } + return bool.getJSONArray(MUST); + } + + private JSONArray filterBoolMustnot(){ + JSONObject bool = this.filterBool; + if(ObjectUtil.isEmpty(bool.get(MUST_NOT))){ + bool.put(MUST_NOT,new JSONArray()); + } + return bool.getJSONArray(MUST_NOT); + } + + public void match(String filed, Serializable value){ + JSONArray boolMust = this.boolMust; + JSONObject match = new JSONObject(); + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,value); + match.put("match",filedJson); + boolMust.add(match); + } + + public void matchPhrase(String filed, Serializable value){ + JSONArray boolMust = this.boolMust; + JSONObject matchPhrase = new JSONObject(); + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,value); + matchPhrase.put("match_phrase",filedJson); + boolMust.add(matchPhrase); + } + + public void term(String filed, Serializable value){ + this.term(filed,value,MUST_FLAG); + } + + public void term(String filed, Serializable value,String mustFlag){ + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,value); + JSONObject term = new JSONObject(); + term.put("term",filedJson); + if(MUST_FLAG.equals(mustFlag)){ + this.filterBoolMust.add(term); + } + if(MUST_NOT_FLAG.equals(mustFlag)){ + this.filterBoolMustnot.add(term); + } + } + + public void terms(String filed,List value){ + this.terms(filed,value,MUST_FLAG); + } + + public void terms(String filed, List value, String mustFlag){ + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,value); + JSONObject terms = new JSONObject(); + terms.put("terms",filedJson); + if(MUST_FLAG.equals(mustFlag)){ + this.filterBoolMust.add(terms); + } + if(MUST_NOT_FLAG.equals(mustFlag)){ + this.filterBoolMustnot.add(terms); + } + } + + public void range(String filed, Serializable from, Serializable to){ + this.range(filed,from,to,MUST_FLAG); + } + + public void range(String filed, Serializable from, Serializable to, String mustFlag){ + JSONObject rangeJson = new JSONObject(); + if(ObjectUtil.isNotEmpty(from)){ + rangeJson.put("from",from); + } + if(ObjectUtil.isNotEmpty(to)){ + rangeJson.put("to",to); + } + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,rangeJson); + JSONObject range = new JSONObject(); + range.put("range",filedJson); + if(MUST_FLAG.equals(mustFlag)){ + this.filterBoolMust.add(range); + } + if(MUST_NOT_FLAG.equals(mustFlag)){ + this.filterBoolMustnot.add(range); + } + } + + public void size(int size){ + this.body.put("size",size); + } + + public void from(int from) { + this.body.put("from",from); + } + + public void sort(String sort, String order){ + JSONObject sortJson = new JSONObject(); + JSONObject orderJson = new JSONObject(); + orderJson.put("order",order); + sortJson.put(sort,orderJson); + this.body.put("sort",sortJson); + } + + public void aggs(String aggsName, String filed){ + JSONObject terms = new JSONObject(); + terms.put("field",filed); + JSONObject aggsTerms = new JSONObject(); + aggsTerms.put("terms",terms); + JSONObject aggsBody = new JSONObject(); + aggsBody.put(aggsName,aggsTerms); + this.body.put("aggs",aggsBody); + } + + public void aggs(JSONObject aggBody){ + this.body.put("aggs",aggBody); + } + + + + public static void main(String[] args) { + ElasticSearchQuery query = new ElasticSearchQuery(); + query.matchPhrase("name","张三"); + query.term("sex","2"); + List deptIds = new ArrayList<>(); + deptIds.add(0L); + deptIds.add(24L); + query.terms("deptId", deptIds); + Date date1 = DateUtil.parse("2020-01-01","yyyy-MM-dd"); + Date date2 = DateUtil.parse("2020-04-01","yyyy-MM-dd"); + query.range("collTime",date1.getTime(),date2.getTime()); + query.term("collState","3",MUST_NOT_FLAG); + query.size(10); + query.from(0); + query.sort("irId","asc"); + System.out.println(query.getBody().toJSONString()); + } + + +} diff --git a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java new file mode 100644 index 0000000..dc9a968 --- /dev/null +++ b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java @@ -0,0 +1,172 @@ +package com.casic.missiles.es; + +import cn.hutool.core.util.ObjectUtil; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.casic.missiles.core.es.EsResponse; +import org.apache.http.nio.entity.NStringEntity; +import org.apache.http.util.EntityUtils; +import org.elasticsearch.client.Request; +import org.elasticsearch.client.RequestOptions; +import org.elasticsearch.client.Response; +import org.elasticsearch.client.RestClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Base64; +import java.util.List; + +/** + * ES工具类 + * + * @author zhangyingjie123 + * @since 2020-11-30 + */ +@Component +public class ElasticSearchUtil { + private static final Logger logger = LoggerFactory.getLogger(ElasticSearchUtil.class); + + @Autowired + private RestClient clientMapping; + @Autowired + private Environment env; + + private static String username; + private static String password; + + private static RestClient client; + + @PostConstruct + public void init(){ + client = clientMapping; + username = env.getProperty("casic.data.es.username"); + password = env.getProperty("casic.data.es.password"); + } + + public static JSONObject selectDocumentById(String index, String type, String id) throws IOException{ + String method = "GET"; + String endPoint = index.concat("/").concat(type).concat("/").concat(id); + Response response = performRequest(method,endPoint,null); + JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); + return result.getJSONObject("_source"); + } + + public static Response addDocumentById(String index, String type, String id, Object entity) throws IOException{ + String method = "POST"; + String endPoint = index.concat("/").concat(type).concat("/"); + if(ObjectUtil.isNotEmpty(id)){ + endPoint.concat(id); + } + endPoint.concat("?refresh=true"); + logger.debug(entity.toString()); + return performRequest(method,endPoint,JSON.toJSONString(entity)); + } + + public static Response deleteDocumentById(String index, String type, String id) throws IOException{ + String method = "DELETE"; + String endPoint = index.concat("/").concat(type).concat("/").concat(id).concat("?refresh=true"); + return performRequest(method,endPoint,null); + } + + public static Response updateDocumentById(String index, String type, String id, Object entity) throws IOException{ + String method = "POST"; + String endPoint = index.concat("/").concat(type).concat("/").concat(id).concat("?refresh=true"); + logger.debug(entity.toString()); + return performRequest(method,endPoint,JSON.toJSONString(entity)); + } + + public static EsResponse searchQuery(String index, String type, ElasticSearchQuery query) throws IOException{ + String method = "POST"; + String entPoint = index.concat("/").concat(type).concat("/").concat("_search"); + logger.debug(query.getBody().toString()); + Response response = performRequest(method, entPoint, query.getBody().toString()); + // 获取response body,转换为json对象 + JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); + return result.toJavaObject(EsResponse.class); + } + + public static Response aggsQuery(String index, String type, ElasticSearchQuery query) throws IOException{ + String method = "POST"; + String entPoint = index.concat("/").concat(type).concat("/").concat("_search"); + logger.debug(query.getBody().toString()); + Response response = performRequest(method, entPoint, query.getBody().toString()); + return response; + } + + public static List searchQueryScroll(String index, String type, ElasticSearchQuery query) throws IOException{ + // 首次查询,提交查询条件,endpoint增加‘?scroll=1m’ + List results = new ArrayList<>(); + String method = "POST"; + String entPoint = index.concat("/").concat(type).concat("/").concat("_search").concat("?scroll=1m"); + logger.debug(query.getBody().toString()); + Response response = performRequest(method, entPoint, query.getBody().toString()); + JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); + EsResponse esResponse = result.toJavaObject(EsResponse.class); + String scrollId = result.getString("_scroll_id"); + if(ObjectUtil.isNotEmpty(esResponse.getDatas())){ + results.addAll(esResponse.getDatas()); + } + // 循环发送scroll请求,直到返回结果为空 + entPoint = "_search/scroll"; + JSONObject scrollBody = new JSONObject(); + scrollBody.put("scroll","1m"); + scrollBody.put("scroll_id",scrollId); + while (esResponse.getDatas().size() > 0){ + method = "GET"; + response = performRequest(method, entPoint, scrollBody.toString()); + result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); + esResponse = result.toJavaObject(EsResponse.class); + if(ObjectUtil.isNotEmpty(esResponse.getDatas())){ + results.addAll(esResponse.getDatas()); + } + } + // 查询完成后,及时删除scroll,释放资源 + scrollBody.remove("scroll"); + method = "DELETE"; + performRequest(method, entPoint, scrollBody.toString()); + return results; + } + + public static Response bulk(String index,String type,String entity) throws IOException{ + String method = "POST"; + StringBuilder endPoint = new StringBuilder(); + if(ObjectUtil.isNotEmpty(index)){ + endPoint.append(index).append("/"); + } + if(ObjectUtil.isNotEmpty(type)){ + endPoint.append(type).append("/"); + } + endPoint.append("_bulk"); + endPoint.append("?refresh=true"); + logger.debug(entity); + return performRequest(method,endPoint.toString(),entity); + } + + private static Response performRequest(String method, String endpoint, String entity) throws IOException { + //logger.info(method + " " + endpoint); + Request request = new Request(method,endpoint); + if(ObjectUtil.isNotEmpty(entity)){ + request.setEntity(new NStringEntity(entity,"utf-8")); + } + + if(ObjectUtil.isNotEmpty(username) && ObjectUtil.isNotEmpty(password)){ + RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder(); + String token = username.concat(":").concat(password); + String base64encode = Base64.getEncoder().encodeToString(token.getBytes("utf-8")); + builder.addHeader("Authorization", "Basic " + base64encode ); + request.setOptions(builder.build()); + } + + + Response response = client.performRequest(request); + logger.debug(response.toString()); + return response; + } + +} diff --git a/casic-common/src/main/java/com/casic/missiles/weigeng/CommDoorUtil.java b/casic-common/src/main/java/com/casic/missiles/weigeng/CommDoorUtil.java index bed1361..7457109 100644 --- a/casic-common/src/main/java/com/casic/missiles/weigeng/CommDoorUtil.java +++ b/casic-common/src/main/java/com/casic/missiles/weigeng/CommDoorUtil.java @@ -118,15 +118,14 @@ } //重启设备 - public static boolean restart(String monitorId) { + public static boolean restart(String controllerIP, String controllerSN) { try { - String doors = ObjectUtil.isNotEmpty(map.get(monitorId)) ? map.get(monitorId).toString() : ""; - if (ObjectUtil.isNotEmpty(doors)) - if (DoorShortUtil.remoteControl(doors.split(",")[1], Long.valueOf(doors.split(",")[0]), 2, 1)) { + if (ObjectUtil.isAllNotEmpty(controllerIP, controllerSN)) + if (DoorShortUtil.remoteControl(controllerIP, Long.valueOf(controllerSN), 2, 1)) { //重新上电需要间隔20秒 Thread.sleep(Integer.valueOf(map.get("time").toString()) * 1000); //重新关闭开关上电 - return DoorShortUtil.remoteControl(doors.split(",")[1], Long.valueOf(doors.split(",")[0]), 2, 2); + return DoorShortUtil.remoteControl(controllerIP, Long.valueOf(controllerSN), 2, 2); } } catch (Exception e) { e.printStackTrace(); @@ -135,16 +134,14 @@ } //重启设备 - public static boolean interruptStart(String monitorId) { + public static boolean interruptStart(String controllerIP, String controllerSN) { try { - String doors = ObjectUtil.isNotEmpty(map.get(monitorId)) ? map.get(monitorId).toString() : ""; - if (ObjectUtil.isNotEmpty(doors)) - if (DoorShortUtil.remoteControl(doors.split(",")[1], Long.valueOf(doors.split(",")[0]), 2, 1)) { + if (ObjectUtil.isAllNotEmpty(controllerIP,controllerSN)) + if (DoorShortUtil.remoteControl(controllerIP, Long.valueOf(controllerSN), 2, 1)) { //重新上电需要间隔20秒 Thread.sleep(Integer.valueOf(map.get("time").toString()) * 1000); //重新关闭开关上电 - DoorShortUtil.remoteControl(doors.split(",")[1], Long.valueOf(doors.split(",")[0]), 2, 2); - + DoorShortUtil.remoteControl(controllerIP, Long.valueOf(controllerSN), 2, 2); Thread.sleep(2 * 60 * 1000); return true; } diff --git a/casic-data/pom.xml b/casic-data/pom.xml index aca0a62..a076800 100644 --- a/casic-data/pom.xml +++ b/casic-data/pom.xml @@ -43,11 +43,6 @@ com.casic - casic-data-es - 1.0.1.alpha - - - com.casic casic-common 2.0.0 compile diff --git a/casic-common/pom.xml b/casic-common/pom.xml index 27e6a55..a47c616 100644 --- a/casic-common/pom.xml +++ b/casic-common/pom.xml @@ -46,6 +46,11 @@ mina-core 2.0.4 + + com.casic + casic-data-es + 1.0.1.alpha + diff --git a/casic-common/src/main/java/com/casic/missiles/es/DataGasEs.java b/casic-common/src/main/java/com/casic/missiles/es/DataGasEs.java new file mode 100644 index 0000000..3b1f3fe --- /dev/null +++ b/casic-common/src/main/java/com/casic/missiles/es/DataGasEs.java @@ -0,0 +1,18 @@ +package com.casic.missiles.es; + +import lombok.Data; + +@Data +public class DataGasEs { + + private Long stationId; + private String stationName; + private Long monitorId; + private String monitorName; + private String devcode; + private Double direction; + private Double pitch; + private Double concentration; + private Double threshold; + private String logTime; +} diff --git a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java new file mode 100644 index 0000000..aeef63d --- /dev/null +++ b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java @@ -0,0 +1,257 @@ +package com.casic.missiles.es; + +import cn.hutool.core.date.DateUtil; +import cn.hutool.core.util.ObjectUtil; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +/** + * ES查询请求体 + * + * @author zhangyingjie123 + * @since 2020-11-30 + */ +public class ElasticSearchQuery { + private JSONObject body; + private JSONObject query; + private JSONObject bool; + private JSONArray boolMust; + private JSONObject filter; + private JSONObject filterBool; + private JSONArray filterBoolMust; + private JSONArray filterBoolMustnot; + + public static final String MUST_FLAG = "1"; + public static final String MUST_NOT_FLAG = "0"; + + public static final String QUERY = "query"; + public static final String BOOL = "bool"; + public static final String MUST = "must"; + public static final String MUST_NOT = "must_not"; + public static final String FILTER = "filter"; + + + /** + * 生成查询请求体基础结构: + * { + * "query": { + * "bool": { + * "must" [], + * "filter": { + * "bool": { + * "must": [], + * "must_not": [] + * } + * } + * } + * } + * } + */ + public ElasticSearchQuery() { + this.body = new JSONObject(); + this.query = this.query(); + this.bool = this.bool(); + this.boolMust = this.boolMust(); + this.filter = this.filter(); + this.filterBool = this.filterBool(); + this.filterBoolMust = this.filterBoolMust(); + this.filterBoolMustnot = this.filterBoolMustnot(); + } + + public ElasticSearchQuery(JSONObject body){ + this.body = body; + } + + public JSONObject getBody() { + return body; + } + + private JSONObject query(){ + if(ObjectUtil.isEmpty(this.body.get(QUERY))){ + this.body.put("query",new JSONObject()); + } + return this.body.getJSONObject(QUERY); + + } + + private JSONObject bool() { + JSONObject query = this.query; + if(ObjectUtil.isEmpty(query.get(BOOL))){ + query.put(BOOL,new JSONObject()); + } + return query.getJSONObject(BOOL); + } + + private JSONArray boolMust(){ + JSONObject bool = this.bool; + if(ObjectUtil.isEmpty(bool.get(MUST))){ + bool.put(MUST,new JSONArray()); + } + return bool.getJSONArray(MUST); + } + + private JSONObject filter(){ + JSONObject bool = this.bool; + if(ObjectUtil.isEmpty(bool.get(FILTER))){ + bool.put(FILTER,new JSONObject()); + } + return bool.getJSONObject(FILTER); + } + + private JSONObject filterBool(){ + JSONObject filter = this.filter; + if(ObjectUtil.isEmpty(filter.get(BOOL))){ + filter.put(BOOL,new JSONObject()); + } + return filter.getJSONObject(BOOL); + } + + private JSONArray filterBoolMust(){ + JSONObject bool = this.filterBool; + if(ObjectUtil.isEmpty(bool.get(MUST))){ + bool.put(MUST,new JSONArray()); + } + return bool.getJSONArray(MUST); + } + + private JSONArray filterBoolMustnot(){ + JSONObject bool = this.filterBool; + if(ObjectUtil.isEmpty(bool.get(MUST_NOT))){ + bool.put(MUST_NOT,new JSONArray()); + } + return bool.getJSONArray(MUST_NOT); + } + + public void match(String filed, Serializable value){ + JSONArray boolMust = this.boolMust; + JSONObject match = new JSONObject(); + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,value); + match.put("match",filedJson); + boolMust.add(match); + } + + public void matchPhrase(String filed, Serializable value){ + JSONArray boolMust = this.boolMust; + JSONObject matchPhrase = new JSONObject(); + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,value); + matchPhrase.put("match_phrase",filedJson); + boolMust.add(matchPhrase); + } + + public void term(String filed, Serializable value){ + this.term(filed,value,MUST_FLAG); + } + + public void term(String filed, Serializable value,String mustFlag){ + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,value); + JSONObject term = new JSONObject(); + term.put("term",filedJson); + if(MUST_FLAG.equals(mustFlag)){ + this.filterBoolMust.add(term); + } + if(MUST_NOT_FLAG.equals(mustFlag)){ + this.filterBoolMustnot.add(term); + } + } + + public void terms(String filed,List value){ + this.terms(filed,value,MUST_FLAG); + } + + public void terms(String filed, List value, String mustFlag){ + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,value); + JSONObject terms = new JSONObject(); + terms.put("terms",filedJson); + if(MUST_FLAG.equals(mustFlag)){ + this.filterBoolMust.add(terms); + } + if(MUST_NOT_FLAG.equals(mustFlag)){ + this.filterBoolMustnot.add(terms); + } + } + + public void range(String filed, Serializable from, Serializable to){ + this.range(filed,from,to,MUST_FLAG); + } + + public void range(String filed, Serializable from, Serializable to, String mustFlag){ + JSONObject rangeJson = new JSONObject(); + if(ObjectUtil.isNotEmpty(from)){ + rangeJson.put("from",from); + } + if(ObjectUtil.isNotEmpty(to)){ + rangeJson.put("to",to); + } + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,rangeJson); + JSONObject range = new JSONObject(); + range.put("range",filedJson); + if(MUST_FLAG.equals(mustFlag)){ + this.filterBoolMust.add(range); + } + if(MUST_NOT_FLAG.equals(mustFlag)){ + this.filterBoolMustnot.add(range); + } + } + + public void size(int size){ + this.body.put("size",size); + } + + public void from(int from) { + this.body.put("from",from); + } + + public void sort(String sort, String order){ + JSONObject sortJson = new JSONObject(); + JSONObject orderJson = new JSONObject(); + orderJson.put("order",order); + sortJson.put(sort,orderJson); + this.body.put("sort",sortJson); + } + + public void aggs(String aggsName, String filed){ + JSONObject terms = new JSONObject(); + terms.put("field",filed); + JSONObject aggsTerms = new JSONObject(); + aggsTerms.put("terms",terms); + JSONObject aggsBody = new JSONObject(); + aggsBody.put(aggsName,aggsTerms); + this.body.put("aggs",aggsBody); + } + + public void aggs(JSONObject aggBody){ + this.body.put("aggs",aggBody); + } + + + + public static void main(String[] args) { + ElasticSearchQuery query = new ElasticSearchQuery(); + query.matchPhrase("name","张三"); + query.term("sex","2"); + List deptIds = new ArrayList<>(); + deptIds.add(0L); + deptIds.add(24L); + query.terms("deptId", deptIds); + Date date1 = DateUtil.parse("2020-01-01","yyyy-MM-dd"); + Date date2 = DateUtil.parse("2020-04-01","yyyy-MM-dd"); + query.range("collTime",date1.getTime(),date2.getTime()); + query.term("collState","3",MUST_NOT_FLAG); + query.size(10); + query.from(0); + query.sort("irId","asc"); + System.out.println(query.getBody().toJSONString()); + } + + +} diff --git a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java new file mode 100644 index 0000000..dc9a968 --- /dev/null +++ b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java @@ -0,0 +1,172 @@ +package com.casic.missiles.es; + +import cn.hutool.core.util.ObjectUtil; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.casic.missiles.core.es.EsResponse; +import org.apache.http.nio.entity.NStringEntity; +import org.apache.http.util.EntityUtils; +import org.elasticsearch.client.Request; +import org.elasticsearch.client.RequestOptions; +import org.elasticsearch.client.Response; +import org.elasticsearch.client.RestClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Base64; +import java.util.List; + +/** + * ES工具类 + * + * @author zhangyingjie123 + * @since 2020-11-30 + */ +@Component +public class ElasticSearchUtil { + private static final Logger logger = LoggerFactory.getLogger(ElasticSearchUtil.class); + + @Autowired + private RestClient clientMapping; + @Autowired + private Environment env; + + private static String username; + private static String password; + + private static RestClient client; + + @PostConstruct + public void init(){ + client = clientMapping; + username = env.getProperty("casic.data.es.username"); + password = env.getProperty("casic.data.es.password"); + } + + public static JSONObject selectDocumentById(String index, String type, String id) throws IOException{ + String method = "GET"; + String endPoint = index.concat("/").concat(type).concat("/").concat(id); + Response response = performRequest(method,endPoint,null); + JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); + return result.getJSONObject("_source"); + } + + public static Response addDocumentById(String index, String type, String id, Object entity) throws IOException{ + String method = "POST"; + String endPoint = index.concat("/").concat(type).concat("/"); + if(ObjectUtil.isNotEmpty(id)){ + endPoint.concat(id); + } + endPoint.concat("?refresh=true"); + logger.debug(entity.toString()); + return performRequest(method,endPoint,JSON.toJSONString(entity)); + } + + public static Response deleteDocumentById(String index, String type, String id) throws IOException{ + String method = "DELETE"; + String endPoint = index.concat("/").concat(type).concat("/").concat(id).concat("?refresh=true"); + return performRequest(method,endPoint,null); + } + + public static Response updateDocumentById(String index, String type, String id, Object entity) throws IOException{ + String method = "POST"; + String endPoint = index.concat("/").concat(type).concat("/").concat(id).concat("?refresh=true"); + logger.debug(entity.toString()); + return performRequest(method,endPoint,JSON.toJSONString(entity)); + } + + public static EsResponse searchQuery(String index, String type, ElasticSearchQuery query) throws IOException{ + String method = "POST"; + String entPoint = index.concat("/").concat(type).concat("/").concat("_search"); + logger.debug(query.getBody().toString()); + Response response = performRequest(method, entPoint, query.getBody().toString()); + // 获取response body,转换为json对象 + JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); + return result.toJavaObject(EsResponse.class); + } + + public static Response aggsQuery(String index, String type, ElasticSearchQuery query) throws IOException{ + String method = "POST"; + String entPoint = index.concat("/").concat(type).concat("/").concat("_search"); + logger.debug(query.getBody().toString()); + Response response = performRequest(method, entPoint, query.getBody().toString()); + return response; + } + + public static List searchQueryScroll(String index, String type, ElasticSearchQuery query) throws IOException{ + // 首次查询,提交查询条件,endpoint增加‘?scroll=1m’ + List results = new ArrayList<>(); + String method = "POST"; + String entPoint = index.concat("/").concat(type).concat("/").concat("_search").concat("?scroll=1m"); + logger.debug(query.getBody().toString()); + Response response = performRequest(method, entPoint, query.getBody().toString()); + JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); + EsResponse esResponse = result.toJavaObject(EsResponse.class); + String scrollId = result.getString("_scroll_id"); + if(ObjectUtil.isNotEmpty(esResponse.getDatas())){ + results.addAll(esResponse.getDatas()); + } + // 循环发送scroll请求,直到返回结果为空 + entPoint = "_search/scroll"; + JSONObject scrollBody = new JSONObject(); + scrollBody.put("scroll","1m"); + scrollBody.put("scroll_id",scrollId); + while (esResponse.getDatas().size() > 0){ + method = "GET"; + response = performRequest(method, entPoint, scrollBody.toString()); + result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); + esResponse = result.toJavaObject(EsResponse.class); + if(ObjectUtil.isNotEmpty(esResponse.getDatas())){ + results.addAll(esResponse.getDatas()); + } + } + // 查询完成后,及时删除scroll,释放资源 + scrollBody.remove("scroll"); + method = "DELETE"; + performRequest(method, entPoint, scrollBody.toString()); + return results; + } + + public static Response bulk(String index,String type,String entity) throws IOException{ + String method = "POST"; + StringBuilder endPoint = new StringBuilder(); + if(ObjectUtil.isNotEmpty(index)){ + endPoint.append(index).append("/"); + } + if(ObjectUtil.isNotEmpty(type)){ + endPoint.append(type).append("/"); + } + endPoint.append("_bulk"); + endPoint.append("?refresh=true"); + logger.debug(entity); + return performRequest(method,endPoint.toString(),entity); + } + + private static Response performRequest(String method, String endpoint, String entity) throws IOException { + //logger.info(method + " " + endpoint); + Request request = new Request(method,endpoint); + if(ObjectUtil.isNotEmpty(entity)){ + request.setEntity(new NStringEntity(entity,"utf-8")); + } + + if(ObjectUtil.isNotEmpty(username) && ObjectUtil.isNotEmpty(password)){ + RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder(); + String token = username.concat(":").concat(password); + String base64encode = Base64.getEncoder().encodeToString(token.getBytes("utf-8")); + builder.addHeader("Authorization", "Basic " + base64encode ); + request.setOptions(builder.build()); + } + + + Response response = client.performRequest(request); + logger.debug(response.toString()); + return response; + } + +} diff --git a/casic-common/src/main/java/com/casic/missiles/weigeng/CommDoorUtil.java b/casic-common/src/main/java/com/casic/missiles/weigeng/CommDoorUtil.java index bed1361..7457109 100644 --- a/casic-common/src/main/java/com/casic/missiles/weigeng/CommDoorUtil.java +++ b/casic-common/src/main/java/com/casic/missiles/weigeng/CommDoorUtil.java @@ -118,15 +118,14 @@ } //重启设备 - public static boolean restart(String monitorId) { + public static boolean restart(String controllerIP, String controllerSN) { try { - String doors = ObjectUtil.isNotEmpty(map.get(monitorId)) ? map.get(monitorId).toString() : ""; - if (ObjectUtil.isNotEmpty(doors)) - if (DoorShortUtil.remoteControl(doors.split(",")[1], Long.valueOf(doors.split(",")[0]), 2, 1)) { + if (ObjectUtil.isAllNotEmpty(controllerIP, controllerSN)) + if (DoorShortUtil.remoteControl(controllerIP, Long.valueOf(controllerSN), 2, 1)) { //重新上电需要间隔20秒 Thread.sleep(Integer.valueOf(map.get("time").toString()) * 1000); //重新关闭开关上电 - return DoorShortUtil.remoteControl(doors.split(",")[1], Long.valueOf(doors.split(",")[0]), 2, 2); + return DoorShortUtil.remoteControl(controllerIP, Long.valueOf(controllerSN), 2, 2); } } catch (Exception e) { e.printStackTrace(); @@ -135,16 +134,14 @@ } //重启设备 - public static boolean interruptStart(String monitorId) { + public static boolean interruptStart(String controllerIP, String controllerSN) { try { - String doors = ObjectUtil.isNotEmpty(map.get(monitorId)) ? map.get(monitorId).toString() : ""; - if (ObjectUtil.isNotEmpty(doors)) - if (DoorShortUtil.remoteControl(doors.split(",")[1], Long.valueOf(doors.split(",")[0]), 2, 1)) { + if (ObjectUtil.isAllNotEmpty(controllerIP,controllerSN)) + if (DoorShortUtil.remoteControl(controllerIP, Long.valueOf(controllerSN), 2, 1)) { //重新上电需要间隔20秒 Thread.sleep(Integer.valueOf(map.get("time").toString()) * 1000); //重新关闭开关上电 - DoorShortUtil.remoteControl(doors.split(",")[1], Long.valueOf(doors.split(",")[0]), 2, 2); - + DoorShortUtil.remoteControl(controllerIP, Long.valueOf(controllerSN), 2, 2); Thread.sleep(2 * 60 * 1000); return true; } diff --git a/casic-data/pom.xml b/casic-data/pom.xml index aca0a62..a076800 100644 --- a/casic-data/pom.xml +++ b/casic-data/pom.xml @@ -43,11 +43,6 @@ com.casic - casic-data-es - 1.0.1.alpha - - - com.casic casic-common 2.0.0 compile diff --git a/casic-data/src/main/java/com/casic/missiles/modular/system/es/DataGasEs.java b/casic-data/src/main/java/com/casic/missiles/modular/system/es/DataGasEs.java deleted file mode 100644 index 821e8f1..0000000 --- a/casic-data/src/main/java/com/casic/missiles/modular/system/es/DataGasEs.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.casic.missiles.modular.system.es; - -import lombok.Data; - -@Data -public class DataGasEs { - - private Long stationId; - private String stationName; - private Long monitorId; - private String monitorName; - private String devcode; - private Double direction; - private Double pitch; - private Double concentration; - private Double threshold; - private String logTime; -} diff --git a/casic-common/pom.xml b/casic-common/pom.xml index 27e6a55..a47c616 100644 --- a/casic-common/pom.xml +++ b/casic-common/pom.xml @@ -46,6 +46,11 @@ mina-core 2.0.4 + + com.casic + casic-data-es + 1.0.1.alpha + diff --git a/casic-common/src/main/java/com/casic/missiles/es/DataGasEs.java b/casic-common/src/main/java/com/casic/missiles/es/DataGasEs.java new file mode 100644 index 0000000..3b1f3fe --- /dev/null +++ b/casic-common/src/main/java/com/casic/missiles/es/DataGasEs.java @@ -0,0 +1,18 @@ +package com.casic.missiles.es; + +import lombok.Data; + +@Data +public class DataGasEs { + + private Long stationId; + private String stationName; + private Long monitorId; + private String monitorName; + private String devcode; + private Double direction; + private Double pitch; + private Double concentration; + private Double threshold; + private String logTime; +} diff --git a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java new file mode 100644 index 0000000..aeef63d --- /dev/null +++ b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java @@ -0,0 +1,257 @@ +package com.casic.missiles.es; + +import cn.hutool.core.date.DateUtil; +import cn.hutool.core.util.ObjectUtil; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +/** + * ES查询请求体 + * + * @author zhangyingjie123 + * @since 2020-11-30 + */ +public class ElasticSearchQuery { + private JSONObject body; + private JSONObject query; + private JSONObject bool; + private JSONArray boolMust; + private JSONObject filter; + private JSONObject filterBool; + private JSONArray filterBoolMust; + private JSONArray filterBoolMustnot; + + public static final String MUST_FLAG = "1"; + public static final String MUST_NOT_FLAG = "0"; + + public static final String QUERY = "query"; + public static final String BOOL = "bool"; + public static final String MUST = "must"; + public static final String MUST_NOT = "must_not"; + public static final String FILTER = "filter"; + + + /** + * 生成查询请求体基础结构: + * { + * "query": { + * "bool": { + * "must" [], + * "filter": { + * "bool": { + * "must": [], + * "must_not": [] + * } + * } + * } + * } + * } + */ + public ElasticSearchQuery() { + this.body = new JSONObject(); + this.query = this.query(); + this.bool = this.bool(); + this.boolMust = this.boolMust(); + this.filter = this.filter(); + this.filterBool = this.filterBool(); + this.filterBoolMust = this.filterBoolMust(); + this.filterBoolMustnot = this.filterBoolMustnot(); + } + + public ElasticSearchQuery(JSONObject body){ + this.body = body; + } + + public JSONObject getBody() { + return body; + } + + private JSONObject query(){ + if(ObjectUtil.isEmpty(this.body.get(QUERY))){ + this.body.put("query",new JSONObject()); + } + return this.body.getJSONObject(QUERY); + + } + + private JSONObject bool() { + JSONObject query = this.query; + if(ObjectUtil.isEmpty(query.get(BOOL))){ + query.put(BOOL,new JSONObject()); + } + return query.getJSONObject(BOOL); + } + + private JSONArray boolMust(){ + JSONObject bool = this.bool; + if(ObjectUtil.isEmpty(bool.get(MUST))){ + bool.put(MUST,new JSONArray()); + } + return bool.getJSONArray(MUST); + } + + private JSONObject filter(){ + JSONObject bool = this.bool; + if(ObjectUtil.isEmpty(bool.get(FILTER))){ + bool.put(FILTER,new JSONObject()); + } + return bool.getJSONObject(FILTER); + } + + private JSONObject filterBool(){ + JSONObject filter = this.filter; + if(ObjectUtil.isEmpty(filter.get(BOOL))){ + filter.put(BOOL,new JSONObject()); + } + return filter.getJSONObject(BOOL); + } + + private JSONArray filterBoolMust(){ + JSONObject bool = this.filterBool; + if(ObjectUtil.isEmpty(bool.get(MUST))){ + bool.put(MUST,new JSONArray()); + } + return bool.getJSONArray(MUST); + } + + private JSONArray filterBoolMustnot(){ + JSONObject bool = this.filterBool; + if(ObjectUtil.isEmpty(bool.get(MUST_NOT))){ + bool.put(MUST_NOT,new JSONArray()); + } + return bool.getJSONArray(MUST_NOT); + } + + public void match(String filed, Serializable value){ + JSONArray boolMust = this.boolMust; + JSONObject match = new JSONObject(); + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,value); + match.put("match",filedJson); + boolMust.add(match); + } + + public void matchPhrase(String filed, Serializable value){ + JSONArray boolMust = this.boolMust; + JSONObject matchPhrase = new JSONObject(); + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,value); + matchPhrase.put("match_phrase",filedJson); + boolMust.add(matchPhrase); + } + + public void term(String filed, Serializable value){ + this.term(filed,value,MUST_FLAG); + } + + public void term(String filed, Serializable value,String mustFlag){ + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,value); + JSONObject term = new JSONObject(); + term.put("term",filedJson); + if(MUST_FLAG.equals(mustFlag)){ + this.filterBoolMust.add(term); + } + if(MUST_NOT_FLAG.equals(mustFlag)){ + this.filterBoolMustnot.add(term); + } + } + + public void terms(String filed,List value){ + this.terms(filed,value,MUST_FLAG); + } + + public void terms(String filed, List value, String mustFlag){ + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,value); + JSONObject terms = new JSONObject(); + terms.put("terms",filedJson); + if(MUST_FLAG.equals(mustFlag)){ + this.filterBoolMust.add(terms); + } + if(MUST_NOT_FLAG.equals(mustFlag)){ + this.filterBoolMustnot.add(terms); + } + } + + public void range(String filed, Serializable from, Serializable to){ + this.range(filed,from,to,MUST_FLAG); + } + + public void range(String filed, Serializable from, Serializable to, String mustFlag){ + JSONObject rangeJson = new JSONObject(); + if(ObjectUtil.isNotEmpty(from)){ + rangeJson.put("from",from); + } + if(ObjectUtil.isNotEmpty(to)){ + rangeJson.put("to",to); + } + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,rangeJson); + JSONObject range = new JSONObject(); + range.put("range",filedJson); + if(MUST_FLAG.equals(mustFlag)){ + this.filterBoolMust.add(range); + } + if(MUST_NOT_FLAG.equals(mustFlag)){ + this.filterBoolMustnot.add(range); + } + } + + public void size(int size){ + this.body.put("size",size); + } + + public void from(int from) { + this.body.put("from",from); + } + + public void sort(String sort, String order){ + JSONObject sortJson = new JSONObject(); + JSONObject orderJson = new JSONObject(); + orderJson.put("order",order); + sortJson.put(sort,orderJson); + this.body.put("sort",sortJson); + } + + public void aggs(String aggsName, String filed){ + JSONObject terms = new JSONObject(); + terms.put("field",filed); + JSONObject aggsTerms = new JSONObject(); + aggsTerms.put("terms",terms); + JSONObject aggsBody = new JSONObject(); + aggsBody.put(aggsName,aggsTerms); + this.body.put("aggs",aggsBody); + } + + public void aggs(JSONObject aggBody){ + this.body.put("aggs",aggBody); + } + + + + public static void main(String[] args) { + ElasticSearchQuery query = new ElasticSearchQuery(); + query.matchPhrase("name","张三"); + query.term("sex","2"); + List deptIds = new ArrayList<>(); + deptIds.add(0L); + deptIds.add(24L); + query.terms("deptId", deptIds); + Date date1 = DateUtil.parse("2020-01-01","yyyy-MM-dd"); + Date date2 = DateUtil.parse("2020-04-01","yyyy-MM-dd"); + query.range("collTime",date1.getTime(),date2.getTime()); + query.term("collState","3",MUST_NOT_FLAG); + query.size(10); + query.from(0); + query.sort("irId","asc"); + System.out.println(query.getBody().toJSONString()); + } + + +} diff --git a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java new file mode 100644 index 0000000..dc9a968 --- /dev/null +++ b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java @@ -0,0 +1,172 @@ +package com.casic.missiles.es; + +import cn.hutool.core.util.ObjectUtil; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.casic.missiles.core.es.EsResponse; +import org.apache.http.nio.entity.NStringEntity; +import org.apache.http.util.EntityUtils; +import org.elasticsearch.client.Request; +import org.elasticsearch.client.RequestOptions; +import org.elasticsearch.client.Response; +import org.elasticsearch.client.RestClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Base64; +import java.util.List; + +/** + * ES工具类 + * + * @author zhangyingjie123 + * @since 2020-11-30 + */ +@Component +public class ElasticSearchUtil { + private static final Logger logger = LoggerFactory.getLogger(ElasticSearchUtil.class); + + @Autowired + private RestClient clientMapping; + @Autowired + private Environment env; + + private static String username; + private static String password; + + private static RestClient client; + + @PostConstruct + public void init(){ + client = clientMapping; + username = env.getProperty("casic.data.es.username"); + password = env.getProperty("casic.data.es.password"); + } + + public static JSONObject selectDocumentById(String index, String type, String id) throws IOException{ + String method = "GET"; + String endPoint = index.concat("/").concat(type).concat("/").concat(id); + Response response = performRequest(method,endPoint,null); + JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); + return result.getJSONObject("_source"); + } + + public static Response addDocumentById(String index, String type, String id, Object entity) throws IOException{ + String method = "POST"; + String endPoint = index.concat("/").concat(type).concat("/"); + if(ObjectUtil.isNotEmpty(id)){ + endPoint.concat(id); + } + endPoint.concat("?refresh=true"); + logger.debug(entity.toString()); + return performRequest(method,endPoint,JSON.toJSONString(entity)); + } + + public static Response deleteDocumentById(String index, String type, String id) throws IOException{ + String method = "DELETE"; + String endPoint = index.concat("/").concat(type).concat("/").concat(id).concat("?refresh=true"); + return performRequest(method,endPoint,null); + } + + public static Response updateDocumentById(String index, String type, String id, Object entity) throws IOException{ + String method = "POST"; + String endPoint = index.concat("/").concat(type).concat("/").concat(id).concat("?refresh=true"); + logger.debug(entity.toString()); + return performRequest(method,endPoint,JSON.toJSONString(entity)); + } + + public static EsResponse searchQuery(String index, String type, ElasticSearchQuery query) throws IOException{ + String method = "POST"; + String entPoint = index.concat("/").concat(type).concat("/").concat("_search"); + logger.debug(query.getBody().toString()); + Response response = performRequest(method, entPoint, query.getBody().toString()); + // 获取response body,转换为json对象 + JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); + return result.toJavaObject(EsResponse.class); + } + + public static Response aggsQuery(String index, String type, ElasticSearchQuery query) throws IOException{ + String method = "POST"; + String entPoint = index.concat("/").concat(type).concat("/").concat("_search"); + logger.debug(query.getBody().toString()); + Response response = performRequest(method, entPoint, query.getBody().toString()); + return response; + } + + public static List searchQueryScroll(String index, String type, ElasticSearchQuery query) throws IOException{ + // 首次查询,提交查询条件,endpoint增加‘?scroll=1m’ + List results = new ArrayList<>(); + String method = "POST"; + String entPoint = index.concat("/").concat(type).concat("/").concat("_search").concat("?scroll=1m"); + logger.debug(query.getBody().toString()); + Response response = performRequest(method, entPoint, query.getBody().toString()); + JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); + EsResponse esResponse = result.toJavaObject(EsResponse.class); + String scrollId = result.getString("_scroll_id"); + if(ObjectUtil.isNotEmpty(esResponse.getDatas())){ + results.addAll(esResponse.getDatas()); + } + // 循环发送scroll请求,直到返回结果为空 + entPoint = "_search/scroll"; + JSONObject scrollBody = new JSONObject(); + scrollBody.put("scroll","1m"); + scrollBody.put("scroll_id",scrollId); + while (esResponse.getDatas().size() > 0){ + method = "GET"; + response = performRequest(method, entPoint, scrollBody.toString()); + result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); + esResponse = result.toJavaObject(EsResponse.class); + if(ObjectUtil.isNotEmpty(esResponse.getDatas())){ + results.addAll(esResponse.getDatas()); + } + } + // 查询完成后,及时删除scroll,释放资源 + scrollBody.remove("scroll"); + method = "DELETE"; + performRequest(method, entPoint, scrollBody.toString()); + return results; + } + + public static Response bulk(String index,String type,String entity) throws IOException{ + String method = "POST"; + StringBuilder endPoint = new StringBuilder(); + if(ObjectUtil.isNotEmpty(index)){ + endPoint.append(index).append("/"); + } + if(ObjectUtil.isNotEmpty(type)){ + endPoint.append(type).append("/"); + } + endPoint.append("_bulk"); + endPoint.append("?refresh=true"); + logger.debug(entity); + return performRequest(method,endPoint.toString(),entity); + } + + private static Response performRequest(String method, String endpoint, String entity) throws IOException { + //logger.info(method + " " + endpoint); + Request request = new Request(method,endpoint); + if(ObjectUtil.isNotEmpty(entity)){ + request.setEntity(new NStringEntity(entity,"utf-8")); + } + + if(ObjectUtil.isNotEmpty(username) && ObjectUtil.isNotEmpty(password)){ + RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder(); + String token = username.concat(":").concat(password); + String base64encode = Base64.getEncoder().encodeToString(token.getBytes("utf-8")); + builder.addHeader("Authorization", "Basic " + base64encode ); + request.setOptions(builder.build()); + } + + + Response response = client.performRequest(request); + logger.debug(response.toString()); + return response; + } + +} diff --git a/casic-common/src/main/java/com/casic/missiles/weigeng/CommDoorUtil.java b/casic-common/src/main/java/com/casic/missiles/weigeng/CommDoorUtil.java index bed1361..7457109 100644 --- a/casic-common/src/main/java/com/casic/missiles/weigeng/CommDoorUtil.java +++ b/casic-common/src/main/java/com/casic/missiles/weigeng/CommDoorUtil.java @@ -118,15 +118,14 @@ } //重启设备 - public static boolean restart(String monitorId) { + public static boolean restart(String controllerIP, String controllerSN) { try { - String doors = ObjectUtil.isNotEmpty(map.get(monitorId)) ? map.get(monitorId).toString() : ""; - if (ObjectUtil.isNotEmpty(doors)) - if (DoorShortUtil.remoteControl(doors.split(",")[1], Long.valueOf(doors.split(",")[0]), 2, 1)) { + if (ObjectUtil.isAllNotEmpty(controllerIP, controllerSN)) + if (DoorShortUtil.remoteControl(controllerIP, Long.valueOf(controllerSN), 2, 1)) { //重新上电需要间隔20秒 Thread.sleep(Integer.valueOf(map.get("time").toString()) * 1000); //重新关闭开关上电 - return DoorShortUtil.remoteControl(doors.split(",")[1], Long.valueOf(doors.split(",")[0]), 2, 2); + return DoorShortUtil.remoteControl(controllerIP, Long.valueOf(controllerSN), 2, 2); } } catch (Exception e) { e.printStackTrace(); @@ -135,16 +134,14 @@ } //重启设备 - public static boolean interruptStart(String monitorId) { + public static boolean interruptStart(String controllerIP, String controllerSN) { try { - String doors = ObjectUtil.isNotEmpty(map.get(monitorId)) ? map.get(monitorId).toString() : ""; - if (ObjectUtil.isNotEmpty(doors)) - if (DoorShortUtil.remoteControl(doors.split(",")[1], Long.valueOf(doors.split(",")[0]), 2, 1)) { + if (ObjectUtil.isAllNotEmpty(controllerIP,controllerSN)) + if (DoorShortUtil.remoteControl(controllerIP, Long.valueOf(controllerSN), 2, 1)) { //重新上电需要间隔20秒 Thread.sleep(Integer.valueOf(map.get("time").toString()) * 1000); //重新关闭开关上电 - DoorShortUtil.remoteControl(doors.split(",")[1], Long.valueOf(doors.split(",")[0]), 2, 2); - + DoorShortUtil.remoteControl(controllerIP, Long.valueOf(controllerSN), 2, 2); Thread.sleep(2 * 60 * 1000); return true; } diff --git a/casic-data/pom.xml b/casic-data/pom.xml index aca0a62..a076800 100644 --- a/casic-data/pom.xml +++ b/casic-data/pom.xml @@ -43,11 +43,6 @@ com.casic - casic-data-es - 1.0.1.alpha - - - com.casic casic-common 2.0.0 compile diff --git a/casic-data/src/main/java/com/casic/missiles/modular/system/es/DataGasEs.java b/casic-data/src/main/java/com/casic/missiles/modular/system/es/DataGasEs.java deleted file mode 100644 index 821e8f1..0000000 --- a/casic-data/src/main/java/com/casic/missiles/modular/system/es/DataGasEs.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.casic.missiles.modular.system.es; - -import lombok.Data; - -@Data -public class DataGasEs { - - private Long stationId; - private String stationName; - private Long monitorId; - private String monitorName; - private String devcode; - private Double direction; - private Double pitch; - private Double concentration; - private Double threshold; - private String logTime; -} diff --git a/casic-data/src/main/java/com/casic/missiles/modular/system/es/ElasticSearchQuery.java b/casic-data/src/main/java/com/casic/missiles/modular/system/es/ElasticSearchQuery.java deleted file mode 100644 index 4d7a38f..0000000 --- a/casic-data/src/main/java/com/casic/missiles/modular/system/es/ElasticSearchQuery.java +++ /dev/null @@ -1,257 +0,0 @@ -package com.casic.missiles.modular.system.es; - -import cn.hutool.core.date.DateUtil; -import cn.hutool.core.util.ObjectUtil; -import com.alibaba.fastjson.JSONArray; -import com.alibaba.fastjson.JSONObject; - -import java.io.Serializable; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; - -/** - * ES查询请求体 - * - * @author zhangyingjie123 - * @since 2020-11-30 - */ -public class ElasticSearchQuery { - private JSONObject body; - private JSONObject query; - private JSONObject bool; - private JSONArray boolMust; - private JSONObject filter; - private JSONObject filterBool; - private JSONArray filterBoolMust; - private JSONArray filterBoolMustnot; - - public static final String MUST_FLAG = "1"; - public static final String MUST_NOT_FLAG = "0"; - - public static final String QUERY = "query"; - public static final String BOOL = "bool"; - public static final String MUST = "must"; - public static final String MUST_NOT = "must_not"; - public static final String FILTER = "filter"; - - - /** - * 生成查询请求体基础结构: - * { - * "query": { - * "bool": { - * "must" [], - * "filter": { - * "bool": { - * "must": [], - * "must_not": [] - * } - * } - * } - * } - * } - */ - public ElasticSearchQuery() { - this.body = new JSONObject(); - this.query = this.query(); - this.bool = this.bool(); - this.boolMust = this.boolMust(); - this.filter = this.filter(); - this.filterBool = this.filterBool(); - this.filterBoolMust = this.filterBoolMust(); - this.filterBoolMustnot = this.filterBoolMustnot(); - } - - public ElasticSearchQuery(JSONObject body){ - this.body = body; - } - - public JSONObject getBody() { - return body; - } - - private JSONObject query(){ - if(ObjectUtil.isEmpty(this.body.get(QUERY))){ - this.body.put("query",new JSONObject()); - } - return this.body.getJSONObject(QUERY); - - } - - private JSONObject bool() { - JSONObject query = this.query; - if(ObjectUtil.isEmpty(query.get(BOOL))){ - query.put(BOOL,new JSONObject()); - } - return query.getJSONObject(BOOL); - } - - private JSONArray boolMust(){ - JSONObject bool = this.bool; - if(ObjectUtil.isEmpty(bool.get(MUST))){ - bool.put(MUST,new JSONArray()); - } - return bool.getJSONArray(MUST); - } - - private JSONObject filter(){ - JSONObject bool = this.bool; - if(ObjectUtil.isEmpty(bool.get(FILTER))){ - bool.put(FILTER,new JSONObject()); - } - return bool.getJSONObject(FILTER); - } - - private JSONObject filterBool(){ - JSONObject filter = this.filter; - if(ObjectUtil.isEmpty(filter.get(BOOL))){ - filter.put(BOOL,new JSONObject()); - } - return filter.getJSONObject(BOOL); - } - - private JSONArray filterBoolMust(){ - JSONObject bool = this.filterBool; - if(ObjectUtil.isEmpty(bool.get(MUST))){ - bool.put(MUST,new JSONArray()); - } - return bool.getJSONArray(MUST); - } - - private JSONArray filterBoolMustnot(){ - JSONObject bool = this.filterBool; - if(ObjectUtil.isEmpty(bool.get(MUST_NOT))){ - bool.put(MUST_NOT,new JSONArray()); - } - return bool.getJSONArray(MUST_NOT); - } - - public void match(String filed, Serializable value){ - JSONArray boolMust = this.boolMust; - JSONObject match = new JSONObject(); - JSONObject filedJson = new JSONObject(); - filedJson.put(filed,value); - match.put("match",filedJson); - boolMust.add(match); - } - - public void matchPhrase(String filed, Serializable value){ - JSONArray boolMust = this.boolMust; - JSONObject matchPhrase = new JSONObject(); - JSONObject filedJson = new JSONObject(); - filedJson.put(filed,value); - matchPhrase.put("match_phrase",filedJson); - boolMust.add(matchPhrase); - } - - public void term(String filed, Serializable value){ - this.term(filed,value,MUST_FLAG); - } - - public void term(String filed, Serializable value,String mustFlag){ - JSONObject filedJson = new JSONObject(); - filedJson.put(filed,value); - JSONObject term = new JSONObject(); - term.put("term",filedJson); - if(MUST_FLAG.equals(mustFlag)){ - this.filterBoolMust.add(term); - } - if(MUST_NOT_FLAG.equals(mustFlag)){ - this.filterBoolMustnot.add(term); - } - } - - public void terms(String filed,List value){ - this.terms(filed,value,MUST_FLAG); - } - - public void terms(String filed, List value, String mustFlag){ - JSONObject filedJson = new JSONObject(); - filedJson.put(filed,value); - JSONObject terms = new JSONObject(); - terms.put("terms",filedJson); - if(MUST_FLAG.equals(mustFlag)){ - this.filterBoolMust.add(terms); - } - if(MUST_NOT_FLAG.equals(mustFlag)){ - this.filterBoolMustnot.add(terms); - } - } - - public void range(String filed, Serializable from, Serializable to){ - this.range(filed,from,to,MUST_FLAG); - } - - public void range(String filed, Serializable from, Serializable to, String mustFlag){ - JSONObject rangeJson = new JSONObject(); - if(ObjectUtil.isNotEmpty(from)){ - rangeJson.put("from",from); - } - if(ObjectUtil.isNotEmpty(to)){ - rangeJson.put("to",to); - } - JSONObject filedJson = new JSONObject(); - filedJson.put(filed,rangeJson); - JSONObject range = new JSONObject(); - range.put("range",filedJson); - if(MUST_FLAG.equals(mustFlag)){ - this.filterBoolMust.add(range); - } - if(MUST_NOT_FLAG.equals(mustFlag)){ - this.filterBoolMustnot.add(range); - } - } - - public void size(int size){ - this.body.put("size",size); - } - - public void from(int from) { - this.body.put("from",from); - } - - public void sort(String sort, String order){ - JSONObject sortJson = new JSONObject(); - JSONObject orderJson = new JSONObject(); - orderJson.put("order",order); - sortJson.put(sort,orderJson); - this.body.put("sort",sortJson); - } - - public void aggs(String aggsName, String filed){ - JSONObject terms = new JSONObject(); - terms.put("field",filed); - JSONObject aggsTerms = new JSONObject(); - aggsTerms.put("terms",terms); - JSONObject aggsBody = new JSONObject(); - aggsBody.put(aggsName,aggsTerms); - this.body.put("aggs",aggsBody); - } - - public void aggs(JSONObject aggBody){ - this.body.put("aggs",aggBody); - } - - - - public static void main(String[] args) { - ElasticSearchQuery query = new ElasticSearchQuery(); - query.matchPhrase("name","张三"); - query.term("sex","2"); - List deptIds = new ArrayList<>(); - deptIds.add(0L); - deptIds.add(24L); - query.terms("deptId", deptIds); - Date date1 = DateUtil.parse("2020-01-01","yyyy-MM-dd"); - Date date2 = DateUtil.parse("2020-04-01","yyyy-MM-dd"); - query.range("collTime",date1.getTime(),date2.getTime()); - query.term("collState","3",MUST_NOT_FLAG); - query.size(10); - query.from(0); - query.sort("irId","asc"); - System.out.println(query.getBody().toJSONString()); - } - - -} diff --git a/casic-common/pom.xml b/casic-common/pom.xml index 27e6a55..a47c616 100644 --- a/casic-common/pom.xml +++ b/casic-common/pom.xml @@ -46,6 +46,11 @@ mina-core 2.0.4 + + com.casic + casic-data-es + 1.0.1.alpha + diff --git a/casic-common/src/main/java/com/casic/missiles/es/DataGasEs.java b/casic-common/src/main/java/com/casic/missiles/es/DataGasEs.java new file mode 100644 index 0000000..3b1f3fe --- /dev/null +++ b/casic-common/src/main/java/com/casic/missiles/es/DataGasEs.java @@ -0,0 +1,18 @@ +package com.casic.missiles.es; + +import lombok.Data; + +@Data +public class DataGasEs { + + private Long stationId; + private String stationName; + private Long monitorId; + private String monitorName; + private String devcode; + private Double direction; + private Double pitch; + private Double concentration; + private Double threshold; + private String logTime; +} diff --git a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java new file mode 100644 index 0000000..aeef63d --- /dev/null +++ b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java @@ -0,0 +1,257 @@ +package com.casic.missiles.es; + +import cn.hutool.core.date.DateUtil; +import cn.hutool.core.util.ObjectUtil; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +/** + * ES查询请求体 + * + * @author zhangyingjie123 + * @since 2020-11-30 + */ +public class ElasticSearchQuery { + private JSONObject body; + private JSONObject query; + private JSONObject bool; + private JSONArray boolMust; + private JSONObject filter; + private JSONObject filterBool; + private JSONArray filterBoolMust; + private JSONArray filterBoolMustnot; + + public static final String MUST_FLAG = "1"; + public static final String MUST_NOT_FLAG = "0"; + + public static final String QUERY = "query"; + public static final String BOOL = "bool"; + public static final String MUST = "must"; + public static final String MUST_NOT = "must_not"; + public static final String FILTER = "filter"; + + + /** + * 生成查询请求体基础结构: + * { + * "query": { + * "bool": { + * "must" [], + * "filter": { + * "bool": { + * "must": [], + * "must_not": [] + * } + * } + * } + * } + * } + */ + public ElasticSearchQuery() { + this.body = new JSONObject(); + this.query = this.query(); + this.bool = this.bool(); + this.boolMust = this.boolMust(); + this.filter = this.filter(); + this.filterBool = this.filterBool(); + this.filterBoolMust = this.filterBoolMust(); + this.filterBoolMustnot = this.filterBoolMustnot(); + } + + public ElasticSearchQuery(JSONObject body){ + this.body = body; + } + + public JSONObject getBody() { + return body; + } + + private JSONObject query(){ + if(ObjectUtil.isEmpty(this.body.get(QUERY))){ + this.body.put("query",new JSONObject()); + } + return this.body.getJSONObject(QUERY); + + } + + private JSONObject bool() { + JSONObject query = this.query; + if(ObjectUtil.isEmpty(query.get(BOOL))){ + query.put(BOOL,new JSONObject()); + } + return query.getJSONObject(BOOL); + } + + private JSONArray boolMust(){ + JSONObject bool = this.bool; + if(ObjectUtil.isEmpty(bool.get(MUST))){ + bool.put(MUST,new JSONArray()); + } + return bool.getJSONArray(MUST); + } + + private JSONObject filter(){ + JSONObject bool = this.bool; + if(ObjectUtil.isEmpty(bool.get(FILTER))){ + bool.put(FILTER,new JSONObject()); + } + return bool.getJSONObject(FILTER); + } + + private JSONObject filterBool(){ + JSONObject filter = this.filter; + if(ObjectUtil.isEmpty(filter.get(BOOL))){ + filter.put(BOOL,new JSONObject()); + } + return filter.getJSONObject(BOOL); + } + + private JSONArray filterBoolMust(){ + JSONObject bool = this.filterBool; + if(ObjectUtil.isEmpty(bool.get(MUST))){ + bool.put(MUST,new JSONArray()); + } + return bool.getJSONArray(MUST); + } + + private JSONArray filterBoolMustnot(){ + JSONObject bool = this.filterBool; + if(ObjectUtil.isEmpty(bool.get(MUST_NOT))){ + bool.put(MUST_NOT,new JSONArray()); + } + return bool.getJSONArray(MUST_NOT); + } + + public void match(String filed, Serializable value){ + JSONArray boolMust = this.boolMust; + JSONObject match = new JSONObject(); + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,value); + match.put("match",filedJson); + boolMust.add(match); + } + + public void matchPhrase(String filed, Serializable value){ + JSONArray boolMust = this.boolMust; + JSONObject matchPhrase = new JSONObject(); + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,value); + matchPhrase.put("match_phrase",filedJson); + boolMust.add(matchPhrase); + } + + public void term(String filed, Serializable value){ + this.term(filed,value,MUST_FLAG); + } + + public void term(String filed, Serializable value,String mustFlag){ + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,value); + JSONObject term = new JSONObject(); + term.put("term",filedJson); + if(MUST_FLAG.equals(mustFlag)){ + this.filterBoolMust.add(term); + } + if(MUST_NOT_FLAG.equals(mustFlag)){ + this.filterBoolMustnot.add(term); + } + } + + public void terms(String filed,List value){ + this.terms(filed,value,MUST_FLAG); + } + + public void terms(String filed, List value, String mustFlag){ + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,value); + JSONObject terms = new JSONObject(); + terms.put("terms",filedJson); + if(MUST_FLAG.equals(mustFlag)){ + this.filterBoolMust.add(terms); + } + if(MUST_NOT_FLAG.equals(mustFlag)){ + this.filterBoolMustnot.add(terms); + } + } + + public void range(String filed, Serializable from, Serializable to){ + this.range(filed,from,to,MUST_FLAG); + } + + public void range(String filed, Serializable from, Serializable to, String mustFlag){ + JSONObject rangeJson = new JSONObject(); + if(ObjectUtil.isNotEmpty(from)){ + rangeJson.put("from",from); + } + if(ObjectUtil.isNotEmpty(to)){ + rangeJson.put("to",to); + } + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,rangeJson); + JSONObject range = new JSONObject(); + range.put("range",filedJson); + if(MUST_FLAG.equals(mustFlag)){ + this.filterBoolMust.add(range); + } + if(MUST_NOT_FLAG.equals(mustFlag)){ + this.filterBoolMustnot.add(range); + } + } + + public void size(int size){ + this.body.put("size",size); + } + + public void from(int from) { + this.body.put("from",from); + } + + public void sort(String sort, String order){ + JSONObject sortJson = new JSONObject(); + JSONObject orderJson = new JSONObject(); + orderJson.put("order",order); + sortJson.put(sort,orderJson); + this.body.put("sort",sortJson); + } + + public void aggs(String aggsName, String filed){ + JSONObject terms = new JSONObject(); + terms.put("field",filed); + JSONObject aggsTerms = new JSONObject(); + aggsTerms.put("terms",terms); + JSONObject aggsBody = new JSONObject(); + aggsBody.put(aggsName,aggsTerms); + this.body.put("aggs",aggsBody); + } + + public void aggs(JSONObject aggBody){ + this.body.put("aggs",aggBody); + } + + + + public static void main(String[] args) { + ElasticSearchQuery query = new ElasticSearchQuery(); + query.matchPhrase("name","张三"); + query.term("sex","2"); + List deptIds = new ArrayList<>(); + deptIds.add(0L); + deptIds.add(24L); + query.terms("deptId", deptIds); + Date date1 = DateUtil.parse("2020-01-01","yyyy-MM-dd"); + Date date2 = DateUtil.parse("2020-04-01","yyyy-MM-dd"); + query.range("collTime",date1.getTime(),date2.getTime()); + query.term("collState","3",MUST_NOT_FLAG); + query.size(10); + query.from(0); + query.sort("irId","asc"); + System.out.println(query.getBody().toJSONString()); + } + + +} diff --git a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java new file mode 100644 index 0000000..dc9a968 --- /dev/null +++ b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java @@ -0,0 +1,172 @@ +package com.casic.missiles.es; + +import cn.hutool.core.util.ObjectUtil; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.casic.missiles.core.es.EsResponse; +import org.apache.http.nio.entity.NStringEntity; +import org.apache.http.util.EntityUtils; +import org.elasticsearch.client.Request; +import org.elasticsearch.client.RequestOptions; +import org.elasticsearch.client.Response; +import org.elasticsearch.client.RestClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Base64; +import java.util.List; + +/** + * ES工具类 + * + * @author zhangyingjie123 + * @since 2020-11-30 + */ +@Component +public class ElasticSearchUtil { + private static final Logger logger = LoggerFactory.getLogger(ElasticSearchUtil.class); + + @Autowired + private RestClient clientMapping; + @Autowired + private Environment env; + + private static String username; + private static String password; + + private static RestClient client; + + @PostConstruct + public void init(){ + client = clientMapping; + username = env.getProperty("casic.data.es.username"); + password = env.getProperty("casic.data.es.password"); + } + + public static JSONObject selectDocumentById(String index, String type, String id) throws IOException{ + String method = "GET"; + String endPoint = index.concat("/").concat(type).concat("/").concat(id); + Response response = performRequest(method,endPoint,null); + JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); + return result.getJSONObject("_source"); + } + + public static Response addDocumentById(String index, String type, String id, Object entity) throws IOException{ + String method = "POST"; + String endPoint = index.concat("/").concat(type).concat("/"); + if(ObjectUtil.isNotEmpty(id)){ + endPoint.concat(id); + } + endPoint.concat("?refresh=true"); + logger.debug(entity.toString()); + return performRequest(method,endPoint,JSON.toJSONString(entity)); + } + + public static Response deleteDocumentById(String index, String type, String id) throws IOException{ + String method = "DELETE"; + String endPoint = index.concat("/").concat(type).concat("/").concat(id).concat("?refresh=true"); + return performRequest(method,endPoint,null); + } + + public static Response updateDocumentById(String index, String type, String id, Object entity) throws IOException{ + String method = "POST"; + String endPoint = index.concat("/").concat(type).concat("/").concat(id).concat("?refresh=true"); + logger.debug(entity.toString()); + return performRequest(method,endPoint,JSON.toJSONString(entity)); + } + + public static EsResponse searchQuery(String index, String type, ElasticSearchQuery query) throws IOException{ + String method = "POST"; + String entPoint = index.concat("/").concat(type).concat("/").concat("_search"); + logger.debug(query.getBody().toString()); + Response response = performRequest(method, entPoint, query.getBody().toString()); + // 获取response body,转换为json对象 + JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); + return result.toJavaObject(EsResponse.class); + } + + public static Response aggsQuery(String index, String type, ElasticSearchQuery query) throws IOException{ + String method = "POST"; + String entPoint = index.concat("/").concat(type).concat("/").concat("_search"); + logger.debug(query.getBody().toString()); + Response response = performRequest(method, entPoint, query.getBody().toString()); + return response; + } + + public static List searchQueryScroll(String index, String type, ElasticSearchQuery query) throws IOException{ + // 首次查询,提交查询条件,endpoint增加‘?scroll=1m’ + List results = new ArrayList<>(); + String method = "POST"; + String entPoint = index.concat("/").concat(type).concat("/").concat("_search").concat("?scroll=1m"); + logger.debug(query.getBody().toString()); + Response response = performRequest(method, entPoint, query.getBody().toString()); + JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); + EsResponse esResponse = result.toJavaObject(EsResponse.class); + String scrollId = result.getString("_scroll_id"); + if(ObjectUtil.isNotEmpty(esResponse.getDatas())){ + results.addAll(esResponse.getDatas()); + } + // 循环发送scroll请求,直到返回结果为空 + entPoint = "_search/scroll"; + JSONObject scrollBody = new JSONObject(); + scrollBody.put("scroll","1m"); + scrollBody.put("scroll_id",scrollId); + while (esResponse.getDatas().size() > 0){ + method = "GET"; + response = performRequest(method, entPoint, scrollBody.toString()); + result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); + esResponse = result.toJavaObject(EsResponse.class); + if(ObjectUtil.isNotEmpty(esResponse.getDatas())){ + results.addAll(esResponse.getDatas()); + } + } + // 查询完成后,及时删除scroll,释放资源 + scrollBody.remove("scroll"); + method = "DELETE"; + performRequest(method, entPoint, scrollBody.toString()); + return results; + } + + public static Response bulk(String index,String type,String entity) throws IOException{ + String method = "POST"; + StringBuilder endPoint = new StringBuilder(); + if(ObjectUtil.isNotEmpty(index)){ + endPoint.append(index).append("/"); + } + if(ObjectUtil.isNotEmpty(type)){ + endPoint.append(type).append("/"); + } + endPoint.append("_bulk"); + endPoint.append("?refresh=true"); + logger.debug(entity); + return performRequest(method,endPoint.toString(),entity); + } + + private static Response performRequest(String method, String endpoint, String entity) throws IOException { + //logger.info(method + " " + endpoint); + Request request = new Request(method,endpoint); + if(ObjectUtil.isNotEmpty(entity)){ + request.setEntity(new NStringEntity(entity,"utf-8")); + } + + if(ObjectUtil.isNotEmpty(username) && ObjectUtil.isNotEmpty(password)){ + RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder(); + String token = username.concat(":").concat(password); + String base64encode = Base64.getEncoder().encodeToString(token.getBytes("utf-8")); + builder.addHeader("Authorization", "Basic " + base64encode ); + request.setOptions(builder.build()); + } + + + Response response = client.performRequest(request); + logger.debug(response.toString()); + return response; + } + +} diff --git a/casic-common/src/main/java/com/casic/missiles/weigeng/CommDoorUtil.java b/casic-common/src/main/java/com/casic/missiles/weigeng/CommDoorUtil.java index bed1361..7457109 100644 --- a/casic-common/src/main/java/com/casic/missiles/weigeng/CommDoorUtil.java +++ b/casic-common/src/main/java/com/casic/missiles/weigeng/CommDoorUtil.java @@ -118,15 +118,14 @@ } //重启设备 - public static boolean restart(String monitorId) { + public static boolean restart(String controllerIP, String controllerSN) { try { - String doors = ObjectUtil.isNotEmpty(map.get(monitorId)) ? map.get(monitorId).toString() : ""; - if (ObjectUtil.isNotEmpty(doors)) - if (DoorShortUtil.remoteControl(doors.split(",")[1], Long.valueOf(doors.split(",")[0]), 2, 1)) { + if (ObjectUtil.isAllNotEmpty(controllerIP, controllerSN)) + if (DoorShortUtil.remoteControl(controllerIP, Long.valueOf(controllerSN), 2, 1)) { //重新上电需要间隔20秒 Thread.sleep(Integer.valueOf(map.get("time").toString()) * 1000); //重新关闭开关上电 - return DoorShortUtil.remoteControl(doors.split(",")[1], Long.valueOf(doors.split(",")[0]), 2, 2); + return DoorShortUtil.remoteControl(controllerIP, Long.valueOf(controllerSN), 2, 2); } } catch (Exception e) { e.printStackTrace(); @@ -135,16 +134,14 @@ } //重启设备 - public static boolean interruptStart(String monitorId) { + public static boolean interruptStart(String controllerIP, String controllerSN) { try { - String doors = ObjectUtil.isNotEmpty(map.get(monitorId)) ? map.get(monitorId).toString() : ""; - if (ObjectUtil.isNotEmpty(doors)) - if (DoorShortUtil.remoteControl(doors.split(",")[1], Long.valueOf(doors.split(",")[0]), 2, 1)) { + if (ObjectUtil.isAllNotEmpty(controllerIP,controllerSN)) + if (DoorShortUtil.remoteControl(controllerIP, Long.valueOf(controllerSN), 2, 1)) { //重新上电需要间隔20秒 Thread.sleep(Integer.valueOf(map.get("time").toString()) * 1000); //重新关闭开关上电 - DoorShortUtil.remoteControl(doors.split(",")[1], Long.valueOf(doors.split(",")[0]), 2, 2); - + DoorShortUtil.remoteControl(controllerIP, Long.valueOf(controllerSN), 2, 2); Thread.sleep(2 * 60 * 1000); return true; } diff --git a/casic-data/pom.xml b/casic-data/pom.xml index aca0a62..a076800 100644 --- a/casic-data/pom.xml +++ b/casic-data/pom.xml @@ -43,11 +43,6 @@ com.casic - casic-data-es - 1.0.1.alpha - - - com.casic casic-common 2.0.0 compile diff --git a/casic-data/src/main/java/com/casic/missiles/modular/system/es/DataGasEs.java b/casic-data/src/main/java/com/casic/missiles/modular/system/es/DataGasEs.java deleted file mode 100644 index 821e8f1..0000000 --- a/casic-data/src/main/java/com/casic/missiles/modular/system/es/DataGasEs.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.casic.missiles.modular.system.es; - -import lombok.Data; - -@Data -public class DataGasEs { - - private Long stationId; - private String stationName; - private Long monitorId; - private String monitorName; - private String devcode; - private Double direction; - private Double pitch; - private Double concentration; - private Double threshold; - private String logTime; -} diff --git a/casic-data/src/main/java/com/casic/missiles/modular/system/es/ElasticSearchQuery.java b/casic-data/src/main/java/com/casic/missiles/modular/system/es/ElasticSearchQuery.java deleted file mode 100644 index 4d7a38f..0000000 --- a/casic-data/src/main/java/com/casic/missiles/modular/system/es/ElasticSearchQuery.java +++ /dev/null @@ -1,257 +0,0 @@ -package com.casic.missiles.modular.system.es; - -import cn.hutool.core.date.DateUtil; -import cn.hutool.core.util.ObjectUtil; -import com.alibaba.fastjson.JSONArray; -import com.alibaba.fastjson.JSONObject; - -import java.io.Serializable; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; - -/** - * ES查询请求体 - * - * @author zhangyingjie123 - * @since 2020-11-30 - */ -public class ElasticSearchQuery { - private JSONObject body; - private JSONObject query; - private JSONObject bool; - private JSONArray boolMust; - private JSONObject filter; - private JSONObject filterBool; - private JSONArray filterBoolMust; - private JSONArray filterBoolMustnot; - - public static final String MUST_FLAG = "1"; - public static final String MUST_NOT_FLAG = "0"; - - public static final String QUERY = "query"; - public static final String BOOL = "bool"; - public static final String MUST = "must"; - public static final String MUST_NOT = "must_not"; - public static final String FILTER = "filter"; - - - /** - * 生成查询请求体基础结构: - * { - * "query": { - * "bool": { - * "must" [], - * "filter": { - * "bool": { - * "must": [], - * "must_not": [] - * } - * } - * } - * } - * } - */ - public ElasticSearchQuery() { - this.body = new JSONObject(); - this.query = this.query(); - this.bool = this.bool(); - this.boolMust = this.boolMust(); - this.filter = this.filter(); - this.filterBool = this.filterBool(); - this.filterBoolMust = this.filterBoolMust(); - this.filterBoolMustnot = this.filterBoolMustnot(); - } - - public ElasticSearchQuery(JSONObject body){ - this.body = body; - } - - public JSONObject getBody() { - return body; - } - - private JSONObject query(){ - if(ObjectUtil.isEmpty(this.body.get(QUERY))){ - this.body.put("query",new JSONObject()); - } - return this.body.getJSONObject(QUERY); - - } - - private JSONObject bool() { - JSONObject query = this.query; - if(ObjectUtil.isEmpty(query.get(BOOL))){ - query.put(BOOL,new JSONObject()); - } - return query.getJSONObject(BOOL); - } - - private JSONArray boolMust(){ - JSONObject bool = this.bool; - if(ObjectUtil.isEmpty(bool.get(MUST))){ - bool.put(MUST,new JSONArray()); - } - return bool.getJSONArray(MUST); - } - - private JSONObject filter(){ - JSONObject bool = this.bool; - if(ObjectUtil.isEmpty(bool.get(FILTER))){ - bool.put(FILTER,new JSONObject()); - } - return bool.getJSONObject(FILTER); - } - - private JSONObject filterBool(){ - JSONObject filter = this.filter; - if(ObjectUtil.isEmpty(filter.get(BOOL))){ - filter.put(BOOL,new JSONObject()); - } - return filter.getJSONObject(BOOL); - } - - private JSONArray filterBoolMust(){ - JSONObject bool = this.filterBool; - if(ObjectUtil.isEmpty(bool.get(MUST))){ - bool.put(MUST,new JSONArray()); - } - return bool.getJSONArray(MUST); - } - - private JSONArray filterBoolMustnot(){ - JSONObject bool = this.filterBool; - if(ObjectUtil.isEmpty(bool.get(MUST_NOT))){ - bool.put(MUST_NOT,new JSONArray()); - } - return bool.getJSONArray(MUST_NOT); - } - - public void match(String filed, Serializable value){ - JSONArray boolMust = this.boolMust; - JSONObject match = new JSONObject(); - JSONObject filedJson = new JSONObject(); - filedJson.put(filed,value); - match.put("match",filedJson); - boolMust.add(match); - } - - public void matchPhrase(String filed, Serializable value){ - JSONArray boolMust = this.boolMust; - JSONObject matchPhrase = new JSONObject(); - JSONObject filedJson = new JSONObject(); - filedJson.put(filed,value); - matchPhrase.put("match_phrase",filedJson); - boolMust.add(matchPhrase); - } - - public void term(String filed, Serializable value){ - this.term(filed,value,MUST_FLAG); - } - - public void term(String filed, Serializable value,String mustFlag){ - JSONObject filedJson = new JSONObject(); - filedJson.put(filed,value); - JSONObject term = new JSONObject(); - term.put("term",filedJson); - if(MUST_FLAG.equals(mustFlag)){ - this.filterBoolMust.add(term); - } - if(MUST_NOT_FLAG.equals(mustFlag)){ - this.filterBoolMustnot.add(term); - } - } - - public void terms(String filed,List value){ - this.terms(filed,value,MUST_FLAG); - } - - public void terms(String filed, List value, String mustFlag){ - JSONObject filedJson = new JSONObject(); - filedJson.put(filed,value); - JSONObject terms = new JSONObject(); - terms.put("terms",filedJson); - if(MUST_FLAG.equals(mustFlag)){ - this.filterBoolMust.add(terms); - } - if(MUST_NOT_FLAG.equals(mustFlag)){ - this.filterBoolMustnot.add(terms); - } - } - - public void range(String filed, Serializable from, Serializable to){ - this.range(filed,from,to,MUST_FLAG); - } - - public void range(String filed, Serializable from, Serializable to, String mustFlag){ - JSONObject rangeJson = new JSONObject(); - if(ObjectUtil.isNotEmpty(from)){ - rangeJson.put("from",from); - } - if(ObjectUtil.isNotEmpty(to)){ - rangeJson.put("to",to); - } - JSONObject filedJson = new JSONObject(); - filedJson.put(filed,rangeJson); - JSONObject range = new JSONObject(); - range.put("range",filedJson); - if(MUST_FLAG.equals(mustFlag)){ - this.filterBoolMust.add(range); - } - if(MUST_NOT_FLAG.equals(mustFlag)){ - this.filterBoolMustnot.add(range); - } - } - - public void size(int size){ - this.body.put("size",size); - } - - public void from(int from) { - this.body.put("from",from); - } - - public void sort(String sort, String order){ - JSONObject sortJson = new JSONObject(); - JSONObject orderJson = new JSONObject(); - orderJson.put("order",order); - sortJson.put(sort,orderJson); - this.body.put("sort",sortJson); - } - - public void aggs(String aggsName, String filed){ - JSONObject terms = new JSONObject(); - terms.put("field",filed); - JSONObject aggsTerms = new JSONObject(); - aggsTerms.put("terms",terms); - JSONObject aggsBody = new JSONObject(); - aggsBody.put(aggsName,aggsTerms); - this.body.put("aggs",aggsBody); - } - - public void aggs(JSONObject aggBody){ - this.body.put("aggs",aggBody); - } - - - - public static void main(String[] args) { - ElasticSearchQuery query = new ElasticSearchQuery(); - query.matchPhrase("name","张三"); - query.term("sex","2"); - List deptIds = new ArrayList<>(); - deptIds.add(0L); - deptIds.add(24L); - query.terms("deptId", deptIds); - Date date1 = DateUtil.parse("2020-01-01","yyyy-MM-dd"); - Date date2 = DateUtil.parse("2020-04-01","yyyy-MM-dd"); - query.range("collTime",date1.getTime(),date2.getTime()); - query.term("collState","3",MUST_NOT_FLAG); - query.size(10); - query.from(0); - query.sort("irId","asc"); - System.out.println(query.getBody().toJSONString()); - } - - -} diff --git a/casic-data/src/main/java/com/casic/missiles/modular/system/es/ElasticSearchUtil.java b/casic-data/src/main/java/com/casic/missiles/modular/system/es/ElasticSearchUtil.java deleted file mode 100644 index e9da021..0000000 --- a/casic-data/src/main/java/com/casic/missiles/modular/system/es/ElasticSearchUtil.java +++ /dev/null @@ -1,172 +0,0 @@ -package com.casic.missiles.modular.system.es; - -import cn.hutool.core.util.ObjectUtil; -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONObject; -import com.casic.missiles.core.es.EsResponse; -import org.apache.http.nio.entity.NStringEntity; -import org.apache.http.util.EntityUtils; -import org.elasticsearch.client.Request; -import org.elasticsearch.client.RequestOptions; -import org.elasticsearch.client.Response; -import org.elasticsearch.client.RestClient; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.core.env.Environment; -import org.springframework.stereotype.Component; - -import javax.annotation.PostConstruct; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Base64; -import java.util.List; - -/** - * ES工具类 - * - * @author zhangyingjie123 - * @since 2020-11-30 - */ -@Component -public class ElasticSearchUtil { - private static final Logger logger = LoggerFactory.getLogger(ElasticSearchUtil.class); - - @Autowired - private RestClient clientMapping; - @Autowired - private Environment env; - - private static String username; - private static String password; - - private static RestClient client; - - @PostConstruct - public void init(){ - client = clientMapping; - username = env.getProperty("casic.data.es.username"); - password = env.getProperty("casic.data.es.password"); - } - - public static JSONObject selectDocumentById(String index, String type, String id) throws IOException{ - String method = "GET"; - String endPoint = index.concat("/").concat(type).concat("/").concat(id); - Response response = performRequest(method,endPoint,null); - JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); - return result.getJSONObject("_source"); - } - - public static Response addDocumentById(String index, String type, String id, Object entity) throws IOException{ - String method = "POST"; - String endPoint = index.concat("/").concat(type).concat("/"); - if(ObjectUtil.isNotEmpty(id)){ - endPoint.concat(id); - } - endPoint.concat("?refresh=true"); - logger.debug(entity.toString()); - return performRequest(method,endPoint,JSON.toJSONString(entity)); - } - - public static Response deleteDocumentById(String index, String type, String id) throws IOException{ - String method = "DELETE"; - String endPoint = index.concat("/").concat(type).concat("/").concat(id).concat("?refresh=true"); - return performRequest(method,endPoint,null); - } - - public static Response updateDocumentById(String index, String type, String id, Object entity) throws IOException{ - String method = "POST"; - String endPoint = index.concat("/").concat(type).concat("/").concat(id).concat("?refresh=true"); - logger.debug(entity.toString()); - return performRequest(method,endPoint,JSON.toJSONString(entity)); - } - - public static EsResponse searchQuery(String index, String type, ElasticSearchQuery query) throws IOException{ - String method = "POST"; - String entPoint = index.concat("/").concat(type).concat("/").concat("_search"); - logger.debug(query.getBody().toString()); - Response response = performRequest(method, entPoint, query.getBody().toString()); - // 获取response body,转换为json对象 - JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); - return result.toJavaObject(EsResponse.class); - } - - public static Response aggsQuery(String index, String type, ElasticSearchQuery query) throws IOException{ - String method = "POST"; - String entPoint = index.concat("/").concat(type).concat("/").concat("_search"); - logger.debug(query.getBody().toString()); - Response response = performRequest(method, entPoint, query.getBody().toString()); - return response; - } - - public static List searchQueryScroll(String index, String type, ElasticSearchQuery query) throws IOException{ - // 首次查询,提交查询条件,endpoint增加‘?scroll=1m’ - List results = new ArrayList<>(); - String method = "POST"; - String entPoint = index.concat("/").concat(type).concat("/").concat("_search").concat("?scroll=1m"); - logger.debug(query.getBody().toString()); - Response response = performRequest(method, entPoint, query.getBody().toString()); - JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); - EsResponse esResponse = result.toJavaObject(EsResponse.class); - String scrollId = result.getString("_scroll_id"); - if(ObjectUtil.isNotEmpty(esResponse.getDatas())){ - results.addAll(esResponse.getDatas()); - } - // 循环发送scroll请求,直到返回结果为空 - entPoint = "_search/scroll"; - JSONObject scrollBody = new JSONObject(); - scrollBody.put("scroll","1m"); - scrollBody.put("scroll_id",scrollId); - while (esResponse.getDatas().size() > 0){ - method = "GET"; - response = performRequest(method, entPoint, scrollBody.toString()); - result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); - esResponse = result.toJavaObject(EsResponse.class); - if(ObjectUtil.isNotEmpty(esResponse.getDatas())){ - results.addAll(esResponse.getDatas()); - } - } - // 查询完成后,及时删除scroll,释放资源 - scrollBody.remove("scroll"); - method = "DELETE"; - performRequest(method, entPoint, scrollBody.toString()); - return results; - } - - public static Response bulk(String index,String type,String entity) throws IOException{ - String method = "POST"; - StringBuilder endPoint = new StringBuilder(); - if(ObjectUtil.isNotEmpty(index)){ - endPoint.append(index).append("/"); - } - if(ObjectUtil.isNotEmpty(type)){ - endPoint.append(type).append("/"); - } - endPoint.append("_bulk"); - endPoint.append("?refresh=true"); - logger.debug(entity); - return performRequest(method,endPoint.toString(),entity); - } - - private static Response performRequest(String method, String endpoint, String entity) throws IOException { - //logger.info(method + " " + endpoint); - Request request = new Request(method,endpoint); - if(ObjectUtil.isNotEmpty(entity)){ - request.setEntity(new NStringEntity(entity,"utf-8")); - } - - if(ObjectUtil.isNotEmpty(username) && ObjectUtil.isNotEmpty(password)){ - RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder(); - String token = username.concat(":").concat(password); - String base64encode = Base64.getEncoder().encodeToString(token.getBytes("utf-8")); - builder.addHeader("Authorization", "Basic " + base64encode ); - request.setOptions(builder.build()); - } - - - Response response = client.performRequest(request); - logger.debug(response.toString()); - return response; - } - -} diff --git a/casic-common/pom.xml b/casic-common/pom.xml index 27e6a55..a47c616 100644 --- a/casic-common/pom.xml +++ b/casic-common/pom.xml @@ -46,6 +46,11 @@ mina-core 2.0.4 + + com.casic + casic-data-es + 1.0.1.alpha + diff --git a/casic-common/src/main/java/com/casic/missiles/es/DataGasEs.java b/casic-common/src/main/java/com/casic/missiles/es/DataGasEs.java new file mode 100644 index 0000000..3b1f3fe --- /dev/null +++ b/casic-common/src/main/java/com/casic/missiles/es/DataGasEs.java @@ -0,0 +1,18 @@ +package com.casic.missiles.es; + +import lombok.Data; + +@Data +public class DataGasEs { + + private Long stationId; + private String stationName; + private Long monitorId; + private String monitorName; + private String devcode; + private Double direction; + private Double pitch; + private Double concentration; + private Double threshold; + private String logTime; +} diff --git a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java new file mode 100644 index 0000000..aeef63d --- /dev/null +++ b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java @@ -0,0 +1,257 @@ +package com.casic.missiles.es; + +import cn.hutool.core.date.DateUtil; +import cn.hutool.core.util.ObjectUtil; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +/** + * ES查询请求体 + * + * @author zhangyingjie123 + * @since 2020-11-30 + */ +public class ElasticSearchQuery { + private JSONObject body; + private JSONObject query; + private JSONObject bool; + private JSONArray boolMust; + private JSONObject filter; + private JSONObject filterBool; + private JSONArray filterBoolMust; + private JSONArray filterBoolMustnot; + + public static final String MUST_FLAG = "1"; + public static final String MUST_NOT_FLAG = "0"; + + public static final String QUERY = "query"; + public static final String BOOL = "bool"; + public static final String MUST = "must"; + public static final String MUST_NOT = "must_not"; + public static final String FILTER = "filter"; + + + /** + * 生成查询请求体基础结构: + * { + * "query": { + * "bool": { + * "must" [], + * "filter": { + * "bool": { + * "must": [], + * "must_not": [] + * } + * } + * } + * } + * } + */ + public ElasticSearchQuery() { + this.body = new JSONObject(); + this.query = this.query(); + this.bool = this.bool(); + this.boolMust = this.boolMust(); + this.filter = this.filter(); + this.filterBool = this.filterBool(); + this.filterBoolMust = this.filterBoolMust(); + this.filterBoolMustnot = this.filterBoolMustnot(); + } + + public ElasticSearchQuery(JSONObject body){ + this.body = body; + } + + public JSONObject getBody() { + return body; + } + + private JSONObject query(){ + if(ObjectUtil.isEmpty(this.body.get(QUERY))){ + this.body.put("query",new JSONObject()); + } + return this.body.getJSONObject(QUERY); + + } + + private JSONObject bool() { + JSONObject query = this.query; + if(ObjectUtil.isEmpty(query.get(BOOL))){ + query.put(BOOL,new JSONObject()); + } + return query.getJSONObject(BOOL); + } + + private JSONArray boolMust(){ + JSONObject bool = this.bool; + if(ObjectUtil.isEmpty(bool.get(MUST))){ + bool.put(MUST,new JSONArray()); + } + return bool.getJSONArray(MUST); + } + + private JSONObject filter(){ + JSONObject bool = this.bool; + if(ObjectUtil.isEmpty(bool.get(FILTER))){ + bool.put(FILTER,new JSONObject()); + } + return bool.getJSONObject(FILTER); + } + + private JSONObject filterBool(){ + JSONObject filter = this.filter; + if(ObjectUtil.isEmpty(filter.get(BOOL))){ + filter.put(BOOL,new JSONObject()); + } + return filter.getJSONObject(BOOL); + } + + private JSONArray filterBoolMust(){ + JSONObject bool = this.filterBool; + if(ObjectUtil.isEmpty(bool.get(MUST))){ + bool.put(MUST,new JSONArray()); + } + return bool.getJSONArray(MUST); + } + + private JSONArray filterBoolMustnot(){ + JSONObject bool = this.filterBool; + if(ObjectUtil.isEmpty(bool.get(MUST_NOT))){ + bool.put(MUST_NOT,new JSONArray()); + } + return bool.getJSONArray(MUST_NOT); + } + + public void match(String filed, Serializable value){ + JSONArray boolMust = this.boolMust; + JSONObject match = new JSONObject(); + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,value); + match.put("match",filedJson); + boolMust.add(match); + } + + public void matchPhrase(String filed, Serializable value){ + JSONArray boolMust = this.boolMust; + JSONObject matchPhrase = new JSONObject(); + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,value); + matchPhrase.put("match_phrase",filedJson); + boolMust.add(matchPhrase); + } + + public void term(String filed, Serializable value){ + this.term(filed,value,MUST_FLAG); + } + + public void term(String filed, Serializable value,String mustFlag){ + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,value); + JSONObject term = new JSONObject(); + term.put("term",filedJson); + if(MUST_FLAG.equals(mustFlag)){ + this.filterBoolMust.add(term); + } + if(MUST_NOT_FLAG.equals(mustFlag)){ + this.filterBoolMustnot.add(term); + } + } + + public void terms(String filed,List value){ + this.terms(filed,value,MUST_FLAG); + } + + public void terms(String filed, List value, String mustFlag){ + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,value); + JSONObject terms = new JSONObject(); + terms.put("terms",filedJson); + if(MUST_FLAG.equals(mustFlag)){ + this.filterBoolMust.add(terms); + } + if(MUST_NOT_FLAG.equals(mustFlag)){ + this.filterBoolMustnot.add(terms); + } + } + + public void range(String filed, Serializable from, Serializable to){ + this.range(filed,from,to,MUST_FLAG); + } + + public void range(String filed, Serializable from, Serializable to, String mustFlag){ + JSONObject rangeJson = new JSONObject(); + if(ObjectUtil.isNotEmpty(from)){ + rangeJson.put("from",from); + } + if(ObjectUtil.isNotEmpty(to)){ + rangeJson.put("to",to); + } + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,rangeJson); + JSONObject range = new JSONObject(); + range.put("range",filedJson); + if(MUST_FLAG.equals(mustFlag)){ + this.filterBoolMust.add(range); + } + if(MUST_NOT_FLAG.equals(mustFlag)){ + this.filterBoolMustnot.add(range); + } + } + + public void size(int size){ + this.body.put("size",size); + } + + public void from(int from) { + this.body.put("from",from); + } + + public void sort(String sort, String order){ + JSONObject sortJson = new JSONObject(); + JSONObject orderJson = new JSONObject(); + orderJson.put("order",order); + sortJson.put(sort,orderJson); + this.body.put("sort",sortJson); + } + + public void aggs(String aggsName, String filed){ + JSONObject terms = new JSONObject(); + terms.put("field",filed); + JSONObject aggsTerms = new JSONObject(); + aggsTerms.put("terms",terms); + JSONObject aggsBody = new JSONObject(); + aggsBody.put(aggsName,aggsTerms); + this.body.put("aggs",aggsBody); + } + + public void aggs(JSONObject aggBody){ + this.body.put("aggs",aggBody); + } + + + + public static void main(String[] args) { + ElasticSearchQuery query = new ElasticSearchQuery(); + query.matchPhrase("name","张三"); + query.term("sex","2"); + List deptIds = new ArrayList<>(); + deptIds.add(0L); + deptIds.add(24L); + query.terms("deptId", deptIds); + Date date1 = DateUtil.parse("2020-01-01","yyyy-MM-dd"); + Date date2 = DateUtil.parse("2020-04-01","yyyy-MM-dd"); + query.range("collTime",date1.getTime(),date2.getTime()); + query.term("collState","3",MUST_NOT_FLAG); + query.size(10); + query.from(0); + query.sort("irId","asc"); + System.out.println(query.getBody().toJSONString()); + } + + +} diff --git a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java new file mode 100644 index 0000000..dc9a968 --- /dev/null +++ b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java @@ -0,0 +1,172 @@ +package com.casic.missiles.es; + +import cn.hutool.core.util.ObjectUtil; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.casic.missiles.core.es.EsResponse; +import org.apache.http.nio.entity.NStringEntity; +import org.apache.http.util.EntityUtils; +import org.elasticsearch.client.Request; +import org.elasticsearch.client.RequestOptions; +import org.elasticsearch.client.Response; +import org.elasticsearch.client.RestClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Base64; +import java.util.List; + +/** + * ES工具类 + * + * @author zhangyingjie123 + * @since 2020-11-30 + */ +@Component +public class ElasticSearchUtil { + private static final Logger logger = LoggerFactory.getLogger(ElasticSearchUtil.class); + + @Autowired + private RestClient clientMapping; + @Autowired + private Environment env; + + private static String username; + private static String password; + + private static RestClient client; + + @PostConstruct + public void init(){ + client = clientMapping; + username = env.getProperty("casic.data.es.username"); + password = env.getProperty("casic.data.es.password"); + } + + public static JSONObject selectDocumentById(String index, String type, String id) throws IOException{ + String method = "GET"; + String endPoint = index.concat("/").concat(type).concat("/").concat(id); + Response response = performRequest(method,endPoint,null); + JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); + return result.getJSONObject("_source"); + } + + public static Response addDocumentById(String index, String type, String id, Object entity) throws IOException{ + String method = "POST"; + String endPoint = index.concat("/").concat(type).concat("/"); + if(ObjectUtil.isNotEmpty(id)){ + endPoint.concat(id); + } + endPoint.concat("?refresh=true"); + logger.debug(entity.toString()); + return performRequest(method,endPoint,JSON.toJSONString(entity)); + } + + public static Response deleteDocumentById(String index, String type, String id) throws IOException{ + String method = "DELETE"; + String endPoint = index.concat("/").concat(type).concat("/").concat(id).concat("?refresh=true"); + return performRequest(method,endPoint,null); + } + + public static Response updateDocumentById(String index, String type, String id, Object entity) throws IOException{ + String method = "POST"; + String endPoint = index.concat("/").concat(type).concat("/").concat(id).concat("?refresh=true"); + logger.debug(entity.toString()); + return performRequest(method,endPoint,JSON.toJSONString(entity)); + } + + public static EsResponse searchQuery(String index, String type, ElasticSearchQuery query) throws IOException{ + String method = "POST"; + String entPoint = index.concat("/").concat(type).concat("/").concat("_search"); + logger.debug(query.getBody().toString()); + Response response = performRequest(method, entPoint, query.getBody().toString()); + // 获取response body,转换为json对象 + JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); + return result.toJavaObject(EsResponse.class); + } + + public static Response aggsQuery(String index, String type, ElasticSearchQuery query) throws IOException{ + String method = "POST"; + String entPoint = index.concat("/").concat(type).concat("/").concat("_search"); + logger.debug(query.getBody().toString()); + Response response = performRequest(method, entPoint, query.getBody().toString()); + return response; + } + + public static List searchQueryScroll(String index, String type, ElasticSearchQuery query) throws IOException{ + // 首次查询,提交查询条件,endpoint增加‘?scroll=1m’ + List results = new ArrayList<>(); + String method = "POST"; + String entPoint = index.concat("/").concat(type).concat("/").concat("_search").concat("?scroll=1m"); + logger.debug(query.getBody().toString()); + Response response = performRequest(method, entPoint, query.getBody().toString()); + JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); + EsResponse esResponse = result.toJavaObject(EsResponse.class); + String scrollId = result.getString("_scroll_id"); + if(ObjectUtil.isNotEmpty(esResponse.getDatas())){ + results.addAll(esResponse.getDatas()); + } + // 循环发送scroll请求,直到返回结果为空 + entPoint = "_search/scroll"; + JSONObject scrollBody = new JSONObject(); + scrollBody.put("scroll","1m"); + scrollBody.put("scroll_id",scrollId); + while (esResponse.getDatas().size() > 0){ + method = "GET"; + response = performRequest(method, entPoint, scrollBody.toString()); + result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); + esResponse = result.toJavaObject(EsResponse.class); + if(ObjectUtil.isNotEmpty(esResponse.getDatas())){ + results.addAll(esResponse.getDatas()); + } + } + // 查询完成后,及时删除scroll,释放资源 + scrollBody.remove("scroll"); + method = "DELETE"; + performRequest(method, entPoint, scrollBody.toString()); + return results; + } + + public static Response bulk(String index,String type,String entity) throws IOException{ + String method = "POST"; + StringBuilder endPoint = new StringBuilder(); + if(ObjectUtil.isNotEmpty(index)){ + endPoint.append(index).append("/"); + } + if(ObjectUtil.isNotEmpty(type)){ + endPoint.append(type).append("/"); + } + endPoint.append("_bulk"); + endPoint.append("?refresh=true"); + logger.debug(entity); + return performRequest(method,endPoint.toString(),entity); + } + + private static Response performRequest(String method, String endpoint, String entity) throws IOException { + //logger.info(method + " " + endpoint); + Request request = new Request(method,endpoint); + if(ObjectUtil.isNotEmpty(entity)){ + request.setEntity(new NStringEntity(entity,"utf-8")); + } + + if(ObjectUtil.isNotEmpty(username) && ObjectUtil.isNotEmpty(password)){ + RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder(); + String token = username.concat(":").concat(password); + String base64encode = Base64.getEncoder().encodeToString(token.getBytes("utf-8")); + builder.addHeader("Authorization", "Basic " + base64encode ); + request.setOptions(builder.build()); + } + + + Response response = client.performRequest(request); + logger.debug(response.toString()); + return response; + } + +} diff --git a/casic-common/src/main/java/com/casic/missiles/weigeng/CommDoorUtil.java b/casic-common/src/main/java/com/casic/missiles/weigeng/CommDoorUtil.java index bed1361..7457109 100644 --- a/casic-common/src/main/java/com/casic/missiles/weigeng/CommDoorUtil.java +++ b/casic-common/src/main/java/com/casic/missiles/weigeng/CommDoorUtil.java @@ -118,15 +118,14 @@ } //重启设备 - public static boolean restart(String monitorId) { + public static boolean restart(String controllerIP, String controllerSN) { try { - String doors = ObjectUtil.isNotEmpty(map.get(monitorId)) ? map.get(monitorId).toString() : ""; - if (ObjectUtil.isNotEmpty(doors)) - if (DoorShortUtil.remoteControl(doors.split(",")[1], Long.valueOf(doors.split(",")[0]), 2, 1)) { + if (ObjectUtil.isAllNotEmpty(controllerIP, controllerSN)) + if (DoorShortUtil.remoteControl(controllerIP, Long.valueOf(controllerSN), 2, 1)) { //重新上电需要间隔20秒 Thread.sleep(Integer.valueOf(map.get("time").toString()) * 1000); //重新关闭开关上电 - return DoorShortUtil.remoteControl(doors.split(",")[1], Long.valueOf(doors.split(",")[0]), 2, 2); + return DoorShortUtil.remoteControl(controllerIP, Long.valueOf(controllerSN), 2, 2); } } catch (Exception e) { e.printStackTrace(); @@ -135,16 +134,14 @@ } //重启设备 - public static boolean interruptStart(String monitorId) { + public static boolean interruptStart(String controllerIP, String controllerSN) { try { - String doors = ObjectUtil.isNotEmpty(map.get(monitorId)) ? map.get(monitorId).toString() : ""; - if (ObjectUtil.isNotEmpty(doors)) - if (DoorShortUtil.remoteControl(doors.split(",")[1], Long.valueOf(doors.split(",")[0]), 2, 1)) { + if (ObjectUtil.isAllNotEmpty(controllerIP,controllerSN)) + if (DoorShortUtil.remoteControl(controllerIP, Long.valueOf(controllerSN), 2, 1)) { //重新上电需要间隔20秒 Thread.sleep(Integer.valueOf(map.get("time").toString()) * 1000); //重新关闭开关上电 - DoorShortUtil.remoteControl(doors.split(",")[1], Long.valueOf(doors.split(",")[0]), 2, 2); - + DoorShortUtil.remoteControl(controllerIP, Long.valueOf(controllerSN), 2, 2); Thread.sleep(2 * 60 * 1000); return true; } diff --git a/casic-data/pom.xml b/casic-data/pom.xml index aca0a62..a076800 100644 --- a/casic-data/pom.xml +++ b/casic-data/pom.xml @@ -43,11 +43,6 @@ com.casic - casic-data-es - 1.0.1.alpha - - - com.casic casic-common 2.0.0 compile diff --git a/casic-data/src/main/java/com/casic/missiles/modular/system/es/DataGasEs.java b/casic-data/src/main/java/com/casic/missiles/modular/system/es/DataGasEs.java deleted file mode 100644 index 821e8f1..0000000 --- a/casic-data/src/main/java/com/casic/missiles/modular/system/es/DataGasEs.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.casic.missiles.modular.system.es; - -import lombok.Data; - -@Data -public class DataGasEs { - - private Long stationId; - private String stationName; - private Long monitorId; - private String monitorName; - private String devcode; - private Double direction; - private Double pitch; - private Double concentration; - private Double threshold; - private String logTime; -} diff --git a/casic-data/src/main/java/com/casic/missiles/modular/system/es/ElasticSearchQuery.java b/casic-data/src/main/java/com/casic/missiles/modular/system/es/ElasticSearchQuery.java deleted file mode 100644 index 4d7a38f..0000000 --- a/casic-data/src/main/java/com/casic/missiles/modular/system/es/ElasticSearchQuery.java +++ /dev/null @@ -1,257 +0,0 @@ -package com.casic.missiles.modular.system.es; - -import cn.hutool.core.date.DateUtil; -import cn.hutool.core.util.ObjectUtil; -import com.alibaba.fastjson.JSONArray; -import com.alibaba.fastjson.JSONObject; - -import java.io.Serializable; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; - -/** - * ES查询请求体 - * - * @author zhangyingjie123 - * @since 2020-11-30 - */ -public class ElasticSearchQuery { - private JSONObject body; - private JSONObject query; - private JSONObject bool; - private JSONArray boolMust; - private JSONObject filter; - private JSONObject filterBool; - private JSONArray filterBoolMust; - private JSONArray filterBoolMustnot; - - public static final String MUST_FLAG = "1"; - public static final String MUST_NOT_FLAG = "0"; - - public static final String QUERY = "query"; - public static final String BOOL = "bool"; - public static final String MUST = "must"; - public static final String MUST_NOT = "must_not"; - public static final String FILTER = "filter"; - - - /** - * 生成查询请求体基础结构: - * { - * "query": { - * "bool": { - * "must" [], - * "filter": { - * "bool": { - * "must": [], - * "must_not": [] - * } - * } - * } - * } - * } - */ - public ElasticSearchQuery() { - this.body = new JSONObject(); - this.query = this.query(); - this.bool = this.bool(); - this.boolMust = this.boolMust(); - this.filter = this.filter(); - this.filterBool = this.filterBool(); - this.filterBoolMust = this.filterBoolMust(); - this.filterBoolMustnot = this.filterBoolMustnot(); - } - - public ElasticSearchQuery(JSONObject body){ - this.body = body; - } - - public JSONObject getBody() { - return body; - } - - private JSONObject query(){ - if(ObjectUtil.isEmpty(this.body.get(QUERY))){ - this.body.put("query",new JSONObject()); - } - return this.body.getJSONObject(QUERY); - - } - - private JSONObject bool() { - JSONObject query = this.query; - if(ObjectUtil.isEmpty(query.get(BOOL))){ - query.put(BOOL,new JSONObject()); - } - return query.getJSONObject(BOOL); - } - - private JSONArray boolMust(){ - JSONObject bool = this.bool; - if(ObjectUtil.isEmpty(bool.get(MUST))){ - bool.put(MUST,new JSONArray()); - } - return bool.getJSONArray(MUST); - } - - private JSONObject filter(){ - JSONObject bool = this.bool; - if(ObjectUtil.isEmpty(bool.get(FILTER))){ - bool.put(FILTER,new JSONObject()); - } - return bool.getJSONObject(FILTER); - } - - private JSONObject filterBool(){ - JSONObject filter = this.filter; - if(ObjectUtil.isEmpty(filter.get(BOOL))){ - filter.put(BOOL,new JSONObject()); - } - return filter.getJSONObject(BOOL); - } - - private JSONArray filterBoolMust(){ - JSONObject bool = this.filterBool; - if(ObjectUtil.isEmpty(bool.get(MUST))){ - bool.put(MUST,new JSONArray()); - } - return bool.getJSONArray(MUST); - } - - private JSONArray filterBoolMustnot(){ - JSONObject bool = this.filterBool; - if(ObjectUtil.isEmpty(bool.get(MUST_NOT))){ - bool.put(MUST_NOT,new JSONArray()); - } - return bool.getJSONArray(MUST_NOT); - } - - public void match(String filed, Serializable value){ - JSONArray boolMust = this.boolMust; - JSONObject match = new JSONObject(); - JSONObject filedJson = new JSONObject(); - filedJson.put(filed,value); - match.put("match",filedJson); - boolMust.add(match); - } - - public void matchPhrase(String filed, Serializable value){ - JSONArray boolMust = this.boolMust; - JSONObject matchPhrase = new JSONObject(); - JSONObject filedJson = new JSONObject(); - filedJson.put(filed,value); - matchPhrase.put("match_phrase",filedJson); - boolMust.add(matchPhrase); - } - - public void term(String filed, Serializable value){ - this.term(filed,value,MUST_FLAG); - } - - public void term(String filed, Serializable value,String mustFlag){ - JSONObject filedJson = new JSONObject(); - filedJson.put(filed,value); - JSONObject term = new JSONObject(); - term.put("term",filedJson); - if(MUST_FLAG.equals(mustFlag)){ - this.filterBoolMust.add(term); - } - if(MUST_NOT_FLAG.equals(mustFlag)){ - this.filterBoolMustnot.add(term); - } - } - - public void terms(String filed,List value){ - this.terms(filed,value,MUST_FLAG); - } - - public void terms(String filed, List value, String mustFlag){ - JSONObject filedJson = new JSONObject(); - filedJson.put(filed,value); - JSONObject terms = new JSONObject(); - terms.put("terms",filedJson); - if(MUST_FLAG.equals(mustFlag)){ - this.filterBoolMust.add(terms); - } - if(MUST_NOT_FLAG.equals(mustFlag)){ - this.filterBoolMustnot.add(terms); - } - } - - public void range(String filed, Serializable from, Serializable to){ - this.range(filed,from,to,MUST_FLAG); - } - - public void range(String filed, Serializable from, Serializable to, String mustFlag){ - JSONObject rangeJson = new JSONObject(); - if(ObjectUtil.isNotEmpty(from)){ - rangeJson.put("from",from); - } - if(ObjectUtil.isNotEmpty(to)){ - rangeJson.put("to",to); - } - JSONObject filedJson = new JSONObject(); - filedJson.put(filed,rangeJson); - JSONObject range = new JSONObject(); - range.put("range",filedJson); - if(MUST_FLAG.equals(mustFlag)){ - this.filterBoolMust.add(range); - } - if(MUST_NOT_FLAG.equals(mustFlag)){ - this.filterBoolMustnot.add(range); - } - } - - public void size(int size){ - this.body.put("size",size); - } - - public void from(int from) { - this.body.put("from",from); - } - - public void sort(String sort, String order){ - JSONObject sortJson = new JSONObject(); - JSONObject orderJson = new JSONObject(); - orderJson.put("order",order); - sortJson.put(sort,orderJson); - this.body.put("sort",sortJson); - } - - public void aggs(String aggsName, String filed){ - JSONObject terms = new JSONObject(); - terms.put("field",filed); - JSONObject aggsTerms = new JSONObject(); - aggsTerms.put("terms",terms); - JSONObject aggsBody = new JSONObject(); - aggsBody.put(aggsName,aggsTerms); - this.body.put("aggs",aggsBody); - } - - public void aggs(JSONObject aggBody){ - this.body.put("aggs",aggBody); - } - - - - public static void main(String[] args) { - ElasticSearchQuery query = new ElasticSearchQuery(); - query.matchPhrase("name","张三"); - query.term("sex","2"); - List deptIds = new ArrayList<>(); - deptIds.add(0L); - deptIds.add(24L); - query.terms("deptId", deptIds); - Date date1 = DateUtil.parse("2020-01-01","yyyy-MM-dd"); - Date date2 = DateUtil.parse("2020-04-01","yyyy-MM-dd"); - query.range("collTime",date1.getTime(),date2.getTime()); - query.term("collState","3",MUST_NOT_FLAG); - query.size(10); - query.from(0); - query.sort("irId","asc"); - System.out.println(query.getBody().toJSONString()); - } - - -} diff --git a/casic-data/src/main/java/com/casic/missiles/modular/system/es/ElasticSearchUtil.java b/casic-data/src/main/java/com/casic/missiles/modular/system/es/ElasticSearchUtil.java deleted file mode 100644 index e9da021..0000000 --- a/casic-data/src/main/java/com/casic/missiles/modular/system/es/ElasticSearchUtil.java +++ /dev/null @@ -1,172 +0,0 @@ -package com.casic.missiles.modular.system.es; - -import cn.hutool.core.util.ObjectUtil; -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONObject; -import com.casic.missiles.core.es.EsResponse; -import org.apache.http.nio.entity.NStringEntity; -import org.apache.http.util.EntityUtils; -import org.elasticsearch.client.Request; -import org.elasticsearch.client.RequestOptions; -import org.elasticsearch.client.Response; -import org.elasticsearch.client.RestClient; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.core.env.Environment; -import org.springframework.stereotype.Component; - -import javax.annotation.PostConstruct; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Base64; -import java.util.List; - -/** - * ES工具类 - * - * @author zhangyingjie123 - * @since 2020-11-30 - */ -@Component -public class ElasticSearchUtil { - private static final Logger logger = LoggerFactory.getLogger(ElasticSearchUtil.class); - - @Autowired - private RestClient clientMapping; - @Autowired - private Environment env; - - private static String username; - private static String password; - - private static RestClient client; - - @PostConstruct - public void init(){ - client = clientMapping; - username = env.getProperty("casic.data.es.username"); - password = env.getProperty("casic.data.es.password"); - } - - public static JSONObject selectDocumentById(String index, String type, String id) throws IOException{ - String method = "GET"; - String endPoint = index.concat("/").concat(type).concat("/").concat(id); - Response response = performRequest(method,endPoint,null); - JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); - return result.getJSONObject("_source"); - } - - public static Response addDocumentById(String index, String type, String id, Object entity) throws IOException{ - String method = "POST"; - String endPoint = index.concat("/").concat(type).concat("/"); - if(ObjectUtil.isNotEmpty(id)){ - endPoint.concat(id); - } - endPoint.concat("?refresh=true"); - logger.debug(entity.toString()); - return performRequest(method,endPoint,JSON.toJSONString(entity)); - } - - public static Response deleteDocumentById(String index, String type, String id) throws IOException{ - String method = "DELETE"; - String endPoint = index.concat("/").concat(type).concat("/").concat(id).concat("?refresh=true"); - return performRequest(method,endPoint,null); - } - - public static Response updateDocumentById(String index, String type, String id, Object entity) throws IOException{ - String method = "POST"; - String endPoint = index.concat("/").concat(type).concat("/").concat(id).concat("?refresh=true"); - logger.debug(entity.toString()); - return performRequest(method,endPoint,JSON.toJSONString(entity)); - } - - public static EsResponse searchQuery(String index, String type, ElasticSearchQuery query) throws IOException{ - String method = "POST"; - String entPoint = index.concat("/").concat(type).concat("/").concat("_search"); - logger.debug(query.getBody().toString()); - Response response = performRequest(method, entPoint, query.getBody().toString()); - // 获取response body,转换为json对象 - JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); - return result.toJavaObject(EsResponse.class); - } - - public static Response aggsQuery(String index, String type, ElasticSearchQuery query) throws IOException{ - String method = "POST"; - String entPoint = index.concat("/").concat(type).concat("/").concat("_search"); - logger.debug(query.getBody().toString()); - Response response = performRequest(method, entPoint, query.getBody().toString()); - return response; - } - - public static List searchQueryScroll(String index, String type, ElasticSearchQuery query) throws IOException{ - // 首次查询,提交查询条件,endpoint增加‘?scroll=1m’ - List results = new ArrayList<>(); - String method = "POST"; - String entPoint = index.concat("/").concat(type).concat("/").concat("_search").concat("?scroll=1m"); - logger.debug(query.getBody().toString()); - Response response = performRequest(method, entPoint, query.getBody().toString()); - JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); - EsResponse esResponse = result.toJavaObject(EsResponse.class); - String scrollId = result.getString("_scroll_id"); - if(ObjectUtil.isNotEmpty(esResponse.getDatas())){ - results.addAll(esResponse.getDatas()); - } - // 循环发送scroll请求,直到返回结果为空 - entPoint = "_search/scroll"; - JSONObject scrollBody = new JSONObject(); - scrollBody.put("scroll","1m"); - scrollBody.put("scroll_id",scrollId); - while (esResponse.getDatas().size() > 0){ - method = "GET"; - response = performRequest(method, entPoint, scrollBody.toString()); - result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); - esResponse = result.toJavaObject(EsResponse.class); - if(ObjectUtil.isNotEmpty(esResponse.getDatas())){ - results.addAll(esResponse.getDatas()); - } - } - // 查询完成后,及时删除scroll,释放资源 - scrollBody.remove("scroll"); - method = "DELETE"; - performRequest(method, entPoint, scrollBody.toString()); - return results; - } - - public static Response bulk(String index,String type,String entity) throws IOException{ - String method = "POST"; - StringBuilder endPoint = new StringBuilder(); - if(ObjectUtil.isNotEmpty(index)){ - endPoint.append(index).append("/"); - } - if(ObjectUtil.isNotEmpty(type)){ - endPoint.append(type).append("/"); - } - endPoint.append("_bulk"); - endPoint.append("?refresh=true"); - logger.debug(entity); - return performRequest(method,endPoint.toString(),entity); - } - - private static Response performRequest(String method, String endpoint, String entity) throws IOException { - //logger.info(method + " " + endpoint); - Request request = new Request(method,endpoint); - if(ObjectUtil.isNotEmpty(entity)){ - request.setEntity(new NStringEntity(entity,"utf-8")); - } - - if(ObjectUtil.isNotEmpty(username) && ObjectUtil.isNotEmpty(password)){ - RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder(); - String token = username.concat(":").concat(password); - String base64encode = Base64.getEncoder().encodeToString(token.getBytes("utf-8")); - builder.addHeader("Authorization", "Basic " + base64encode ); - request.setOptions(builder.build()); - } - - - Response response = client.performRequest(request); - logger.debug(response.toString()); - return response; - } - -} diff --git a/casic-data/src/main/java/com/casic/missiles/modular/system/service/IDataGasService.java b/casic-data/src/main/java/com/casic/missiles/modular/system/service/IDataGasService.java index e644294..b2aa6cb 100644 --- a/casic-data/src/main/java/com/casic/missiles/modular/system/service/IDataGasService.java +++ b/casic-data/src/main/java/com/casic/missiles/modular/system/service/IDataGasService.java @@ -1,6 +1,6 @@ package com.casic.missiles.modular.system.service; -import com.casic.missiles.modular.system.es.DataGasEs; +import com.casic.missiles.es.DataGasEs; import com.casic.missiles.modular.system.model.DataGas; import com.baomidou.mybatisplus.extension.service.IService; diff --git a/casic-common/pom.xml b/casic-common/pom.xml index 27e6a55..a47c616 100644 --- a/casic-common/pom.xml +++ b/casic-common/pom.xml @@ -46,6 +46,11 @@ mina-core 2.0.4 + + com.casic + casic-data-es + 1.0.1.alpha + diff --git a/casic-common/src/main/java/com/casic/missiles/es/DataGasEs.java b/casic-common/src/main/java/com/casic/missiles/es/DataGasEs.java new file mode 100644 index 0000000..3b1f3fe --- /dev/null +++ b/casic-common/src/main/java/com/casic/missiles/es/DataGasEs.java @@ -0,0 +1,18 @@ +package com.casic.missiles.es; + +import lombok.Data; + +@Data +public class DataGasEs { + + private Long stationId; + private String stationName; + private Long monitorId; + private String monitorName; + private String devcode; + private Double direction; + private Double pitch; + private Double concentration; + private Double threshold; + private String logTime; +} diff --git a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java new file mode 100644 index 0000000..aeef63d --- /dev/null +++ b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java @@ -0,0 +1,257 @@ +package com.casic.missiles.es; + +import cn.hutool.core.date.DateUtil; +import cn.hutool.core.util.ObjectUtil; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +/** + * ES查询请求体 + * + * @author zhangyingjie123 + * @since 2020-11-30 + */ +public class ElasticSearchQuery { + private JSONObject body; + private JSONObject query; + private JSONObject bool; + private JSONArray boolMust; + private JSONObject filter; + private JSONObject filterBool; + private JSONArray filterBoolMust; + private JSONArray filterBoolMustnot; + + public static final String MUST_FLAG = "1"; + public static final String MUST_NOT_FLAG = "0"; + + public static final String QUERY = "query"; + public static final String BOOL = "bool"; + public static final String MUST = "must"; + public static final String MUST_NOT = "must_not"; + public static final String FILTER = "filter"; + + + /** + * 生成查询请求体基础结构: + * { + * "query": { + * "bool": { + * "must" [], + * "filter": { + * "bool": { + * "must": [], + * "must_not": [] + * } + * } + * } + * } + * } + */ + public ElasticSearchQuery() { + this.body = new JSONObject(); + this.query = this.query(); + this.bool = this.bool(); + this.boolMust = this.boolMust(); + this.filter = this.filter(); + this.filterBool = this.filterBool(); + this.filterBoolMust = this.filterBoolMust(); + this.filterBoolMustnot = this.filterBoolMustnot(); + } + + public ElasticSearchQuery(JSONObject body){ + this.body = body; + } + + public JSONObject getBody() { + return body; + } + + private JSONObject query(){ + if(ObjectUtil.isEmpty(this.body.get(QUERY))){ + this.body.put("query",new JSONObject()); + } + return this.body.getJSONObject(QUERY); + + } + + private JSONObject bool() { + JSONObject query = this.query; + if(ObjectUtil.isEmpty(query.get(BOOL))){ + query.put(BOOL,new JSONObject()); + } + return query.getJSONObject(BOOL); + } + + private JSONArray boolMust(){ + JSONObject bool = this.bool; + if(ObjectUtil.isEmpty(bool.get(MUST))){ + bool.put(MUST,new JSONArray()); + } + return bool.getJSONArray(MUST); + } + + private JSONObject filter(){ + JSONObject bool = this.bool; + if(ObjectUtil.isEmpty(bool.get(FILTER))){ + bool.put(FILTER,new JSONObject()); + } + return bool.getJSONObject(FILTER); + } + + private JSONObject filterBool(){ + JSONObject filter = this.filter; + if(ObjectUtil.isEmpty(filter.get(BOOL))){ + filter.put(BOOL,new JSONObject()); + } + return filter.getJSONObject(BOOL); + } + + private JSONArray filterBoolMust(){ + JSONObject bool = this.filterBool; + if(ObjectUtil.isEmpty(bool.get(MUST))){ + bool.put(MUST,new JSONArray()); + } + return bool.getJSONArray(MUST); + } + + private JSONArray filterBoolMustnot(){ + JSONObject bool = this.filterBool; + if(ObjectUtil.isEmpty(bool.get(MUST_NOT))){ + bool.put(MUST_NOT,new JSONArray()); + } + return bool.getJSONArray(MUST_NOT); + } + + public void match(String filed, Serializable value){ + JSONArray boolMust = this.boolMust; + JSONObject match = new JSONObject(); + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,value); + match.put("match",filedJson); + boolMust.add(match); + } + + public void matchPhrase(String filed, Serializable value){ + JSONArray boolMust = this.boolMust; + JSONObject matchPhrase = new JSONObject(); + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,value); + matchPhrase.put("match_phrase",filedJson); + boolMust.add(matchPhrase); + } + + public void term(String filed, Serializable value){ + this.term(filed,value,MUST_FLAG); + } + + public void term(String filed, Serializable value,String mustFlag){ + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,value); + JSONObject term = new JSONObject(); + term.put("term",filedJson); + if(MUST_FLAG.equals(mustFlag)){ + this.filterBoolMust.add(term); + } + if(MUST_NOT_FLAG.equals(mustFlag)){ + this.filterBoolMustnot.add(term); + } + } + + public void terms(String filed,List value){ + this.terms(filed,value,MUST_FLAG); + } + + public void terms(String filed, List value, String mustFlag){ + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,value); + JSONObject terms = new JSONObject(); + terms.put("terms",filedJson); + if(MUST_FLAG.equals(mustFlag)){ + this.filterBoolMust.add(terms); + } + if(MUST_NOT_FLAG.equals(mustFlag)){ + this.filterBoolMustnot.add(terms); + } + } + + public void range(String filed, Serializable from, Serializable to){ + this.range(filed,from,to,MUST_FLAG); + } + + public void range(String filed, Serializable from, Serializable to, String mustFlag){ + JSONObject rangeJson = new JSONObject(); + if(ObjectUtil.isNotEmpty(from)){ + rangeJson.put("from",from); + } + if(ObjectUtil.isNotEmpty(to)){ + rangeJson.put("to",to); + } + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,rangeJson); + JSONObject range = new JSONObject(); + range.put("range",filedJson); + if(MUST_FLAG.equals(mustFlag)){ + this.filterBoolMust.add(range); + } + if(MUST_NOT_FLAG.equals(mustFlag)){ + this.filterBoolMustnot.add(range); + } + } + + public void size(int size){ + this.body.put("size",size); + } + + public void from(int from) { + this.body.put("from",from); + } + + public void sort(String sort, String order){ + JSONObject sortJson = new JSONObject(); + JSONObject orderJson = new JSONObject(); + orderJson.put("order",order); + sortJson.put(sort,orderJson); + this.body.put("sort",sortJson); + } + + public void aggs(String aggsName, String filed){ + JSONObject terms = new JSONObject(); + terms.put("field",filed); + JSONObject aggsTerms = new JSONObject(); + aggsTerms.put("terms",terms); + JSONObject aggsBody = new JSONObject(); + aggsBody.put(aggsName,aggsTerms); + this.body.put("aggs",aggsBody); + } + + public void aggs(JSONObject aggBody){ + this.body.put("aggs",aggBody); + } + + + + public static void main(String[] args) { + ElasticSearchQuery query = new ElasticSearchQuery(); + query.matchPhrase("name","张三"); + query.term("sex","2"); + List deptIds = new ArrayList<>(); + deptIds.add(0L); + deptIds.add(24L); + query.terms("deptId", deptIds); + Date date1 = DateUtil.parse("2020-01-01","yyyy-MM-dd"); + Date date2 = DateUtil.parse("2020-04-01","yyyy-MM-dd"); + query.range("collTime",date1.getTime(),date2.getTime()); + query.term("collState","3",MUST_NOT_FLAG); + query.size(10); + query.from(0); + query.sort("irId","asc"); + System.out.println(query.getBody().toJSONString()); + } + + +} diff --git a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java new file mode 100644 index 0000000..dc9a968 --- /dev/null +++ b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java @@ -0,0 +1,172 @@ +package com.casic.missiles.es; + +import cn.hutool.core.util.ObjectUtil; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.casic.missiles.core.es.EsResponse; +import org.apache.http.nio.entity.NStringEntity; +import org.apache.http.util.EntityUtils; +import org.elasticsearch.client.Request; +import org.elasticsearch.client.RequestOptions; +import org.elasticsearch.client.Response; +import org.elasticsearch.client.RestClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Base64; +import java.util.List; + +/** + * ES工具类 + * + * @author zhangyingjie123 + * @since 2020-11-30 + */ +@Component +public class ElasticSearchUtil { + private static final Logger logger = LoggerFactory.getLogger(ElasticSearchUtil.class); + + @Autowired + private RestClient clientMapping; + @Autowired + private Environment env; + + private static String username; + private static String password; + + private static RestClient client; + + @PostConstruct + public void init(){ + client = clientMapping; + username = env.getProperty("casic.data.es.username"); + password = env.getProperty("casic.data.es.password"); + } + + public static JSONObject selectDocumentById(String index, String type, String id) throws IOException{ + String method = "GET"; + String endPoint = index.concat("/").concat(type).concat("/").concat(id); + Response response = performRequest(method,endPoint,null); + JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); + return result.getJSONObject("_source"); + } + + public static Response addDocumentById(String index, String type, String id, Object entity) throws IOException{ + String method = "POST"; + String endPoint = index.concat("/").concat(type).concat("/"); + if(ObjectUtil.isNotEmpty(id)){ + endPoint.concat(id); + } + endPoint.concat("?refresh=true"); + logger.debug(entity.toString()); + return performRequest(method,endPoint,JSON.toJSONString(entity)); + } + + public static Response deleteDocumentById(String index, String type, String id) throws IOException{ + String method = "DELETE"; + String endPoint = index.concat("/").concat(type).concat("/").concat(id).concat("?refresh=true"); + return performRequest(method,endPoint,null); + } + + public static Response updateDocumentById(String index, String type, String id, Object entity) throws IOException{ + String method = "POST"; + String endPoint = index.concat("/").concat(type).concat("/").concat(id).concat("?refresh=true"); + logger.debug(entity.toString()); + return performRequest(method,endPoint,JSON.toJSONString(entity)); + } + + public static EsResponse searchQuery(String index, String type, ElasticSearchQuery query) throws IOException{ + String method = "POST"; + String entPoint = index.concat("/").concat(type).concat("/").concat("_search"); + logger.debug(query.getBody().toString()); + Response response = performRequest(method, entPoint, query.getBody().toString()); + // 获取response body,转换为json对象 + JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); + return result.toJavaObject(EsResponse.class); + } + + public static Response aggsQuery(String index, String type, ElasticSearchQuery query) throws IOException{ + String method = "POST"; + String entPoint = index.concat("/").concat(type).concat("/").concat("_search"); + logger.debug(query.getBody().toString()); + Response response = performRequest(method, entPoint, query.getBody().toString()); + return response; + } + + public static List searchQueryScroll(String index, String type, ElasticSearchQuery query) throws IOException{ + // 首次查询,提交查询条件,endpoint增加‘?scroll=1m’ + List results = new ArrayList<>(); + String method = "POST"; + String entPoint = index.concat("/").concat(type).concat("/").concat("_search").concat("?scroll=1m"); + logger.debug(query.getBody().toString()); + Response response = performRequest(method, entPoint, query.getBody().toString()); + JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); + EsResponse esResponse = result.toJavaObject(EsResponse.class); + String scrollId = result.getString("_scroll_id"); + if(ObjectUtil.isNotEmpty(esResponse.getDatas())){ + results.addAll(esResponse.getDatas()); + } + // 循环发送scroll请求,直到返回结果为空 + entPoint = "_search/scroll"; + JSONObject scrollBody = new JSONObject(); + scrollBody.put("scroll","1m"); + scrollBody.put("scroll_id",scrollId); + while (esResponse.getDatas().size() > 0){ + method = "GET"; + response = performRequest(method, entPoint, scrollBody.toString()); + result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); + esResponse = result.toJavaObject(EsResponse.class); + if(ObjectUtil.isNotEmpty(esResponse.getDatas())){ + results.addAll(esResponse.getDatas()); + } + } + // 查询完成后,及时删除scroll,释放资源 + scrollBody.remove("scroll"); + method = "DELETE"; + performRequest(method, entPoint, scrollBody.toString()); + return results; + } + + public static Response bulk(String index,String type,String entity) throws IOException{ + String method = "POST"; + StringBuilder endPoint = new StringBuilder(); + if(ObjectUtil.isNotEmpty(index)){ + endPoint.append(index).append("/"); + } + if(ObjectUtil.isNotEmpty(type)){ + endPoint.append(type).append("/"); + } + endPoint.append("_bulk"); + endPoint.append("?refresh=true"); + logger.debug(entity); + return performRequest(method,endPoint.toString(),entity); + } + + private static Response performRequest(String method, String endpoint, String entity) throws IOException { + //logger.info(method + " " + endpoint); + Request request = new Request(method,endpoint); + if(ObjectUtil.isNotEmpty(entity)){ + request.setEntity(new NStringEntity(entity,"utf-8")); + } + + if(ObjectUtil.isNotEmpty(username) && ObjectUtil.isNotEmpty(password)){ + RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder(); + String token = username.concat(":").concat(password); + String base64encode = Base64.getEncoder().encodeToString(token.getBytes("utf-8")); + builder.addHeader("Authorization", "Basic " + base64encode ); + request.setOptions(builder.build()); + } + + + Response response = client.performRequest(request); + logger.debug(response.toString()); + return response; + } + +} diff --git a/casic-common/src/main/java/com/casic/missiles/weigeng/CommDoorUtil.java b/casic-common/src/main/java/com/casic/missiles/weigeng/CommDoorUtil.java index bed1361..7457109 100644 --- a/casic-common/src/main/java/com/casic/missiles/weigeng/CommDoorUtil.java +++ b/casic-common/src/main/java/com/casic/missiles/weigeng/CommDoorUtil.java @@ -118,15 +118,14 @@ } //重启设备 - public static boolean restart(String monitorId) { + public static boolean restart(String controllerIP, String controllerSN) { try { - String doors = ObjectUtil.isNotEmpty(map.get(monitorId)) ? map.get(monitorId).toString() : ""; - if (ObjectUtil.isNotEmpty(doors)) - if (DoorShortUtil.remoteControl(doors.split(",")[1], Long.valueOf(doors.split(",")[0]), 2, 1)) { + if (ObjectUtil.isAllNotEmpty(controllerIP, controllerSN)) + if (DoorShortUtil.remoteControl(controllerIP, Long.valueOf(controllerSN), 2, 1)) { //重新上电需要间隔20秒 Thread.sleep(Integer.valueOf(map.get("time").toString()) * 1000); //重新关闭开关上电 - return DoorShortUtil.remoteControl(doors.split(",")[1], Long.valueOf(doors.split(",")[0]), 2, 2); + return DoorShortUtil.remoteControl(controllerIP, Long.valueOf(controllerSN), 2, 2); } } catch (Exception e) { e.printStackTrace(); @@ -135,16 +134,14 @@ } //重启设备 - public static boolean interruptStart(String monitorId) { + public static boolean interruptStart(String controllerIP, String controllerSN) { try { - String doors = ObjectUtil.isNotEmpty(map.get(monitorId)) ? map.get(monitorId).toString() : ""; - if (ObjectUtil.isNotEmpty(doors)) - if (DoorShortUtil.remoteControl(doors.split(",")[1], Long.valueOf(doors.split(",")[0]), 2, 1)) { + if (ObjectUtil.isAllNotEmpty(controllerIP,controllerSN)) + if (DoorShortUtil.remoteControl(controllerIP, Long.valueOf(controllerSN), 2, 1)) { //重新上电需要间隔20秒 Thread.sleep(Integer.valueOf(map.get("time").toString()) * 1000); //重新关闭开关上电 - DoorShortUtil.remoteControl(doors.split(",")[1], Long.valueOf(doors.split(",")[0]), 2, 2); - + DoorShortUtil.remoteControl(controllerIP, Long.valueOf(controllerSN), 2, 2); Thread.sleep(2 * 60 * 1000); return true; } diff --git a/casic-data/pom.xml b/casic-data/pom.xml index aca0a62..a076800 100644 --- a/casic-data/pom.xml +++ b/casic-data/pom.xml @@ -43,11 +43,6 @@ com.casic - casic-data-es - 1.0.1.alpha - - - com.casic casic-common 2.0.0 compile diff --git a/casic-data/src/main/java/com/casic/missiles/modular/system/es/DataGasEs.java b/casic-data/src/main/java/com/casic/missiles/modular/system/es/DataGasEs.java deleted file mode 100644 index 821e8f1..0000000 --- a/casic-data/src/main/java/com/casic/missiles/modular/system/es/DataGasEs.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.casic.missiles.modular.system.es; - -import lombok.Data; - -@Data -public class DataGasEs { - - private Long stationId; - private String stationName; - private Long monitorId; - private String monitorName; - private String devcode; - private Double direction; - private Double pitch; - private Double concentration; - private Double threshold; - private String logTime; -} diff --git a/casic-data/src/main/java/com/casic/missiles/modular/system/es/ElasticSearchQuery.java b/casic-data/src/main/java/com/casic/missiles/modular/system/es/ElasticSearchQuery.java deleted file mode 100644 index 4d7a38f..0000000 --- a/casic-data/src/main/java/com/casic/missiles/modular/system/es/ElasticSearchQuery.java +++ /dev/null @@ -1,257 +0,0 @@ -package com.casic.missiles.modular.system.es; - -import cn.hutool.core.date.DateUtil; -import cn.hutool.core.util.ObjectUtil; -import com.alibaba.fastjson.JSONArray; -import com.alibaba.fastjson.JSONObject; - -import java.io.Serializable; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; - -/** - * ES查询请求体 - * - * @author zhangyingjie123 - * @since 2020-11-30 - */ -public class ElasticSearchQuery { - private JSONObject body; - private JSONObject query; - private JSONObject bool; - private JSONArray boolMust; - private JSONObject filter; - private JSONObject filterBool; - private JSONArray filterBoolMust; - private JSONArray filterBoolMustnot; - - public static final String MUST_FLAG = "1"; - public static final String MUST_NOT_FLAG = "0"; - - public static final String QUERY = "query"; - public static final String BOOL = "bool"; - public static final String MUST = "must"; - public static final String MUST_NOT = "must_not"; - public static final String FILTER = "filter"; - - - /** - * 生成查询请求体基础结构: - * { - * "query": { - * "bool": { - * "must" [], - * "filter": { - * "bool": { - * "must": [], - * "must_not": [] - * } - * } - * } - * } - * } - */ - public ElasticSearchQuery() { - this.body = new JSONObject(); - this.query = this.query(); - this.bool = this.bool(); - this.boolMust = this.boolMust(); - this.filter = this.filter(); - this.filterBool = this.filterBool(); - this.filterBoolMust = this.filterBoolMust(); - this.filterBoolMustnot = this.filterBoolMustnot(); - } - - public ElasticSearchQuery(JSONObject body){ - this.body = body; - } - - public JSONObject getBody() { - return body; - } - - private JSONObject query(){ - if(ObjectUtil.isEmpty(this.body.get(QUERY))){ - this.body.put("query",new JSONObject()); - } - return this.body.getJSONObject(QUERY); - - } - - private JSONObject bool() { - JSONObject query = this.query; - if(ObjectUtil.isEmpty(query.get(BOOL))){ - query.put(BOOL,new JSONObject()); - } - return query.getJSONObject(BOOL); - } - - private JSONArray boolMust(){ - JSONObject bool = this.bool; - if(ObjectUtil.isEmpty(bool.get(MUST))){ - bool.put(MUST,new JSONArray()); - } - return bool.getJSONArray(MUST); - } - - private JSONObject filter(){ - JSONObject bool = this.bool; - if(ObjectUtil.isEmpty(bool.get(FILTER))){ - bool.put(FILTER,new JSONObject()); - } - return bool.getJSONObject(FILTER); - } - - private JSONObject filterBool(){ - JSONObject filter = this.filter; - if(ObjectUtil.isEmpty(filter.get(BOOL))){ - filter.put(BOOL,new JSONObject()); - } - return filter.getJSONObject(BOOL); - } - - private JSONArray filterBoolMust(){ - JSONObject bool = this.filterBool; - if(ObjectUtil.isEmpty(bool.get(MUST))){ - bool.put(MUST,new JSONArray()); - } - return bool.getJSONArray(MUST); - } - - private JSONArray filterBoolMustnot(){ - JSONObject bool = this.filterBool; - if(ObjectUtil.isEmpty(bool.get(MUST_NOT))){ - bool.put(MUST_NOT,new JSONArray()); - } - return bool.getJSONArray(MUST_NOT); - } - - public void match(String filed, Serializable value){ - JSONArray boolMust = this.boolMust; - JSONObject match = new JSONObject(); - JSONObject filedJson = new JSONObject(); - filedJson.put(filed,value); - match.put("match",filedJson); - boolMust.add(match); - } - - public void matchPhrase(String filed, Serializable value){ - JSONArray boolMust = this.boolMust; - JSONObject matchPhrase = new JSONObject(); - JSONObject filedJson = new JSONObject(); - filedJson.put(filed,value); - matchPhrase.put("match_phrase",filedJson); - boolMust.add(matchPhrase); - } - - public void term(String filed, Serializable value){ - this.term(filed,value,MUST_FLAG); - } - - public void term(String filed, Serializable value,String mustFlag){ - JSONObject filedJson = new JSONObject(); - filedJson.put(filed,value); - JSONObject term = new JSONObject(); - term.put("term",filedJson); - if(MUST_FLAG.equals(mustFlag)){ - this.filterBoolMust.add(term); - } - if(MUST_NOT_FLAG.equals(mustFlag)){ - this.filterBoolMustnot.add(term); - } - } - - public void terms(String filed,List value){ - this.terms(filed,value,MUST_FLAG); - } - - public void terms(String filed, List value, String mustFlag){ - JSONObject filedJson = new JSONObject(); - filedJson.put(filed,value); - JSONObject terms = new JSONObject(); - terms.put("terms",filedJson); - if(MUST_FLAG.equals(mustFlag)){ - this.filterBoolMust.add(terms); - } - if(MUST_NOT_FLAG.equals(mustFlag)){ - this.filterBoolMustnot.add(terms); - } - } - - public void range(String filed, Serializable from, Serializable to){ - this.range(filed,from,to,MUST_FLAG); - } - - public void range(String filed, Serializable from, Serializable to, String mustFlag){ - JSONObject rangeJson = new JSONObject(); - if(ObjectUtil.isNotEmpty(from)){ - rangeJson.put("from",from); - } - if(ObjectUtil.isNotEmpty(to)){ - rangeJson.put("to",to); - } - JSONObject filedJson = new JSONObject(); - filedJson.put(filed,rangeJson); - JSONObject range = new JSONObject(); - range.put("range",filedJson); - if(MUST_FLAG.equals(mustFlag)){ - this.filterBoolMust.add(range); - } - if(MUST_NOT_FLAG.equals(mustFlag)){ - this.filterBoolMustnot.add(range); - } - } - - public void size(int size){ - this.body.put("size",size); - } - - public void from(int from) { - this.body.put("from",from); - } - - public void sort(String sort, String order){ - JSONObject sortJson = new JSONObject(); - JSONObject orderJson = new JSONObject(); - orderJson.put("order",order); - sortJson.put(sort,orderJson); - this.body.put("sort",sortJson); - } - - public void aggs(String aggsName, String filed){ - JSONObject terms = new JSONObject(); - terms.put("field",filed); - JSONObject aggsTerms = new JSONObject(); - aggsTerms.put("terms",terms); - JSONObject aggsBody = new JSONObject(); - aggsBody.put(aggsName,aggsTerms); - this.body.put("aggs",aggsBody); - } - - public void aggs(JSONObject aggBody){ - this.body.put("aggs",aggBody); - } - - - - public static void main(String[] args) { - ElasticSearchQuery query = new ElasticSearchQuery(); - query.matchPhrase("name","张三"); - query.term("sex","2"); - List deptIds = new ArrayList<>(); - deptIds.add(0L); - deptIds.add(24L); - query.terms("deptId", deptIds); - Date date1 = DateUtil.parse("2020-01-01","yyyy-MM-dd"); - Date date2 = DateUtil.parse("2020-04-01","yyyy-MM-dd"); - query.range("collTime",date1.getTime(),date2.getTime()); - query.term("collState","3",MUST_NOT_FLAG); - query.size(10); - query.from(0); - query.sort("irId","asc"); - System.out.println(query.getBody().toJSONString()); - } - - -} diff --git a/casic-data/src/main/java/com/casic/missiles/modular/system/es/ElasticSearchUtil.java b/casic-data/src/main/java/com/casic/missiles/modular/system/es/ElasticSearchUtil.java deleted file mode 100644 index e9da021..0000000 --- a/casic-data/src/main/java/com/casic/missiles/modular/system/es/ElasticSearchUtil.java +++ /dev/null @@ -1,172 +0,0 @@ -package com.casic.missiles.modular.system.es; - -import cn.hutool.core.util.ObjectUtil; -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONObject; -import com.casic.missiles.core.es.EsResponse; -import org.apache.http.nio.entity.NStringEntity; -import org.apache.http.util.EntityUtils; -import org.elasticsearch.client.Request; -import org.elasticsearch.client.RequestOptions; -import org.elasticsearch.client.Response; -import org.elasticsearch.client.RestClient; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.core.env.Environment; -import org.springframework.stereotype.Component; - -import javax.annotation.PostConstruct; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Base64; -import java.util.List; - -/** - * ES工具类 - * - * @author zhangyingjie123 - * @since 2020-11-30 - */ -@Component -public class ElasticSearchUtil { - private static final Logger logger = LoggerFactory.getLogger(ElasticSearchUtil.class); - - @Autowired - private RestClient clientMapping; - @Autowired - private Environment env; - - private static String username; - private static String password; - - private static RestClient client; - - @PostConstruct - public void init(){ - client = clientMapping; - username = env.getProperty("casic.data.es.username"); - password = env.getProperty("casic.data.es.password"); - } - - public static JSONObject selectDocumentById(String index, String type, String id) throws IOException{ - String method = "GET"; - String endPoint = index.concat("/").concat(type).concat("/").concat(id); - Response response = performRequest(method,endPoint,null); - JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); - return result.getJSONObject("_source"); - } - - public static Response addDocumentById(String index, String type, String id, Object entity) throws IOException{ - String method = "POST"; - String endPoint = index.concat("/").concat(type).concat("/"); - if(ObjectUtil.isNotEmpty(id)){ - endPoint.concat(id); - } - endPoint.concat("?refresh=true"); - logger.debug(entity.toString()); - return performRequest(method,endPoint,JSON.toJSONString(entity)); - } - - public static Response deleteDocumentById(String index, String type, String id) throws IOException{ - String method = "DELETE"; - String endPoint = index.concat("/").concat(type).concat("/").concat(id).concat("?refresh=true"); - return performRequest(method,endPoint,null); - } - - public static Response updateDocumentById(String index, String type, String id, Object entity) throws IOException{ - String method = "POST"; - String endPoint = index.concat("/").concat(type).concat("/").concat(id).concat("?refresh=true"); - logger.debug(entity.toString()); - return performRequest(method,endPoint,JSON.toJSONString(entity)); - } - - public static EsResponse searchQuery(String index, String type, ElasticSearchQuery query) throws IOException{ - String method = "POST"; - String entPoint = index.concat("/").concat(type).concat("/").concat("_search"); - logger.debug(query.getBody().toString()); - Response response = performRequest(method, entPoint, query.getBody().toString()); - // 获取response body,转换为json对象 - JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); - return result.toJavaObject(EsResponse.class); - } - - public static Response aggsQuery(String index, String type, ElasticSearchQuery query) throws IOException{ - String method = "POST"; - String entPoint = index.concat("/").concat(type).concat("/").concat("_search"); - logger.debug(query.getBody().toString()); - Response response = performRequest(method, entPoint, query.getBody().toString()); - return response; - } - - public static List searchQueryScroll(String index, String type, ElasticSearchQuery query) throws IOException{ - // 首次查询,提交查询条件,endpoint增加‘?scroll=1m’ - List results = new ArrayList<>(); - String method = "POST"; - String entPoint = index.concat("/").concat(type).concat("/").concat("_search").concat("?scroll=1m"); - logger.debug(query.getBody().toString()); - Response response = performRequest(method, entPoint, query.getBody().toString()); - JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); - EsResponse esResponse = result.toJavaObject(EsResponse.class); - String scrollId = result.getString("_scroll_id"); - if(ObjectUtil.isNotEmpty(esResponse.getDatas())){ - results.addAll(esResponse.getDatas()); - } - // 循环发送scroll请求,直到返回结果为空 - entPoint = "_search/scroll"; - JSONObject scrollBody = new JSONObject(); - scrollBody.put("scroll","1m"); - scrollBody.put("scroll_id",scrollId); - while (esResponse.getDatas().size() > 0){ - method = "GET"; - response = performRequest(method, entPoint, scrollBody.toString()); - result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); - esResponse = result.toJavaObject(EsResponse.class); - if(ObjectUtil.isNotEmpty(esResponse.getDatas())){ - results.addAll(esResponse.getDatas()); - } - } - // 查询完成后,及时删除scroll,释放资源 - scrollBody.remove("scroll"); - method = "DELETE"; - performRequest(method, entPoint, scrollBody.toString()); - return results; - } - - public static Response bulk(String index,String type,String entity) throws IOException{ - String method = "POST"; - StringBuilder endPoint = new StringBuilder(); - if(ObjectUtil.isNotEmpty(index)){ - endPoint.append(index).append("/"); - } - if(ObjectUtil.isNotEmpty(type)){ - endPoint.append(type).append("/"); - } - endPoint.append("_bulk"); - endPoint.append("?refresh=true"); - logger.debug(entity); - return performRequest(method,endPoint.toString(),entity); - } - - private static Response performRequest(String method, String endpoint, String entity) throws IOException { - //logger.info(method + " " + endpoint); - Request request = new Request(method,endpoint); - if(ObjectUtil.isNotEmpty(entity)){ - request.setEntity(new NStringEntity(entity,"utf-8")); - } - - if(ObjectUtil.isNotEmpty(username) && ObjectUtil.isNotEmpty(password)){ - RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder(); - String token = username.concat(":").concat(password); - String base64encode = Base64.getEncoder().encodeToString(token.getBytes("utf-8")); - builder.addHeader("Authorization", "Basic " + base64encode ); - request.setOptions(builder.build()); - } - - - Response response = client.performRequest(request); - logger.debug(response.toString()); - return response; - } - -} diff --git a/casic-data/src/main/java/com/casic/missiles/modular/system/service/IDataGasService.java b/casic-data/src/main/java/com/casic/missiles/modular/system/service/IDataGasService.java index e644294..b2aa6cb 100644 --- a/casic-data/src/main/java/com/casic/missiles/modular/system/service/IDataGasService.java +++ b/casic-data/src/main/java/com/casic/missiles/modular/system/service/IDataGasService.java @@ -1,6 +1,6 @@ package com.casic.missiles.modular.system.service; -import com.casic.missiles.modular.system.es.DataGasEs; +import com.casic.missiles.es.DataGasEs; import com.casic.missiles.modular.system.model.DataGas; import com.baomidou.mybatisplus.extension.service.IService; diff --git a/casic-data/src/main/java/com/casic/missiles/modular/system/service/impl/DataGasServiceImpl.java b/casic-data/src/main/java/com/casic/missiles/modular/system/service/impl/DataGasServiceImpl.java index 1aa01e5..ba3a38d 100644 --- a/casic-data/src/main/java/com/casic/missiles/modular/system/service/impl/DataGasServiceImpl.java +++ b/casic-data/src/main/java/com/casic/missiles/modular/system/service/impl/DataGasServiceImpl.java @@ -1,12 +1,12 @@ package com.casic.missiles.modular.system.service.impl; -import com.casic.missiles.modular.system.consts.DataConst; -import com.casic.missiles.modular.system.es.DataGasEs; -import com.casic.missiles.modular.system.es.ElasticSearchUtil; -import com.casic.missiles.modular.system.model.DataGas; -import com.casic.missiles.modular.system.dao.DataGasMapper; -import com.casic.missiles.modular.system.service.IDataGasService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.casic.missiles.es.DataGasEs; +import com.casic.missiles.es.ElasticSearchUtil; +import com.casic.missiles.modular.system.consts.DataConst; +import com.casic.missiles.modular.system.dao.DataGasMapper; +import com.casic.missiles.modular.system.model.DataGas; +import com.casic.missiles.modular.system.service.IDataGasService; import org.springframework.stereotype.Service; import java.io.IOException; diff --git a/casic-common/pom.xml b/casic-common/pom.xml index 27e6a55..a47c616 100644 --- a/casic-common/pom.xml +++ b/casic-common/pom.xml @@ -46,6 +46,11 @@ mina-core 2.0.4 + + com.casic + casic-data-es + 1.0.1.alpha + diff --git a/casic-common/src/main/java/com/casic/missiles/es/DataGasEs.java b/casic-common/src/main/java/com/casic/missiles/es/DataGasEs.java new file mode 100644 index 0000000..3b1f3fe --- /dev/null +++ b/casic-common/src/main/java/com/casic/missiles/es/DataGasEs.java @@ -0,0 +1,18 @@ +package com.casic.missiles.es; + +import lombok.Data; + +@Data +public class DataGasEs { + + private Long stationId; + private String stationName; + private Long monitorId; + private String monitorName; + private String devcode; + private Double direction; + private Double pitch; + private Double concentration; + private Double threshold; + private String logTime; +} diff --git a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java new file mode 100644 index 0000000..aeef63d --- /dev/null +++ b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java @@ -0,0 +1,257 @@ +package com.casic.missiles.es; + +import cn.hutool.core.date.DateUtil; +import cn.hutool.core.util.ObjectUtil; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +/** + * ES查询请求体 + * + * @author zhangyingjie123 + * @since 2020-11-30 + */ +public class ElasticSearchQuery { + private JSONObject body; + private JSONObject query; + private JSONObject bool; + private JSONArray boolMust; + private JSONObject filter; + private JSONObject filterBool; + private JSONArray filterBoolMust; + private JSONArray filterBoolMustnot; + + public static final String MUST_FLAG = "1"; + public static final String MUST_NOT_FLAG = "0"; + + public static final String QUERY = "query"; + public static final String BOOL = "bool"; + public static final String MUST = "must"; + public static final String MUST_NOT = "must_not"; + public static final String FILTER = "filter"; + + + /** + * 生成查询请求体基础结构: + * { + * "query": { + * "bool": { + * "must" [], + * "filter": { + * "bool": { + * "must": [], + * "must_not": [] + * } + * } + * } + * } + * } + */ + public ElasticSearchQuery() { + this.body = new JSONObject(); + this.query = this.query(); + this.bool = this.bool(); + this.boolMust = this.boolMust(); + this.filter = this.filter(); + this.filterBool = this.filterBool(); + this.filterBoolMust = this.filterBoolMust(); + this.filterBoolMustnot = this.filterBoolMustnot(); + } + + public ElasticSearchQuery(JSONObject body){ + this.body = body; + } + + public JSONObject getBody() { + return body; + } + + private JSONObject query(){ + if(ObjectUtil.isEmpty(this.body.get(QUERY))){ + this.body.put("query",new JSONObject()); + } + return this.body.getJSONObject(QUERY); + + } + + private JSONObject bool() { + JSONObject query = this.query; + if(ObjectUtil.isEmpty(query.get(BOOL))){ + query.put(BOOL,new JSONObject()); + } + return query.getJSONObject(BOOL); + } + + private JSONArray boolMust(){ + JSONObject bool = this.bool; + if(ObjectUtil.isEmpty(bool.get(MUST))){ + bool.put(MUST,new JSONArray()); + } + return bool.getJSONArray(MUST); + } + + private JSONObject filter(){ + JSONObject bool = this.bool; + if(ObjectUtil.isEmpty(bool.get(FILTER))){ + bool.put(FILTER,new JSONObject()); + } + return bool.getJSONObject(FILTER); + } + + private JSONObject filterBool(){ + JSONObject filter = this.filter; + if(ObjectUtil.isEmpty(filter.get(BOOL))){ + filter.put(BOOL,new JSONObject()); + } + return filter.getJSONObject(BOOL); + } + + private JSONArray filterBoolMust(){ + JSONObject bool = this.filterBool; + if(ObjectUtil.isEmpty(bool.get(MUST))){ + bool.put(MUST,new JSONArray()); + } + return bool.getJSONArray(MUST); + } + + private JSONArray filterBoolMustnot(){ + JSONObject bool = this.filterBool; + if(ObjectUtil.isEmpty(bool.get(MUST_NOT))){ + bool.put(MUST_NOT,new JSONArray()); + } + return bool.getJSONArray(MUST_NOT); + } + + public void match(String filed, Serializable value){ + JSONArray boolMust = this.boolMust; + JSONObject match = new JSONObject(); + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,value); + match.put("match",filedJson); + boolMust.add(match); + } + + public void matchPhrase(String filed, Serializable value){ + JSONArray boolMust = this.boolMust; + JSONObject matchPhrase = new JSONObject(); + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,value); + matchPhrase.put("match_phrase",filedJson); + boolMust.add(matchPhrase); + } + + public void term(String filed, Serializable value){ + this.term(filed,value,MUST_FLAG); + } + + public void term(String filed, Serializable value,String mustFlag){ + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,value); + JSONObject term = new JSONObject(); + term.put("term",filedJson); + if(MUST_FLAG.equals(mustFlag)){ + this.filterBoolMust.add(term); + } + if(MUST_NOT_FLAG.equals(mustFlag)){ + this.filterBoolMustnot.add(term); + } + } + + public void terms(String filed,List value){ + this.terms(filed,value,MUST_FLAG); + } + + public void terms(String filed, List value, String mustFlag){ + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,value); + JSONObject terms = new JSONObject(); + terms.put("terms",filedJson); + if(MUST_FLAG.equals(mustFlag)){ + this.filterBoolMust.add(terms); + } + if(MUST_NOT_FLAG.equals(mustFlag)){ + this.filterBoolMustnot.add(terms); + } + } + + public void range(String filed, Serializable from, Serializable to){ + this.range(filed,from,to,MUST_FLAG); + } + + public void range(String filed, Serializable from, Serializable to, String mustFlag){ + JSONObject rangeJson = new JSONObject(); + if(ObjectUtil.isNotEmpty(from)){ + rangeJson.put("from",from); + } + if(ObjectUtil.isNotEmpty(to)){ + rangeJson.put("to",to); + } + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,rangeJson); + JSONObject range = new JSONObject(); + range.put("range",filedJson); + if(MUST_FLAG.equals(mustFlag)){ + this.filterBoolMust.add(range); + } + if(MUST_NOT_FLAG.equals(mustFlag)){ + this.filterBoolMustnot.add(range); + } + } + + public void size(int size){ + this.body.put("size",size); + } + + public void from(int from) { + this.body.put("from",from); + } + + public void sort(String sort, String order){ + JSONObject sortJson = new JSONObject(); + JSONObject orderJson = new JSONObject(); + orderJson.put("order",order); + sortJson.put(sort,orderJson); + this.body.put("sort",sortJson); + } + + public void aggs(String aggsName, String filed){ + JSONObject terms = new JSONObject(); + terms.put("field",filed); + JSONObject aggsTerms = new JSONObject(); + aggsTerms.put("terms",terms); + JSONObject aggsBody = new JSONObject(); + aggsBody.put(aggsName,aggsTerms); + this.body.put("aggs",aggsBody); + } + + public void aggs(JSONObject aggBody){ + this.body.put("aggs",aggBody); + } + + + + public static void main(String[] args) { + ElasticSearchQuery query = new ElasticSearchQuery(); + query.matchPhrase("name","张三"); + query.term("sex","2"); + List deptIds = new ArrayList<>(); + deptIds.add(0L); + deptIds.add(24L); + query.terms("deptId", deptIds); + Date date1 = DateUtil.parse("2020-01-01","yyyy-MM-dd"); + Date date2 = DateUtil.parse("2020-04-01","yyyy-MM-dd"); + query.range("collTime",date1.getTime(),date2.getTime()); + query.term("collState","3",MUST_NOT_FLAG); + query.size(10); + query.from(0); + query.sort("irId","asc"); + System.out.println(query.getBody().toJSONString()); + } + + +} diff --git a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java new file mode 100644 index 0000000..dc9a968 --- /dev/null +++ b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java @@ -0,0 +1,172 @@ +package com.casic.missiles.es; + +import cn.hutool.core.util.ObjectUtil; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.casic.missiles.core.es.EsResponse; +import org.apache.http.nio.entity.NStringEntity; +import org.apache.http.util.EntityUtils; +import org.elasticsearch.client.Request; +import org.elasticsearch.client.RequestOptions; +import org.elasticsearch.client.Response; +import org.elasticsearch.client.RestClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Base64; +import java.util.List; + +/** + * ES工具类 + * + * @author zhangyingjie123 + * @since 2020-11-30 + */ +@Component +public class ElasticSearchUtil { + private static final Logger logger = LoggerFactory.getLogger(ElasticSearchUtil.class); + + @Autowired + private RestClient clientMapping; + @Autowired + private Environment env; + + private static String username; + private static String password; + + private static RestClient client; + + @PostConstruct + public void init(){ + client = clientMapping; + username = env.getProperty("casic.data.es.username"); + password = env.getProperty("casic.data.es.password"); + } + + public static JSONObject selectDocumentById(String index, String type, String id) throws IOException{ + String method = "GET"; + String endPoint = index.concat("/").concat(type).concat("/").concat(id); + Response response = performRequest(method,endPoint,null); + JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); + return result.getJSONObject("_source"); + } + + public static Response addDocumentById(String index, String type, String id, Object entity) throws IOException{ + String method = "POST"; + String endPoint = index.concat("/").concat(type).concat("/"); + if(ObjectUtil.isNotEmpty(id)){ + endPoint.concat(id); + } + endPoint.concat("?refresh=true"); + logger.debug(entity.toString()); + return performRequest(method,endPoint,JSON.toJSONString(entity)); + } + + public static Response deleteDocumentById(String index, String type, String id) throws IOException{ + String method = "DELETE"; + String endPoint = index.concat("/").concat(type).concat("/").concat(id).concat("?refresh=true"); + return performRequest(method,endPoint,null); + } + + public static Response updateDocumentById(String index, String type, String id, Object entity) throws IOException{ + String method = "POST"; + String endPoint = index.concat("/").concat(type).concat("/").concat(id).concat("?refresh=true"); + logger.debug(entity.toString()); + return performRequest(method,endPoint,JSON.toJSONString(entity)); + } + + public static EsResponse searchQuery(String index, String type, ElasticSearchQuery query) throws IOException{ + String method = "POST"; + String entPoint = index.concat("/").concat(type).concat("/").concat("_search"); + logger.debug(query.getBody().toString()); + Response response = performRequest(method, entPoint, query.getBody().toString()); + // 获取response body,转换为json对象 + JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); + return result.toJavaObject(EsResponse.class); + } + + public static Response aggsQuery(String index, String type, ElasticSearchQuery query) throws IOException{ + String method = "POST"; + String entPoint = index.concat("/").concat(type).concat("/").concat("_search"); + logger.debug(query.getBody().toString()); + Response response = performRequest(method, entPoint, query.getBody().toString()); + return response; + } + + public static List searchQueryScroll(String index, String type, ElasticSearchQuery query) throws IOException{ + // 首次查询,提交查询条件,endpoint增加‘?scroll=1m’ + List results = new ArrayList<>(); + String method = "POST"; + String entPoint = index.concat("/").concat(type).concat("/").concat("_search").concat("?scroll=1m"); + logger.debug(query.getBody().toString()); + Response response = performRequest(method, entPoint, query.getBody().toString()); + JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); + EsResponse esResponse = result.toJavaObject(EsResponse.class); + String scrollId = result.getString("_scroll_id"); + if(ObjectUtil.isNotEmpty(esResponse.getDatas())){ + results.addAll(esResponse.getDatas()); + } + // 循环发送scroll请求,直到返回结果为空 + entPoint = "_search/scroll"; + JSONObject scrollBody = new JSONObject(); + scrollBody.put("scroll","1m"); + scrollBody.put("scroll_id",scrollId); + while (esResponse.getDatas().size() > 0){ + method = "GET"; + response = performRequest(method, entPoint, scrollBody.toString()); + result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); + esResponse = result.toJavaObject(EsResponse.class); + if(ObjectUtil.isNotEmpty(esResponse.getDatas())){ + results.addAll(esResponse.getDatas()); + } + } + // 查询完成后,及时删除scroll,释放资源 + scrollBody.remove("scroll"); + method = "DELETE"; + performRequest(method, entPoint, scrollBody.toString()); + return results; + } + + public static Response bulk(String index,String type,String entity) throws IOException{ + String method = "POST"; + StringBuilder endPoint = new StringBuilder(); + if(ObjectUtil.isNotEmpty(index)){ + endPoint.append(index).append("/"); + } + if(ObjectUtil.isNotEmpty(type)){ + endPoint.append(type).append("/"); + } + endPoint.append("_bulk"); + endPoint.append("?refresh=true"); + logger.debug(entity); + return performRequest(method,endPoint.toString(),entity); + } + + private static Response performRequest(String method, String endpoint, String entity) throws IOException { + //logger.info(method + " " + endpoint); + Request request = new Request(method,endpoint); + if(ObjectUtil.isNotEmpty(entity)){ + request.setEntity(new NStringEntity(entity,"utf-8")); + } + + if(ObjectUtil.isNotEmpty(username) && ObjectUtil.isNotEmpty(password)){ + RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder(); + String token = username.concat(":").concat(password); + String base64encode = Base64.getEncoder().encodeToString(token.getBytes("utf-8")); + builder.addHeader("Authorization", "Basic " + base64encode ); + request.setOptions(builder.build()); + } + + + Response response = client.performRequest(request); + logger.debug(response.toString()); + return response; + } + +} diff --git a/casic-common/src/main/java/com/casic/missiles/weigeng/CommDoorUtil.java b/casic-common/src/main/java/com/casic/missiles/weigeng/CommDoorUtil.java index bed1361..7457109 100644 --- a/casic-common/src/main/java/com/casic/missiles/weigeng/CommDoorUtil.java +++ b/casic-common/src/main/java/com/casic/missiles/weigeng/CommDoorUtil.java @@ -118,15 +118,14 @@ } //重启设备 - public static boolean restart(String monitorId) { + public static boolean restart(String controllerIP, String controllerSN) { try { - String doors = ObjectUtil.isNotEmpty(map.get(monitorId)) ? map.get(monitorId).toString() : ""; - if (ObjectUtil.isNotEmpty(doors)) - if (DoorShortUtil.remoteControl(doors.split(",")[1], Long.valueOf(doors.split(",")[0]), 2, 1)) { + if (ObjectUtil.isAllNotEmpty(controllerIP, controllerSN)) + if (DoorShortUtil.remoteControl(controllerIP, Long.valueOf(controllerSN), 2, 1)) { //重新上电需要间隔20秒 Thread.sleep(Integer.valueOf(map.get("time").toString()) * 1000); //重新关闭开关上电 - return DoorShortUtil.remoteControl(doors.split(",")[1], Long.valueOf(doors.split(",")[0]), 2, 2); + return DoorShortUtil.remoteControl(controllerIP, Long.valueOf(controllerSN), 2, 2); } } catch (Exception e) { e.printStackTrace(); @@ -135,16 +134,14 @@ } //重启设备 - public static boolean interruptStart(String monitorId) { + public static boolean interruptStart(String controllerIP, String controllerSN) { try { - String doors = ObjectUtil.isNotEmpty(map.get(monitorId)) ? map.get(monitorId).toString() : ""; - if (ObjectUtil.isNotEmpty(doors)) - if (DoorShortUtil.remoteControl(doors.split(",")[1], Long.valueOf(doors.split(",")[0]), 2, 1)) { + if (ObjectUtil.isAllNotEmpty(controllerIP,controllerSN)) + if (DoorShortUtil.remoteControl(controllerIP, Long.valueOf(controllerSN), 2, 1)) { //重新上电需要间隔20秒 Thread.sleep(Integer.valueOf(map.get("time").toString()) * 1000); //重新关闭开关上电 - DoorShortUtil.remoteControl(doors.split(",")[1], Long.valueOf(doors.split(",")[0]), 2, 2); - + DoorShortUtil.remoteControl(controllerIP, Long.valueOf(controllerSN), 2, 2); Thread.sleep(2 * 60 * 1000); return true; } diff --git a/casic-data/pom.xml b/casic-data/pom.xml index aca0a62..a076800 100644 --- a/casic-data/pom.xml +++ b/casic-data/pom.xml @@ -43,11 +43,6 @@ com.casic - casic-data-es - 1.0.1.alpha - - - com.casic casic-common 2.0.0 compile diff --git a/casic-data/src/main/java/com/casic/missiles/modular/system/es/DataGasEs.java b/casic-data/src/main/java/com/casic/missiles/modular/system/es/DataGasEs.java deleted file mode 100644 index 821e8f1..0000000 --- a/casic-data/src/main/java/com/casic/missiles/modular/system/es/DataGasEs.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.casic.missiles.modular.system.es; - -import lombok.Data; - -@Data -public class DataGasEs { - - private Long stationId; - private String stationName; - private Long monitorId; - private String monitorName; - private String devcode; - private Double direction; - private Double pitch; - private Double concentration; - private Double threshold; - private String logTime; -} diff --git a/casic-data/src/main/java/com/casic/missiles/modular/system/es/ElasticSearchQuery.java b/casic-data/src/main/java/com/casic/missiles/modular/system/es/ElasticSearchQuery.java deleted file mode 100644 index 4d7a38f..0000000 --- a/casic-data/src/main/java/com/casic/missiles/modular/system/es/ElasticSearchQuery.java +++ /dev/null @@ -1,257 +0,0 @@ -package com.casic.missiles.modular.system.es; - -import cn.hutool.core.date.DateUtil; -import cn.hutool.core.util.ObjectUtil; -import com.alibaba.fastjson.JSONArray; -import com.alibaba.fastjson.JSONObject; - -import java.io.Serializable; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; - -/** - * ES查询请求体 - * - * @author zhangyingjie123 - * @since 2020-11-30 - */ -public class ElasticSearchQuery { - private JSONObject body; - private JSONObject query; - private JSONObject bool; - private JSONArray boolMust; - private JSONObject filter; - private JSONObject filterBool; - private JSONArray filterBoolMust; - private JSONArray filterBoolMustnot; - - public static final String MUST_FLAG = "1"; - public static final String MUST_NOT_FLAG = "0"; - - public static final String QUERY = "query"; - public static final String BOOL = "bool"; - public static final String MUST = "must"; - public static final String MUST_NOT = "must_not"; - public static final String FILTER = "filter"; - - - /** - * 生成查询请求体基础结构: - * { - * "query": { - * "bool": { - * "must" [], - * "filter": { - * "bool": { - * "must": [], - * "must_not": [] - * } - * } - * } - * } - * } - */ - public ElasticSearchQuery() { - this.body = new JSONObject(); - this.query = this.query(); - this.bool = this.bool(); - this.boolMust = this.boolMust(); - this.filter = this.filter(); - this.filterBool = this.filterBool(); - this.filterBoolMust = this.filterBoolMust(); - this.filterBoolMustnot = this.filterBoolMustnot(); - } - - public ElasticSearchQuery(JSONObject body){ - this.body = body; - } - - public JSONObject getBody() { - return body; - } - - private JSONObject query(){ - if(ObjectUtil.isEmpty(this.body.get(QUERY))){ - this.body.put("query",new JSONObject()); - } - return this.body.getJSONObject(QUERY); - - } - - private JSONObject bool() { - JSONObject query = this.query; - if(ObjectUtil.isEmpty(query.get(BOOL))){ - query.put(BOOL,new JSONObject()); - } - return query.getJSONObject(BOOL); - } - - private JSONArray boolMust(){ - JSONObject bool = this.bool; - if(ObjectUtil.isEmpty(bool.get(MUST))){ - bool.put(MUST,new JSONArray()); - } - return bool.getJSONArray(MUST); - } - - private JSONObject filter(){ - JSONObject bool = this.bool; - if(ObjectUtil.isEmpty(bool.get(FILTER))){ - bool.put(FILTER,new JSONObject()); - } - return bool.getJSONObject(FILTER); - } - - private JSONObject filterBool(){ - JSONObject filter = this.filter; - if(ObjectUtil.isEmpty(filter.get(BOOL))){ - filter.put(BOOL,new JSONObject()); - } - return filter.getJSONObject(BOOL); - } - - private JSONArray filterBoolMust(){ - JSONObject bool = this.filterBool; - if(ObjectUtil.isEmpty(bool.get(MUST))){ - bool.put(MUST,new JSONArray()); - } - return bool.getJSONArray(MUST); - } - - private JSONArray filterBoolMustnot(){ - JSONObject bool = this.filterBool; - if(ObjectUtil.isEmpty(bool.get(MUST_NOT))){ - bool.put(MUST_NOT,new JSONArray()); - } - return bool.getJSONArray(MUST_NOT); - } - - public void match(String filed, Serializable value){ - JSONArray boolMust = this.boolMust; - JSONObject match = new JSONObject(); - JSONObject filedJson = new JSONObject(); - filedJson.put(filed,value); - match.put("match",filedJson); - boolMust.add(match); - } - - public void matchPhrase(String filed, Serializable value){ - JSONArray boolMust = this.boolMust; - JSONObject matchPhrase = new JSONObject(); - JSONObject filedJson = new JSONObject(); - filedJson.put(filed,value); - matchPhrase.put("match_phrase",filedJson); - boolMust.add(matchPhrase); - } - - public void term(String filed, Serializable value){ - this.term(filed,value,MUST_FLAG); - } - - public void term(String filed, Serializable value,String mustFlag){ - JSONObject filedJson = new JSONObject(); - filedJson.put(filed,value); - JSONObject term = new JSONObject(); - term.put("term",filedJson); - if(MUST_FLAG.equals(mustFlag)){ - this.filterBoolMust.add(term); - } - if(MUST_NOT_FLAG.equals(mustFlag)){ - this.filterBoolMustnot.add(term); - } - } - - public void terms(String filed,List value){ - this.terms(filed,value,MUST_FLAG); - } - - public void terms(String filed, List value, String mustFlag){ - JSONObject filedJson = new JSONObject(); - filedJson.put(filed,value); - JSONObject terms = new JSONObject(); - terms.put("terms",filedJson); - if(MUST_FLAG.equals(mustFlag)){ - this.filterBoolMust.add(terms); - } - if(MUST_NOT_FLAG.equals(mustFlag)){ - this.filterBoolMustnot.add(terms); - } - } - - public void range(String filed, Serializable from, Serializable to){ - this.range(filed,from,to,MUST_FLAG); - } - - public void range(String filed, Serializable from, Serializable to, String mustFlag){ - JSONObject rangeJson = new JSONObject(); - if(ObjectUtil.isNotEmpty(from)){ - rangeJson.put("from",from); - } - if(ObjectUtil.isNotEmpty(to)){ - rangeJson.put("to",to); - } - JSONObject filedJson = new JSONObject(); - filedJson.put(filed,rangeJson); - JSONObject range = new JSONObject(); - range.put("range",filedJson); - if(MUST_FLAG.equals(mustFlag)){ - this.filterBoolMust.add(range); - } - if(MUST_NOT_FLAG.equals(mustFlag)){ - this.filterBoolMustnot.add(range); - } - } - - public void size(int size){ - this.body.put("size",size); - } - - public void from(int from) { - this.body.put("from",from); - } - - public void sort(String sort, String order){ - JSONObject sortJson = new JSONObject(); - JSONObject orderJson = new JSONObject(); - orderJson.put("order",order); - sortJson.put(sort,orderJson); - this.body.put("sort",sortJson); - } - - public void aggs(String aggsName, String filed){ - JSONObject terms = new JSONObject(); - terms.put("field",filed); - JSONObject aggsTerms = new JSONObject(); - aggsTerms.put("terms",terms); - JSONObject aggsBody = new JSONObject(); - aggsBody.put(aggsName,aggsTerms); - this.body.put("aggs",aggsBody); - } - - public void aggs(JSONObject aggBody){ - this.body.put("aggs",aggBody); - } - - - - public static void main(String[] args) { - ElasticSearchQuery query = new ElasticSearchQuery(); - query.matchPhrase("name","张三"); - query.term("sex","2"); - List deptIds = new ArrayList<>(); - deptIds.add(0L); - deptIds.add(24L); - query.terms("deptId", deptIds); - Date date1 = DateUtil.parse("2020-01-01","yyyy-MM-dd"); - Date date2 = DateUtil.parse("2020-04-01","yyyy-MM-dd"); - query.range("collTime",date1.getTime(),date2.getTime()); - query.term("collState","3",MUST_NOT_FLAG); - query.size(10); - query.from(0); - query.sort("irId","asc"); - System.out.println(query.getBody().toJSONString()); - } - - -} diff --git a/casic-data/src/main/java/com/casic/missiles/modular/system/es/ElasticSearchUtil.java b/casic-data/src/main/java/com/casic/missiles/modular/system/es/ElasticSearchUtil.java deleted file mode 100644 index e9da021..0000000 --- a/casic-data/src/main/java/com/casic/missiles/modular/system/es/ElasticSearchUtil.java +++ /dev/null @@ -1,172 +0,0 @@ -package com.casic.missiles.modular.system.es; - -import cn.hutool.core.util.ObjectUtil; -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONObject; -import com.casic.missiles.core.es.EsResponse; -import org.apache.http.nio.entity.NStringEntity; -import org.apache.http.util.EntityUtils; -import org.elasticsearch.client.Request; -import org.elasticsearch.client.RequestOptions; -import org.elasticsearch.client.Response; -import org.elasticsearch.client.RestClient; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.core.env.Environment; -import org.springframework.stereotype.Component; - -import javax.annotation.PostConstruct; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Base64; -import java.util.List; - -/** - * ES工具类 - * - * @author zhangyingjie123 - * @since 2020-11-30 - */ -@Component -public class ElasticSearchUtil { - private static final Logger logger = LoggerFactory.getLogger(ElasticSearchUtil.class); - - @Autowired - private RestClient clientMapping; - @Autowired - private Environment env; - - private static String username; - private static String password; - - private static RestClient client; - - @PostConstruct - public void init(){ - client = clientMapping; - username = env.getProperty("casic.data.es.username"); - password = env.getProperty("casic.data.es.password"); - } - - public static JSONObject selectDocumentById(String index, String type, String id) throws IOException{ - String method = "GET"; - String endPoint = index.concat("/").concat(type).concat("/").concat(id); - Response response = performRequest(method,endPoint,null); - JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); - return result.getJSONObject("_source"); - } - - public static Response addDocumentById(String index, String type, String id, Object entity) throws IOException{ - String method = "POST"; - String endPoint = index.concat("/").concat(type).concat("/"); - if(ObjectUtil.isNotEmpty(id)){ - endPoint.concat(id); - } - endPoint.concat("?refresh=true"); - logger.debug(entity.toString()); - return performRequest(method,endPoint,JSON.toJSONString(entity)); - } - - public static Response deleteDocumentById(String index, String type, String id) throws IOException{ - String method = "DELETE"; - String endPoint = index.concat("/").concat(type).concat("/").concat(id).concat("?refresh=true"); - return performRequest(method,endPoint,null); - } - - public static Response updateDocumentById(String index, String type, String id, Object entity) throws IOException{ - String method = "POST"; - String endPoint = index.concat("/").concat(type).concat("/").concat(id).concat("?refresh=true"); - logger.debug(entity.toString()); - return performRequest(method,endPoint,JSON.toJSONString(entity)); - } - - public static EsResponse searchQuery(String index, String type, ElasticSearchQuery query) throws IOException{ - String method = "POST"; - String entPoint = index.concat("/").concat(type).concat("/").concat("_search"); - logger.debug(query.getBody().toString()); - Response response = performRequest(method, entPoint, query.getBody().toString()); - // 获取response body,转换为json对象 - JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); - return result.toJavaObject(EsResponse.class); - } - - public static Response aggsQuery(String index, String type, ElasticSearchQuery query) throws IOException{ - String method = "POST"; - String entPoint = index.concat("/").concat(type).concat("/").concat("_search"); - logger.debug(query.getBody().toString()); - Response response = performRequest(method, entPoint, query.getBody().toString()); - return response; - } - - public static List searchQueryScroll(String index, String type, ElasticSearchQuery query) throws IOException{ - // 首次查询,提交查询条件,endpoint增加‘?scroll=1m’ - List results = new ArrayList<>(); - String method = "POST"; - String entPoint = index.concat("/").concat(type).concat("/").concat("_search").concat("?scroll=1m"); - logger.debug(query.getBody().toString()); - Response response = performRequest(method, entPoint, query.getBody().toString()); - JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); - EsResponse esResponse = result.toJavaObject(EsResponse.class); - String scrollId = result.getString("_scroll_id"); - if(ObjectUtil.isNotEmpty(esResponse.getDatas())){ - results.addAll(esResponse.getDatas()); - } - // 循环发送scroll请求,直到返回结果为空 - entPoint = "_search/scroll"; - JSONObject scrollBody = new JSONObject(); - scrollBody.put("scroll","1m"); - scrollBody.put("scroll_id",scrollId); - while (esResponse.getDatas().size() > 0){ - method = "GET"; - response = performRequest(method, entPoint, scrollBody.toString()); - result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); - esResponse = result.toJavaObject(EsResponse.class); - if(ObjectUtil.isNotEmpty(esResponse.getDatas())){ - results.addAll(esResponse.getDatas()); - } - } - // 查询完成后,及时删除scroll,释放资源 - scrollBody.remove("scroll"); - method = "DELETE"; - performRequest(method, entPoint, scrollBody.toString()); - return results; - } - - public static Response bulk(String index,String type,String entity) throws IOException{ - String method = "POST"; - StringBuilder endPoint = new StringBuilder(); - if(ObjectUtil.isNotEmpty(index)){ - endPoint.append(index).append("/"); - } - if(ObjectUtil.isNotEmpty(type)){ - endPoint.append(type).append("/"); - } - endPoint.append("_bulk"); - endPoint.append("?refresh=true"); - logger.debug(entity); - return performRequest(method,endPoint.toString(),entity); - } - - private static Response performRequest(String method, String endpoint, String entity) throws IOException { - //logger.info(method + " " + endpoint); - Request request = new Request(method,endpoint); - if(ObjectUtil.isNotEmpty(entity)){ - request.setEntity(new NStringEntity(entity,"utf-8")); - } - - if(ObjectUtil.isNotEmpty(username) && ObjectUtil.isNotEmpty(password)){ - RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder(); - String token = username.concat(":").concat(password); - String base64encode = Base64.getEncoder().encodeToString(token.getBytes("utf-8")); - builder.addHeader("Authorization", "Basic " + base64encode ); - request.setOptions(builder.build()); - } - - - Response response = client.performRequest(request); - logger.debug(response.toString()); - return response; - } - -} diff --git a/casic-data/src/main/java/com/casic/missiles/modular/system/service/IDataGasService.java b/casic-data/src/main/java/com/casic/missiles/modular/system/service/IDataGasService.java index e644294..b2aa6cb 100644 --- a/casic-data/src/main/java/com/casic/missiles/modular/system/service/IDataGasService.java +++ b/casic-data/src/main/java/com/casic/missiles/modular/system/service/IDataGasService.java @@ -1,6 +1,6 @@ package com.casic.missiles.modular.system.service; -import com.casic.missiles.modular.system.es.DataGasEs; +import com.casic.missiles.es.DataGasEs; import com.casic.missiles.modular.system.model.DataGas; import com.baomidou.mybatisplus.extension.service.IService; diff --git a/casic-data/src/main/java/com/casic/missiles/modular/system/service/impl/DataGasServiceImpl.java b/casic-data/src/main/java/com/casic/missiles/modular/system/service/impl/DataGasServiceImpl.java index 1aa01e5..ba3a38d 100644 --- a/casic-data/src/main/java/com/casic/missiles/modular/system/service/impl/DataGasServiceImpl.java +++ b/casic-data/src/main/java/com/casic/missiles/modular/system/service/impl/DataGasServiceImpl.java @@ -1,12 +1,12 @@ package com.casic.missiles.modular.system.service.impl; -import com.casic.missiles.modular.system.consts.DataConst; -import com.casic.missiles.modular.system.es.DataGasEs; -import com.casic.missiles.modular.system.es.ElasticSearchUtil; -import com.casic.missiles.modular.system.model.DataGas; -import com.casic.missiles.modular.system.dao.DataGasMapper; -import com.casic.missiles.modular.system.service.IDataGasService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.casic.missiles.es.DataGasEs; +import com.casic.missiles.es.ElasticSearchUtil; +import com.casic.missiles.modular.system.consts.DataConst; +import com.casic.missiles.modular.system.dao.DataGasMapper; +import com.casic.missiles.modular.system.model.DataGas; +import com.casic.missiles.modular.system.service.IDataGasService; import org.springframework.stereotype.Service; import java.io.IOException; diff --git a/casic-server/src/main/java/com/casic/missiles/modular/system/controller/HCNetController.java b/casic-server/src/main/java/com/casic/missiles/modular/system/controller/HCNetController.java index 941768a..f334286 100644 --- a/casic-server/src/main/java/com/casic/missiles/modular/system/controller/HCNetController.java +++ b/casic-server/src/main/java/com/casic/missiles/modular/system/controller/HCNetController.java @@ -4,6 +4,7 @@ import com.casic.missiles.model.response.ResponseData; import com.casic.missiles.modular.system.dto.DeviceCommom; import com.casic.missiles.modular.system.dto.DeviceInfo; +import com.casic.missiles.modular.system.dto.DeviceTypeEnum; import com.casic.missiles.modular.system.dto.SelectDTO; import com.casic.missiles.modular.system.model.BusMonipoiInfo; import com.casic.missiles.modular.system.service.IBusMonipoiInfoService; @@ -146,8 +147,6 @@ @ResponseBody public Object control(String deviceIp, String command, Integer isStop) { -// DoorShortUtil.remoteControl("192.168.1.103",153203040,1,isStop); - Long userId = getUserIdByIp(deviceIp); if (ObjectUtil.isEmpty(userId) || userId < 0) { return ResponseData.error("设备注册失败"); @@ -278,7 +277,7 @@ } BusMonipoiInfo monipoiInfo = monipoiInfoService.selectByDeviceIp(deviceIp); boolean res; - if (ObjectUtil.isNotEmpty(monipoiInfo) && "1".equals(monipoiInfo.getDeviceType())) { + if (ObjectUtil.isNotEmpty(monipoiInfo) && DeviceTypeEnum.SELF_TYPE.getCode().equals(monipoiInfo.getDeviceType())) { res = ihcNetService.setPosition(userId, Double.valueOf(horizontalAngle), Double.valueOf(verticalAngle)); } else { res = ihcNetService.setPositionNew(userId, Double.valueOf(horizontalAngle), Double.valueOf(verticalAngle)); diff --git a/casic-common/pom.xml b/casic-common/pom.xml index 27e6a55..a47c616 100644 --- a/casic-common/pom.xml +++ b/casic-common/pom.xml @@ -46,6 +46,11 @@ mina-core 2.0.4 + + com.casic + casic-data-es + 1.0.1.alpha + diff --git a/casic-common/src/main/java/com/casic/missiles/es/DataGasEs.java b/casic-common/src/main/java/com/casic/missiles/es/DataGasEs.java new file mode 100644 index 0000000..3b1f3fe --- /dev/null +++ b/casic-common/src/main/java/com/casic/missiles/es/DataGasEs.java @@ -0,0 +1,18 @@ +package com.casic.missiles.es; + +import lombok.Data; + +@Data +public class DataGasEs { + + private Long stationId; + private String stationName; + private Long monitorId; + private String monitorName; + private String devcode; + private Double direction; + private Double pitch; + private Double concentration; + private Double threshold; + private String logTime; +} diff --git a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java new file mode 100644 index 0000000..aeef63d --- /dev/null +++ b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java @@ -0,0 +1,257 @@ +package com.casic.missiles.es; + +import cn.hutool.core.date.DateUtil; +import cn.hutool.core.util.ObjectUtil; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +/** + * ES查询请求体 + * + * @author zhangyingjie123 + * @since 2020-11-30 + */ +public class ElasticSearchQuery { + private JSONObject body; + private JSONObject query; + private JSONObject bool; + private JSONArray boolMust; + private JSONObject filter; + private JSONObject filterBool; + private JSONArray filterBoolMust; + private JSONArray filterBoolMustnot; + + public static final String MUST_FLAG = "1"; + public static final String MUST_NOT_FLAG = "0"; + + public static final String QUERY = "query"; + public static final String BOOL = "bool"; + public static final String MUST = "must"; + public static final String MUST_NOT = "must_not"; + public static final String FILTER = "filter"; + + + /** + * 生成查询请求体基础结构: + * { + * "query": { + * "bool": { + * "must" [], + * "filter": { + * "bool": { + * "must": [], + * "must_not": [] + * } + * } + * } + * } + * } + */ + public ElasticSearchQuery() { + this.body = new JSONObject(); + this.query = this.query(); + this.bool = this.bool(); + this.boolMust = this.boolMust(); + this.filter = this.filter(); + this.filterBool = this.filterBool(); + this.filterBoolMust = this.filterBoolMust(); + this.filterBoolMustnot = this.filterBoolMustnot(); + } + + public ElasticSearchQuery(JSONObject body){ + this.body = body; + } + + public JSONObject getBody() { + return body; + } + + private JSONObject query(){ + if(ObjectUtil.isEmpty(this.body.get(QUERY))){ + this.body.put("query",new JSONObject()); + } + return this.body.getJSONObject(QUERY); + + } + + private JSONObject bool() { + JSONObject query = this.query; + if(ObjectUtil.isEmpty(query.get(BOOL))){ + query.put(BOOL,new JSONObject()); + } + return query.getJSONObject(BOOL); + } + + private JSONArray boolMust(){ + JSONObject bool = this.bool; + if(ObjectUtil.isEmpty(bool.get(MUST))){ + bool.put(MUST,new JSONArray()); + } + return bool.getJSONArray(MUST); + } + + private JSONObject filter(){ + JSONObject bool = this.bool; + if(ObjectUtil.isEmpty(bool.get(FILTER))){ + bool.put(FILTER,new JSONObject()); + } + return bool.getJSONObject(FILTER); + } + + private JSONObject filterBool(){ + JSONObject filter = this.filter; + if(ObjectUtil.isEmpty(filter.get(BOOL))){ + filter.put(BOOL,new JSONObject()); + } + return filter.getJSONObject(BOOL); + } + + private JSONArray filterBoolMust(){ + JSONObject bool = this.filterBool; + if(ObjectUtil.isEmpty(bool.get(MUST))){ + bool.put(MUST,new JSONArray()); + } + return bool.getJSONArray(MUST); + } + + private JSONArray filterBoolMustnot(){ + JSONObject bool = this.filterBool; + if(ObjectUtil.isEmpty(bool.get(MUST_NOT))){ + bool.put(MUST_NOT,new JSONArray()); + } + return bool.getJSONArray(MUST_NOT); + } + + public void match(String filed, Serializable value){ + JSONArray boolMust = this.boolMust; + JSONObject match = new JSONObject(); + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,value); + match.put("match",filedJson); + boolMust.add(match); + } + + public void matchPhrase(String filed, Serializable value){ + JSONArray boolMust = this.boolMust; + JSONObject matchPhrase = new JSONObject(); + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,value); + matchPhrase.put("match_phrase",filedJson); + boolMust.add(matchPhrase); + } + + public void term(String filed, Serializable value){ + this.term(filed,value,MUST_FLAG); + } + + public void term(String filed, Serializable value,String mustFlag){ + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,value); + JSONObject term = new JSONObject(); + term.put("term",filedJson); + if(MUST_FLAG.equals(mustFlag)){ + this.filterBoolMust.add(term); + } + if(MUST_NOT_FLAG.equals(mustFlag)){ + this.filterBoolMustnot.add(term); + } + } + + public void terms(String filed,List value){ + this.terms(filed,value,MUST_FLAG); + } + + public void terms(String filed, List value, String mustFlag){ + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,value); + JSONObject terms = new JSONObject(); + terms.put("terms",filedJson); + if(MUST_FLAG.equals(mustFlag)){ + this.filterBoolMust.add(terms); + } + if(MUST_NOT_FLAG.equals(mustFlag)){ + this.filterBoolMustnot.add(terms); + } + } + + public void range(String filed, Serializable from, Serializable to){ + this.range(filed,from,to,MUST_FLAG); + } + + public void range(String filed, Serializable from, Serializable to, String mustFlag){ + JSONObject rangeJson = new JSONObject(); + if(ObjectUtil.isNotEmpty(from)){ + rangeJson.put("from",from); + } + if(ObjectUtil.isNotEmpty(to)){ + rangeJson.put("to",to); + } + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,rangeJson); + JSONObject range = new JSONObject(); + range.put("range",filedJson); + if(MUST_FLAG.equals(mustFlag)){ + this.filterBoolMust.add(range); + } + if(MUST_NOT_FLAG.equals(mustFlag)){ + this.filterBoolMustnot.add(range); + } + } + + public void size(int size){ + this.body.put("size",size); + } + + public void from(int from) { + this.body.put("from",from); + } + + public void sort(String sort, String order){ + JSONObject sortJson = new JSONObject(); + JSONObject orderJson = new JSONObject(); + orderJson.put("order",order); + sortJson.put(sort,orderJson); + this.body.put("sort",sortJson); + } + + public void aggs(String aggsName, String filed){ + JSONObject terms = new JSONObject(); + terms.put("field",filed); + JSONObject aggsTerms = new JSONObject(); + aggsTerms.put("terms",terms); + JSONObject aggsBody = new JSONObject(); + aggsBody.put(aggsName,aggsTerms); + this.body.put("aggs",aggsBody); + } + + public void aggs(JSONObject aggBody){ + this.body.put("aggs",aggBody); + } + + + + public static void main(String[] args) { + ElasticSearchQuery query = new ElasticSearchQuery(); + query.matchPhrase("name","张三"); + query.term("sex","2"); + List deptIds = new ArrayList<>(); + deptIds.add(0L); + deptIds.add(24L); + query.terms("deptId", deptIds); + Date date1 = DateUtil.parse("2020-01-01","yyyy-MM-dd"); + Date date2 = DateUtil.parse("2020-04-01","yyyy-MM-dd"); + query.range("collTime",date1.getTime(),date2.getTime()); + query.term("collState","3",MUST_NOT_FLAG); + query.size(10); + query.from(0); + query.sort("irId","asc"); + System.out.println(query.getBody().toJSONString()); + } + + +} diff --git a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java new file mode 100644 index 0000000..dc9a968 --- /dev/null +++ b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java @@ -0,0 +1,172 @@ +package com.casic.missiles.es; + +import cn.hutool.core.util.ObjectUtil; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.casic.missiles.core.es.EsResponse; +import org.apache.http.nio.entity.NStringEntity; +import org.apache.http.util.EntityUtils; +import org.elasticsearch.client.Request; +import org.elasticsearch.client.RequestOptions; +import org.elasticsearch.client.Response; +import org.elasticsearch.client.RestClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Base64; +import java.util.List; + +/** + * ES工具类 + * + * @author zhangyingjie123 + * @since 2020-11-30 + */ +@Component +public class ElasticSearchUtil { + private static final Logger logger = LoggerFactory.getLogger(ElasticSearchUtil.class); + + @Autowired + private RestClient clientMapping; + @Autowired + private Environment env; + + private static String username; + private static String password; + + private static RestClient client; + + @PostConstruct + public void init(){ + client = clientMapping; + username = env.getProperty("casic.data.es.username"); + password = env.getProperty("casic.data.es.password"); + } + + public static JSONObject selectDocumentById(String index, String type, String id) throws IOException{ + String method = "GET"; + String endPoint = index.concat("/").concat(type).concat("/").concat(id); + Response response = performRequest(method,endPoint,null); + JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); + return result.getJSONObject("_source"); + } + + public static Response addDocumentById(String index, String type, String id, Object entity) throws IOException{ + String method = "POST"; + String endPoint = index.concat("/").concat(type).concat("/"); + if(ObjectUtil.isNotEmpty(id)){ + endPoint.concat(id); + } + endPoint.concat("?refresh=true"); + logger.debug(entity.toString()); + return performRequest(method,endPoint,JSON.toJSONString(entity)); + } + + public static Response deleteDocumentById(String index, String type, String id) throws IOException{ + String method = "DELETE"; + String endPoint = index.concat("/").concat(type).concat("/").concat(id).concat("?refresh=true"); + return performRequest(method,endPoint,null); + } + + public static Response updateDocumentById(String index, String type, String id, Object entity) throws IOException{ + String method = "POST"; + String endPoint = index.concat("/").concat(type).concat("/").concat(id).concat("?refresh=true"); + logger.debug(entity.toString()); + return performRequest(method,endPoint,JSON.toJSONString(entity)); + } + + public static EsResponse searchQuery(String index, String type, ElasticSearchQuery query) throws IOException{ + String method = "POST"; + String entPoint = index.concat("/").concat(type).concat("/").concat("_search"); + logger.debug(query.getBody().toString()); + Response response = performRequest(method, entPoint, query.getBody().toString()); + // 获取response body,转换为json对象 + JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); + return result.toJavaObject(EsResponse.class); + } + + public static Response aggsQuery(String index, String type, ElasticSearchQuery query) throws IOException{ + String method = "POST"; + String entPoint = index.concat("/").concat(type).concat("/").concat("_search"); + logger.debug(query.getBody().toString()); + Response response = performRequest(method, entPoint, query.getBody().toString()); + return response; + } + + public static List searchQueryScroll(String index, String type, ElasticSearchQuery query) throws IOException{ + // 首次查询,提交查询条件,endpoint增加‘?scroll=1m’ + List results = new ArrayList<>(); + String method = "POST"; + String entPoint = index.concat("/").concat(type).concat("/").concat("_search").concat("?scroll=1m"); + logger.debug(query.getBody().toString()); + Response response = performRequest(method, entPoint, query.getBody().toString()); + JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); + EsResponse esResponse = result.toJavaObject(EsResponse.class); + String scrollId = result.getString("_scroll_id"); + if(ObjectUtil.isNotEmpty(esResponse.getDatas())){ + results.addAll(esResponse.getDatas()); + } + // 循环发送scroll请求,直到返回结果为空 + entPoint = "_search/scroll"; + JSONObject scrollBody = new JSONObject(); + scrollBody.put("scroll","1m"); + scrollBody.put("scroll_id",scrollId); + while (esResponse.getDatas().size() > 0){ + method = "GET"; + response = performRequest(method, entPoint, scrollBody.toString()); + result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); + esResponse = result.toJavaObject(EsResponse.class); + if(ObjectUtil.isNotEmpty(esResponse.getDatas())){ + results.addAll(esResponse.getDatas()); + } + } + // 查询完成后,及时删除scroll,释放资源 + scrollBody.remove("scroll"); + method = "DELETE"; + performRequest(method, entPoint, scrollBody.toString()); + return results; + } + + public static Response bulk(String index,String type,String entity) throws IOException{ + String method = "POST"; + StringBuilder endPoint = new StringBuilder(); + if(ObjectUtil.isNotEmpty(index)){ + endPoint.append(index).append("/"); + } + if(ObjectUtil.isNotEmpty(type)){ + endPoint.append(type).append("/"); + } + endPoint.append("_bulk"); + endPoint.append("?refresh=true"); + logger.debug(entity); + return performRequest(method,endPoint.toString(),entity); + } + + private static Response performRequest(String method, String endpoint, String entity) throws IOException { + //logger.info(method + " " + endpoint); + Request request = new Request(method,endpoint); + if(ObjectUtil.isNotEmpty(entity)){ + request.setEntity(new NStringEntity(entity,"utf-8")); + } + + if(ObjectUtil.isNotEmpty(username) && ObjectUtil.isNotEmpty(password)){ + RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder(); + String token = username.concat(":").concat(password); + String base64encode = Base64.getEncoder().encodeToString(token.getBytes("utf-8")); + builder.addHeader("Authorization", "Basic " + base64encode ); + request.setOptions(builder.build()); + } + + + Response response = client.performRequest(request); + logger.debug(response.toString()); + return response; + } + +} diff --git a/casic-common/src/main/java/com/casic/missiles/weigeng/CommDoorUtil.java b/casic-common/src/main/java/com/casic/missiles/weigeng/CommDoorUtil.java index bed1361..7457109 100644 --- a/casic-common/src/main/java/com/casic/missiles/weigeng/CommDoorUtil.java +++ b/casic-common/src/main/java/com/casic/missiles/weigeng/CommDoorUtil.java @@ -118,15 +118,14 @@ } //重启设备 - public static boolean restart(String monitorId) { + public static boolean restart(String controllerIP, String controllerSN) { try { - String doors = ObjectUtil.isNotEmpty(map.get(monitorId)) ? map.get(monitorId).toString() : ""; - if (ObjectUtil.isNotEmpty(doors)) - if (DoorShortUtil.remoteControl(doors.split(",")[1], Long.valueOf(doors.split(",")[0]), 2, 1)) { + if (ObjectUtil.isAllNotEmpty(controllerIP, controllerSN)) + if (DoorShortUtil.remoteControl(controllerIP, Long.valueOf(controllerSN), 2, 1)) { //重新上电需要间隔20秒 Thread.sleep(Integer.valueOf(map.get("time").toString()) * 1000); //重新关闭开关上电 - return DoorShortUtil.remoteControl(doors.split(",")[1], Long.valueOf(doors.split(",")[0]), 2, 2); + return DoorShortUtil.remoteControl(controllerIP, Long.valueOf(controllerSN), 2, 2); } } catch (Exception e) { e.printStackTrace(); @@ -135,16 +134,14 @@ } //重启设备 - public static boolean interruptStart(String monitorId) { + public static boolean interruptStart(String controllerIP, String controllerSN) { try { - String doors = ObjectUtil.isNotEmpty(map.get(monitorId)) ? map.get(monitorId).toString() : ""; - if (ObjectUtil.isNotEmpty(doors)) - if (DoorShortUtil.remoteControl(doors.split(",")[1], Long.valueOf(doors.split(",")[0]), 2, 1)) { + if (ObjectUtil.isAllNotEmpty(controllerIP,controllerSN)) + if (DoorShortUtil.remoteControl(controllerIP, Long.valueOf(controllerSN), 2, 1)) { //重新上电需要间隔20秒 Thread.sleep(Integer.valueOf(map.get("time").toString()) * 1000); //重新关闭开关上电 - DoorShortUtil.remoteControl(doors.split(",")[1], Long.valueOf(doors.split(",")[0]), 2, 2); - + DoorShortUtil.remoteControl(controllerIP, Long.valueOf(controllerSN), 2, 2); Thread.sleep(2 * 60 * 1000); return true; } diff --git a/casic-data/pom.xml b/casic-data/pom.xml index aca0a62..a076800 100644 --- a/casic-data/pom.xml +++ b/casic-data/pom.xml @@ -43,11 +43,6 @@ com.casic - casic-data-es - 1.0.1.alpha - - - com.casic casic-common 2.0.0 compile diff --git a/casic-data/src/main/java/com/casic/missiles/modular/system/es/DataGasEs.java b/casic-data/src/main/java/com/casic/missiles/modular/system/es/DataGasEs.java deleted file mode 100644 index 821e8f1..0000000 --- a/casic-data/src/main/java/com/casic/missiles/modular/system/es/DataGasEs.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.casic.missiles.modular.system.es; - -import lombok.Data; - -@Data -public class DataGasEs { - - private Long stationId; - private String stationName; - private Long monitorId; - private String monitorName; - private String devcode; - private Double direction; - private Double pitch; - private Double concentration; - private Double threshold; - private String logTime; -} diff --git a/casic-data/src/main/java/com/casic/missiles/modular/system/es/ElasticSearchQuery.java b/casic-data/src/main/java/com/casic/missiles/modular/system/es/ElasticSearchQuery.java deleted file mode 100644 index 4d7a38f..0000000 --- a/casic-data/src/main/java/com/casic/missiles/modular/system/es/ElasticSearchQuery.java +++ /dev/null @@ -1,257 +0,0 @@ -package com.casic.missiles.modular.system.es; - -import cn.hutool.core.date.DateUtil; -import cn.hutool.core.util.ObjectUtil; -import com.alibaba.fastjson.JSONArray; -import com.alibaba.fastjson.JSONObject; - -import java.io.Serializable; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; - -/** - * ES查询请求体 - * - * @author zhangyingjie123 - * @since 2020-11-30 - */ -public class ElasticSearchQuery { - private JSONObject body; - private JSONObject query; - private JSONObject bool; - private JSONArray boolMust; - private JSONObject filter; - private JSONObject filterBool; - private JSONArray filterBoolMust; - private JSONArray filterBoolMustnot; - - public static final String MUST_FLAG = "1"; - public static final String MUST_NOT_FLAG = "0"; - - public static final String QUERY = "query"; - public static final String BOOL = "bool"; - public static final String MUST = "must"; - public static final String MUST_NOT = "must_not"; - public static final String FILTER = "filter"; - - - /** - * 生成查询请求体基础结构: - * { - * "query": { - * "bool": { - * "must" [], - * "filter": { - * "bool": { - * "must": [], - * "must_not": [] - * } - * } - * } - * } - * } - */ - public ElasticSearchQuery() { - this.body = new JSONObject(); - this.query = this.query(); - this.bool = this.bool(); - this.boolMust = this.boolMust(); - this.filter = this.filter(); - this.filterBool = this.filterBool(); - this.filterBoolMust = this.filterBoolMust(); - this.filterBoolMustnot = this.filterBoolMustnot(); - } - - public ElasticSearchQuery(JSONObject body){ - this.body = body; - } - - public JSONObject getBody() { - return body; - } - - private JSONObject query(){ - if(ObjectUtil.isEmpty(this.body.get(QUERY))){ - this.body.put("query",new JSONObject()); - } - return this.body.getJSONObject(QUERY); - - } - - private JSONObject bool() { - JSONObject query = this.query; - if(ObjectUtil.isEmpty(query.get(BOOL))){ - query.put(BOOL,new JSONObject()); - } - return query.getJSONObject(BOOL); - } - - private JSONArray boolMust(){ - JSONObject bool = this.bool; - if(ObjectUtil.isEmpty(bool.get(MUST))){ - bool.put(MUST,new JSONArray()); - } - return bool.getJSONArray(MUST); - } - - private JSONObject filter(){ - JSONObject bool = this.bool; - if(ObjectUtil.isEmpty(bool.get(FILTER))){ - bool.put(FILTER,new JSONObject()); - } - return bool.getJSONObject(FILTER); - } - - private JSONObject filterBool(){ - JSONObject filter = this.filter; - if(ObjectUtil.isEmpty(filter.get(BOOL))){ - filter.put(BOOL,new JSONObject()); - } - return filter.getJSONObject(BOOL); - } - - private JSONArray filterBoolMust(){ - JSONObject bool = this.filterBool; - if(ObjectUtil.isEmpty(bool.get(MUST))){ - bool.put(MUST,new JSONArray()); - } - return bool.getJSONArray(MUST); - } - - private JSONArray filterBoolMustnot(){ - JSONObject bool = this.filterBool; - if(ObjectUtil.isEmpty(bool.get(MUST_NOT))){ - bool.put(MUST_NOT,new JSONArray()); - } - return bool.getJSONArray(MUST_NOT); - } - - public void match(String filed, Serializable value){ - JSONArray boolMust = this.boolMust; - JSONObject match = new JSONObject(); - JSONObject filedJson = new JSONObject(); - filedJson.put(filed,value); - match.put("match",filedJson); - boolMust.add(match); - } - - public void matchPhrase(String filed, Serializable value){ - JSONArray boolMust = this.boolMust; - JSONObject matchPhrase = new JSONObject(); - JSONObject filedJson = new JSONObject(); - filedJson.put(filed,value); - matchPhrase.put("match_phrase",filedJson); - boolMust.add(matchPhrase); - } - - public void term(String filed, Serializable value){ - this.term(filed,value,MUST_FLAG); - } - - public void term(String filed, Serializable value,String mustFlag){ - JSONObject filedJson = new JSONObject(); - filedJson.put(filed,value); - JSONObject term = new JSONObject(); - term.put("term",filedJson); - if(MUST_FLAG.equals(mustFlag)){ - this.filterBoolMust.add(term); - } - if(MUST_NOT_FLAG.equals(mustFlag)){ - this.filterBoolMustnot.add(term); - } - } - - public void terms(String filed,List value){ - this.terms(filed,value,MUST_FLAG); - } - - public void terms(String filed, List value, String mustFlag){ - JSONObject filedJson = new JSONObject(); - filedJson.put(filed,value); - JSONObject terms = new JSONObject(); - terms.put("terms",filedJson); - if(MUST_FLAG.equals(mustFlag)){ - this.filterBoolMust.add(terms); - } - if(MUST_NOT_FLAG.equals(mustFlag)){ - this.filterBoolMustnot.add(terms); - } - } - - public void range(String filed, Serializable from, Serializable to){ - this.range(filed,from,to,MUST_FLAG); - } - - public void range(String filed, Serializable from, Serializable to, String mustFlag){ - JSONObject rangeJson = new JSONObject(); - if(ObjectUtil.isNotEmpty(from)){ - rangeJson.put("from",from); - } - if(ObjectUtil.isNotEmpty(to)){ - rangeJson.put("to",to); - } - JSONObject filedJson = new JSONObject(); - filedJson.put(filed,rangeJson); - JSONObject range = new JSONObject(); - range.put("range",filedJson); - if(MUST_FLAG.equals(mustFlag)){ - this.filterBoolMust.add(range); - } - if(MUST_NOT_FLAG.equals(mustFlag)){ - this.filterBoolMustnot.add(range); - } - } - - public void size(int size){ - this.body.put("size",size); - } - - public void from(int from) { - this.body.put("from",from); - } - - public void sort(String sort, String order){ - JSONObject sortJson = new JSONObject(); - JSONObject orderJson = new JSONObject(); - orderJson.put("order",order); - sortJson.put(sort,orderJson); - this.body.put("sort",sortJson); - } - - public void aggs(String aggsName, String filed){ - JSONObject terms = new JSONObject(); - terms.put("field",filed); - JSONObject aggsTerms = new JSONObject(); - aggsTerms.put("terms",terms); - JSONObject aggsBody = new JSONObject(); - aggsBody.put(aggsName,aggsTerms); - this.body.put("aggs",aggsBody); - } - - public void aggs(JSONObject aggBody){ - this.body.put("aggs",aggBody); - } - - - - public static void main(String[] args) { - ElasticSearchQuery query = new ElasticSearchQuery(); - query.matchPhrase("name","张三"); - query.term("sex","2"); - List deptIds = new ArrayList<>(); - deptIds.add(0L); - deptIds.add(24L); - query.terms("deptId", deptIds); - Date date1 = DateUtil.parse("2020-01-01","yyyy-MM-dd"); - Date date2 = DateUtil.parse("2020-04-01","yyyy-MM-dd"); - query.range("collTime",date1.getTime(),date2.getTime()); - query.term("collState","3",MUST_NOT_FLAG); - query.size(10); - query.from(0); - query.sort("irId","asc"); - System.out.println(query.getBody().toJSONString()); - } - - -} diff --git a/casic-data/src/main/java/com/casic/missiles/modular/system/es/ElasticSearchUtil.java b/casic-data/src/main/java/com/casic/missiles/modular/system/es/ElasticSearchUtil.java deleted file mode 100644 index e9da021..0000000 --- a/casic-data/src/main/java/com/casic/missiles/modular/system/es/ElasticSearchUtil.java +++ /dev/null @@ -1,172 +0,0 @@ -package com.casic.missiles.modular.system.es; - -import cn.hutool.core.util.ObjectUtil; -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONObject; -import com.casic.missiles.core.es.EsResponse; -import org.apache.http.nio.entity.NStringEntity; -import org.apache.http.util.EntityUtils; -import org.elasticsearch.client.Request; -import org.elasticsearch.client.RequestOptions; -import org.elasticsearch.client.Response; -import org.elasticsearch.client.RestClient; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.core.env.Environment; -import org.springframework.stereotype.Component; - -import javax.annotation.PostConstruct; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Base64; -import java.util.List; - -/** - * ES工具类 - * - * @author zhangyingjie123 - * @since 2020-11-30 - */ -@Component -public class ElasticSearchUtil { - private static final Logger logger = LoggerFactory.getLogger(ElasticSearchUtil.class); - - @Autowired - private RestClient clientMapping; - @Autowired - private Environment env; - - private static String username; - private static String password; - - private static RestClient client; - - @PostConstruct - public void init(){ - client = clientMapping; - username = env.getProperty("casic.data.es.username"); - password = env.getProperty("casic.data.es.password"); - } - - public static JSONObject selectDocumentById(String index, String type, String id) throws IOException{ - String method = "GET"; - String endPoint = index.concat("/").concat(type).concat("/").concat(id); - Response response = performRequest(method,endPoint,null); - JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); - return result.getJSONObject("_source"); - } - - public static Response addDocumentById(String index, String type, String id, Object entity) throws IOException{ - String method = "POST"; - String endPoint = index.concat("/").concat(type).concat("/"); - if(ObjectUtil.isNotEmpty(id)){ - endPoint.concat(id); - } - endPoint.concat("?refresh=true"); - logger.debug(entity.toString()); - return performRequest(method,endPoint,JSON.toJSONString(entity)); - } - - public static Response deleteDocumentById(String index, String type, String id) throws IOException{ - String method = "DELETE"; - String endPoint = index.concat("/").concat(type).concat("/").concat(id).concat("?refresh=true"); - return performRequest(method,endPoint,null); - } - - public static Response updateDocumentById(String index, String type, String id, Object entity) throws IOException{ - String method = "POST"; - String endPoint = index.concat("/").concat(type).concat("/").concat(id).concat("?refresh=true"); - logger.debug(entity.toString()); - return performRequest(method,endPoint,JSON.toJSONString(entity)); - } - - public static EsResponse searchQuery(String index, String type, ElasticSearchQuery query) throws IOException{ - String method = "POST"; - String entPoint = index.concat("/").concat(type).concat("/").concat("_search"); - logger.debug(query.getBody().toString()); - Response response = performRequest(method, entPoint, query.getBody().toString()); - // 获取response body,转换为json对象 - JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); - return result.toJavaObject(EsResponse.class); - } - - public static Response aggsQuery(String index, String type, ElasticSearchQuery query) throws IOException{ - String method = "POST"; - String entPoint = index.concat("/").concat(type).concat("/").concat("_search"); - logger.debug(query.getBody().toString()); - Response response = performRequest(method, entPoint, query.getBody().toString()); - return response; - } - - public static List searchQueryScroll(String index, String type, ElasticSearchQuery query) throws IOException{ - // 首次查询,提交查询条件,endpoint增加‘?scroll=1m’ - List results = new ArrayList<>(); - String method = "POST"; - String entPoint = index.concat("/").concat(type).concat("/").concat("_search").concat("?scroll=1m"); - logger.debug(query.getBody().toString()); - Response response = performRequest(method, entPoint, query.getBody().toString()); - JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); - EsResponse esResponse = result.toJavaObject(EsResponse.class); - String scrollId = result.getString("_scroll_id"); - if(ObjectUtil.isNotEmpty(esResponse.getDatas())){ - results.addAll(esResponse.getDatas()); - } - // 循环发送scroll请求,直到返回结果为空 - entPoint = "_search/scroll"; - JSONObject scrollBody = new JSONObject(); - scrollBody.put("scroll","1m"); - scrollBody.put("scroll_id",scrollId); - while (esResponse.getDatas().size() > 0){ - method = "GET"; - response = performRequest(method, entPoint, scrollBody.toString()); - result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); - esResponse = result.toJavaObject(EsResponse.class); - if(ObjectUtil.isNotEmpty(esResponse.getDatas())){ - results.addAll(esResponse.getDatas()); - } - } - // 查询完成后,及时删除scroll,释放资源 - scrollBody.remove("scroll"); - method = "DELETE"; - performRequest(method, entPoint, scrollBody.toString()); - return results; - } - - public static Response bulk(String index,String type,String entity) throws IOException{ - String method = "POST"; - StringBuilder endPoint = new StringBuilder(); - if(ObjectUtil.isNotEmpty(index)){ - endPoint.append(index).append("/"); - } - if(ObjectUtil.isNotEmpty(type)){ - endPoint.append(type).append("/"); - } - endPoint.append("_bulk"); - endPoint.append("?refresh=true"); - logger.debug(entity); - return performRequest(method,endPoint.toString(),entity); - } - - private static Response performRequest(String method, String endpoint, String entity) throws IOException { - //logger.info(method + " " + endpoint); - Request request = new Request(method,endpoint); - if(ObjectUtil.isNotEmpty(entity)){ - request.setEntity(new NStringEntity(entity,"utf-8")); - } - - if(ObjectUtil.isNotEmpty(username) && ObjectUtil.isNotEmpty(password)){ - RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder(); - String token = username.concat(":").concat(password); - String base64encode = Base64.getEncoder().encodeToString(token.getBytes("utf-8")); - builder.addHeader("Authorization", "Basic " + base64encode ); - request.setOptions(builder.build()); - } - - - Response response = client.performRequest(request); - logger.debug(response.toString()); - return response; - } - -} diff --git a/casic-data/src/main/java/com/casic/missiles/modular/system/service/IDataGasService.java b/casic-data/src/main/java/com/casic/missiles/modular/system/service/IDataGasService.java index e644294..b2aa6cb 100644 --- a/casic-data/src/main/java/com/casic/missiles/modular/system/service/IDataGasService.java +++ b/casic-data/src/main/java/com/casic/missiles/modular/system/service/IDataGasService.java @@ -1,6 +1,6 @@ package com.casic.missiles.modular.system.service; -import com.casic.missiles.modular.system.es.DataGasEs; +import com.casic.missiles.es.DataGasEs; import com.casic.missiles.modular.system.model.DataGas; import com.baomidou.mybatisplus.extension.service.IService; diff --git a/casic-data/src/main/java/com/casic/missiles/modular/system/service/impl/DataGasServiceImpl.java b/casic-data/src/main/java/com/casic/missiles/modular/system/service/impl/DataGasServiceImpl.java index 1aa01e5..ba3a38d 100644 --- a/casic-data/src/main/java/com/casic/missiles/modular/system/service/impl/DataGasServiceImpl.java +++ b/casic-data/src/main/java/com/casic/missiles/modular/system/service/impl/DataGasServiceImpl.java @@ -1,12 +1,12 @@ package com.casic.missiles.modular.system.service.impl; -import com.casic.missiles.modular.system.consts.DataConst; -import com.casic.missiles.modular.system.es.DataGasEs; -import com.casic.missiles.modular.system.es.ElasticSearchUtil; -import com.casic.missiles.modular.system.model.DataGas; -import com.casic.missiles.modular.system.dao.DataGasMapper; -import com.casic.missiles.modular.system.service.IDataGasService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.casic.missiles.es.DataGasEs; +import com.casic.missiles.es.ElasticSearchUtil; +import com.casic.missiles.modular.system.consts.DataConst; +import com.casic.missiles.modular.system.dao.DataGasMapper; +import com.casic.missiles.modular.system.model.DataGas; +import com.casic.missiles.modular.system.service.IDataGasService; import org.springframework.stereotype.Service; import java.io.IOException; diff --git a/casic-server/src/main/java/com/casic/missiles/modular/system/controller/HCNetController.java b/casic-server/src/main/java/com/casic/missiles/modular/system/controller/HCNetController.java index 941768a..f334286 100644 --- a/casic-server/src/main/java/com/casic/missiles/modular/system/controller/HCNetController.java +++ b/casic-server/src/main/java/com/casic/missiles/modular/system/controller/HCNetController.java @@ -4,6 +4,7 @@ import com.casic.missiles.model.response.ResponseData; import com.casic.missiles.modular.system.dto.DeviceCommom; import com.casic.missiles.modular.system.dto.DeviceInfo; +import com.casic.missiles.modular.system.dto.DeviceTypeEnum; import com.casic.missiles.modular.system.dto.SelectDTO; import com.casic.missiles.modular.system.model.BusMonipoiInfo; import com.casic.missiles.modular.system.service.IBusMonipoiInfoService; @@ -146,8 +147,6 @@ @ResponseBody public Object control(String deviceIp, String command, Integer isStop) { -// DoorShortUtil.remoteControl("192.168.1.103",153203040,1,isStop); - Long userId = getUserIdByIp(deviceIp); if (ObjectUtil.isEmpty(userId) || userId < 0) { return ResponseData.error("设备注册失败"); @@ -278,7 +277,7 @@ } BusMonipoiInfo monipoiInfo = monipoiInfoService.selectByDeviceIp(deviceIp); boolean res; - if (ObjectUtil.isNotEmpty(monipoiInfo) && "1".equals(monipoiInfo.getDeviceType())) { + if (ObjectUtil.isNotEmpty(monipoiInfo) && DeviceTypeEnum.SELF_TYPE.getCode().equals(monipoiInfo.getDeviceType())) { res = ihcNetService.setPosition(userId, Double.valueOf(horizontalAngle), Double.valueOf(verticalAngle)); } else { res = ihcNetService.setPositionNew(userId, Double.valueOf(horizontalAngle), Double.valueOf(verticalAngle)); diff --git a/casic-server/src/main/java/com/casic/missiles/modular/system/dto/DeviceTypeEnum.java b/casic-server/src/main/java/com/casic/missiles/modular/system/dto/DeviceTypeEnum.java index 9357adb..1c43528 100644 --- a/casic-server/src/main/java/com/casic/missiles/modular/system/dto/DeviceTypeEnum.java +++ b/casic-server/src/main/java/com/casic/missiles/modular/system/dto/DeviceTypeEnum.java @@ -6,8 +6,8 @@ * @Date: 2022/11/18 14:40 */ public enum DeviceTypeEnum { - SELF_TYPE("1", "天讯通"), ARD_TYPE("0", "奥瑞德"), + SELF_TYPE("1", "天讯通"), XST_TYPE("2", "宇视通"); private String code; diff --git a/casic-common/pom.xml b/casic-common/pom.xml index 27e6a55..a47c616 100644 --- a/casic-common/pom.xml +++ b/casic-common/pom.xml @@ -46,6 +46,11 @@ mina-core 2.0.4 + + com.casic + casic-data-es + 1.0.1.alpha + diff --git a/casic-common/src/main/java/com/casic/missiles/es/DataGasEs.java b/casic-common/src/main/java/com/casic/missiles/es/DataGasEs.java new file mode 100644 index 0000000..3b1f3fe --- /dev/null +++ b/casic-common/src/main/java/com/casic/missiles/es/DataGasEs.java @@ -0,0 +1,18 @@ +package com.casic.missiles.es; + +import lombok.Data; + +@Data +public class DataGasEs { + + private Long stationId; + private String stationName; + private Long monitorId; + private String monitorName; + private String devcode; + private Double direction; + private Double pitch; + private Double concentration; + private Double threshold; + private String logTime; +} diff --git a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java new file mode 100644 index 0000000..aeef63d --- /dev/null +++ b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java @@ -0,0 +1,257 @@ +package com.casic.missiles.es; + +import cn.hutool.core.date.DateUtil; +import cn.hutool.core.util.ObjectUtil; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +/** + * ES查询请求体 + * + * @author zhangyingjie123 + * @since 2020-11-30 + */ +public class ElasticSearchQuery { + private JSONObject body; + private JSONObject query; + private JSONObject bool; + private JSONArray boolMust; + private JSONObject filter; + private JSONObject filterBool; + private JSONArray filterBoolMust; + private JSONArray filterBoolMustnot; + + public static final String MUST_FLAG = "1"; + public static final String MUST_NOT_FLAG = "0"; + + public static final String QUERY = "query"; + public static final String BOOL = "bool"; + public static final String MUST = "must"; + public static final String MUST_NOT = "must_not"; + public static final String FILTER = "filter"; + + + /** + * 生成查询请求体基础结构: + * { + * "query": { + * "bool": { + * "must" [], + * "filter": { + * "bool": { + * "must": [], + * "must_not": [] + * } + * } + * } + * } + * } + */ + public ElasticSearchQuery() { + this.body = new JSONObject(); + this.query = this.query(); + this.bool = this.bool(); + this.boolMust = this.boolMust(); + this.filter = this.filter(); + this.filterBool = this.filterBool(); + this.filterBoolMust = this.filterBoolMust(); + this.filterBoolMustnot = this.filterBoolMustnot(); + } + + public ElasticSearchQuery(JSONObject body){ + this.body = body; + } + + public JSONObject getBody() { + return body; + } + + private JSONObject query(){ + if(ObjectUtil.isEmpty(this.body.get(QUERY))){ + this.body.put("query",new JSONObject()); + } + return this.body.getJSONObject(QUERY); + + } + + private JSONObject bool() { + JSONObject query = this.query; + if(ObjectUtil.isEmpty(query.get(BOOL))){ + query.put(BOOL,new JSONObject()); + } + return query.getJSONObject(BOOL); + } + + private JSONArray boolMust(){ + JSONObject bool = this.bool; + if(ObjectUtil.isEmpty(bool.get(MUST))){ + bool.put(MUST,new JSONArray()); + } + return bool.getJSONArray(MUST); + } + + private JSONObject filter(){ + JSONObject bool = this.bool; + if(ObjectUtil.isEmpty(bool.get(FILTER))){ + bool.put(FILTER,new JSONObject()); + } + return bool.getJSONObject(FILTER); + } + + private JSONObject filterBool(){ + JSONObject filter = this.filter; + if(ObjectUtil.isEmpty(filter.get(BOOL))){ + filter.put(BOOL,new JSONObject()); + } + return filter.getJSONObject(BOOL); + } + + private JSONArray filterBoolMust(){ + JSONObject bool = this.filterBool; + if(ObjectUtil.isEmpty(bool.get(MUST))){ + bool.put(MUST,new JSONArray()); + } + return bool.getJSONArray(MUST); + } + + private JSONArray filterBoolMustnot(){ + JSONObject bool = this.filterBool; + if(ObjectUtil.isEmpty(bool.get(MUST_NOT))){ + bool.put(MUST_NOT,new JSONArray()); + } + return bool.getJSONArray(MUST_NOT); + } + + public void match(String filed, Serializable value){ + JSONArray boolMust = this.boolMust; + JSONObject match = new JSONObject(); + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,value); + match.put("match",filedJson); + boolMust.add(match); + } + + public void matchPhrase(String filed, Serializable value){ + JSONArray boolMust = this.boolMust; + JSONObject matchPhrase = new JSONObject(); + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,value); + matchPhrase.put("match_phrase",filedJson); + boolMust.add(matchPhrase); + } + + public void term(String filed, Serializable value){ + this.term(filed,value,MUST_FLAG); + } + + public void term(String filed, Serializable value,String mustFlag){ + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,value); + JSONObject term = new JSONObject(); + term.put("term",filedJson); + if(MUST_FLAG.equals(mustFlag)){ + this.filterBoolMust.add(term); + } + if(MUST_NOT_FLAG.equals(mustFlag)){ + this.filterBoolMustnot.add(term); + } + } + + public void terms(String filed,List value){ + this.terms(filed,value,MUST_FLAG); + } + + public void terms(String filed, List value, String mustFlag){ + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,value); + JSONObject terms = new JSONObject(); + terms.put("terms",filedJson); + if(MUST_FLAG.equals(mustFlag)){ + this.filterBoolMust.add(terms); + } + if(MUST_NOT_FLAG.equals(mustFlag)){ + this.filterBoolMustnot.add(terms); + } + } + + public void range(String filed, Serializable from, Serializable to){ + this.range(filed,from,to,MUST_FLAG); + } + + public void range(String filed, Serializable from, Serializable to, String mustFlag){ + JSONObject rangeJson = new JSONObject(); + if(ObjectUtil.isNotEmpty(from)){ + rangeJson.put("from",from); + } + if(ObjectUtil.isNotEmpty(to)){ + rangeJson.put("to",to); + } + JSONObject filedJson = new JSONObject(); + filedJson.put(filed,rangeJson); + JSONObject range = new JSONObject(); + range.put("range",filedJson); + if(MUST_FLAG.equals(mustFlag)){ + this.filterBoolMust.add(range); + } + if(MUST_NOT_FLAG.equals(mustFlag)){ + this.filterBoolMustnot.add(range); + } + } + + public void size(int size){ + this.body.put("size",size); + } + + public void from(int from) { + this.body.put("from",from); + } + + public void sort(String sort, String order){ + JSONObject sortJson = new JSONObject(); + JSONObject orderJson = new JSONObject(); + orderJson.put("order",order); + sortJson.put(sort,orderJson); + this.body.put("sort",sortJson); + } + + public void aggs(String aggsName, String filed){ + JSONObject terms = new JSONObject(); + terms.put("field",filed); + JSONObject aggsTerms = new JSONObject(); + aggsTerms.put("terms",terms); + JSONObject aggsBody = new JSONObject(); + aggsBody.put(aggsName,aggsTerms); + this.body.put("aggs",aggsBody); + } + + public void aggs(JSONObject aggBody){ + this.body.put("aggs",aggBody); + } + + + + public static void main(String[] args) { + ElasticSearchQuery query = new ElasticSearchQuery(); + query.matchPhrase("name","张三"); + query.term("sex","2"); + List deptIds = new ArrayList<>(); + deptIds.add(0L); + deptIds.add(24L); + query.terms("deptId", deptIds); + Date date1 = DateUtil.parse("2020-01-01","yyyy-MM-dd"); + Date date2 = DateUtil.parse("2020-04-01","yyyy-MM-dd"); + query.range("collTime",date1.getTime(),date2.getTime()); + query.term("collState","3",MUST_NOT_FLAG); + query.size(10); + query.from(0); + query.sort("irId","asc"); + System.out.println(query.getBody().toJSONString()); + } + + +} diff --git a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java new file mode 100644 index 0000000..dc9a968 --- /dev/null +++ b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java @@ -0,0 +1,172 @@ +package com.casic.missiles.es; + +import cn.hutool.core.util.ObjectUtil; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.casic.missiles.core.es.EsResponse; +import org.apache.http.nio.entity.NStringEntity; +import org.apache.http.util.EntityUtils; +import org.elasticsearch.client.Request; +import org.elasticsearch.client.RequestOptions; +import org.elasticsearch.client.Response; +import org.elasticsearch.client.RestClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Base64; +import java.util.List; + +/** + * ES工具类 + * + * @author zhangyingjie123 + * @since 2020-11-30 + */ +@Component +public class ElasticSearchUtil { + private static final Logger logger = LoggerFactory.getLogger(ElasticSearchUtil.class); + + @Autowired + private RestClient clientMapping; + @Autowired + private Environment env; + + private static String username; + private static String password; + + private static RestClient client; + + @PostConstruct + public void init(){ + client = clientMapping; + username = env.getProperty("casic.data.es.username"); + password = env.getProperty("casic.data.es.password"); + } + + public static JSONObject selectDocumentById(String index, String type, String id) throws IOException{ + String method = "GET"; + String endPoint = index.concat("/").concat(type).concat("/").concat(id); + Response response = performRequest(method,endPoint,null); + JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); + return result.getJSONObject("_source"); + } + + public static Response addDocumentById(String index, String type, String id, Object entity) throws IOException{ + String method = "POST"; + String endPoint = index.concat("/").concat(type).concat("/"); + if(ObjectUtil.isNotEmpty(id)){ + endPoint.concat(id); + } + endPoint.concat("?refresh=true"); + logger.debug(entity.toString()); + return performRequest(method,endPoint,JSON.toJSONString(entity)); + } + + public static Response deleteDocumentById(String index, String type, String id) throws IOException{ + String method = "DELETE"; + String endPoint = index.concat("/").concat(type).concat("/").concat(id).concat("?refresh=true"); + return performRequest(method,endPoint,null); + } + + public static Response updateDocumentById(String index, String type, String id, Object entity) throws IOException{ + String method = "POST"; + String endPoint = index.concat("/").concat(type).concat("/").concat(id).concat("?refresh=true"); + logger.debug(entity.toString()); + return performRequest(method,endPoint,JSON.toJSONString(entity)); + } + + public static EsResponse searchQuery(String index, String type, ElasticSearchQuery query) throws IOException{ + String method = "POST"; + String entPoint = index.concat("/").concat(type).concat("/").concat("_search"); + logger.debug(query.getBody().toString()); + Response response = performRequest(method, entPoint, query.getBody().toString()); + // 获取response body,转换为json对象 + JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); + return result.toJavaObject(EsResponse.class); + } + + public static Response aggsQuery(String index, String type, ElasticSearchQuery query) throws IOException{ + String method = "POST"; + String entPoint = index.concat("/").concat(type).concat("/").concat("_search"); + logger.debug(query.getBody().toString()); + Response response = performRequest(method, entPoint, query.getBody().toString()); + return response; + } + + public static List searchQueryScroll(String index, String type, ElasticSearchQuery query) throws IOException{ + // 首次查询,提交查询条件,endpoint增加‘?scroll=1m’ + List results = new ArrayList<>(); + String method = "POST"; + String entPoint = index.concat("/").concat(type).concat("/").concat("_search").concat("?scroll=1m"); + logger.debug(query.getBody().toString()); + Response response = performRequest(method, entPoint, query.getBody().toString()); + JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); + EsResponse esResponse = result.toJavaObject(EsResponse.class); + String scrollId = result.getString("_scroll_id"); + if(ObjectUtil.isNotEmpty(esResponse.getDatas())){ + results.addAll(esResponse.getDatas()); + } + // 循环发送scroll请求,直到返回结果为空 + entPoint = "_search/scroll"; + JSONObject scrollBody = new JSONObject(); + scrollBody.put("scroll","1m"); + scrollBody.put("scroll_id",scrollId); + while (esResponse.getDatas().size() > 0){ + method = "GET"; + response = performRequest(method, entPoint, scrollBody.toString()); + result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); + esResponse = result.toJavaObject(EsResponse.class); + if(ObjectUtil.isNotEmpty(esResponse.getDatas())){ + results.addAll(esResponse.getDatas()); + } + } + // 查询完成后,及时删除scroll,释放资源 + scrollBody.remove("scroll"); + method = "DELETE"; + performRequest(method, entPoint, scrollBody.toString()); + return results; + } + + public static Response bulk(String index,String type,String entity) throws IOException{ + String method = "POST"; + StringBuilder endPoint = new StringBuilder(); + if(ObjectUtil.isNotEmpty(index)){ + endPoint.append(index).append("/"); + } + if(ObjectUtil.isNotEmpty(type)){ + endPoint.append(type).append("/"); + } + endPoint.append("_bulk"); + endPoint.append("?refresh=true"); + logger.debug(entity); + return performRequest(method,endPoint.toString(),entity); + } + + private static Response performRequest(String method, String endpoint, String entity) throws IOException { + //logger.info(method + " " + endpoint); + Request request = new Request(method,endpoint); + if(ObjectUtil.isNotEmpty(entity)){ + request.setEntity(new NStringEntity(entity,"utf-8")); + } + + if(ObjectUtil.isNotEmpty(username) && ObjectUtil.isNotEmpty(password)){ + RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder(); + String token = username.concat(":").concat(password); + String base64encode = Base64.getEncoder().encodeToString(token.getBytes("utf-8")); + builder.addHeader("Authorization", "Basic " + base64encode ); + request.setOptions(builder.build()); + } + + + Response response = client.performRequest(request); + logger.debug(response.toString()); + return response; + } + +} diff --git a/casic-common/src/main/java/com/casic/missiles/weigeng/CommDoorUtil.java b/casic-common/src/main/java/com/casic/missiles/weigeng/CommDoorUtil.java index bed1361..7457109 100644 --- a/casic-common/src/main/java/com/casic/missiles/weigeng/CommDoorUtil.java +++ b/casic-common/src/main/java/com/casic/missiles/weigeng/CommDoorUtil.java @@ -118,15 +118,14 @@ } //重启设备 - public static boolean restart(String monitorId) { + public static boolean restart(String controllerIP, String controllerSN) { try { - String doors = ObjectUtil.isNotEmpty(map.get(monitorId)) ? map.get(monitorId).toString() : ""; - if (ObjectUtil.isNotEmpty(doors)) - if (DoorShortUtil.remoteControl(doors.split(",")[1], Long.valueOf(doors.split(",")[0]), 2, 1)) { + if (ObjectUtil.isAllNotEmpty(controllerIP, controllerSN)) + if (DoorShortUtil.remoteControl(controllerIP, Long.valueOf(controllerSN), 2, 1)) { //重新上电需要间隔20秒 Thread.sleep(Integer.valueOf(map.get("time").toString()) * 1000); //重新关闭开关上电 - return DoorShortUtil.remoteControl(doors.split(",")[1], Long.valueOf(doors.split(",")[0]), 2, 2); + return DoorShortUtil.remoteControl(controllerIP, Long.valueOf(controllerSN), 2, 2); } } catch (Exception e) { e.printStackTrace(); @@ -135,16 +134,14 @@ } //重启设备 - public static boolean interruptStart(String monitorId) { + public static boolean interruptStart(String controllerIP, String controllerSN) { try { - String doors = ObjectUtil.isNotEmpty(map.get(monitorId)) ? map.get(monitorId).toString() : ""; - if (ObjectUtil.isNotEmpty(doors)) - if (DoorShortUtil.remoteControl(doors.split(",")[1], Long.valueOf(doors.split(",")[0]), 2, 1)) { + if (ObjectUtil.isAllNotEmpty(controllerIP,controllerSN)) + if (DoorShortUtil.remoteControl(controllerIP, Long.valueOf(controllerSN), 2, 1)) { //重新上电需要间隔20秒 Thread.sleep(Integer.valueOf(map.get("time").toString()) * 1000); //重新关闭开关上电 - DoorShortUtil.remoteControl(doors.split(",")[1], Long.valueOf(doors.split(",")[0]), 2, 2); - + DoorShortUtil.remoteControl(controllerIP, Long.valueOf(controllerSN), 2, 2); Thread.sleep(2 * 60 * 1000); return true; } diff --git a/casic-data/pom.xml b/casic-data/pom.xml index aca0a62..a076800 100644 --- a/casic-data/pom.xml +++ b/casic-data/pom.xml @@ -43,11 +43,6 @@ com.casic - casic-data-es - 1.0.1.alpha - - - com.casic casic-common 2.0.0 compile diff --git a/casic-data/src/main/java/com/casic/missiles/modular/system/es/DataGasEs.java b/casic-data/src/main/java/com/casic/missiles/modular/system/es/DataGasEs.java deleted file mode 100644 index 821e8f1..0000000 --- a/casic-data/src/main/java/com/casic/missiles/modular/system/es/DataGasEs.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.casic.missiles.modular.system.es; - -import lombok.Data; - -@Data -public class DataGasEs { - - private Long stationId; - private String stationName; - private Long monitorId; - private String monitorName; - private String devcode; - private Double direction; - private Double pitch; - private Double concentration; - private Double threshold; - private String logTime; -} diff --git a/casic-data/src/main/java/com/casic/missiles/modular/system/es/ElasticSearchQuery.java b/casic-data/src/main/java/com/casic/missiles/modular/system/es/ElasticSearchQuery.java deleted file mode 100644 index 4d7a38f..0000000 --- a/casic-data/src/main/java/com/casic/missiles/modular/system/es/ElasticSearchQuery.java +++ /dev/null @@ -1,257 +0,0 @@ -package com.casic.missiles.modular.system.es; - -import cn.hutool.core.date.DateUtil; -import cn.hutool.core.util.ObjectUtil; -import com.alibaba.fastjson.JSONArray; -import com.alibaba.fastjson.JSONObject; - -import java.io.Serializable; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; - -/** - * ES查询请求体 - * - * @author zhangyingjie123 - * @since 2020-11-30 - */ -public class ElasticSearchQuery { - private JSONObject body; - private JSONObject query; - private JSONObject bool; - private JSONArray boolMust; - private JSONObject filter; - private JSONObject filterBool; - private JSONArray filterBoolMust; - private JSONArray filterBoolMustnot; - - public static final String MUST_FLAG = "1"; - public static final String MUST_NOT_FLAG = "0"; - - public static final String QUERY = "query"; - public static final String BOOL = "bool"; - public static final String MUST = "must"; - public static final String MUST_NOT = "must_not"; - public static final String FILTER = "filter"; - - - /** - * 生成查询请求体基础结构: - * { - * "query": { - * "bool": { - * "must" [], - * "filter": { - * "bool": { - * "must": [], - * "must_not": [] - * } - * } - * } - * } - * } - */ - public ElasticSearchQuery() { - this.body = new JSONObject(); - this.query = this.query(); - this.bool = this.bool(); - this.boolMust = this.boolMust(); - this.filter = this.filter(); - this.filterBool = this.filterBool(); - this.filterBoolMust = this.filterBoolMust(); - this.filterBoolMustnot = this.filterBoolMustnot(); - } - - public ElasticSearchQuery(JSONObject body){ - this.body = body; - } - - public JSONObject getBody() { - return body; - } - - private JSONObject query(){ - if(ObjectUtil.isEmpty(this.body.get(QUERY))){ - this.body.put("query",new JSONObject()); - } - return this.body.getJSONObject(QUERY); - - } - - private JSONObject bool() { - JSONObject query = this.query; - if(ObjectUtil.isEmpty(query.get(BOOL))){ - query.put(BOOL,new JSONObject()); - } - return query.getJSONObject(BOOL); - } - - private JSONArray boolMust(){ - JSONObject bool = this.bool; - if(ObjectUtil.isEmpty(bool.get(MUST))){ - bool.put(MUST,new JSONArray()); - } - return bool.getJSONArray(MUST); - } - - private JSONObject filter(){ - JSONObject bool = this.bool; - if(ObjectUtil.isEmpty(bool.get(FILTER))){ - bool.put(FILTER,new JSONObject()); - } - return bool.getJSONObject(FILTER); - } - - private JSONObject filterBool(){ - JSONObject filter = this.filter; - if(ObjectUtil.isEmpty(filter.get(BOOL))){ - filter.put(BOOL,new JSONObject()); - } - return filter.getJSONObject(BOOL); - } - - private JSONArray filterBoolMust(){ - JSONObject bool = this.filterBool; - if(ObjectUtil.isEmpty(bool.get(MUST))){ - bool.put(MUST,new JSONArray()); - } - return bool.getJSONArray(MUST); - } - - private JSONArray filterBoolMustnot(){ - JSONObject bool = this.filterBool; - if(ObjectUtil.isEmpty(bool.get(MUST_NOT))){ - bool.put(MUST_NOT,new JSONArray()); - } - return bool.getJSONArray(MUST_NOT); - } - - public void match(String filed, Serializable value){ - JSONArray boolMust = this.boolMust; - JSONObject match = new JSONObject(); - JSONObject filedJson = new JSONObject(); - filedJson.put(filed,value); - match.put("match",filedJson); - boolMust.add(match); - } - - public void matchPhrase(String filed, Serializable value){ - JSONArray boolMust = this.boolMust; - JSONObject matchPhrase = new JSONObject(); - JSONObject filedJson = new JSONObject(); - filedJson.put(filed,value); - matchPhrase.put("match_phrase",filedJson); - boolMust.add(matchPhrase); - } - - public void term(String filed, Serializable value){ - this.term(filed,value,MUST_FLAG); - } - - public void term(String filed, Serializable value,String mustFlag){ - JSONObject filedJson = new JSONObject(); - filedJson.put(filed,value); - JSONObject term = new JSONObject(); - term.put("term",filedJson); - if(MUST_FLAG.equals(mustFlag)){ - this.filterBoolMust.add(term); - } - if(MUST_NOT_FLAG.equals(mustFlag)){ - this.filterBoolMustnot.add(term); - } - } - - public void terms(String filed,List value){ - this.terms(filed,value,MUST_FLAG); - } - - public void terms(String filed, List value, String mustFlag){ - JSONObject filedJson = new JSONObject(); - filedJson.put(filed,value); - JSONObject terms = new JSONObject(); - terms.put("terms",filedJson); - if(MUST_FLAG.equals(mustFlag)){ - this.filterBoolMust.add(terms); - } - if(MUST_NOT_FLAG.equals(mustFlag)){ - this.filterBoolMustnot.add(terms); - } - } - - public void range(String filed, Serializable from, Serializable to){ - this.range(filed,from,to,MUST_FLAG); - } - - public void range(String filed, Serializable from, Serializable to, String mustFlag){ - JSONObject rangeJson = new JSONObject(); - if(ObjectUtil.isNotEmpty(from)){ - rangeJson.put("from",from); - } - if(ObjectUtil.isNotEmpty(to)){ - rangeJson.put("to",to); - } - JSONObject filedJson = new JSONObject(); - filedJson.put(filed,rangeJson); - JSONObject range = new JSONObject(); - range.put("range",filedJson); - if(MUST_FLAG.equals(mustFlag)){ - this.filterBoolMust.add(range); - } - if(MUST_NOT_FLAG.equals(mustFlag)){ - this.filterBoolMustnot.add(range); - } - } - - public void size(int size){ - this.body.put("size",size); - } - - public void from(int from) { - this.body.put("from",from); - } - - public void sort(String sort, String order){ - JSONObject sortJson = new JSONObject(); - JSONObject orderJson = new JSONObject(); - orderJson.put("order",order); - sortJson.put(sort,orderJson); - this.body.put("sort",sortJson); - } - - public void aggs(String aggsName, String filed){ - JSONObject terms = new JSONObject(); - terms.put("field",filed); - JSONObject aggsTerms = new JSONObject(); - aggsTerms.put("terms",terms); - JSONObject aggsBody = new JSONObject(); - aggsBody.put(aggsName,aggsTerms); - this.body.put("aggs",aggsBody); - } - - public void aggs(JSONObject aggBody){ - this.body.put("aggs",aggBody); - } - - - - public static void main(String[] args) { - ElasticSearchQuery query = new ElasticSearchQuery(); - query.matchPhrase("name","张三"); - query.term("sex","2"); - List deptIds = new ArrayList<>(); - deptIds.add(0L); - deptIds.add(24L); - query.terms("deptId", deptIds); - Date date1 = DateUtil.parse("2020-01-01","yyyy-MM-dd"); - Date date2 = DateUtil.parse("2020-04-01","yyyy-MM-dd"); - query.range("collTime",date1.getTime(),date2.getTime()); - query.term("collState","3",MUST_NOT_FLAG); - query.size(10); - query.from(0); - query.sort("irId","asc"); - System.out.println(query.getBody().toJSONString()); - } - - -} diff --git a/casic-data/src/main/java/com/casic/missiles/modular/system/es/ElasticSearchUtil.java b/casic-data/src/main/java/com/casic/missiles/modular/system/es/ElasticSearchUtil.java deleted file mode 100644 index e9da021..0000000 --- a/casic-data/src/main/java/com/casic/missiles/modular/system/es/ElasticSearchUtil.java +++ /dev/null @@ -1,172 +0,0 @@ -package com.casic.missiles.modular.system.es; - -import cn.hutool.core.util.ObjectUtil; -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONObject; -import com.casic.missiles.core.es.EsResponse; -import org.apache.http.nio.entity.NStringEntity; -import org.apache.http.util.EntityUtils; -import org.elasticsearch.client.Request; -import org.elasticsearch.client.RequestOptions; -import org.elasticsearch.client.Response; -import org.elasticsearch.client.RestClient; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.core.env.Environment; -import org.springframework.stereotype.Component; - -import javax.annotation.PostConstruct; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Base64; -import java.util.List; - -/** - * ES工具类 - * - * @author zhangyingjie123 - * @since 2020-11-30 - */ -@Component -public class ElasticSearchUtil { - private static final Logger logger = LoggerFactory.getLogger(ElasticSearchUtil.class); - - @Autowired - private RestClient clientMapping; - @Autowired - private Environment env; - - private static String username; - private static String password; - - private static RestClient client; - - @PostConstruct - public void init(){ - client = clientMapping; - username = env.getProperty("casic.data.es.username"); - password = env.getProperty("casic.data.es.password"); - } - - public static JSONObject selectDocumentById(String index, String type, String id) throws IOException{ - String method = "GET"; - String endPoint = index.concat("/").concat(type).concat("/").concat(id); - Response response = performRequest(method,endPoint,null); - JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); - return result.getJSONObject("_source"); - } - - public static Response addDocumentById(String index, String type, String id, Object entity) throws IOException{ - String method = "POST"; - String endPoint = index.concat("/").concat(type).concat("/"); - if(ObjectUtil.isNotEmpty(id)){ - endPoint.concat(id); - } - endPoint.concat("?refresh=true"); - logger.debug(entity.toString()); - return performRequest(method,endPoint,JSON.toJSONString(entity)); - } - - public static Response deleteDocumentById(String index, String type, String id) throws IOException{ - String method = "DELETE"; - String endPoint = index.concat("/").concat(type).concat("/").concat(id).concat("?refresh=true"); - return performRequest(method,endPoint,null); - } - - public static Response updateDocumentById(String index, String type, String id, Object entity) throws IOException{ - String method = "POST"; - String endPoint = index.concat("/").concat(type).concat("/").concat(id).concat("?refresh=true"); - logger.debug(entity.toString()); - return performRequest(method,endPoint,JSON.toJSONString(entity)); - } - - public static EsResponse searchQuery(String index, String type, ElasticSearchQuery query) throws IOException{ - String method = "POST"; - String entPoint = index.concat("/").concat(type).concat("/").concat("_search"); - logger.debug(query.getBody().toString()); - Response response = performRequest(method, entPoint, query.getBody().toString()); - // 获取response body,转换为json对象 - JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); - return result.toJavaObject(EsResponse.class); - } - - public static Response aggsQuery(String index, String type, ElasticSearchQuery query) throws IOException{ - String method = "POST"; - String entPoint = index.concat("/").concat(type).concat("/").concat("_search"); - logger.debug(query.getBody().toString()); - Response response = performRequest(method, entPoint, query.getBody().toString()); - return response; - } - - public static List searchQueryScroll(String index, String type, ElasticSearchQuery query) throws IOException{ - // 首次查询,提交查询条件,endpoint增加‘?scroll=1m’ - List results = new ArrayList<>(); - String method = "POST"; - String entPoint = index.concat("/").concat(type).concat("/").concat("_search").concat("?scroll=1m"); - logger.debug(query.getBody().toString()); - Response response = performRequest(method, entPoint, query.getBody().toString()); - JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); - EsResponse esResponse = result.toJavaObject(EsResponse.class); - String scrollId = result.getString("_scroll_id"); - if(ObjectUtil.isNotEmpty(esResponse.getDatas())){ - results.addAll(esResponse.getDatas()); - } - // 循环发送scroll请求,直到返回结果为空 - entPoint = "_search/scroll"; - JSONObject scrollBody = new JSONObject(); - scrollBody.put("scroll","1m"); - scrollBody.put("scroll_id",scrollId); - while (esResponse.getDatas().size() > 0){ - method = "GET"; - response = performRequest(method, entPoint, scrollBody.toString()); - result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); - esResponse = result.toJavaObject(EsResponse.class); - if(ObjectUtil.isNotEmpty(esResponse.getDatas())){ - results.addAll(esResponse.getDatas()); - } - } - // 查询完成后,及时删除scroll,释放资源 - scrollBody.remove("scroll"); - method = "DELETE"; - performRequest(method, entPoint, scrollBody.toString()); - return results; - } - - public static Response bulk(String index,String type,String entity) throws IOException{ - String method = "POST"; - StringBuilder endPoint = new StringBuilder(); - if(ObjectUtil.isNotEmpty(index)){ - endPoint.append(index).append("/"); - } - if(ObjectUtil.isNotEmpty(type)){ - endPoint.append(type).append("/"); - } - endPoint.append("_bulk"); - endPoint.append("?refresh=true"); - logger.debug(entity); - return performRequest(method,endPoint.toString(),entity); - } - - private static Response performRequest(String method, String endpoint, String entity) throws IOException { - //logger.info(method + " " + endpoint); - Request request = new Request(method,endpoint); - if(ObjectUtil.isNotEmpty(entity)){ - request.setEntity(new NStringEntity(entity,"utf-8")); - } - - if(ObjectUtil.isNotEmpty(username) && ObjectUtil.isNotEmpty(password)){ - RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder(); - String token = username.concat(":").concat(password); - String base64encode = Base64.getEncoder().encodeToString(token.getBytes("utf-8")); - builder.addHeader("Authorization", "Basic " + base64encode ); - request.setOptions(builder.build()); - } - - - Response response = client.performRequest(request); - logger.debug(response.toString()); - return response; - } - -} diff --git a/casic-data/src/main/java/com/casic/missiles/modular/system/service/IDataGasService.java b/casic-data/src/main/java/com/casic/missiles/modular/system/service/IDataGasService.java index e644294..b2aa6cb 100644 --- a/casic-data/src/main/java/com/casic/missiles/modular/system/service/IDataGasService.java +++ b/casic-data/src/main/java/com/casic/missiles/modular/system/service/IDataGasService.java @@ -1,6 +1,6 @@ package com.casic.missiles.modular.system.service; -import com.casic.missiles.modular.system.es.DataGasEs; +import com.casic.missiles.es.DataGasEs; import com.casic.missiles.modular.system.model.DataGas; import com.baomidou.mybatisplus.extension.service.IService; diff --git a/casic-data/src/main/java/com/casic/missiles/modular/system/service/impl/DataGasServiceImpl.java b/casic-data/src/main/java/com/casic/missiles/modular/system/service/impl/DataGasServiceImpl.java index 1aa01e5..ba3a38d 100644 --- a/casic-data/src/main/java/com/casic/missiles/modular/system/service/impl/DataGasServiceImpl.java +++ b/casic-data/src/main/java/com/casic/missiles/modular/system/service/impl/DataGasServiceImpl.java @@ -1,12 +1,12 @@ package com.casic.missiles.modular.system.service.impl; -import com.casic.missiles.modular.system.consts.DataConst; -import com.casic.missiles.modular.system.es.DataGasEs; -import com.casic.missiles.modular.system.es.ElasticSearchUtil; -import com.casic.missiles.modular.system.model.DataGas; -import com.casic.missiles.modular.system.dao.DataGasMapper; -import com.casic.missiles.modular.system.service.IDataGasService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.casic.missiles.es.DataGasEs; +import com.casic.missiles.es.ElasticSearchUtil; +import com.casic.missiles.modular.system.consts.DataConst; +import com.casic.missiles.modular.system.dao.DataGasMapper; +import com.casic.missiles.modular.system.model.DataGas; +import com.casic.missiles.modular.system.service.IDataGasService; import org.springframework.stereotype.Service; import java.io.IOException; diff --git a/casic-server/src/main/java/com/casic/missiles/modular/system/controller/HCNetController.java b/casic-server/src/main/java/com/casic/missiles/modular/system/controller/HCNetController.java index 941768a..f334286 100644 --- a/casic-server/src/main/java/com/casic/missiles/modular/system/controller/HCNetController.java +++ b/casic-server/src/main/java/com/casic/missiles/modular/system/controller/HCNetController.java @@ -4,6 +4,7 @@ import com.casic.missiles.model.response.ResponseData; import com.casic.missiles.modular.system.dto.DeviceCommom; import com.casic.missiles.modular.system.dto.DeviceInfo; +import com.casic.missiles.modular.system.dto.DeviceTypeEnum; import com.casic.missiles.modular.system.dto.SelectDTO; import com.casic.missiles.modular.system.model.BusMonipoiInfo; import com.casic.missiles.modular.system.service.IBusMonipoiInfoService; @@ -146,8 +147,6 @@ @ResponseBody public Object control(String deviceIp, String command, Integer isStop) { -// DoorShortUtil.remoteControl("192.168.1.103",153203040,1,isStop); - Long userId = getUserIdByIp(deviceIp); if (ObjectUtil.isEmpty(userId) || userId < 0) { return ResponseData.error("设备注册失败"); @@ -278,7 +277,7 @@ } BusMonipoiInfo monipoiInfo = monipoiInfoService.selectByDeviceIp(deviceIp); boolean res; - if (ObjectUtil.isNotEmpty(monipoiInfo) && "1".equals(monipoiInfo.getDeviceType())) { + if (ObjectUtil.isNotEmpty(monipoiInfo) && DeviceTypeEnum.SELF_TYPE.getCode().equals(monipoiInfo.getDeviceType())) { res = ihcNetService.setPosition(userId, Double.valueOf(horizontalAngle), Double.valueOf(verticalAngle)); } else { res = ihcNetService.setPositionNew(userId, Double.valueOf(horizontalAngle), Double.valueOf(verticalAngle)); diff --git a/casic-server/src/main/java/com/casic/missiles/modular/system/dto/DeviceTypeEnum.java b/casic-server/src/main/java/com/casic/missiles/modular/system/dto/DeviceTypeEnum.java index 9357adb..1c43528 100644 --- a/casic-server/src/main/java/com/casic/missiles/modular/system/dto/DeviceTypeEnum.java +++ b/casic-server/src/main/java/com/casic/missiles/modular/system/dto/DeviceTypeEnum.java @@ -6,8 +6,8 @@ * @Date: 2022/11/18 14:40 */ public enum DeviceTypeEnum { - SELF_TYPE("1", "天讯通"), ARD_TYPE("0", "奥瑞德"), + SELF_TYPE("1", "天讯通"), XST_TYPE("2", "宇视通"); private String code; diff --git a/casic-server/src/main/java/com/casic/missiles/modular/system/service/impl/HCNetServiceImpl.java b/casic-server/src/main/java/com/casic/missiles/modular/system/service/impl/HCNetServiceImpl.java index c2add20..30a88ca 100644 --- a/casic-server/src/main/java/com/casic/missiles/modular/system/service/impl/HCNetServiceImpl.java +++ b/casic-server/src/main/java/com/casic/missiles/modular/system/service/impl/HCNetServiceImpl.java @@ -4,8 +4,8 @@ import cn.hutool.core.util.ObjectUtil; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.casic.missiles.es.DataGasEs; import com.casic.missiles.modular.system.dto.*; -import com.casic.missiles.modular.system.es.DataGasEs; import com.casic.missiles.modular.system.model.*; import com.casic.missiles.modular.system.service.*; import com.casic.missiles.modular.system.task.AutoCruiseResertTask; @@ -484,7 +484,7 @@ BusMonipoiInfo monipoiInfo = monipoiInfoService.selectByDeviceIp(deviceIp); if (ObjectUtil.isNotEmpty(monipoiInfo)) { - return CommDoorUtil.interruptStart(monipoiInfo.getId() + ""); + return CommDoorUtil.interruptStart(monipoiInfo.getDoorIp(),monipoiInfo.getDoorSn()); } return false; } @@ -1774,12 +1774,11 @@ } public boolean restart(Integer userId) { - //toDo:用门禁控制器2通道控制断电重启 DeviceInfo deviceInfo = DeviceCommom.selectByUserId(Long.valueOf(userId)); if (ObjectUtil.isNotEmpty(deviceInfo)) { BusMonipoiInfo monipoiInfo = monipoiInfoService.selectByDeviceIp(deviceInfo.getDeviceIp()); if (ObjectUtil.isNotEmpty(monipoiInfo)) { - return CommDoorUtil.restart(monipoiInfo.getId() + ""); + return CommDoorUtil.restart(monipoiInfo.getDoorIp(),monipoiInfo.getDoorSn()); } } return false;