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

313 lines
11 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-06-17 09:14:42 +08:00
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Executor;
import javax.annotation.Resource;
2026-08-05 13:01:59 +08:00
import com.maibu.core.business.*;
import com.maibu.mapper.*;
2026-08-03 14:46:36 +08:00
import com.maibu.service.impl.IotDeviceCommonService;
2026-06-17 09:14:42 +08:00
import org.eclipse.paho.client.mqttv3.MqttException;
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;
import org.springframework.util.CollectionUtils;
2026-04-17 17:12:41 +08:00
import com.maibu.constant.FastBeeConstant;
2026-06-03 16:53:02 +08:00
import com.maibu.core.domain.entity.SysSite;
2026-04-30 15:25:58 +08:00
import com.maibu.core.enums.DeviceTaskStaus;
2026-06-17 09:14:42 +08:00
import com.maibu.influxdb.util.InfluxDBUtil;
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-06-17 09:14:42 +08:00
import com.maibu.mqtt.MqttClientUtil;
2026-04-17 17:12:41 +08:00
import com.maibu.mybatis.LambdaQueryWrapperX;
import com.maibu.service.DevicePlanTaskMonitorService;
import com.maibu.service.DeviceThreadService;
2026-06-17 09:14:42 +08:00
import org.springframework.beans.factory.annotation.Value;
2026-04-17 10:42:16 +08:00
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Component
@Order(20)
public class InitThread implements ApplicationRunner {
@Resource(name = FastBeeConstant.TASK.DEVICE_ERROR_MONITOR)
private Executor deviceErrorMonitorExecutor;
2026-08-19 11:02:52 +08:00
@Resource(name = FastBeeConstant.TASK.DEVICE_HEARTBEAT_MONITOR)
private Executor deviceHeartBeatMonitorExecutor;
2026-08-03 14:46:36 +08:00
@Resource(name = FastBeeConstant.TASK.COMMON_DEVICE_MONITOR)
private Executor commonDeviceMonitorExecutor;
2026-04-17 10:42:16 +08:00
@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
2026-07-09 08:40:32 +08:00
@Autowired
private DeviceRunParamMapper deviceRunParamMapper;
2026-08-05 13:01:59 +08:00
@Autowired
private IotDeviceCommonMapper iotDeviceCommonMapper;
@Autowired
private DeviceProductMapper deviceProductMapper;
@Autowired
private SysDeviceModelMapper deviceModelMapper;
2026-07-14 12:43:23 +08:00
@Autowired
private ErrorIdentificationStandardMapper errorIdentificationStandardMapper;
@Autowired
private OrderModelMapper orderModelMapper;
@Autowired
private AlarmOrderConfigMapper alarmOrderConfigMapper;
2026-07-09 08:40:32 +08:00
2026-08-03 14:46:36 +08:00
@Autowired
private IotDeviceCommonService commonService;
2026-06-17 09:14:42 +08:00
@Value("${influxdb.url}")
private String influxDBUrl;
@Value("${influxdb.username}")
private String influxDBUsername;
@Value("${influxdb.password}")
private String influxDBPassword;
@Value("${influxdb.bucket}")
private String influxDBBucket;
@Value("${influxdb.org}")
private String influxDBOrg;
@Value("${influxdb.token}")
private String influxDBToken;
@Value("${mqtt.username}")
private String mqttUsername;
@Value("${mqtt.password}")
private String mqttPassword;
@Value("${mqtt.host-url}")
private String mqttHostUrl;
@Value("${mqtt.client-id}")
private String mqttClientId;
2026-04-17 10:42:16 +08:00
@Override
2026-06-17 09:14:42 +08:00
public void run(ApplicationArguments args) throws MqttException {
2026-04-17 10:42:16 +08:00
recoverMemory();
init();
}
2026-06-17 09:14:42 +08:00
private void init() throws MqttException {
// 初始化InfluxDBUtil
GlobalMemory.influxDBUtil = new InfluxDBUtil(influxDBUrl, influxDBToken, influxDBOrg, influxDBBucket);
2026-08-07 08:45:43 +08:00
GlobalMemory.initMqttDeviceMonitor(mqttHostUrl, mqttClientId, mqttUsername, mqttPassword);
2026-08-19 11:02:52 +08:00
// 上位机设备心跳监控
deviceHeartBeatMonitorExecutor.execute(() -> {
deviceThreadService.heartBeatMonitor();
});
2026-06-17 09:14:42 +08:00
// 设备错误监听线程
2026-04-17 10:42:16 +08:00
deviceErrorMonitorExecutor.execute(() -> {
try {
deviceThreadService.deviceErrorMonitor();
} catch (InterruptedException | IOException e) {
throw new RuntimeException(e);
}
});
2026-06-17 09:14:42 +08:00
// 设备任务处理线程
2026-04-17 10:42:16 +08:00
deviceTaskHandlerExecutor.execute(() -> {
2026-06-17 09:14:42 +08:00
// 任务下发
2026-04-17 10:42:16 +08:00
try {
devicePlanTaskMonitorService.generatePlanTask();
2026-06-17 09:14:42 +08:00
// 执行准备队列
2026-04-17 10:42:16 +08:00
devicePlanTaskMonitorService.doLoop();
2026-06-17 09:14:42 +08:00
// 执行任务
2026-04-17 10:42:16 +08:00
devicePlanTaskMonitorService.doExecute();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
2026-08-05 13:01:59 +08:00
commonDeviceMonitorExecutor.execute(() -> {
commonService.commonDeviceMonitor();
});
2026-04-17 10:42:16 +08:00
}
/**
* 恢复内存
*/
2026-06-03 16:53:02 +08:00
public void recoverMemory() {
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();
2026-07-09 08:40:32 +08:00
siteMemory.setSiteId(siteId);
siteMemory.setSysSite(x);
2026-06-03 16:53:02 +08:00
GlobalMemory.addSiteMemory(orgId, siteId, siteMemory);
}
});
}
2026-08-05 13:01:59 +08:00
//外部设备
List<IoTCommonDevice> ioTCommonDeviceList = iotDeviceCommonMapper.selectList();
if (!CollectionUtils.isEmpty(ioTCommonDeviceList)) {
ioTCommonDeviceList.forEach(x -> {
Long orgId = x.getOrgId();
Long siteId = x.getSiteId();
SiteMemory siteMemory = GlobalMemory.getSiteMemory(orgId, siteId);
if (siteMemory != null) {
siteMemory.saveCommonDevice(x);
}
});
}
//产品
List<Product> productList = deviceProductMapper.selectList();
if (!CollectionUtils.isEmpty(productList)) {
productList.forEach(x -> {
Long orgId = x.getOrgId();
Long siteId = x.getSiteId();
SiteMemory siteMemory = GlobalMemory.getSiteMemory(orgId, siteId);
if (siteMemory != null) {
siteMemory.saveProduct(x);
}
});
}
//设备模型
List<DeviceModel> deviceModelList = deviceModelMapper.selectList();
if (!CollectionUtils.isEmpty(deviceModelList)) {
deviceModelList.forEach(x -> {
Long orgId = x.getOrgId();
Long siteId = x.getSiteId();
SiteMemory siteMemory = GlobalMemory.getSiteMemory(orgId, siteId);
if (siteMemory != null) {
siteMemory.saveDeviceModel(x);
}
});
}
//车辆参数
2026-07-09 08:40:32 +08:00
List<DeviceRunParam> deviceRunParamList = deviceRunParamMapper.selectList();
if (!CollectionUtils.isEmpty(deviceRunParamList)) {
deviceRunParamList.forEach(x -> {
Long orgId = x.getOrgId();
2026-07-29 13:08:49 +08:00
Long siteId = x.getSiteId();
2026-07-09 08:40:32 +08:00
SiteMemory siteMemory = GlobalMemory.getSiteMemory(orgId, siteId);
if (siteMemory != null) {
siteMemory.saveDeviceRunParam(x);
}
});
}
2026-08-05 13:01:59 +08:00
//错误标准
2026-07-14 12:43:23 +08:00
List<ErrorIdentificationStandard> standadList = errorIdentificationStandardMapper.selectList();
if (!CollectionUtils.isEmpty(standadList)) {
standadList.forEach(x -> {
Long orgId = x.getOrgId();
2026-07-29 13:08:49 +08:00
Long siteId = x.getSiteId();
2026-07-14 12:43:23 +08:00
SiteMemory siteMemory = GlobalMemory.getSiteMemory(orgId, siteId);
if (siteMemory != null) {
siteMemory.saveErrorIdentificationStandard(x);
}
});
}
2026-08-05 13:01:59 +08:00
//订单模型
2026-07-14 12:43:23 +08:00
List<OrderModel> orderModelList = orderModelMapper.selectList();
if (!CollectionUtils.isEmpty(orderModelList)) {
orderModelList.forEach(x -> {
Long orgId = x.getOrgId();
2026-07-29 13:08:49 +08:00
Long siteId = x.getSiteId();
2026-07-14 12:43:23 +08:00
SiteMemory siteMemory = GlobalMemory.getSiteMemory(orgId, siteId);
if (siteMemory != null) {
siteMemory.saveOrderModel(x);
}
});
}
2026-08-05 13:01:59 +08:00
//告警订单配置
2026-07-14 12:43:23 +08:00
List<AlarmOrderConfig> alarmOrderList = alarmOrderConfigMapper.selectList();
if (!CollectionUtils.isEmpty(alarmOrderList)) {
alarmOrderList.forEach(x -> {
Long orgId = x.getOrgId();
2026-07-29 13:08:49 +08:00
Long siteId = x.getSiteId();
2026-07-14 12:43:23 +08:00
SiteMemory siteMemory = GlobalMemory.getSiteMemory(orgId, siteId);
if (siteMemory != null) {
siteMemory.saveAlarmOrderConfig(x);
}
});
}
2026-04-17 10:42:16 +08:00
// 未执行完的重复计划
LambdaQueryWrapperX<DevicePlan> query = new LambdaQueryWrapperX<>();
2026-06-17 09:14:42 +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);
2026-06-17 09:14:42 +08:00
if (siteMemory != null) {
2026-06-05 08:51:12 +08:00
siteMemory.addDevicePlan(x);
}
2026-06-03 16:53:02 +08:00
});
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);
2026-06-17 09:14:42 +08:00
if (siteMemory != null) {
2026-06-05 08:51:12 +08:00
siteMemory.addDevicePlanPrepare(x);
}
2026-06-03 16:53:02 +08:00
});
}
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);
2026-06-17 09:14:42 +08:00
if (siteMemory != null) {
2026-06-05 08:51:12 +08:00
siteMemory.addDevicePlanExecute(x);
}
2026-06-03 16:53:02 +08:00
});
}
2026-04-17 10:42:16 +08:00
}
}