代码优化
1.推送消息屏蔽,只推送治理设备信息; 2.文件下载优化 3.公共方法提取
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
package com.njcn.access.pojo.dto;
|
||||
|
||||
import com.njcn.access.annotation.ParamName;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 类的介绍:用来组装通知用户事件的实体
|
||||
*
|
||||
* @author xuyang
|
||||
* @version 1.0.0
|
||||
* @createTime 2023/9/14 14:48
|
||||
*/
|
||||
@Data
|
||||
public class NoticeUserDto implements Serializable {
|
||||
|
||||
@ParamName("push_clientid")
|
||||
private List<String> pushClientId;
|
||||
|
||||
@ParamName("title")
|
||||
private String title;
|
||||
|
||||
@ParamName("content")
|
||||
private String content;
|
||||
|
||||
@ParamName("payload")
|
||||
private Payload payload;
|
||||
|
||||
@Data
|
||||
public static class Payload implements Serializable {
|
||||
|
||||
@ApiModelProperty("事件类型 0:暂态事件 1:稳态事件 2:设备事件 3:设备告警")
|
||||
@ParamName("type")
|
||||
private Integer type;
|
||||
@ApiModelProperty("设备告警 /pages/message/report\n" +
|
||||
"运行时间 /pages/message/run\n" +
|
||||
"暂态事件 /pages/message/transient\n" +
|
||||
"稳态越限 /pages/message/steady")
|
||||
@ParamName("path")
|
||||
private String path;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.njcn.access.utils;
|
||||
|
||||
import com.alibaba.nacos.shaded.com.google.gson.Gson;
|
||||
import com.njcn.access.pojo.dto.NoticeUserDto;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
* 推送消息
|
||||
* @author xy
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
@AllArgsConstructor
|
||||
public class SendMessageUtil {
|
||||
|
||||
public void sendEventToUser(NoticeUserDto noticeUserDto) {
|
||||
try {
|
||||
// 创建一个URL对象,指定目标HTTPS接口地址
|
||||
URL url = new URL("https://fc-mp-ff7b310f-94c9-4468-8260-109111c0a6b2.next.bspapp.com/push");
|
||||
// 打开HTTPS连接
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
// 设置请求方法为POST
|
||||
connection.setRequestMethod("POST");
|
||||
// 设置请求头,指定Content-Type为application/json
|
||||
connection.setRequestProperty("Content-Type", "application/json");
|
||||
// 启用输出流以发送JSON数据
|
||||
connection.setDoOutput(true);
|
||||
// 将JSON数据写入输出流
|
||||
OutputStream outputStream = connection.getOutputStream();
|
||||
log.info(new Gson().toJson(noticeUserDto).replace("pushClientId", "push_clientid"));
|
||||
outputStream.write(new Gson().toJson(noticeUserDto).replace("pushClientId", "push_clientid").getBytes(StandardCharsets.UTF_8));
|
||||
outputStream.flush();
|
||||
outputStream.close();
|
||||
// 获取响应代码
|
||||
int responseCode = connection.getResponseCode();
|
||||
log.info("Response Code: " + responseCode);
|
||||
// 读取响应数据
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||
String inputLine;
|
||||
StringBuilder response = new StringBuilder();
|
||||
while ((inputLine = reader.readLine()) != null) {
|
||||
response.append(inputLine);
|
||||
}
|
||||
reader.close();
|
||||
// 打印响应内容
|
||||
log.info("Response Content: " + response.toString());
|
||||
// 关闭连接
|
||||
connection.disconnect();
|
||||
} catch (IOException e) {
|
||||
e.getMessage();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user