diff --git a/iot-analysis/analysis-zl-event/zl-event-api/pom.xml b/iot-analysis/analysis-zl-event/zl-event-api/pom.xml index 12e23d4..f82da98 100644 --- a/iot-analysis/analysis-zl-event/zl-event-api/pom.xml +++ b/iot-analysis/analysis-zl-event/zl-event-api/pom.xml @@ -33,6 +33,11 @@ common-mq ${project.version} + + com.njcn + access-api + ${project.version} + zl-event-api diff --git a/iot-analysis/analysis-zl-event/zl-event-api/src/main/java/com/njcn/zlevent/pojo/dto/NoticeUserDto.java b/iot-analysis/analysis-zl-event/zl-event-api/src/main/java/com/njcn/zlevent/pojo/dto/NoticeUserDto.java new file mode 100644 index 0000000..4d8faba --- /dev/null +++ b/iot-analysis/analysis-zl-event/zl-event-api/src/main/java/com/njcn/zlevent/pojo/dto/NoticeUserDto.java @@ -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 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; + } + +} diff --git a/iot-analysis/analysis-zl-event/zl-event-boot/src/main/java/com/njcn/zlevent/service/impl/EventServiceImpl.java b/iot-analysis/analysis-zl-event/zl-event-boot/src/main/java/com/njcn/zlevent/service/impl/EventServiceImpl.java index 8fb0386..749b9a0 100644 --- a/iot-analysis/analysis-zl-event/zl-event-boot/src/main/java/com/njcn/zlevent/service/impl/EventServiceImpl.java +++ b/iot-analysis/analysis-zl-event/zl-event-boot/src/main/java/com/njcn/zlevent/service/impl/EventServiceImpl.java @@ -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 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 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 getEventUser(String devId) { List result = new ArrayList<>(); + List devCode = new ArrayList<>(); //获取设备下主用户和子用户集合 List 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 users = userFeignClient.getUserByIdList(result).getData(); + devCode = users.stream().map(User::getDevCode).collect(Collectors.toList()); + } + return devCode; } /** * 发送通知消息 */ - - + public void sendEventToUser(List 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(); + } + } }