package com.zhgd.xmgl.async; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.zhgd.mqtt.bean.PushPayload; import com.zhgd.mqtt.server.IMqttSender; import com.zhgd.xmgl.modules.basicdata.entity.CompanyConfig; 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; import com.zhgd.xmgl.modules.video.entity.AiAnalyseHardWareAlarmRecord; import com.zhgd.xmgl.push.service.UniPushService; 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; 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; @Value("${mqtt-scope}") private String scope; @Async("taskExecutor") public void sendAiAnalyse(AiAnalyseHardWareAlarmRecord aiAnalyseHardWareAlarmRecord) { try { String title = getTitleByAlarmType(aiAnalyseHardWareAlarmRecord.getAlarmType()); String msg = title + ",位置" + aiAnalyseHardWareAlarmRecord.getLocation(); List systemUserList = systemUserService.list(Wrappers.lambdaQuery().eq(SystemUser::getSn, aiAnalyseHardWareAlarmRecord.getProjectSn()).eq(SystemUser::getAccountType, 5)); // List systemUserList=systemUserMapper.selectProjectSystemUserList(aiAnalyseHardWareAlarmRecord.getProjectSn()); title = "AI分析" + title; //向项目管理员和子账号推送通知 if (systemUserList.size() > 0) { for (SystemUser systemUser : systemUserList) { PushPayload pushMessage = PushPayload.getPushPayloadBuider().setAccountId(systemUser.getUserId().toString()) .setTitle(title) .setContent(msg) .setType("8") .setItemType(aiAnalyseHardWareAlarmRecord.getAlarmType().toString()) .bulid(); String kdTopic = scope + systemUser.getUserId(); mqttPushClient.sendToMqtt(kdTopic, pushMessage.toString()); } sendAppNotice(aiAnalyseHardWareAlarmRecord.getProjectSn(), title, msg, systemUserList, "/pages/potentialRisk/potentialRisk?id=" + aiAnalyseHardWareAlarmRecord.getId()); } } catch (Exception e) { e.printStackTrace(); } } public String getTitleByAlarmType(Integer alarmType) { //1-烟感、2-明火、3-人员倒地、4-未戴安全帽、5-区域入侵、6-越界入侵、7-人员聚集衣 8-反光衣、9-裸土覆盖,13口罩识别,14徘徊预警,15物体滞留监测,16绊线监测 String title = ""; if (alarmType == 1) { title = "烟感报警"; } else if (alarmType == 2) { title = "明火报警"; } else if (alarmType == 3) { title = "人员倒地报警"; } else if (alarmType == 4) { title = "未戴安全帽报警"; } else if (alarmType == 5) { title = "区域入侵报警"; } else if (alarmType == 6) { title = "越界入侵报警"; } else if (alarmType == 7) { title = "人员聚集衣报警"; } else if (alarmType == 8) { title = "反光衣报警"; } else if (alarmType == 9) { title = "裸土覆盖报警"; } else if (alarmType == 13) { title = "口罩识别报警"; } else if (alarmType == 14) { title = "徘徊预警"; } else if (alarmType == 15) { title = "物体滞留监测"; } else if (alarmType == 16) { title = "绊线监测"; } return title; } /** * 向手机端推送通知 * * @param projectSn * @param title * @param msg * @param systemUserList */ public void sendAppNotice(String projectSn, String title, String msg, List 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) { e.printStackTrace(); } } /** * 责任人收到AI警告后推送消息给整改责任人 * * @param record AI警告消息 * @param systemUserList 需要推送的人员集合 */ public void sendCorrectAnalyse(AiAnalyseHardWareAlarmRecord record, List systemUserList) { String msg = "你有一条待整改消息,位置-----" + record.getLocation(); String title = getTitleByAlarmType(record.getAlarmType()); 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 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 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) { e.printStackTrace(); } } }