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

461 lines
17 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;
2026-10-12 15:40:23 +08:00
import java.util.ArrayList;
2026-06-17 09:14:42 +08:00
import java.util.Arrays;
import java.util.List;
2026-10-12 15:40:23 +08:00
import java.util.Map;
2026-10-27 17:08:27 +08:00
import java.util.Objects;
2026-06-17 09:14:42 +08:00
import java.util.concurrent.Executor;
2026-10-12 15:40:23 +08:00
import java.util.stream.Collectors;
2026-06-17 09:14:42 +08:00
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
2026-10-12 15:40:23 +08:00
import org.springframework.beans.factory.annotation.Value;
2026-06-17 09:14:42 +08:00
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
2026-10-12 15:40:23 +08:00
import org.springframework.core.io.ResourceLoader;
2026-06-17 09:14:42 +08:00
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
2026-10-12 15:40:23 +08:00
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
2026-04-17 17:12:41 +08:00
import com.maibu.constant.FastBeeConstant;
2026-10-12 15:40:23 +08:00
import com.maibu.core.business.AlarmOrderConfig;
import com.maibu.core.business.DeviceModel;
import com.maibu.core.business.DevicePlan;
import com.maibu.core.business.DevicePlanTask;
import com.maibu.core.business.DeviceRunParam;
import com.maibu.core.business.ErrorIdentificationStandard;
import com.maibu.core.business.IoTCommonDevice;
import com.maibu.core.business.OrderModel;
import com.maibu.core.business.Product;
2026-10-27 17:08:27 +08:00
import com.maibu.core.business.WorkRecord;
2026-10-12 15:40:23 +08:00
import com.maibu.core.domain.entity.SysParam;
2026-06-03 16:53:02 +08:00
import com.maibu.core.domain.entity.SysSite;
2026-10-27 17:08:27 +08:00
import com.maibu.core.domain.map.SiteMapInfo;
2026-04-30 15:25:58 +08:00
import com.maibu.core.enums.DeviceTaskStaus;
2026-10-27 17:08:27 +08:00
import com.maibu.core.enums.RouteType;
2026-06-17 09:14:42 +08:00
import com.maibu.influxdb.util.InfluxDBUtil;
2026-10-12 15:40:23 +08:00
import com.maibu.mapper.AlarmOrderConfigMapper;
import com.maibu.mapper.DevicePlanMapper;
import com.maibu.mapper.DevicePlanTaskMapper;
import com.maibu.mapper.DeviceProductMapper;
import com.maibu.mapper.DeviceRunParamMapper;
import com.maibu.mapper.ErrorIdentificationStandardMapper;
import com.maibu.mapper.IotDeviceCommonMapper;
import com.maibu.mapper.OrderModelMapper;
2026-10-27 17:08:27 +08:00
import com.maibu.mapper.SiteMapInfoMapper;
2026-10-12 15:40:23 +08:00
import com.maibu.mapper.SysDeviceModelMapper;
import com.maibu.mapper.SysParamMapper;
import com.maibu.mapper.SysSiteMapper;
2026-10-27 17:08:27 +08:00
import com.maibu.mapper.WorkRecordMapper;
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-10-12 15:40:23 +08:00
import com.maibu.service.impl.IotDeviceCommonService;
import cn.hutool.core.io.IoUtil;
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-09-14 10:37:27 +08:00
@Resource(name = FastBeeConstant.TASK.MAP_MESSAGE_PUSH)
private Executor mapMessagePushExecutor;
2026-10-27 17:08:27 +08:00
@Resource(name = FastBeeConstant.TASK.AUTO_RULE)
private Executor autoRuleExecutor;
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-10-27 17:08:27 +08:00
@Autowired
private SiteMapInfoMapper siteMapInfoMapper;
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
2026-10-27 17:08:27 +08:00
2026-07-14 12:43:23 +08:00
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-10-12 15:40:23 +08:00
@Autowired
private ResourceLoader resourceLoader;
@Autowired
private SysParamMapper sysParamMapper;
2026-10-27 17:08:27 +08:00
@Autowired
private WorkRecordMapper workRecordMapper;
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-10-12 15:40:23 +08:00
public void run(ApplicationArguments args) throws Exception {
2026-04-17 10:42:16 +08:00
recoverMemory();
init();
}
2026-10-12 15:40:23 +08:00
private void init() throws Exception {
2026-10-27 17:08:27 +08:00
// 初始化系统参数
2026-10-12 15:40:23 +08:00
initSystemParam();
2026-10-27 17:08:27 +08:00
initDefaultWorkRecord();
2026-06-17 09:14:42 +08:00
// 初始化InfluxDBUtil
GlobalMemory.influxDBUtil = new InfluxDBUtil(influxDBUrl, influxDBToken, influxDBOrg, influxDBBucket);
2026-08-07 08:45:43 +08:00
GlobalMemory.initMqttDeviceMonitor(mqttHostUrl, mqttClientId, mqttUsername, mqttPassword);
2026-10-27 17:08:27 +08:00
// 规则触发线程
autoRuleExecutor.execute(() -> {
deviceThreadService.autoRule();
});
2026-09-14 10:37:27 +08:00
// 地图监控推送
mapMessagePushExecutor.execute(() -> {
deviceThreadService.mapMessagePush();
});
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
2026-10-27 17:08:27 +08:00
// 场站的 地图配置信息
List<SiteMapInfo> siteMapInfoList = siteMapInfoMapper.selectList();
if (!CollectionUtils.isEmpty(siteMapInfoList)) {
siteMapInfoList.forEach(x -> {
Long orgId = x.getOrgId();
Long siteId = x.getSiteId();
SiteMemory siteMemory = GlobalMemory.getSiteMemory(orgId, siteId);
if (siteMemory != null) {
siteMemory.getSysSite().setSiteMapInfo(x);
}
});
}
2026-10-12 15:40:23 +08:00
// 外部设备
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);
}
});
}
2026-10-12 15:40:23 +08:00
// 产品
2026-08-05 13:01:59 +08:00
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);
}
});
}
2026-10-12 15:40:23 +08:00
// 设备模型
2026-08-05 13:01:59 +08:00
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-10-12 15:40:23 +08:00
// 车辆参数
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-10-12 15:40:23 +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-10-12 15:40:23 +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-10-12 15:40:23 +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
}
2026-10-12 15:40:23 +08:00
public void initSystemParam() throws IOException {
LambdaQueryWrapper<SysParam> queryWrapper = new LambdaQueryWrapper<>();
List<SysParam> list = sysParamMapper.selectList(queryWrapper);
try {
// 读取resource目录下的dsystemParam.json文件
org.springframework.core.io.Resource resource = resourceLoader
.getResource("classpath:systemParam.json");
// 使用fastjson2转换为DeviceConfig对象
String jsonContent;
jsonContent = IoUtil.readUtf8(resource.getInputStream());
List<SysParam> params = JSON.parseArray(jsonContent, SysParam.class);
List<SysParam> saveList = new ArrayList<>();
if (CollectionUtils.isEmpty(list)) {
saveList.addAll(params);
} else {
Map<String, SysParam> map = list.stream()
.collect(Collectors.toMap(
SysParam::getParamKey,
sysParam -> sysParam));
if (!CollectionUtils.isEmpty(params)) {
params.forEach(x -> {
if (!map.containsKey(x.getParamKey())) {
saveList.add(x);
}
});
}
}
sysParamMapper.saveOrUpdateBatch(saveList);
} catch (Exception e) {
log.error("初始化系统参数失败", e);
}
}
2026-10-27 17:08:27 +08:00
public void initDefaultWorkRecord() throws IOException {
LambdaQueryWrapper<WorkRecord> queryWrapper = new LambdaQueryWrapper<>();
List<WorkRecord> list = workRecordMapper.selectList(queryWrapper);
try {
// 读取resource目录下的dsystemParam.json文件
org.springframework.core.io.Resource resource = resourceLoader
.getResource("classpath:workRecordTemplete.json");
// 使用fastjson2转换为DeviceConfig对象
String jsonContent;
jsonContent = IoUtil.readUtf8(resource.getInputStream());
List<WorkRecord> records = JSON.parseArray(jsonContent, WorkRecord.class);
List<WorkRecord> saveList = new ArrayList<>();
if (CollectionUtils.isEmpty(list)) {
saveList.addAll(records);
} else {
Map<String, List<WorkRecord>> map = list.stream()
.filter(Objects::nonNull)
.filter(workRecord -> workRecord.getRouteType() != null)
.collect(Collectors.groupingBy(
workRecord -> workRecord.getRouteType().toString()));
if (!CollectionUtils.isEmpty(records)) {
records.forEach(x -> {
if (!map.containsKey(x.getRouteType())) {
x.setUserId(1L);
saveList.add(x);
}
});
}
}
workRecordMapper.saveOrUpdateBatch(saveList);
} catch (Exception e) {
log.error("初始化系统参数失败", e);
}
}
2026-04-17 10:42:16 +08:00
}