事件推送消息
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user