Files
MiddlePlatform/maibu-netty-server/src/main/java/com/maibu/init/InitThread.java

150 lines
5.4 KiB
Java
Raw Normal View History

2026-04-17 17:12:41 +08:00
package com.maibu.init;
2026-04-17 10:42:16 +08:00
2026-04-17 17:12:41 +08:00
import com.maibu.constant.FastBeeConstant;
2026-04-30 15:25:58 +08:00
import com.maibu.core.business.DevicePlan;
2026-06-03 16:53:02 +08:00
import com.maibu.core.business.DevicePlanTask;
import com.maibu.core.domain.entity.SysSite;
2026-04-30 15:25:58 +08:00
import com.maibu.core.enums.DeviceTaskStaus;
2026-04-17 17:12:41 +08:00
import com.maibu.mapper.DevicePlanMapper;
2026-06-03 16:53:02 +08:00
import com.maibu.mapper.DevicePlanTaskMapper;
import com.maibu.mapper.SysSiteMapper;
2026-04-17 17:12:41 +08:00
import com.maibu.mapper.SysUserClientMapper;
2026-06-03 16:53:02 +08:00
import com.maibu.memory.GlobalMemory;
2026-04-30 15:25:58 +08:00
import com.maibu.memory.SiteMemory;
2026-04-17 17:12:41 +08:00
import com.maibu.mybatis.LambdaQueryWrapperX;
import com.maibu.service.DevicePlanTaskMonitorService;
import com.maibu.service.DeviceThreadService;
2026-04-17 10:42:16 +08:00
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
2026-04-21 09:20:55 +08:00
import org.springframework.util.CollectionUtils;
2026-04-17 10:42:16 +08:00
import javax.annotation.Resource;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Executor;
@Slf4j
@Component
@Order(20)
public class InitThread implements ApplicationRunner {
@Resource(name = FastBeeConstant.TASK.DEVICE_ERROR_MONITOR)
private Executor deviceErrorMonitorExecutor;
@Resource(name = FastBeeConstant.TASK.DEVICE_TASK_HANDLER)
private Executor deviceTaskHandlerExecutor;
@Autowired
private DeviceThreadService deviceThreadService;
@Autowired
private DevicePlanTaskMonitorService devicePlanTaskMonitorService;
@Autowired
private DevicePlanMapper devicePlanMapper;
@Autowired
2026-06-03 16:53:02 +08:00
private DevicePlanTaskMapper devicePlanTaskMapper;
@Autowired
private SysSiteMapper sysSiteMapper;
2026-04-17 10:42:16 +08:00
@Override
public void run(ApplicationArguments args) {
recoverMemory();
init();
}
private void init() {
//设备错误监听线程
deviceErrorMonitorExecutor.execute(() -> {
try {
deviceThreadService.deviceErrorMonitor();
} catch (InterruptedException | IOException e) {
throw new RuntimeException(e);
}
});
//设备任务处理线程
deviceTaskHandlerExecutor.execute(() -> {
//任务下发
try {
devicePlanTaskMonitorService.generatePlanTask();
//执行准备队列
devicePlanTaskMonitorService.doLoop();
//执行任务
devicePlanTaskMonitorService.doExecute();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
}
/**
* 恢复内存
*/
2026-06-03 16:53:02 +08:00
public void recoverMemory() {
2026-04-17 10:42:16 +08:00
//todo 启动时清除当前登录记录
// System.out.println("开始清除登录信息");
// sysUserClientMapper.clearDeviceName();
2026-06-03 16:53:02 +08:00
List<SysSite> siteList = sysSiteMapper.selectList();
if (!CollectionUtils.isEmpty(siteList)) {
siteList.forEach(x -> {
Long orgId = x.getOrgId();
Long siteId = x.getId();
SiteMemory siteMemory = GlobalMemory.getSiteMemory(orgId, siteId);
if (siteMemory == null) {
siteMemory = new SiteMemory();
GlobalMemory.addSiteMemory(orgId, siteId, siteMemory);
}
});
}
2026-04-17 10:42:16 +08:00
// 未执行完的重复计划
LambdaQueryWrapperX<DevicePlan> query = new LambdaQueryWrapperX<>();
2026-06-03 16:53:02 +08:00
List<String> list = Arrays.asList(DeviceTaskStaus.NEW.getCode(), DeviceTaskStaus.EXECUTING.getCode(), DeviceTaskStaus.PAUSE.getCode());
2026-04-17 10:42:16 +08:00
query.in(DevicePlan::getTaskStaus, list);
List<DevicePlan> devicePlanList = devicePlanMapper.selectList(query);
2026-06-03 16:53:02 +08:00
if (!CollectionUtils.isEmpty(devicePlanList)) {
devicePlanList.forEach(x -> {
Long orgId = x.getOrgId();
Long siteId = x.getSiteId();
SiteMemory siteMemory = GlobalMemory.getSiteMemory(orgId, siteId);
siteMemory.addDevicePlan(x);
});
2026-04-17 10:42:16 +08:00
}
2026-06-03 16:53:02 +08:00
// 未执行完的重复计划
LambdaQueryWrapperX<DevicePlanTask> taskQuery = new LambdaQueryWrapperX<>();
taskQuery.eq(DevicePlanTask::getTaskStaus, DeviceTaskStaus.NEW.getCode());
List<DevicePlanTask> newStatusList = devicePlanTaskMapper.selectList(taskQuery);
if (!CollectionUtils.isEmpty(newStatusList)) {
newStatusList.forEach(x -> {
Long orgId = x.getOrgId();
Long siteId = x.getSiteId();
SiteMemory siteMemory = GlobalMemory.getSiteMemory(orgId, siteId);
siteMemory.addDevicePlanPrepare(x);
});
}
LambdaQueryWrapperX<DevicePlanTask> executeTaskQuery = new LambdaQueryWrapperX<>();
executeTaskQuery.eq(DevicePlanTask::getTaskStaus, DeviceTaskStaus.EXECUTING.getCode());
List<DevicePlanTask> executeStatusList = devicePlanTaskMapper.selectList(executeTaskQuery);
if (!CollectionUtils.isEmpty(executeStatusList)) {
executeStatusList.forEach(x -> {
Long orgId = x.getOrgId();
Long siteId = x.getSiteId();
SiteMemory siteMemory = GlobalMemory.getSiteMemory(orgId, siteId);
siteMemory.addDevicePlanExecute(x);
});
}
2026-04-17 10:42:16 +08:00
}
}