This commit is contained in:
2026-08-19 11:02:52 +08:00
parent ffd9720939
commit 1b19e32aa5
30 changed files with 629 additions and 156 deletions

View File

@@ -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);
}
}