wisdomisite-java/src/main/java/com/zhgd/xmgl/task/DevExcavationTask.java
2023-02-16 15:28:15 +08:00

118 lines
5.3 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.zhgd.xmgl.task;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.zhgd.xmgl.async.AsyncCommon;
import com.zhgd.xmgl.async.AsyncDevExcavation;
import com.zhgd.xmgl.modules.bigdevice.entity.DevMonitorPlugin;
import com.zhgd.xmgl.modules.bigdevice.enums.DeviceStateEnum;
import com.zhgd.xmgl.modules.bigdevice.service.DevMonitorPluginService;
import com.zhgd.xmgl.modules.foundation.entity.DeepExcavationCurrentData;
import com.zhgd.xmgl.modules.foundation.entity.DeepExcavationSensor;
import com.zhgd.xmgl.modules.foundation.mapper.DeepExcavationSensorMapper;
import com.zhgd.xmgl.modules.foundation.service.IDeepExcavationCurrentDataService;
import lombok.extern.log4j.Log4j;
import net.javacrumbs.shedlock.core.SchedulerLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* @author 邱平毅
* @ClassName DevExcavationTask
* @date 2022/8/29 14:44
* @Version 1.0
* 基坑定时器
*/
@Log4j
@Component
public class DevExcavationTask {
@Autowired
IDeepExcavationCurrentDataService deepExcavationCurrentDataService;
@Autowired
DeepExcavationSensorMapper deepExcavationSensorMapper;
@Autowired
AsyncDevExcavation asyncDevExcavation;
@Autowired
private DevMonitorPluginService devMonitorPluginService;
@Autowired
AsyncCommon asyncCommon;
/**
* 每个小时15分时判断设备是否下线
*/
@SchedulerLock(name = "deepExcavationDropped", lockAtMostFor = 1000 * 60 * 60, lockAtLeastFor = 1000 * 60 * 5)
@Scheduled(cron = "0 15 * * * ?")
public void deepExcavationDropped() {
List<DeepExcavationCurrentData> allEndDataList = deepExcavationCurrentDataService.getAllEndData().stream().filter(data -> data.getSensorSn() != null).collect(Collectors.toList());
List<DeepExcavationSensor> allSensorList = deepExcavationSensorMapper.getListBySensorSnSet(null);
Map<String, DeepExcavationSensor> sensorMap = allSensorList.stream().collect(Collectors.toMap(DeepExcavationSensor::getSensorSn, Function.identity()));
// 获取有数据的编号以及全部传感器编号,取出没有数据的传感器编号
Set<String> haveDataSensorSnSet = allEndDataList.stream().map(DeepExcavationCurrentData::getSensorSn).collect(Collectors.toSet());
Set<String> allSensorSnSet = allSensorList.stream().map(DeepExcavationSensor::getSensorSn).collect(Collectors.toSet());
Set<String> droppedSnSet = allSensorSnSet.stream().filter(sensorSn -> !haveDataSensorSnSet.contains(sensorSn)).collect(Collectors.toSet());
// TODO 刚新增的传感器也会报错,是否需要判断
// Set<String> droppedSnSet = new LinkedHashSet<>();
for (DeepExcavationCurrentData currentData : allEndDataList) {
String receiveTime = currentData.getReceiveTime();
String sensorSn = currentData.getSensorSn();
if (receiveTime == null) {
droppedSnSet.add(sensorSn);
continue;
}
long between = DateUtil.between(new DateTime(), DateUtil.parse(receiveTime), DateUnit.DAY);
if (between >= 1) {
droppedSnSet.add(sensorSn);
}
}
List<DeepExcavationSensor> droppedSensorList = sensorMap.entrySet().stream().filter(sensor -> droppedSnSet.contains(sensor.getKey())).map(Map.Entry::getValue).collect(Collectors.toList());
asyncDevExcavation.sendDroppedSensor(droppedSensorList);
}
/**
* 每5分时判断插件是否下线
*/
@SchedulerLock(name = "pluginExcavationDropped", lockAtMostFor = 1000 * 60 * 2, lockAtLeastFor = 1000 * 60 * 2)
@Scheduled(cron = "0 */5 * * * ?")
public void pluginExcavationDropped() {
List<DevMonitorPlugin> pluginList = devMonitorPluginService.list();
List<DevMonitorPlugin> droppedList = new LinkedList<>();
for (DevMonitorPlugin plugin : pluginList) {
String lastDataTime = plugin.getLastDataTime();
// 是否之前就没有掉线
boolean isUpdateState = !Objects.equals(plugin.getDeviceState(), DeviceStateEnum.OFF_LINE.getStatus());
if (isUpdateState) {
//
boolean isPluginDropped = lastDataTime == null || (DateUtil.between(new DateTime(), DateUtil.parse(lastDataTime), DateUnit.MINUTE) >= 3);
if (isPluginDropped) {
plugin.setDeviceState(DeviceStateEnum.OFF_LINE.getStatus());
droppedList.add(plugin);
}
}
}
if (CollUtil.isNotEmpty(droppedList)) {
devMonitorPluginService.updateBatchById(droppedList);
String title = "设备监测插件通知";
for (DevMonitorPlugin plugin : droppedList) {
asyncCommon.sendMqAndApp(title, "名称为:" + plugin.getName() + "的插件及相同ip设备Ai主机" + DeviceStateEnum.getByStatus(plugin.getDeviceState()).getStatusName(), title, plugin.getProjectSn(), "/pages/projectEnd/projectIndex/projectIndex");
}
}
}
}