|
@@ -9,14 +9,13 @@ import cn.hutool.core.util.ObjectUtil;
|
|
import cn.hutool.core.util.StrUtil;
|
|
import cn.hutool.core.util.StrUtil;
|
|
import cn.hutool.json.JSONObject;
|
|
import cn.hutool.json.JSONObject;
|
|
import cn.hutool.json.JSONUtil;
|
|
import cn.hutool.json.JSONUtil;
|
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
import com.xy.config.DeviceThreadPoolConfig;
|
|
import com.xy.config.DeviceThreadPoolConfig;
|
|
import com.xy.device.EnumDeviceOnlineStatus;
|
|
import com.xy.device.EnumDeviceOnlineStatus;
|
|
import com.xy.dto.*;
|
|
import com.xy.dto.*;
|
|
import com.xy.dto.be.MercDto;
|
|
import com.xy.dto.be.MercDto;
|
|
import com.xy.dto.be.MercUserDeviceDto;
|
|
import com.xy.dto.be.MercUserDeviceDto;
|
|
-import com.xy.entity.DeviceEventMsg;
|
|
|
|
-import com.xy.entity.DeviceInfo;
|
|
|
|
-import com.xy.entity.SysCodeConfigureRedis;
|
|
|
|
|
|
+import com.xy.entity.*;
|
|
import com.xy.enums.ChannelType;
|
|
import com.xy.enums.ChannelType;
|
|
import com.xy.enums.MsgConfigId;
|
|
import com.xy.enums.MsgConfigId;
|
|
import com.xy.enums.MsgType;
|
|
import com.xy.enums.MsgType;
|
|
@@ -27,16 +26,15 @@ import com.xy.utils.SysDictUtils;
|
|
import io.swagger.annotations.Api;
|
|
import io.swagger.annotations.Api;
|
|
import io.swagger.annotations.ApiOperation;
|
|
import io.swagger.annotations.ApiOperation;
|
|
import lombok.AllArgsConstructor;
|
|
import lombok.AllArgsConstructor;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.scheduling.annotation.Async;
|
|
import org.springframework.scheduling.annotation.Async;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.time.LocalDateTime;
|
|
import java.time.LocalDateTime;
|
|
-import java.util.ArrayList;
|
|
|
|
-import java.util.List;
|
|
|
|
-import java.util.Map;
|
|
|
|
-import java.util.Set;
|
|
|
|
|
|
+import java.util.*;
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
+@Slf4j
|
|
@Service
|
|
@Service
|
|
@AllArgsConstructor
|
|
@AllArgsConstructor
|
|
@Api(tags = "设备消费者")
|
|
@Api(tags = "设备消费者")
|
|
@@ -56,6 +54,10 @@ public class DeviceMqttConsumerImpl implements DeviceMqttConsumer {
|
|
|
|
|
|
private MsgSysMonitorNoticeService msgSysMonitorNoticeService;
|
|
private MsgSysMonitorNoticeService msgSysMonitorNoticeService;
|
|
|
|
|
|
|
|
+ private DeviceFaultLogServiceImpl deviceFaultLogService;
|
|
|
|
+
|
|
|
|
+ private DeviceFaultInfoServiceImpl deviceFaultInfoService;
|
|
|
|
+
|
|
@Override
|
|
@Override
|
|
@ApiOperation("设备在线")
|
|
@ApiOperation("设备在线")
|
|
@Async(DeviceThreadPoolConfig.DEVICE_NETWORK_POLL)
|
|
@Async(DeviceThreadPoolConfig.DEVICE_NETWORK_POLL)
|
|
@@ -196,6 +198,96 @@ public class DeviceMqttConsumerImpl implements DeviceMqttConsumer {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 设备故障
|
|
|
|
+ *
|
|
|
|
+ * @param requestParams
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ public void deviceFault(DeviceMqttDto.RequestParams requestParams) {
|
|
|
|
+ DeviceFaultLogDto.MqttData mqttData = JSONUtil.toBean(requestParams.getData(), DeviceFaultLogDto.MqttData.class);
|
|
|
|
+// log.info("设备故障监控事件消费,接收mqtt data:{}", JSONUtil.toJsonPrettyStr(mqttData));
|
|
|
|
+ if (mqttData != null) {
|
|
|
|
+ DeviceEventMsgDto.Vo deviceEventMsg = mqttData.getDeviceEventMsg();
|
|
|
|
+ Long mercId = deviceEventMsg.getMercId();
|
|
|
|
+ String code = deviceEventMsg.getCode();
|
|
|
|
+ Long deviceId = deviceEventMsg.getDeviceId();
|
|
|
|
+ String msg = deviceEventMsg.getMsg();
|
|
|
|
+ //加入故障日志
|
|
|
|
+ DeviceFaultLog log = new DeviceFaultLog()
|
|
|
|
+ .setMercId(mercId)
|
|
|
|
+ .setDescribes(msg)
|
|
|
|
+ .setEventCode(code)
|
|
|
|
+ .setEventTime(deviceEventMsg.getCreateTime())
|
|
|
|
+ .setDeviceId(deviceId);
|
|
|
|
+ deviceFaultLogService.save(log);
|
|
|
|
+ List<DeviceFaultMonitorConfigDto.Vo> configList = mqttData.getConfigList();
|
|
|
|
+
|
|
|
|
+ // 按故障级别排序 降序
|
|
|
|
+ List<DeviceFaultMonitorConfigDto.Vo> configSortedList = configList.stream()
|
|
|
|
+ .sorted(Comparator.comparing(DeviceFaultMonitorConfigDto.Vo::getFaultLevel).reversed())
|
|
|
|
+ .collect(Collectors.toList());
|
|
|
|
+ for (DeviceFaultMonitorConfigDto.Vo config : configSortedList) {
|
|
|
|
+ Integer eventNum = config.getEventNum();
|
|
|
|
+
|
|
|
|
+ Long minuteNum = config.getMinuteNum();
|
|
|
|
+
|
|
|
|
+ Integer faultLevel = config.getFaultLevel();
|
|
|
|
+
|
|
|
|
+ // 获取当前时间
|
|
|
|
+ Date now = DateUtil.date();
|
|
|
|
+
|
|
|
|
+ // 计算最近n分钟的开始时间
|
|
|
|
+ Date minutesAgo = DateUtil.offsetMinute(now, -minuteNum.intValue());
|
|
|
|
+
|
|
|
|
+ //统计同类事件累计次数
|
|
|
|
+ Long count = deviceFaultLogService.count(Wrappers.<DeviceFaultLog>lambdaQuery()
|
|
|
|
+ .eq(DeviceFaultLog::getDeviceId, deviceId)
|
|
|
|
+ .eq(DeviceFaultLog::getMercId, mercId)
|
|
|
|
+ .eq(DeviceFaultLog::getEventCode, code)
|
|
|
|
+ .ge(DeviceFaultLog::getEventTime, minutesAgo)
|
|
|
|
+ );
|
|
|
|
+ //达到阈值次数
|
|
|
|
+ if (count != null && count.intValue() >= eventNum) {
|
|
|
|
+ //故障表入库
|
|
|
|
+ //是否已存在 未解决的
|
|
|
|
+ DeviceFaultInfo deviceFaultInfo = deviceFaultInfoService.getOne(Wrappers.<DeviceFaultInfo>lambdaQuery()
|
|
|
|
+ .eq(DeviceFaultInfo::getDeviceId, deviceId)
|
|
|
|
+ .eq(DeviceFaultInfo::getMercId, mercId)
|
|
|
|
+ .eq(DeviceFaultInfo::getEventCode, code)
|
|
|
|
+ .eq(DeviceFaultInfo::getState, false)
|
|
|
|
+ );
|
|
|
|
+ if (deviceFaultInfo != null) {
|
|
|
|
+ Integer faultLevel1 = deviceFaultInfo.getFaultLevel();
|
|
|
|
+ if (faultLevel1.intValue() < faultLevel) {
|
|
|
|
+ //小于,当前故障级别,更新
|
|
|
|
+ deviceFaultInfo.setFaultLevel(faultLevel);
|
|
|
|
+ deviceInfoService.updateById(new DeviceInfo().setDeviceId(deviceId).setFaultLevel(faultLevel));
|
|
|
|
+ }
|
|
|
|
+ //累加次数
|
|
|
|
+ deviceFaultInfo.setEventNum(deviceFaultInfo.getEventNum() + 1);
|
|
|
|
+ deviceFaultInfoService.updateById(deviceFaultInfo);
|
|
|
|
+ } else {
|
|
|
|
+ deviceFaultInfoService.save(new DeviceFaultInfo()
|
|
|
|
+ .setMercId(mercId)
|
|
|
|
+ .setDeviceId(deviceId)
|
|
|
|
+ .setDescribes(msg)
|
|
|
|
+ .setFaultLevel(faultLevel)
|
|
|
|
+ .setEventCode(code)
|
|
|
|
+ .setEventNum(count.intValue())
|
|
|
|
+ );
|
|
|
|
+ deviceInfoService.updateById(new DeviceInfo().setDeviceId(deviceId).setFaultLevel(faultLevel));
|
|
|
|
+ }
|
|
|
|
+ //已按最大故障级别排序,所以满足条件就退出循环
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 设备联网状态
|
|
* 设备联网状态
|
|
*
|
|
*
|