#update
This commit is contained in:
@@ -3,6 +3,7 @@ package com.maibu.controller;
|
||||
import java.time.LocalDate;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.eclipse.paho.client.mqttv3.MqttException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
@@ -125,12 +126,12 @@ public class DeviceTaskController extends BaseController {
|
||||
}
|
||||
|
||||
@PostMapping("/cancelTask")
|
||||
public AjaxResult cancelTask(@RequestBody DeviceTaskCommandDTO dto) {
|
||||
public AjaxResult cancelTask(@RequestBody DeviceTaskCommandDTO dto) throws MqttException {
|
||||
String deviceId = dto.getDeviceId();
|
||||
if (StringUtils.isEmpty(deviceId) && dto.getTaskId() == null) {
|
||||
return AjaxResult.error("deviceId或taskId为空!");
|
||||
}
|
||||
return AjaxResult.success(deviceTaskService.cancelTask(dto, getUsername()));
|
||||
return AjaxResult.success(deviceTaskService.cancelTask(dto));
|
||||
}
|
||||
|
||||
@PostMapping("/pauseTask")
|
||||
|
||||
@@ -39,6 +39,9 @@ public class InitThread implements ApplicationRunner {
|
||||
@Resource(name = FastBeeConstant.TASK.DEVICE_ERROR_MONITOR)
|
||||
private Executor deviceErrorMonitorExecutor;
|
||||
|
||||
@Resource(name = FastBeeConstant.TASK.DEVICE_HEARTBEAT_MONITOR)
|
||||
private Executor deviceHeartBeatMonitorExecutor;
|
||||
|
||||
@Resource(name = FastBeeConstant.TASK.COMMON_DEVICE_MONITOR)
|
||||
private Executor commonDeviceMonitorExecutor;
|
||||
|
||||
@@ -124,6 +127,11 @@ public class InitThread implements ApplicationRunner {
|
||||
|
||||
GlobalMemory.initMqttDeviceMonitor(mqttHostUrl, mqttClientId, mqttUsername, mqttPassword);
|
||||
|
||||
// 上位机设备心跳监控
|
||||
deviceHeartBeatMonitorExecutor.execute(() -> {
|
||||
deviceThreadService.heartBeatMonitor();
|
||||
});
|
||||
|
||||
// 设备错误监听线程
|
||||
deviceErrorMonitorExecutor.execute(() -> {
|
||||
try {
|
||||
|
||||
@@ -11,6 +11,7 @@ import java.util.stream.Collectors;
|
||||
|
||||
import com.maibu.memory.DeviceSessionManager;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.eclipse.paho.client.mqttv3.MqttException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
@@ -476,7 +477,7 @@ public class DeviceTaskService {
|
||||
return task.getId();
|
||||
}
|
||||
|
||||
public boolean cancelTask(DeviceTaskCommandDTO dto, String username) {
|
||||
public boolean cancelTask(DeviceTaskCommandDTO dto) throws MqttException {
|
||||
String deviceId = dto.getDeviceId();
|
||||
DevicePlanTask devicePlanTask = null;
|
||||
if (StringUtils.isEmpty(dto.getDeviceId())) {
|
||||
@@ -488,7 +489,11 @@ public class DeviceTaskService {
|
||||
}
|
||||
NettyDevice device = deviceSessionManager.getDevice(deviceId);
|
||||
if (device != null && device.getTask() != null) {
|
||||
return device.cancelTask();
|
||||
if(dto.isForceCancel()){
|
||||
return device.canceledTask();
|
||||
}else {
|
||||
return device.cancelingTask();
|
||||
}
|
||||
} else {
|
||||
if (devicePlanTask != null) {
|
||||
devicePlanTask.setTaskStaus(DeviceTaskStaus.CANCELED);
|
||||
|
||||
@@ -14,12 +14,15 @@ import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import com.maibu.constant.Constant;
|
||||
import com.maibu.core.host.HeartBeatDTO;
|
||||
import com.maibu.memory.DeviceSessionManager;
|
||||
import com.maibu.utils.json.JsonUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.eclipse.paho.client.mqttv3.MqttException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.core.Constants;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -254,4 +257,41 @@ public class DeviceThreadService {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 监控心跳
|
||||
*/
|
||||
public void heartBeatMonitor() {
|
||||
|
||||
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
|
||||
executor.scheduleWithFixedDelay(() -> {
|
||||
try {
|
||||
Long nowTime = System.currentTimeMillis();
|
||||
List<HeartBeatDTO> heartBeatDTOS = GlobalMemory.customMqttDeviceMonitor.getAllHeartBeat();
|
||||
if (!CollectionUtils.isEmpty(heartBeatDTOS)) {
|
||||
heartBeatDTOS.forEach(x -> {
|
||||
Long lastActiveTime = x.getSysTimestamp();
|
||||
NettyDevice device = sessionManager.getDevice(x.getSn());
|
||||
if (nowTime - lastActiveTime >= Constant.hostOfflineInterval) {
|
||||
//超时了判断离线
|
||||
if (device != null) {
|
||||
device.status(0);
|
||||
//todo 推送设备离线
|
||||
|
||||
}
|
||||
} else {
|
||||
|
||||
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("执行监测心跳线程错误:{}", e.getMessage());
|
||||
}
|
||||
}, 0, 500, TimeUnit.MILLISECONDS);
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user