@ -1,41 +1,49 @@
package cn.iocoder.yudao.module.alert.utils ;
import cn.iocoder.yudao.framework.common.pojo.PageResult ;
import cn.iocoder.yudao.module.alert.controller.admin.exa.vo.EXAHttp ;
import cn.iocoder.yudao.module.alert.controller.admin.exa.vo.EXAPageReqVO ;
import cn.iocoder.yudao.module.alert.controller.admin.exa.vo.* ;
import com.alibaba.fastjson.JSON ;
import com.alibaba.fastjson.JSONArray ;
import com.google.gson.Gson ;
import com.google.gson.GsonBuilder ;
import com.google.gson.reflect.TypeToken ;
import org.apache.http.HttpEntity ;
import org.apache.http.client.fluent.Request ;
import org.apache.http.client.methods.CloseableHttpResponse ;
import org.apache.http.client.methods.HttpDelete ;
import org.apache.http.client.methods.HttpGet ;
import org.apache.http.client.methods.HttpPost ;
import org.apache.http.client.utils.URIBuilder ;
import org.apache.http.entity.ContentType ;
import org.apache.http.entity.StringEntity ;
import org.apache.http.impl.client.CloseableHttpClient ;
import org.apache.http.impl.client.HttpClients ;
import org.apache.http.util.EntityUtils ;
import org.springframework.beans.factory.annotation.Value ;
import java.io.IOException ;
import java.lang.reflect.Type ;
import java.net.URISyntaxException ;
import java.nio.charset.Charset ;
import java.util.ArrayList ;
import java.util.Arrays ;
import java.util.List ;
import static cn.iocoder.yudao.module.alert.utils.PageUtils.getPartList ;
public class EXAUtils {
/ * *
* 根据分页条件和测点名称测点描述模糊查询
*
* @param reqVO 传入对象 , 页数 、 页数量 、 查询条件
*
* @return exa列表
* /
public List < EXAHttp > getPointInfo ( EXAPageReqVO reqVO ) throws URISyntaxException , IOException {
public List < EXAHttp > getPointInfo ( String EXA_IP , EXAPageReqVO reqVO ) throws URISyntaxException , IOException {
// PageResult<EXAHttp> result = new PageResult<>();
List < EXAHttp > exaListAll = new ArrayList < EXAHttp > ( ) ;
try {
// 目标 RPC 服务的 URL
String url = "http://47.98.32.148 :9000/exawebapi/exaitem/getitems" ;
String url = "http://" + EXA_IP + " :9000/exawebapi/exaitem/getitems" ;
//创建HttpClient对象
CloseableHttpClient httpClient = HttpClients . createDefault ( ) ;
/ *
@ -56,8 +64,7 @@ public class EXAUtils {
int statusCode = response . getStatusLine ( ) . getStatusCode ( ) ;
System . out . println ( "服务端返回成功的状态码为:" + statusCode ) ;
HttpEntity entity = response . getEntity ( ) ;
String body = EntityUtils . toString ( entity ) . replaceAll ( "\\\\" , "" ) ;
System . out . println ( "服务端返回的数据为:" + body ) ;
String body = EntityUtils . toString ( entity ) . replaceAll ( "\\\\\\\\\\\\\"" , "" ) . replaceAll ( "\\\\" , "" ) ;
// 步骤1:检查字符串是否为空
// if (body == null || body.isEmpty()) {
// return body; // 如果字符串为空或为null,直接返回
@ -68,10 +75,17 @@ public class EXAUtils {
body = body . substring ( 1 , body . length ( ) - 1 ) ; // 去掉首尾的双引号
}
Gson gson = new GsonBuilder ( ) . serializeNulls ( ) . setPrettyPrinting ( ) . create ( ) ;
Type listType = new TypeToken < List < EXAHttp > > ( ) {
} . getType ( ) ;
exaListAll = gson . fromJson ( body , listType ) ;
try {
exaListAll = JSONArray . parseArray ( body , EXAHttp . class ) ;
} catch ( Exception e ) {
System . out . println ( e ) ;
}
// Gson gson = new GsonBuilder().serializeNulls().setPrettyPrinting().create();
// Type listType = new TypeToken<List<EXAHttp>>() {
// }.getType();
// exaListAll = gson.fromJson(body, listType);
//关闭资源
@ -83,11 +97,18 @@ public class EXAUtils {
return exaListAll ;
}
public List < String > getNowData ( String itemNames ) throws URISyntaxException , IOException {
/ * *
* 根据多个点号查询实时值
*
* @param itemNames 多个点号 , 用逗号隔开 传入对象 , 页数 、 页数量 、 查询条件
* @return exa列表
* /
public List < String > getNowData ( String EXA_IP , String itemNames ) throws URISyntaxException , IOException {
List < String > result = new ArrayList < > ( ) ;
try {
// 目标 RPC 服务的 URL
String url = "http://47.98.32.148:9000/exawebapi/exanow/getfloatvaluebatch" ;
String url = "http://" + EXA_IP + " :9000/exawebapi/exanow/getfloatvaluebatch" ;
//创建HttpClient对象
CloseableHttpClient httpClient = HttpClients . createDefault ( ) ;
/ *
@ -127,5 +148,160 @@ public class EXAUtils {
return result ;
}
/ * *
* 根据分页条件和测点名称测点描述模糊查询
*
* @param exaHistoryReqVo 传入对象 , 点号 、 开始时间 、 结束时间
* @return exa列表
* /
public List < String > getHistory ( String EXA_IP , EXAHistoryReqVO exaHistoryReqVo ) {
List < String > result = new ArrayList < > ( ) ;
try {
// 目标 RPC 服务的 URL
String url = "http://" + EXA_IP + ":9000/exawebapi/exatime/GetRawValueArrayFloat" ;
//创建HttpClient对象
CloseableHttpClient httpClient = HttpClients . createDefault ( ) ;
/ *
* 由于GET请求的参数都是拼装在URL地址后方 , 所以我们要构建一个URL , 带参数
* /
URIBuilder uriBuilder = new URIBuilder ( url ) ;
/** 添加参数 */
uriBuilder . addParameter ( "ItemName" , exaHistoryReqVo . getItemName ( ) ) ;
uriBuilder . addParameter ( "StartingTime" , exaHistoryReqVo . getStartTime ( ) ) ;
uriBuilder . addParameter ( "TerminalTime" , exaHistoryReqVo . getEndTime ( ) ) ;
//创建请求对象
HttpGet httpGet = new HttpGet ( uriBuilder . build ( ) ) ;
// 传输的类型
httpGet . addHeader ( "Content-Type" , "application/json" ) ;
//发送请求,请求响应结果
CloseableHttpResponse response = httpClient . execute ( httpGet ) ;
//获取服务器返回的状态码
int statusCode = response . getStatusLine ( ) . getStatusCode ( ) ;
System . out . println ( "服务端返回成功的状态码为:" + statusCode ) ;
HttpEntity entity = response . getEntity ( ) ;
String body = EntityUtils . toString ( entity ) . replaceAll ( "\\\\" , "" ) ;
System . out . println ( "服务端返回的数据为:" + body ) ;
if ( body . startsWith ( "\"" ) & & body . endsWith ( "\"" ) ) {
// 步骤3:去掉双引号
body = body . substring ( 1 , body . length ( ) - 1 ) ; // 去掉首尾的双引号
}
Gson gson = new GsonBuilder ( ) . serializeNulls ( ) . setPrettyPrinting ( ) . create ( ) ;
Type listType = new TypeToken < List < List < Double > > > ( ) {
} . getType ( ) ;
result = gson . fromJson ( body , listType ) ;
//关闭资源
response . close ( ) ;
httpClient . close ( ) ;
} catch ( Exception e ) {
}
return result ;
}
public List < List < String > > getHistorys ( String EXA_IP , EXAHistoryReqVO exaHistoryReqVo ) {
List < List < String > > results = new ArrayList < > ( ) ;
try {
String itemNames = exaHistoryReqVo . getItemName ( ) ;
List < String > pointName = Arrays . asList ( itemNames . split ( "," ) ) ;
pointName . forEach ( entity - > {
List < String > result = new ArrayList < > ( ) ;
exaHistoryReqVo . setItemName ( entity ) ;
result = getHistory ( EXA_IP , exaHistoryReqVo ) ;
results . add ( result ) ;
} ) ;
} catch ( Exception e ) {
}
return results ;
}
//写入点号
public String setPoint ( String EXA_IP , Point point ) throws IOException {
String requestPathAddItem = "http://" + EXA_IP + ":9000/exawebapi/exaitem/AddItem" ;
String param = JSON . toJSONString ( point ) ;
String result = Request . Post ( requestPathAddItem )
. addHeader ( "Content-type" , "application/json" )
. bodyString ( param , ContentType . APPLICATION_JSON )
. execute ( ) . returnContent ( ) . asString ( Charset . forName ( "utf-8" ) ) ;
// 反序列化--之前的版本是这个样子,新版本没有了
// EXAResult vo = JSON.parseObject(result, EXAResult.class);
return result ;
}
public List < Object > getGroup ( String EXA_IP ) throws URISyntaxException , IOException {
List < Object > result = new ArrayList < > ( ) ;
try {
// 目标 RPC 服务的 URL
String url = "http://" + EXA_IP + ":9000/exawebapi/exagroup/getgroups" ;
//创建HttpClient对象
CloseableHttpClient httpClient = HttpClients . createDefault ( ) ;
/ *
* 由于GET请求的参数都是拼装在URL地址后方 , 所以我们要构建一个URL , 带参数
* /
URIBuilder uriBuilder = new URIBuilder ( url ) ;
/** 添加参数 */
// uriBuilder.addParameter("ItemNames", itemNames);
//创建请求对象
HttpGet httpGet = new HttpGet ( uriBuilder . build ( ) ) ;
// 传输的类型
httpGet . addHeader ( "Content-Type" , "application/json" ) ;
//发送请求,请求响应结果
CloseableHttpResponse response = httpClient . execute ( httpGet ) ;
//获取服务器返回的状态码
int statusCode = response . getStatusLine ( ) . getStatusCode ( ) ;
System . out . println ( "服务端返回成功的状态码为:" + statusCode ) ;
HttpEntity entity = response . getEntity ( ) ;
String body = EntityUtils . toString ( entity ) . replaceAll ( "\\\\" , "" ) ;
System . out . println ( "服务端返回的数据为:" + body ) ;
if ( body . startsWith ( "\"" ) & & body . endsWith ( "\"" ) ) {
// 步骤3:去掉双引号
body = body . substring ( 1 , body . length ( ) - 1 ) ; // 去掉首尾的双引号
}
Gson gson = new GsonBuilder ( ) . serializeNulls ( ) . setPrettyPrinting ( ) . create ( ) ;
Type listType = new TypeToken < List < Object > > ( ) {
} . getType ( ) ;
result = gson . fromJson ( body , listType ) ;
//关闭资源
response . close ( ) ;
httpClient . close ( ) ;
} catch ( Exception e ) {
}
return result ;
}
public String deletePoint ( String EXA_IP , String ItemName ) {
String requestPathDeleteItem = "http://" + EXA_IP + ":9000/exawebapi/exaitem/DeleteItem?ItemName=" + ItemName ;
// 创建HttpClient
try ( CloseableHttpClient httpClient = HttpClients . createDefault ( ) ) {
// 创建HttpDelete请求
HttpDelete request = new HttpDelete ( requestPathDeleteItem ) ;
// 执行请求
try ( CloseableHttpResponse response = httpClient . execute ( request ) ) {
// 获取HTTP响应状态
System . out . println ( "Response Code: " + response . getStatusLine ( ) . getStatusCode ( ) ) ;
// 获取HTTP响应内容
String content = EntityUtils . toString ( response . getEntity ( ) ) ;
System . out . println ( "Response Content: \n" + content ) ;
return content ;
}
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
return null ;
}
}