wisdomisite-java/src/main/java/com/zhgd/xmgl/async/AsyncAiAnalyse.java

182 lines
7.8 KiB
Java
Raw Normal View History

2023-02-16 15:28:15 +08:00
package com.zhgd.xmgl.async;
import com.zhgd.mqtt.bean.PushPayload;
import com.zhgd.mqtt.server.IMqttSender;
2024-05-25 15:56:29 +08:00
import com.zhgd.xmgl.constant.Cts;
2024-01-19 11:26:36 +08:00
import com.zhgd.xmgl.modules.basicdata.constant.DictionaryConstant;
2023-02-16 15:28:15 +08:00
import com.zhgd.xmgl.modules.basicdata.entity.CompanyConfig;
2024-01-19 11:26:36 +08:00
import com.zhgd.xmgl.modules.basicdata.entity.DictionaryItem;
2023-02-16 15:28:15 +08:00
import com.zhgd.xmgl.modules.basicdata.entity.SystemUser;
import com.zhgd.xmgl.modules.basicdata.mapper.SystemUserMapper;
import com.zhgd.xmgl.modules.basicdata.service.ICompanyConfigService;
import com.zhgd.xmgl.modules.basicdata.service.ISystemUserService;
2024-01-19 11:26:36 +08:00
import com.zhgd.xmgl.modules.basicdata.service.impl.DictionaryItemServiceImpl;
2024-05-11 01:58:58 +08:00
import com.zhgd.xmgl.modules.basicdata.service.impl.NoticeServiceImpl;
2023-02-16 15:28:15 +08:00
import com.zhgd.xmgl.modules.video.entity.AiAnalyseHardWareAlarmRecord;
import com.zhgd.xmgl.push.service.UniPushService;
2024-05-25 15:56:29 +08:00
import com.zhgd.xmgl.util.MapBuilder;
2023-02-16 15:28:15 +08:00
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
2024-05-11 01:58:58 +08:00
import org.springframework.context.annotation.Lazy;
2023-02-16 15:28:15 +08:00
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* @program: wisdomSite
* @description: 异步AI分析数据处理
* @author: Mr.Peng
* @create: 2022-04-20 14:09
**/
@Slf4j
@Component
public class AsyncAiAnalyse {
@Autowired
private IMqttSender mqttPushClient;
@Autowired
private SystemUserMapper systemUserMapper;
@Autowired
private ISystemUserService systemUserService;
@Autowired
private ICompanyConfigService companyConfigService;
@Autowired
private UniPushService uniPushService;
2024-01-19 11:26:36 +08:00
@Autowired
private DictionaryItemServiceImpl dictionaryItemService;
2024-05-11 01:58:58 +08:00
@Lazy
@Autowired
private NoticeServiceImpl noticeService;
2023-02-16 15:28:15 +08:00
@Value("${mqtt-scope}")
private String scope;
2024-05-25 15:56:29 +08:00
/**
* @param record
*/
2023-02-16 15:28:15 +08:00
@Async("taskExecutor")
2024-05-25 15:56:29 +08:00
public void sendAiAnalyse(AiAnalyseHardWareAlarmRecord record, String type) {
2023-02-16 15:28:15 +08:00
try {
2024-05-25 15:56:29 +08:00
String title = getTitleByAlarmType(record.getAlarmType(), record.getProjectSn());
2024-05-18 10:36:36 +08:00
title = title == null ? "其他类型" : title;
2024-05-25 15:56:29 +08:00
String msg = title + ",位置" + record.getLocation();
List<SystemUser> systemUserList;
if (type.equals(Cts.PROJECT_LEVEL)) {
systemUserList = systemUserService.getSystemUsersBySn(new MapBuilder<String, Object>().put(Cts.SN, record.getProjectSn()).put(Cts.QUERY_TYPE, Cts.PROJECT_LEVEL).build());
} else {
systemUserList = systemUserService.getSystemUsersBySn(new MapBuilder<String, Object>().put(Cts.SN, record.getProjectSn()).put(Cts.QUERY_TYPE, Cts.PROJECT_LEVEL_AND_CHILDREN).build());
}
2023-02-16 15:28:15 +08:00
title = "AI分析" + title;
//向项目管理员和子账号推送通知
if (systemUserList.size() > 0) {
for (SystemUser systemUser : systemUserList) {
2024-05-11 01:58:58 +08:00
noticeService.addUserNotice(systemUser.getUserId(), msg, title, "8");
2023-02-16 15:28:15 +08:00
}
2024-05-25 15:56:29 +08:00
sendAppNotice(record.getProjectSn(), title, msg, systemUserList, "/pages/potentialRisk/potentialRisk?id=" + record.getId());
2023-02-16 15:28:15 +08:00
}
} catch (Exception e) {
2024-04-14 21:05:01 +08:00
log.error("error", e);
2023-02-16 15:28:15 +08:00
}
}
2024-05-18 10:36:36 +08:00
public String getTitleByAlarmType(Integer alarmType, String projectSn) {
DictionaryItem dict = dictionaryItemService.getDict(DictionaryConstant.AI_ANALYSE_HARD_WARE_ALARM_RECORD_TYPE, alarmType + "", projectSn);
2024-01-19 11:26:36 +08:00
return dict != null ? dict.getName() : null;
2023-02-16 15:28:15 +08:00
}
/**
* 向手机端推送通知
*
* @param projectSn
* @param title
* @param msg
* @param systemUserList
*/
public void sendAppNotice(String projectSn, String title, String msg, List<SystemUser> systemUserList, String payload) {
try {
CompanyConfig companyConfig = companyConfigService.getCompanyConfigByProjectSn(projectSn);
log.info("companyConfig-------------发送开关" + (companyConfig == null ? "" : companyConfig.getAppNoticeType()));
if (companyConfig != null && companyConfig.getAppNoticeType() != null && companyConfig.getAppNoticeType() == 1) {
for (SystemUser systemUser : systemUserList) {
if (systemUser.getAccountType() == 5 || systemUser.getAccountType() == 6) {
log.info("发送-----" + systemUser.getAccount() + "消息:" + msg);
uniPushService.pushToSingleByAlias(systemUser.getClientId(), title, msg, payload);
}
}
}
} catch (Exception e) {
2024-04-14 21:05:01 +08:00
log.error("error", e);
2023-02-16 15:28:15 +08:00
}
}
/**
* 责任人收到AI警告后推送消息给整改责任人
*
* @param record AI警告消息
* @param systemUserList 需要推送的人员集合
*/
public void sendCorrectAnalyse(AiAnalyseHardWareAlarmRecord record, List<SystemUser> systemUserList) {
String msg = "你有一条待整改消息,位置-----" + record.getLocation();
2024-05-18 10:36:36 +08:00
String title = getTitleByAlarmType(record.getAlarmType(), record.getProjectSn());
2023-02-16 15:28:15 +08:00
if (CollectionUtils.isNotEmpty(systemUserList)) {
for (SystemUser systemUser : systemUserList) {
log.info(systemUser.getAccount() + "有一条消息,位置-----" + record.getLocation());
PushPayload pushMessage = PushPayload.getPushPayloadBuider().setAccountId(systemUser.getUserId().toString())
.setTitle(title)
.setContent(msg)
.setType("8")
.setItemType(record.getAlarmType().toString()).bulid();
String kdTopic = scope + systemUser.getUserId();
mqttPushClient.sendToMqtt(kdTopic, pushMessage.toString());
}
sendAppNotice(record.getProjectSn(), title, msg, systemUserList, "/pages/potentialRisk/potentialRisk?id=" + record.getId());
}
}
/**
* 推送巡检信息到责任人
*/
public void sendMsgGotoPeople(List<SystemUser> systemUserList) {
String msg = "您有一条日常巡检信息待处理请及时处理";
String title = "日常巡检";
if (CollectionUtils.isNotEmpty(systemUserList)) {
for (SystemUser systemUser : systemUserList) {
log.info(systemUser.getAccount() + "有一条消息");
PushPayload pushMessage = PushPayload.getPushPayloadBuider().setAccountId(systemUser.getUserId().toString())
.setTitle(title)
.setContent(msg)
.setType("8")
.setItemType("日常巡检".toString()).bulid();
String kdTopic = scope + systemUser.getUserId();
mqttPushClient.sendToMqtt(kdTopic, pushMessage.toString());
}
sendMsgToApp(title, msg, systemUserList, "/pages/projectEnd/dailyCheck/index");
}
}
/**
* 巡检消息推送到责任人
*
* @param title
* @param msg
* @param systemUserList
* @param payload
*/
private void sendMsgToApp(String title, String msg, List<SystemUser> systemUserList, String payload) {
try {
for (SystemUser systemUser : systemUserList) {
if (systemUser.getAccountType() == 5 || systemUser.getAccountType() == 6) {
log.info("发送-----" + systemUser.getAccount() + "消息:" + msg);
uniPushService.pushToSingleByAlias(systemUser.getClientId(), title, msg, payload);
}
}
} catch (Exception e) {
2024-04-14 21:05:01 +08:00
log.error("error", e);
2023-02-16 15:28:15 +08:00
}
}
}