wisdomisite-java/src/main/java/com/zhgd/xmgl/task/DevExcavationTask.java

120 lines
5.3 KiB
Java
Raw Normal View History

2023-02-16 15:28:15 +08:00
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;
2024-01-20 10:18:46 +08:00
import lombok.extern.slf4j.Slf4j;
2023-02-16 15:28:15 +08:00
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
* 基坑定时器
*/
2024-01-20 10:18:46 +08:00
@Slf4j
2023-02-16 15:28:15 +08:00
@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");
}
}
}
2024-01-20 10:18:46 +08:00
}