代码优化

1.推送消息屏蔽,只推送治理设备信息;
2.文件下载优化
3.公共方法提取
This commit is contained in:
xy
2024-11-01 11:22:38 +08:00
parent 749a814bfd
commit e2f46ebcde
13 changed files with 272 additions and 279 deletions

View File

@@ -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;
}
}

View File

@@ -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();
}
}
}

View File

@@ -339,7 +339,7 @@ public class MqttMessageHandler {
//更新电网侧、负载侧监测点信息
askDevData(nDid,version,3,(res.getMid()+1));
//录波任务倒计时
redisUtil.saveByKeyWithExpire("startFile",null,120L);
redisUtil.saveByKeyWithExpire("startFile:" + nDid,null,60L);
} else {
log.info(AccessResponseEnum.ACCESS_RESPONSE_ERROR.getMessage());
logDto.setResult(0);

View File

@@ -1,13 +1,14 @@
package com.njcn.access.listener;
import com.alibaba.nacos.shaded.com.google.gson.Gson;
import com.njcn.access.enums.AccessEnum;
import com.njcn.access.pojo.dto.NoticeUserDto;
import com.njcn.access.pojo.po.CsDeviceOnlineLogs;
import com.njcn.access.service.ICsDeviceOnlineLogsService;
import com.njcn.access.service.ICsEquipmentDeliveryService;
import com.njcn.access.service.ICsTopicService;
import com.njcn.access.service.impl.CsDeviceServiceImpl;
import com.njcn.access.utils.MqttUtil;
import com.njcn.access.utils.SendMessageUtil;
import com.njcn.common.pojo.dto.DeviceLogDTO;
import com.njcn.csdevice.api.CsDeviceUserFeignClient;
import com.njcn.csdevice.api.CsLedgerFeignClient;
@@ -20,7 +21,6 @@ import com.njcn.redis.utils.RedisUtil;
import com.njcn.user.api.AppUserFeignClient;
import com.njcn.user.api.UserFeignClient;
import com.njcn.user.pojo.po.User;
import com.njcn.zlevent.pojo.dto.NoticeUserDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.connection.Message;
@@ -29,13 +29,6 @@ import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
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;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
@@ -79,6 +72,8 @@ public class RedisKeyExpirationListener extends KeyExpirationEventMessageListene
private UserFeignClient userFeignClient;
@Resource
private RedisUtil redisUtil;
@Resource
private SendMessageUtil sendMessageUtil;
private final Object lock = new Object();
public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
@@ -153,7 +148,7 @@ public class RedisKeyExpirationListener extends KeyExpirationEventMessageListene
private void startScheduledTask(ScheduledExecutorService scheduler, String nDid, String version) {
synchronized (lock) {
NoticeUserDto dto = sendOffLine(nDid);
sendEventToUser(dto);
sendMessageUtil.sendEventToUser(dto);
addLogs(dto);
String clientName = "NJCN-" + nDid.substring(nDid.length() - 6);
ScheduledFuture<?> future = scheduler.scheduleAtFixedRate(() -> {
@@ -178,7 +173,7 @@ public class RedisKeyExpirationListener extends KeyExpirationEventMessageListene
MAX_WARNING_TIMES++;
if (MAX_WARNING_TIMES == 30) {
NoticeUserDto dto2 = sendConnectFail(nDid);
sendEventToUser(dto2);
sendMessageUtil.sendEventToUser(dto2);
addLogs(dto2);
}
//记录装置掉线时间
@@ -194,7 +189,7 @@ public class RedisKeyExpirationListener extends KeyExpirationEventMessageListene
MAX_WARNING_TIMES++;
if (MAX_WARNING_TIMES == 30) {
NoticeUserDto dto2 = sendConnectFail(nDid);
sendEventToUser(dto2);
sendMessageUtil.sendEventToUser(dto2);
addLogs(dto2);
}
logDto.setResult(0);
@@ -260,42 +255,4 @@ public class RedisKeyExpirationListener extends KeyExpirationEventMessageListene
List<User> users = userFeignClient.appuserByIdList(adminList).getData();
return users.stream().map(User::getDevCode).filter(Objects::nonNull).filter(StringUtils::isNotBlank).distinct().collect(Collectors.toList());
}
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();
}
}
}

View File

@@ -34,6 +34,7 @@ import com.njcn.system.enums.DicDataEnum;
import com.njcn.system.pojo.po.SysDicTreePO;
import com.njcn.web.utils.RequestUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
@@ -52,6 +53,7 @@ import java.util.stream.Collectors;
*/
@Service
@AllArgsConstructor
@Slf4j
public class CsDeviceServiceImpl implements ICsDeviceService {
private static final Logger logger = LoggerFactory.getLogger(CsDeviceServiceImpl.class);
@@ -575,7 +577,7 @@ public class CsDeviceServiceImpl implements ICsDeviceService {
//发起接入
publisher.send("/Pfm/DevCmd/"+version+"/"+nDid, new Gson().toJson(getJson(mid,TypeEnum.TYPE_5.getCode())),1,false);
//录波任务倒计时
redisUtil.saveByKeyWithExpire("startFile",null,120L);
redisUtil.saveByKeyWithExpire("startFile:" + nDid,null,60L);
result = true;
}
} catch (InterruptedException e) {