事件推送消息

This commit is contained in:
2023-09-14 16:10:17 +08:00
parent b4a7a31c9a
commit 51770e12d4
3 changed files with 117 additions and 9 deletions

View File

@@ -33,6 +33,11 @@
<artifactId>common-mq</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.njcn</groupId>
<artifactId>access-api</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<artifactId>zl-event-api</artifactId>

View File

@@ -0,0 +1,40 @@
package com.njcn.zlevent.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;
}
}

View File

@@ -22,7 +22,10 @@ import com.njcn.system.enums.DicDataEnum;
import com.njcn.system.pojo.dto.EpdDTO;
import com.njcn.system.pojo.po.DictData;
import com.njcn.user.api.AppInfoSetFeignClient;
import com.njcn.user.api.UserFeignClient;
import com.njcn.user.pojo.po.User;
import com.njcn.user.pojo.po.app.AppInfoSet;
import com.njcn.zlevent.pojo.dto.NoticeUserDto;
import com.njcn.zlevent.service.ICsEventService;
import com.njcn.zlevent.service.ICsEventUserService;
import com.njcn.zlevent.service.IEventService;
@@ -35,6 +38,13 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
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.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@@ -74,6 +84,8 @@ public class EventServiceImpl implements IEventService {
private final AppInfoSetFeignClient appInfoSetFeignClient;
private final UserFeignClient userFeignClient;
@Override
@Transactional(rollbackFor = Exception.class)
public void analysis(AppEventMessage appEventMessage) {
@@ -83,6 +95,7 @@ public class EventServiceImpl implements IEventService {
List<CsEventUserPO> list2 = new ArrayList<>();
//获取监测点id
String lineId = null;
LocalDateTime eventTime = null;
Object object1 = redisUtil.getObjectByKey(AppRedisKey.LINE_POSITION+appEventMessage.getId());
//判断字典数据是否存在
if (Objects.isNull(redisUtil.getObjectByKey(AppRedisKey.ELE_EPD_PQD))){
@@ -107,6 +120,7 @@ public class EventServiceImpl implements IEventService {
csEvent.setId(id);
csEvent.setLineId(lineId);
csEvent.setDeviceId(deviceId);
eventTime = timeFormat(item.getDataTimeSec(),item.getDataTimeUSec());
csEvent.setStartTime(timeFormat(item.getDataTimeSec(),item.getDataTimeUSec()));
csEvent.setTag(item.getName());
csEvent.setType(0);
@@ -144,12 +158,9 @@ public class EventServiceImpl implements IEventService {
if (CollectionUtil.isNotEmpty(records)) {
influxDbUtils.batchInsert(influxDbUtils.getDbName(), "", InfluxDB.ConsistencyLevel.ALL, TimeUnit.MILLISECONDS, records);
}
//todo 通知用户有新的事件发生
List<String> eventUser = getEventUser(deviceId);
//todo 根据不同事件需要做处理,目前先测试消息通知
String content = appEventMessage.getId() + "" +eventTime+ "发生暂态事件";
sendEventToUser(getEventUser(deviceId),"暂态事件",content,1);
}
/**
@@ -231,6 +242,7 @@ public class EventServiceImpl implements IEventService {
*/
public List<String> getEventUser(String devId) {
List<String> result = new ArrayList<>();
List<String> devCode = new ArrayList<>();
//获取设备下主用户和子用户集合
List<String> list = csDeviceUserFeignClient.findUserById(devId).getData();
//查询哪些用户打开了事件提示
@@ -240,12 +252,63 @@ public class EventServiceImpl implements IEventService {
.filter(person -> person.getEventInfo() == 1)
.map(AppInfoSet::getUserId).collect(Collectors.toList());
}
return result;
//获取用户的devCode
if (CollectionUtil.isNotEmpty(list)){
List<User> users = userFeignClient.getUserByIdList(result).getData();
devCode = users.stream().map(User::getDevCode).collect(Collectors.toList());
}
return devCode;
}
/**
* 发送通知消息
*/
public void sendEventToUser(List<String> devCodeList, String title, String content, Integer type) {
try {
if (CollectionUtil.isNotEmpty(devCodeList)){
// 创建一个URL对象指定目标HTTPS接口地址
URL url = new URL("https://fc-mp-b46c4dff-7244-4f7c-ae8b-7c1194d8cce8.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
NoticeUserDto noticeUserDto = new NoticeUserDto();
noticeUserDto.setPushClientId(devCodeList);
noticeUserDto.setTitle(title);
noticeUserDto.setContent(content);
NoticeUserDto.Payload payload = new NoticeUserDto.Payload();
payload.setType(type);
noticeUserDto.setPayload(payload);
// 将JSON数据写入输出流
OutputStream outputStream = connection.getOutputStream();
System.out.println("json==:" + new Gson().toJson(noticeUserDto));
outputStream.write(new Gson().toJson(noticeUserDto).getBytes(StandardCharsets.UTF_8));
outputStream.flush();
outputStream.close();
// 获取响应代码
int responseCode = connection.getResponseCode();
System.out.println("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();
// 打印响应内容
System.out.println("Response Content: ");
System.out.println(response.toString());
// 关闭连接
connection.disconnect();
}
} catch (IOException e) {
e.getMessage();
}
}
}