update
This commit is contained in:
@@ -1,20 +1,13 @@
|
||||
package com.maibu.init;
|
||||
|
||||
import com.maibu.constant.FastBeeConstant;
|
||||
import com.maibu.core.business.DevicePlan;
|
||||
import com.maibu.core.business.DevicePlanTask;
|
||||
import com.maibu.core.domain.entity.SysSite;
|
||||
import com.maibu.core.enums.DeviceTaskStaus;
|
||||
import com.maibu.mapper.DevicePlanMapper;
|
||||
import com.maibu.mapper.DevicePlanTaskMapper;
|
||||
import com.maibu.mapper.SysSiteMapper;
|
||||
import com.maibu.mapper.SysUserClientMapper;
|
||||
import com.maibu.memory.GlobalMemory;
|
||||
import com.maibu.memory.SiteMemory;
|
||||
import com.maibu.mybatis.LambdaQueryWrapperX;
|
||||
import com.maibu.service.DevicePlanTaskMonitorService;
|
||||
import com.maibu.service.DeviceThreadService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.eclipse.paho.client.mqttv3.MqttException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
@@ -22,19 +15,29 @@ import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
import com.maibu.constant.FastBeeConstant;
|
||||
import com.maibu.core.business.DevicePlan;
|
||||
import com.maibu.core.business.DevicePlanTask;
|
||||
import com.maibu.core.domain.entity.SysSite;
|
||||
import com.maibu.core.enums.DeviceTaskStaus;
|
||||
import com.maibu.influxdb.util.InfluxDBUtil;
|
||||
import com.maibu.mapper.DevicePlanMapper;
|
||||
import com.maibu.mapper.DevicePlanTaskMapper;
|
||||
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 lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
@Order(20)
|
||||
public class InitThread implements ApplicationRunner {
|
||||
|
||||
|
||||
@Resource(name = FastBeeConstant.TASK.DEVICE_ERROR_MONITOR)
|
||||
private Executor deviceErrorMonitorExecutor;
|
||||
|
||||
@@ -56,15 +59,48 @@ public class InitThread implements ApplicationRunner {
|
||||
@Autowired
|
||||
private SysSiteMapper sysSiteMapper;
|
||||
|
||||
@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;
|
||||
|
||||
@Override
|
||||
public void run(ApplicationArguments args) {
|
||||
public void run(ApplicationArguments args) throws MqttException {
|
||||
recoverMemory();
|
||||
init();
|
||||
}
|
||||
|
||||
|
||||
private void init() {
|
||||
//设备错误监听线程
|
||||
private void init() throws MqttException {
|
||||
// 初始化InfluxDBUtil
|
||||
GlobalMemory.influxDBUtil = new InfluxDBUtil(influxDBUrl, influxDBToken, influxDBOrg, influxDBBucket);
|
||||
// 初始化MqttClientUtil
|
||||
GlobalMemory.mqttClientUtil = new MqttClientUtil(mqttHostUrl, mqttClientId, mqttUsername, mqttPassword);
|
||||
// 设备错误监听线程
|
||||
deviceErrorMonitorExecutor.execute(() -> {
|
||||
try {
|
||||
deviceThreadService.deviceErrorMonitor();
|
||||
@@ -72,14 +108,14 @@ public class InitThread implements ApplicationRunner {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
//设备任务处理线程
|
||||
// 设备任务处理线程
|
||||
deviceTaskHandlerExecutor.execute(() -> {
|
||||
//任务下发
|
||||
// 任务下发
|
||||
try {
|
||||
devicePlanTaskMonitorService.generatePlanTask();
|
||||
//执行准备队列
|
||||
// 执行准备队列
|
||||
devicePlanTaskMonitorService.doLoop();
|
||||
//执行任务
|
||||
// 执行任务
|
||||
devicePlanTaskMonitorService.doExecute();
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
@@ -91,9 +127,9 @@ public class InitThread implements ApplicationRunner {
|
||||
* 恢复内存
|
||||
*/
|
||||
public void recoverMemory() {
|
||||
//todo 启动时清除当前登录记录
|
||||
// System.out.println("开始清除登录信息");
|
||||
// sysUserClientMapper.clearDeviceName();
|
||||
// todo 启动时清除当前登录记录
|
||||
// System.out.println("开始清除登录信息");
|
||||
// sysUserClientMapper.clearDeviceName();
|
||||
List<SysSite> siteList = sysSiteMapper.selectList();
|
||||
if (!CollectionUtils.isEmpty(siteList)) {
|
||||
siteList.forEach(x -> {
|
||||
@@ -108,7 +144,8 @@ public class InitThread implements ApplicationRunner {
|
||||
}
|
||||
// 未执行完的重复计划
|
||||
LambdaQueryWrapperX<DevicePlan> query = new LambdaQueryWrapperX<>();
|
||||
List<String> list = Arrays.asList(DeviceTaskStaus.NEW.getCode(), DeviceTaskStaus.EXECUTING.getCode(), DeviceTaskStaus.PAUSE.getCode());
|
||||
List<String> list = Arrays.asList(DeviceTaskStaus.NEW.getCode(), DeviceTaskStaus.EXECUTING.getCode(),
|
||||
DeviceTaskStaus.PAUSE.getCode());
|
||||
query.in(DevicePlan::getTaskStaus, list);
|
||||
List<DevicePlan> devicePlanList = devicePlanMapper.selectList(query);
|
||||
if (!CollectionUtils.isEmpty(devicePlanList)) {
|
||||
@@ -116,7 +153,7 @@ public class InitThread implements ApplicationRunner {
|
||||
Long orgId = x.getOrgId();
|
||||
Long siteId = x.getSiteId();
|
||||
SiteMemory siteMemory = GlobalMemory.getSiteMemory(orgId, siteId);
|
||||
if(siteMemory!=null){
|
||||
if (siteMemory != null) {
|
||||
siteMemory.addDevicePlan(x);
|
||||
}
|
||||
});
|
||||
@@ -131,7 +168,7 @@ public class InitThread implements ApplicationRunner {
|
||||
Long orgId = x.getOrgId();
|
||||
Long siteId = x.getSiteId();
|
||||
SiteMemory siteMemory = GlobalMemory.getSiteMemory(orgId, siteId);
|
||||
if(siteMemory!=null){
|
||||
if (siteMemory != null) {
|
||||
siteMemory.addDevicePlanPrepare(x);
|
||||
}
|
||||
});
|
||||
@@ -145,7 +182,7 @@ public class InitThread implements ApplicationRunner {
|
||||
Long orgId = x.getOrgId();
|
||||
Long siteId = x.getSiteId();
|
||||
SiteMemory siteMemory = GlobalMemory.getSiteMemory(orgId, siteId);
|
||||
if(siteMemory!=null){
|
||||
if (siteMemory != null) {
|
||||
siteMemory.addDevicePlanExecute(x);
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user