Jelajahi Sumber

告警监控定时任务,发送消息

lipenghui 2 hari lalu
induk
melakukan
12cfe908f7

+ 1 - 0
yudao-module-pms/yudao-module-pms-biz/src/main/java/cn/iocoder/yudao/module/pms/constant/PmsConstants.java

@@ -4,6 +4,7 @@ public interface PmsConstants {
     String GENERATE_INSPECT = "generateInspect";
     String GENERATE_MAINTAIN = "generateMaintain";
     String FAILURE_NO_PASS = "failureNoPass";
+    String ALARM_MESSAGE = "alarmMessage";
     String GENERATE_MAINTAIN_TEMPLATE = "generate-maintain";
     String GENERATE_MAINTENANCE = "generateMaintenance";
     String FAILURE_REPORT = "failureReport";

+ 8 - 0
yudao-module-pms/yudao-module-pms-biz/src/main/java/cn/iocoder/yudao/module/pms/dal/mysql/TDDeviceMapper.java

@@ -109,4 +109,12 @@ public interface TDDeviceMapper extends BaseMapperX<TDDeviceDO> {
             "</script>"
     })
     int batchInsert(@Param("data") TDLogDO data);
+
+    @Select("SELECT count(*) FROM iot_log.device_${deviceName} " +
+            "WHERE ts between #{start} and #{end} and identity = #{identifier} and log_value between #{min} and #{max}" )
+    @DS("tdengine")
+    @TenantIgnore
+    @InterceptorIgnore(tenantLine = "true")
+    Integer selectRangeCount(@Param("deviceName") String tableName, @Param("identifier") String identifier,@Param("start") Timestamp start,@Param("end") Timestamp end,
+                                    @Param("max") String max,@Param("min") String min);
 }

+ 101 - 0
yudao-module-pms/yudao-module-pms-biz/src/main/java/cn/iocoder/yudao/module/pms/job/alarm/AlarmJob.java

