用户对接
This commit is contained in:
parent
cd6f4887c4
commit
aa9a4285d4
|
|
@ -0,0 +1,252 @@
|
|||
package com.dkl.large.utli;
|
||||
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import com.dkl.common.core.controller.BaseController;
|
||||
|
||||
import com.dkl.large.domain.DklCameraAnalysis;
|
||||
import com.dkl.large.domain.DklMonitoringCamera;
|
||||
import com.dkl.large.domain.DklMonitoringPoints;
|
||||
import com.dkl.large.domain.DklWarningInformation;
|
||||
import com.dkl.large.mapper.DklCameraAnalysisMapper;
|
||||
import com.dkl.large.mapper.DklMonitoringCameraMapper;
|
||||
import com.dkl.large.mapper.DklMonitoringPointsMapper;
|
||||
import com.dkl.large.mapper.DklWarningInformationMapper;
|
||||
import org.apache.rocketmq.client.producer.DefaultMQProducer;
|
||||
import org.apache.rocketmq.client.producer.SendCallback;
|
||||
import org.apache.rocketmq.client.producer.SendResult;
|
||||
import org.apache.rocketmq.common.message.MessageConst;
|
||||
import org.apache.rocketmq.spring.core.RocketMQTemplate;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
|
||||
@Component("automaticAlertGeneration")
|
||||
public class AutomaticAlertGeneration extends BaseController {
|
||||
private static final String PATTERN = "yyyy-MM-dd HH:mm:ss";
|
||||
@Autowired
|
||||
DklCameraAnalysisMapper dklCameraAnalysisMapper;
|
||||
@Autowired
|
||||
RocketMQTemplate rocketMQTemplate;
|
||||
@Autowired
|
||||
DklWarningInformationMapper dklWarningInformationMapper;
|
||||
@Autowired
|
||||
DklMonitoringCameraMapper dklMonitoringCameraMapper;
|
||||
private static final Logger log = LoggerFactory.getLogger(AutomaticAlertGeneration.class);
|
||||
//mqtopic
|
||||
@Value("${mqTopic}")
|
||||
String mqTopic;
|
||||
@Autowired
|
||||
private DefaultMQProducer producer;
|
||||
//生成预警
|
||||
public void generateAlerts() {
|
||||
//创建两个小时区间去查询数据
|
||||
DklCameraAnalysis dklCameraAnalysis = new DklCameraAnalysis();
|
||||
dklCameraAnalysis.setStartTime(getHoursBefore(2));
|
||||
dklCameraAnalysis.setEndTime(new Date());
|
||||
List<DklCameraAnalysis> dklCameraAnalysisList = dklCameraAnalysisMapper.getDklCameraAnalysisGroup(dklCameraAnalysis);
|
||||
//循环判断
|
||||
for (DklCameraAnalysis dklCameraAnalysisInfo: dklCameraAnalysisList) {
|
||||
//当区域预测人流超过最高流量的60%时,系统显示蓝色预警;超过80%则为黄色预警;90%橙色,达到100%时则为红色预警 目前为预警等级 0 1 2
|
||||
if (isEightyPercentGreaterOrEqual( Double.parseDouble( dklCameraAnalysisInfo.getCaneraMax()),Double.parseDouble( (dklCameraAnalysisInfo.getLoadmax())),1)){
|
||||
//预警
|
||||
DklMonitoringCamera dklMonitoringCamera =dklMonitoringCameraMapper.selectDklMonitoringCameraByPath(dklCameraAnalysisInfo.getDmcpoid());
|
||||
//创建预警信息
|
||||
DklWarningInformation dklWarningInformation = new DklWarningInformation();
|
||||
//预警唯一标识符
|
||||
dklWarningInformation.setWarningSigns(getDateString()+dklMonitoringCamera.getName()+"预警");
|
||||
//发布单位
|
||||
dklWarningInformation.setUnitName("系统识别");
|
||||
//发布时间
|
||||
dklWarningInformation.setReleaseTime(new Date());
|
||||
//预警级别
|
||||
dklWarningInformation.setWarningLevel("2");
|
||||
//预警状态
|
||||
dklWarningInformation.setWarningStatus("1");
|
||||
//经度
|
||||
dklWarningInformation.setLng(dklMonitoringCamera.getLng());
|
||||
//经度
|
||||
dklWarningInformation.setLat(dklMonitoringCamera.getLat());
|
||||
//预计持续时间
|
||||
dklWarningInformation.setExpectedDuration("持续关注");
|
||||
//可能波及范围
|
||||
dklWarningInformation.setScopeImpact(dklMonitoringCamera.getName()+"周边区域");
|
||||
//异常数据值
|
||||
dklWarningInformation.setAbnormalData("监测到"+dklMonitoringCamera.getName()+"客流异常,人流量骤增已达容量100%!");
|
||||
//发生点位
|
||||
// dklWarningInformation.setUnitCode(dklCameraAnalysisInfo.getDmpid());
|
||||
//预警点位id
|
||||
dklWarningInformation.setPointsId(dklCameraAnalysisInfo.getDmpid());
|
||||
dklWarningInformation.setPointName(dklCameraAnalysisInfo.getDmpName());
|
||||
//预警摄像id
|
||||
dklWarningInformation.setCameraId((long) dklMonitoringCamera.getId());
|
||||
dklWarningInformation.setCameraName( dklMonitoringCamera.getName());
|
||||
//添加系统信息
|
||||
dklWarningInformation.setDelFlag("0");
|
||||
dklWarningInformation.setCreateTime(new Date());
|
||||
dklWarningInformation.setCreateBy("系统监测");
|
||||
dklWarningInformation.setDeptId(dklCameraAnalysisInfo.getDeptid());
|
||||
dklWarningInformation.setUpdateTime(new Date());
|
||||
dklWarningInformation.setUpdateBy("系统监测");
|
||||
//向中台发送
|
||||
sendAlertMessage(dklWarningInformation);
|
||||
continue;
|
||||
} else if (isEightyPercentGreaterOrEqual( Double.parseDouble( dklCameraAnalysisInfo.getCaneraMax()),Double.parseDouble( (dklCameraAnalysisInfo.getLoadmax())),0.8)){
|
||||
//预警
|
||||
DklMonitoringCamera dklMonitoringCamera =dklMonitoringCameraMapper.selectDklMonitoringCameraByPath(dklCameraAnalysisInfo.getDmcpoid());
|
||||
//创建预警信息
|
||||
DklWarningInformation dklWarningInformation = new DklWarningInformation();
|
||||
//预警名称
|
||||
dklWarningInformation.setWarningSigns(getDateString()+dklMonitoringCamera.getName()+"预警");
|
||||
//发布单位
|
||||
dklWarningInformation.setUnitName("系统识别");
|
||||
//发布时间
|
||||
dklWarningInformation.setReleaseTime(new Date());
|
||||
//预警级别
|
||||
dklWarningInformation.setWarningLevel("1");
|
||||
//预警状态
|
||||
dklWarningInformation.setWarningStatus("1");
|
||||
//经度
|
||||
dklWarningInformation.setLng(dklMonitoringCamera.getLng());
|
||||
//经度
|
||||
dklWarningInformation.setLat(dklMonitoringCamera.getLat());
|
||||
//预计持续时间
|
||||
dklWarningInformation.setExpectedDuration("持续关注");
|
||||
//可能波及范围
|
||||
dklWarningInformation.setScopeImpact(dklMonitoringCamera.getName()+"周边区域");
|
||||
//异常数据值
|
||||
dklWarningInformation.setAbnormalData("监测到"+dklMonitoringCamera.getName()+"客流异常,人流量骤增已达容量80%!");
|
||||
//发生点位
|
||||
// dklWarningInformation.setUnitCode(dklCameraAnalysisInfo.getDmpid());
|
||||
//预警点位id
|
||||
dklWarningInformation.setPointsId(dklCameraAnalysisInfo.getDmpid());
|
||||
dklWarningInformation.setPointName(dklCameraAnalysisInfo.getDmpName());
|
||||
//预警摄像id
|
||||
dklWarningInformation.setCameraId((long) dklMonitoringCamera.getId());
|
||||
dklWarningInformation.setCameraName( dklMonitoringCamera.getName());
|
||||
//添加系统信息
|
||||
dklWarningInformation.setDelFlag("0");
|
||||
dklWarningInformation.setCreateTime(new Date());
|
||||
dklWarningInformation.setCreateBy("系统监测");
|
||||
dklWarningInformation.setDeptId(dklCameraAnalysisInfo.getDeptid());
|
||||
dklWarningInformation.setUpdateTime(new Date());
|
||||
dklWarningInformation.setUpdateBy("系统监测");
|
||||
//向中台发送
|
||||
sendAlertMessage(dklWarningInformation);
|
||||
continue;
|
||||
}
|
||||
else if (isEightyPercentGreaterOrEqual( Double.parseDouble( dklCameraAnalysisInfo.getCaneraMax()),Double.parseDouble( (dklCameraAnalysisInfo.getLoadmax())),0.6)){
|
||||
//预警
|
||||
DklMonitoringCamera dklMonitoringCamera =dklMonitoringCameraMapper.selectDklMonitoringCameraByPath(dklCameraAnalysisInfo.getDmcpoid());
|
||||
//创建预警信息
|
||||
DklWarningInformation dklWarningInformation = new DklWarningInformation();
|
||||
//预警唯一标识符
|
||||
dklWarningInformation.setWarningSigns(getDateString()+dklMonitoringCamera.getName()+"预警");
|
||||
//发布单位
|
||||
dklWarningInformation.setUnitName("系统识别");
|
||||
//发布时间
|
||||
dklWarningInformation.setReleaseTime(new Date());
|
||||
//预警级别
|
||||
dklWarningInformation.setWarningLevel("0");
|
||||
//预警状态
|
||||
dklWarningInformation.setWarningStatus("1");
|
||||
//经度
|
||||
dklWarningInformation.setLng(dklMonitoringCamera.getLng());
|
||||
//经度
|
||||
dklWarningInformation.setLat(dklMonitoringCamera.getLat());
|
||||
//预计持续时间
|
||||
dklWarningInformation.setExpectedDuration("持续关注");
|
||||
//可能波及范围
|
||||
dklWarningInformation.setScopeImpact(dklMonitoringCamera.getName()+"周边区域");
|
||||
//异常数据值
|
||||
dklWarningInformation.setAbnormalData("监测到"+dklMonitoringCamera.getName()+"客流异常,人流量骤增60%!");
|
||||
//发生点位
|
||||
// dklWarningInformation.setUnitCode(dklCameraAnalysisInfo.getDmpid());
|
||||
//预警点位id
|
||||
dklWarningInformation.setPointsId(dklCameraAnalysisInfo.getDmpid());
|
||||
dklWarningInformation.setPointName(dklCameraAnalysisInfo.getDmpName());
|
||||
//预警摄像id
|
||||
dklWarningInformation.setCameraId((long) dklMonitoringCamera.getId());
|
||||
dklWarningInformation.setCameraName( dklMonitoringCamera.getName());
|
||||
//添加系统信息
|
||||
dklWarningInformation.setDelFlag("0");
|
||||
dklWarningInformation.setCreateTime(new Date());
|
||||
dklWarningInformation.setCreateBy("系统监测");
|
||||
dklWarningInformation.setDeptId(dklCameraAnalysisInfo.getDeptid());
|
||||
dklWarningInformation.setUpdateTime(new Date());
|
||||
dklWarningInformation.setUpdateBy("系统监测");
|
||||
//向中台发送
|
||||
sendAlertMessage(dklWarningInformation);
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送预警消息
|
||||
* @param dklWarningInformation 消息内容
|
||||
*/
|
||||
public void sendAlertMessage(DklWarningInformation dklWarningInformation) {
|
||||
//生成预警信息数据
|
||||
dklWarningInformationMapper.insertDklWarningInformation(dklWarningInformation);
|
||||
// 构建消息对象
|
||||
Message<DklWarningInformation> message = MessageBuilder.withPayload(dklWarningInformation)
|
||||
.setHeader(MessageConst.PROPERTY_TAGS, "userTag")
|
||||
.build();
|
||||
|
||||
// 异步发送(带回调)
|
||||
rocketMQTemplate.asyncSend(mqTopic, message, new SendCallback() {
|
||||
@Override
|
||||
public void onSuccess(SendResult sendResult) {
|
||||
log.info("消息发送成功, ID: {}, 队列: {}",
|
||||
sendResult.getMsgId(),
|
||||
sendResult.getMessageQueue().getQueueId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onException(Throwable e) {
|
||||
log.error("消息发送失败", e);
|
||||
// 可添加补偿逻辑
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public static Date getHoursBefore(int hours) {
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.add(Calendar.HOUR_OF_DAY, -hours);
|
||||
return calendar.getTime();
|
||||
}
|
||||
public static String getDateString() {
|
||||
// 定义中文时间格式
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH时mm分ss秒");
|
||||
// 获取并格式化当前时间
|
||||
return LocalDateTime.now().format(formatter);
|
||||
}
|
||||
|
||||
public static boolean isEightyPercentGreaterOrEqual(double a, double b,double ratio) {
|
||||
return (a * ratio) >= b;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue