Commit 905e42bf authored by ma's avatar ma

更换报警方式,改成定时发送

parent 6d71c671
package iot.sixiang.license.util;
package iot.sixiang.license.component;
import iot.sixiang.license.operate.OperateManager;
import iot.sixiang.license.service.MonitorService;
......@@ -16,12 +16,12 @@ public class OperateSchedu {
private OperateManager operateManager;
@Autowired
private MonitorService monitorService;
@Scheduled(cron = "0 0 0/1 * * ?")
public void scheduled() {
operateManager.createProxyClient();
int count = operateManager.getCount();
monitorService.addMonitor(count);
operateManager.clearCount();
}
}
package iot.sixiang.license.component;
import iot.sixiang.license.model.vo.ReportStatsVO;
import iot.sixiang.license.service.ReportService;
import iot.sixiang.license.util.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* Created by m33
* Date 2023/2/8 14:10
* Description
*/
@Component
@Slf4j
public class ReportSchedu {
@Resource
private ReportService reportService;
private final static String PATTERN = "yyyy-MM-dd HH:mm:00";
/**
* 每整10分钟执行一次
*/
@Scheduled(cron = "0 0/10 * * * ?")
public void scheduled() {
Date date = new Date();
String startTime = CommonUtil.dateToString(CommonUtil.getOffsetDate(date, -10), PATTERN);
String endTime = CommonUtil.dateToString(date, PATTERN);
List<ReportStatsVO> reportStatsList = reportService.getReportStatsList(startTime, endTime);
if (reportStatsList.size() != 0) {
Map<String, List<ReportStatsVO>> map = reportStatsList.stream().collect(Collectors.groupingBy(ReportStatsVO::getUserName));
for (List<ReportStatsVO> reportStatsVOS : map.values()) {
reportService.reportToEmail(reportStatsVOS, startTime, endTime);
}
}
}
}
......@@ -34,7 +34,6 @@ import iot.sixiang.license.util.EmailUtils;
import iot.sixiang.license.util.TreeUtil;
import iot.sixiang.license.xss.XssUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.WebDataBinder;
......@@ -80,6 +79,8 @@ public class UserController {
private Integer changePwdMaxErrCount;
@Value("${spring.mail.to}")
private String account;
@Value("${other.web}")
private String webUrl;
private static final String OPERATION_CHANGE = "change";
......@@ -124,7 +125,7 @@ public class UserController {
}
boolean res = userService.addUser(userName, company, password, user.getUserId(), ++level);
if (res) {
String content = "注册成功!感谢您使用实名制身份验证云应用平台,请点击以下链接登录:http://web.license.srthinker.com/user#/login";
String content = "注册成功!感谢您使用实名制身份验证云应用平台,请点击以下链接登录:" + webUrl + "/#/login";
emailUtils.sendSimpleMail(userName, "感谢您使用实名制身份验证云应用平台", content);
return BaseResult.success();
} else {
......
......@@ -10,11 +10,7 @@ import io.netty.channel.socket.SocketChannel;
import iot.sixiang.license.auth.AuthManager;
import iot.sixiang.license.consts.Constant;
import iot.sixiang.license.entity.Report;
import iot.sixiang.license.event.CreateForwardClientEvent;
import iot.sixiang.license.event.DeviceClientInactiveEvent;
import iot.sixiang.license.event.DeviceClientLicenseEvent;
import iot.sixiang.license.event.EventPublisher;
import iot.sixiang.license.event.ForwardClientRequestEvent;
import iot.sixiang.license.event.*;
import iot.sixiang.license.mapper.UserMapper;
import iot.sixiang.license.model.SessionContext;
import iot.sixiang.license.model.dto.ReportErrorMsgDTO;
......@@ -184,7 +180,7 @@ public class DeviceServerHandler extends SimpleChannelInboundHandler<Object> {
UserInfoVO user = userMapper.getUserBySn(sn);
Report report = Report.builder().category(0).type(6).sn(sn).userName(user.getUserName())
.userCompany(user.getCompany()).createTime(new Date()).updateTime(new Date()).build();
reportService.reportToEmail(report, user.getNotify());
reportService.addReport(report);
}
return license;
}
......
......@@ -59,11 +59,11 @@ public class OperateSAMStatusResponseEventHandler {
int typeId = 2;
String title = "SAM故障";
String content = "index为" + index + "的SAM发生故障";
alarmService.addAlarm(typeId,title,content);
alarmService.addAlarm(typeId, title, content);
UserInfoVO user = userMapper.getRootAccount();
Report report = Report.builder().category(1).type(1).userName(user.getUserName())
.userCompany(user.getCompany()).createTime(new Date()).updateTime(new Date()).build();
reportService.reportToEmail(report, user.getNotify());
reportService.addReport(report);
}
}
SamMonitor samMonitor = new SamMonitor();
......@@ -71,15 +71,15 @@ public class OperateSAMStatusResponseEventHandler {
samMonitor.setOnlineCount(onlineCount);
samMonitor.setSamCount(samCount);
operateManager.putSamMonitorMap(serverIp, samMonitor);
if ((float)onlineCount / samCount > 0.7) {
if ((float) onlineCount / samCount > 0.7) {
int typeId = 3;
String title = "SAM不足";
String content = "当前在线客户端已超过70%";
alarmService.addAlarm(typeId,title,content);
alarmService.addAlarm(typeId, title, content);
UserInfoVO user = userMapper.getRootAccount();
Report report = Report.builder().category(1).type(2).userName(user.getUserName())
.userCompany(user.getCompany()).createTime(new Date()).updateTime(new Date()).build();
reportService.reportToEmail(report, user.getNotify());
reportService.addReport(report);
}
}
}
......@@ -46,7 +46,7 @@ public class ForwardConnectionListener extends BaseConnectionListener {
Report report = Report.builder().category(1).type(3).userName(user.getUserName())
.description("桥接客户端,连接服务器" + this.host + ":" + this.port + "失败")
.userCompany(user.getCompany()).createTime(new Date()).updateTime(new Date()).build();
reportService.reportToEmail(report, user.getNotify());
reportService.addReport(report);
// forward client连接失败,则强制踢掉设备客户端
DeviceClientBeForcedOfflineEvent deviceClientBeForcedOfflineEvent = new DeviceClientBeForcedOfflineEvent();
deviceClientBeForcedOfflineEvent.setSn(this.sn);
......
......@@ -2,6 +2,7 @@ package iot.sixiang.license.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import iot.sixiang.license.entity.Report;
import iot.sixiang.license.model.vo.ReportStatsVO;
import iot.sixiang.license.model.vo.ReportVO;
import java.util.List;
......@@ -12,5 +13,8 @@ import java.util.List;
* Description
*/
public interface ReportMapper extends BaseMapper<Report> {
List<ReportVO> getReportList(Integer type, String sn, String startTime, String endTime, List<Integer> userChildIds);
List<ReportStatsVO> getReportStatsList(String startTime, String endTime);
}
package iot.sixiang.license.model.vo;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
/**
* Created by m33
* Date 2023/2/8 15:12
* Description
*/
@Data
public class ReportStatsVO {
@ApiModelProperty("系统报警数量")
private Integer systemError;
@ApiModelProperty("设备报警数量")
private Integer deviceError;
@ApiModelProperty("设备编码")
private String sn;
@ApiModelProperty("报警账户")
private String userName;
@ApiModelProperty("报警公司")
private String userCompany;
@ApiModelProperty("报警邮箱")
private String notify;
}
......@@ -37,7 +37,7 @@ public class OperateConnectionListener extends BaseConnectionListener {
Report report = Report.builder().category(1).type(3).userName(user.getUserName())
.description("运维客户端,连接服务器" + this.host + ":" + this.port + "失败")
.userCompany(user.getCompany()).createTime(new Date()).updateTime(new Date()).build();
reportService.reportToEmail(report, user.getNotify());
reportService.addReport(report);
} else {
log.info("运维客户端,连接服务器成功:{},{}", this.host, this.port);
//TODO 查询SAM状态
......
package iot.sixiang.license.service;
import iot.sixiang.license.entity.Report;
import iot.sixiang.license.model.BaseResult;
import iot.sixiang.license.model.PageResult;
import iot.sixiang.license.model.dto.ReportDTO;
import iot.sixiang.license.model.vo.ReportStatsVO;
import iot.sixiang.license.model.vo.ReportVO;
import java.util.List;
/**
* Created by M=54G
* Date 11/23/22 3:09 PM
......@@ -15,5 +17,9 @@ public interface ReportService {
PageResult<ReportVO> getReportList(ReportDTO reportDTO);
void reportToEmail(Report report, String notify);
List<ReportStatsVO> getReportStatsList(String startTime, String endTime);
void addReport(Report report);
void reportToEmail(List<ReportStatsVO> reportStatsVOS, String startTime, String endTime);
}
......@@ -2,19 +2,19 @@ package iot.sixiang.license.service.impl;
import cn.hutool.core.lang.Validator;
import iot.sixiang.license.entity.Report;
import iot.sixiang.license.entity.ReportType;
import iot.sixiang.license.handler.IotLicenseException;
import iot.sixiang.license.jwt.UserUtils;
import iot.sixiang.license.mapper.ReportMapper;
import iot.sixiang.license.mapper.ReportTypeMapper;
import iot.sixiang.license.model.PageResult;
import iot.sixiang.license.model.dto.ReportDTO;
import iot.sixiang.license.model.vo.ReportStatsVO;
import iot.sixiang.license.model.vo.ReportVO;
import iot.sixiang.license.service.ReportService;
import iot.sixiang.license.util.CommonUtil;
import iot.sixiang.license.util.EmailUtils;
import iot.sixiang.license.util.TreeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
......@@ -36,6 +36,8 @@ public class ReportServiceImpl implements ReportService {
EmailUtils emailUtils;
@Resource
ReportTypeMapper reportTypeMapper;
@Value("${other.web}")
private String webUrl;
@Override
public PageResult<ReportVO> getReportList(ReportDTO reportDTO) {
......@@ -70,30 +72,60 @@ public class ReportServiceImpl implements ReportService {
return new PageResult(200, "查找成功", pageNo, pages, total, result);
}
@Override
public List<ReportStatsVO> getReportStatsList(String startTime, String endTime) {
if (StringUtils.isEmpty(startTime) || StringUtils.isEmpty(endTime)) {
throw new IotLicenseException(403, "统计报警失败,时间参数为空");
}
return reportMapper.getReportStatsList(startTime, endTime);
}
@Override
public void addReport(Report report) {
reportMapper.insert(report);
}
@Async("mailExecutor")
@Override
public void reportToEmail(Report report, String notify) {
public void reportToEmail(List<ReportStatsVO> reportStatsVOS, String startTime, String endTime) {
if (reportStatsVOS.size() == 0) {
return;
}
String notify = reportStatsVOS.get(0).getNotify();
String userName = reportStatsVOS.get(0).getUserName();
String userCompany = reportStatsVOS.get(0).getUserCompany();
if (!Validator.isEmail(notify)) {
throw new IotLicenseException(403, "报警失败,无效的邮箱账号");
}
reportMapper.insert(report);
Integer type = report.getType();
ReportType reportType = reportTypeMapper.selectById(type);
if (reportType == null) {
throw new IotLicenseException(403, "报警失败,无效的报警类型");
String content;
String url = webUrl + "/#/warn";
int systemErrorTotal = 0;
boolean deviceErrorFlag = false;
StringBuilder deviceStr = new StringBuilder();
for (ReportStatsVO reportStatsVO : reportStatsVOS) {
systemErrorTotal += reportStatsVO.getSystemError();
int deviceErrorCnt = reportStatsVO.getDeviceError();
String sn = reportStatsVO.getSn();
if (deviceErrorCnt != 0) {
deviceErrorFlag = true;
deviceStr.append("<p>").append("设备 ").append(sn).append(": ").append(deviceErrorCnt).append("条;</p></br>");
}
Integer category = report.getCategory();
String content = "";
if (category == 1) {
content = "【实名制身份验证云平台】系统报警!报警时间:" + CommonUtil.getSystemTime() + ",报警事件:" + reportType.getDescription()
+ ",报警用户:" + report.getUserName() + ",公司名称:" + report.getUserCompany() + ",请核实!";
} else if (category == 0) {
content = "【实名制身份验证云平台】设备报警!报警时间:" + CommonUtil.getSystemTime() + ",报警事件:" + reportType.getDescription()
+ ",设备SN:" + report.getSn() + ",报警用户:" + report.getUserName() + ",公司名称:" + report.getUserCompany()
+ ",请核实!";
}
if (systemErrorTotal != 0 && deviceErrorFlag) {
content = "<p>【实名制身份验证云平台】系统和设备报警!</p><p>报警时间:" + startTime + "至" + endTime + ";</p>"
+ "<p>系统报警:" + systemErrorTotal + "条;</p>" + deviceStr + "<p>报警用户:" + userName + ", 公司名称:" + userCompany + ";</p>"
+ "<a style='color:blue;text-decoration: none;' href='" + url + "'>请点击链接核实!</a>";
} else {
if (systemErrorTotal != 0) {
content = "<p>【实名制身份验证云平台】系统报警!</p><p>报警时间:" + startTime + "至" + endTime + ";</p>"
+ "<p>系统报警:" + systemErrorTotal + "条;</p>" + "<p>报警用户:" + userName + ", 公司名称:" + userCompany + ";</p>"
+ "<a style='color:blue;text-decoration: none;' href='" + url + "'>请点击链接核实!</a>";
} else {
throw new IotLicenseException(403, "报警失败,无效的报警大类");
content = "<p>【实名制身份验证云平台】设备报警!</p><p>报警时间:" + startTime + "至" + endTime + ";</p>"
+ deviceStr + "<p>报警用户:" + userName + ", 公司名称:" + userCompany + ";</p>"
+ "<a style='color:blue;text-decoration: none;' href='" + url + "'>请点击此处核实!</a>";
}
}
emailUtils.sendSimpleMail(notify, "实名制身份验证云平台", content);
emailUtils.sendHtmlMail(notify, "实名制身份验证云平台", content);
}
}
......@@ -98,7 +98,7 @@ public class TerminalDeviceServiceImpl implements TerminalDeviceService {
}
Report report = Report.builder().category(0).type(type).sn(pmsUseLog.getSn()).userName(user.getUserName())
.userCompany(user.getCompany()).createTime(new Date()).updateTime(new Date()).build();
reportService.reportToEmail(report, user.getNotify());
reportService.addReport(report);
boolean res = pmsUseService.reportErrorMsg(reportErrorMsgDTO);
if (!res) {
return BaseResult.failed();
......
......@@ -50,6 +50,15 @@ public class CommonUtil {
return ft.format(date);
}
/**
* 获取指定日期前或后几分钟
*
* @return
*/
public static Date getOffsetDate(Date date, int minutes) {
return new Date(date.getTime() + (minutes * 60 * 1000));
}
/**
* 验证密码-是否包含用户名字符(密码应与用户名无相关性,密码中不得包含用户名的完整字符串、大小写变位或形似变换的字符串)
......@@ -259,7 +268,7 @@ public class CommonUtil {
}
public static String getServerParentDirectory() {
return new File(new ApplicationHome(Constant.class).getSource().getParentFile().getPath()) + File.separator + "file";
return new File(new ApplicationHome(Constant.class).getSource().getParentFile().getParentFile().getPath()) + File.separator + "lib";
}
/**
......
......@@ -7,9 +7,12 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
/**
* Title: EmailUtils
......@@ -49,4 +52,19 @@ public class EmailUtils {
throw new IotLicenseException(405, "短信邮件发送失败");
}
}
public void sendHtmlMail(String to, String subject, String content) {
MimeMessage message = mailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
mailSender.send(message);
logger.info("=============》一份html邮件已成功发送");
} catch (MessagingException e) {
logger.error("=============》发送html邮件时发生异常!", e);
}
}
}
......@@ -36,6 +36,7 @@ cros:
cros_allowed_method: GET,POST
other:
web: http://web.license.srthinker.com
md5:
salt: PI7dBYlEfeP8IZ6vogqFL1U5pVnyCuNAGja3lsREx4M9r0SX
error_count:
......
......@@ -33,4 +33,22 @@
AND report_type.deleted = 0
ORDER BY report.create_time DESC
</select>
<select id="getReportStatsList" resultType="iot.sixiang.license.model.vo.ReportStatsVO">
SELECT
sum( IF ( category = 1, 1, 0 ) ) system_error,
sum( IF ( category = 0, 1, 0 ) ) device_error,
sn,
report.user_name,
report.user_company,
notify
FROM
`report`
INNER JOIN `user` ON report.user_name = `user`.user_name
WHERE
report.deleted = 0
AND report.create_time >= #{startTime}
AND #{endTime} > report.create_time
GROUP BY sn
</select>
</mapper>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment