#增加系统参数配置模块

This commit is contained in:
2026-10-12 15:40:23 +08:00
parent 13eefaf152
commit bde6b89965
12 changed files with 444 additions and 53 deletions

View File

@@ -1,34 +1,64 @@
package com.maibu.init;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import com.maibu.core.business.*;
import com.maibu.mapper.*;
import com.maibu.service.impl.IotDeviceCommonService;
import org.checkerframework.checker.units.qual.s;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.maibu.constant.FastBeeConstant;
import com.maibu.core.business.AlarmOrderConfig;
import com.maibu.core.business.Device;
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;
import com.maibu.core.domain.entity.SysParam;
import com.maibu.core.domain.entity.SysSite;
import com.maibu.core.enums.DeviceTaskStaus;
import com.maibu.influxdb.util.InfluxDBUtil;
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;
import com.maibu.mapper.SysDeviceModelMapper;
import com.maibu.mapper.SysParamMapper;
import com.maibu.mapper.SysSiteMapper;
import com.maibu.memory.GlobalMemory;
import com.maibu.memory.SiteMemory;
import com.maibu.mqtt.MqttClientUtil;
import com.maibu.mybatis.LambdaQueryWrapperX;
import com.maibu.service.DevicePlanTaskMonitorService;
import com.maibu.service.DeviceThreadService;
import org.springframework.beans.factory.annotation.Value;
import com.maibu.service.impl.IotDeviceCommonService;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.io.IoUtil;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@@ -81,6 +111,7 @@ public class InitThread implements ApplicationRunner {
@Autowired
private ErrorIdentificationStandardMapper errorIdentificationStandardMapper;
@Autowired
private OrderModelMapper orderModelMapper;
@Autowired
private AlarmOrderConfigMapper alarmOrderConfigMapper;
@@ -88,6 +119,12 @@ public class InitThread implements ApplicationRunner {
@Autowired
private IotDeviceCommonService commonService;
@Autowired
private ResourceLoader resourceLoader;
@Autowired
private SysParamMapper sysParamMapper;
@Value("${influxdb.url}")
private String influxDBUrl;
@@ -119,12 +156,15 @@ public class InitThread implements ApplicationRunner {
private String mqttClientId;
@Override
public void run(ApplicationArguments args) throws MqttException {
public void run(ApplicationArguments args) throws Exception {
recoverMemory();
init();
}
private void init() throws MqttException {
private void init() throws Exception {
//初始化系统参数
initSystemParam();
// 初始化InfluxDBUtil
GlobalMemory.influxDBUtil = new InfluxDBUtil(influxDBUrl, influxDBToken, influxDBOrg, influxDBBucket);
@@ -185,7 +225,7 @@ public class InitThread implements ApplicationRunner {
});
}
//外部设备
// 外部设备
List<IoTCommonDevice> ioTCommonDeviceList = iotDeviceCommonMapper.selectList();
if (!CollectionUtils.isEmpty(ioTCommonDeviceList)) {
ioTCommonDeviceList.forEach(x -> {
@@ -197,7 +237,7 @@ public class InitThread implements ApplicationRunner {
}
});
}
//产品
// 产品
List<Product> productList = deviceProductMapper.selectList();
if (!CollectionUtils.isEmpty(productList)) {
productList.forEach(x -> {
@@ -209,7 +249,7 @@ public class InitThread implements ApplicationRunner {
}
});
}
//设备模型
// 设备模型
List<DeviceModel> deviceModelList = deviceModelMapper.selectList();
if (!CollectionUtils.isEmpty(deviceModelList)) {
deviceModelList.forEach(x -> {
@@ -221,7 +261,7 @@ public class InitThread implements ApplicationRunner {
}
});
}
//车辆参数
// 车辆参数
List<DeviceRunParam> deviceRunParamList = deviceRunParamMapper.selectList();
if (!CollectionUtils.isEmpty(deviceRunParamList)) {
deviceRunParamList.forEach(x -> {
@@ -233,7 +273,7 @@ public class InitThread implements ApplicationRunner {
}
});
}
//错误标准
// 错误标准
List<ErrorIdentificationStandard> standadList = errorIdentificationStandardMapper.selectList();
if (!CollectionUtils.isEmpty(standadList)) {
standadList.forEach(x -> {
@@ -245,7 +285,7 @@ public class InitThread implements ApplicationRunner {
}
});
}
//订单模型
// 订单模型
List<OrderModel> orderModelList = orderModelMapper.selectList();
if (!CollectionUtils.isEmpty(orderModelList)) {
orderModelList.forEach(x -> {
@@ -257,7 +297,7 @@ public class InitThread implements ApplicationRunner {
}
});
}
//告警订单配置
// 告警订单配置
List<AlarmOrderConfig> alarmOrderList = alarmOrderConfigMapper.selectList();
if (!CollectionUtils.isEmpty(alarmOrderList)) {
alarmOrderList.forEach(x -> {
@@ -317,4 +357,38 @@ public class InitThread implements ApplicationRunner {
}
}
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);
}
}
}