@@ -0,0 +1,101 @@
+package cn.iocoder.yudao.module.pms.job.alarm;
+
+import cn.hutool.core.collection.CollUtil;
+import cn.hutool.core.date.DatePattern;
+import cn.hutool.core.date.DateTime;
+import cn.hutool.core.date.DateUtil;
+import cn.iocoder.yudao.framework.common.pojo.PageParam;
+import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
+import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
+import cn.iocoder.yudao.module.pms.ThingsModelDTO;
+import cn.iocoder.yudao.module.pms.constant.PmsConstants;
+import cn.iocoder.yudao.module.pms.controller.admin.iotdeviceperson.vo.IotDevicePersonPageReqVO;
+import cn.iocoder.yudao.module.pms.controller.admin.vo.DeviceVO;
+import cn.iocoder.yudao.module.pms.controller.admin.vo.IotDevicePageReqVO;
+import cn.iocoder.yudao.module.pms.dal.dataobject.IotDeviceDO;
+import cn.iocoder.yudao.module.pms.dal.dataobject.alarm.IotAlarmSettingDO;
+import cn.iocoder.yudao.module.pms.dal.dataobject.iotdeviceperson.IotDevicePersonDO;
+import cn.iocoder.yudao.module.pms.dal.mysql.IotDeviceMapper;
+import cn.iocoder.yudao.module.pms.dal.mysql.TDDeviceMapper;
+import cn.iocoder.yudao.module.pms.dal.mysql.alarm.IotAlarmSettingMapper;
+import cn.iocoder.yudao.module.pms.message.PmsMessage;
+import cn.iocoder.yudao.module.pms.service.IotDeviceService;
+import cn.iocoder.yudao.module.pms.service.iotdeviceperson.IotDevicePersonService;
+import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
+import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
+import com.google.common.collect.ImmutableList;
+import liquibase.pro.packaged.F;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.Resource;
+import java.sql.Timestamp;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+import java.util.Objects;
+import java.util.stream.Collectors;
+
+import static cn.iocoder.yudao.framework.common.util.date.DateUtils.generateTimestamp;
+
+@Component
+@Slf4j
+public class AlarmJob implements JobHandler {
+    @Resource
+    private IotAlarmSettingMapper alarmSettingMapper;
+    @Autowired
+    private IotDeviceService iotDeviceService;
+    @Resource
+    private TDDeviceMapper tdDeviceMapper;
+    @Resource
+    private PmsMessage pmsMessage;
+    @Autowired
+    private IotDevicePersonService iotDevicePersonService;
+    @Autowired
+    private AdminUserApi adminUserApi;
+
+    @Override
+    @TenantIgnore
+    public String execute(String param) throws Exception {
+        //查询出所有的设置了区间阈值的设备与分类
+        List<IotAlarmSettingDO> alarmSettingDOS = alarmSettingMapper.selectList();
+        IotDevicePageReqVO reqVO = new IotDevicePageReqVO();
+        reqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
+        List<IotDeviceDO> list = iotDeviceService.getIotDeviceTdPage(reqVO, null).getList();
+        //获取十分钟前的时间
+        DateTime tenMinute = DateUtil.offsetMinute(new Date(), -10);
+        String tenMinuteString = tenMinute.toString(DatePattern.NORM_DATETIME_FORMAT);
+        String now = DateUtil.now();
+        Timestamp start = generateTimestamp(tenMinuteString);
+        Timestamp end = generateTimestamp(now);
+        list.stream().filter(e -> e.getIfInline()==3).forEach(f ->{
+            //调用延凡接口获取该设备的所有参数
+            if (f.getId()==73){
+                System.out.println("2222");
+            }
+            List<ThingsModelDTO> tdParams = iotDeviceService.getTdParams(f);
+            tdParams.forEach(model ->{
+                //获取设备id与属性标识相同的告警配置
+                alarmSettingDOS.stream().filter(g -> g.getDeviceId().equals(f.getId())&&g.getPropertyCode().equals(model.getIdentifier())).forEach(t -> {
+                    String maxValue = t.getMaxValue();
+                    String minValue = t.getMinValue();
+                    Integer count = tdDeviceMapper.selectRangeCount(f.getDeviceCode(), t.getPropertyCode(), start, end, maxValue, minValue);
+                    if (count == 0) {
+                        IotDevicePersonPageReqVO pageReqVO = new IotDevicePersonPageReqVO();
+                        pageReqVO.setDeviceIds(ImmutableList.of(f.getId()));
+                        List<IotDevicePersonDO> persons = iotDevicePersonService.getPersonsByDeviceIds(pageReqVO);
+                        if (CollUtil.isNotEmpty(persons)) {
+                            AdminUserRespDTO user = adminUserApi.getUser(persons.get(0).getPersonId());
+                            if (Objects.nonNull(user)) {
+                                pmsMessage.sendMessage(f.getId(), f.getDeviceCode()+f.getDeviceName()+","+model.getModelName(), PmsConstants.ALARM_MESSAGE, user.getId(), user.getMobile());
+                            }
+                        }
+                    }
+                });
+            });
+        });
+
+        return "";
+    }
+}

+ 10 - 1
yudao-module-pms/yudao-module-pms-biz/src/main/java/cn/iocoder/yudao/module/pms/message/PmsMessage.java

@@ -58,6 +58,11 @@ public class PmsMessage {
             templateParams.put("name", name);
             templateParams.put("businessType", businessType);
             templateParams.put("businessId", businessId);
+            if (PmsConstants.ALARM_MESSAGE.equals(businessType)) {
+                String[] split = name.split(",");
+                templateParams.put("name", split[0]);
+                templateParams.put("identifier", split[1]);
+            }
         }
 
         notifyMessageSendApi.sendSingleMessageToAdmin(new NotifySendSingleToUserReqDTO()
@@ -80,6 +85,10 @@ public class PmsMessage {
             if (PmsConstants.FAILURE_NO_PASS.equals(businessType)) {
                 msg = name+"故障上报流程审批完成,没有生成维修工单,将设备的状态调整为观察使用。";
             }
+            if (PmsConstants.ALARM_MESSAGE.equals(businessType)) {
+                String[] split = name.split(",");
+                msg = split[0]+"的"+split[1]+"数采的数值近十分钟内不在设定区间内,请前往系统查看。";
+            }
             String url = "";
 
             if(PmsConstants.GENERATE_OPERATION.equals(businessType)){
@@ -94,7 +103,7 @@ public class PmsMessage {
                 url=systemUrl + "dingding?id="+businessId+"&type="+businessType+"&userId="+userId;
             }
 
-            if (PmsConstants.FAILURE_NO_PASS.equals(businessType)) {
+            if (PmsConstants.FAILURE_NO_PASS.equals(businessType)||PmsConstants.ALARM_MESSAGE.equals(businessType)) {
                 //通知设备状态变更为观察使用
                 dingtalkSendApi.send(mobile, msg, "text",url);
             } else {