第一次提交
This commit is contained in:
45
fastbee-plugs/fastbee-ruleEngine/pom.xml
Normal file
45
fastbee-plugs/fastbee-ruleEngine/pom.xml
Normal file
@ -0,0 +1,45 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.fastbee</groupId>
|
||||
<artifactId>fastbee-plugs</artifactId>
|
||||
<version>3.8.5</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>fastbee-ruleEngine</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.yomahub</groupId>
|
||||
<artifactId>liteflow-core</artifactId>
|
||||
<version>${liteflow.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fastbee</groupId>
|
||||
<artifactId>fastbee-mqtt-client</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -0,0 +1,20 @@
|
||||
package com.fastbee.ruleEngine.config;
|
||||
|
||||
import com.yomahub.liteflow.thread.ExecutorBuilder;
|
||||
import org.springframework.scheduling.concurrent.CustomizableThreadFactory;
|
||||
|
||||
import java.util.concurrent.*;
|
||||
|
||||
public class MainExecutorBuilder implements ExecutorBuilder {
|
||||
private ThreadFactory springThreadFactory = new CustomizableThreadFactory("liteflow-main-");
|
||||
@Override
|
||||
public ExecutorService buildExecutor() {
|
||||
return new ThreadPoolExecutor(
|
||||
10,
|
||||
30,
|
||||
5,
|
||||
TimeUnit.MINUTES,
|
||||
new ArrayBlockingQueue<Runnable>(1000),
|
||||
springThreadFactory);
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package com.fastbee.ruleEngine.config;
|
||||
|
||||
import com.yomahub.liteflow.thread.ExecutorBuilder;
|
||||
import org.springframework.scheduling.concurrent.CustomizableThreadFactory;
|
||||
|
||||
import java.util.concurrent.*;
|
||||
|
||||
public class WhenExecutorBuilder implements ExecutorBuilder {
|
||||
private ThreadFactory springThreadFactory = new CustomizableThreadFactory("liteflow-when-");
|
||||
@Override
|
||||
public ExecutorService buildExecutor() {
|
||||
return new ThreadPoolExecutor(
|
||||
10,
|
||||
30,
|
||||
5,
|
||||
TimeUnit.MINUTES,
|
||||
new ArrayBlockingQueue<Runnable>(1000),
|
||||
springThreadFactory);
|
||||
}
|
||||
}
|
@ -0,0 +1,127 @@
|
||||
package com.fastbee.ruleEngine.core;
|
||||
|
||||
import com.fastbee.common.utils.DateUtils;
|
||||
import com.fastbee.common.utils.gateway.mq.TopicsUtils;
|
||||
import com.fastbee.mqttclient.PubMqttClient;
|
||||
import com.yomahub.liteflow.core.FlowExecutor;
|
||||
import com.yomahub.liteflow.flow.LiteflowResponse;
|
||||
import com.yomahub.liteflow.flow.entity.CmpStep;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class FlowLogExecutor {
|
||||
@Resource
|
||||
private FlowExecutor flowExecutor;
|
||||
@Resource
|
||||
private PubMqttClient mqttClient;
|
||||
|
||||
Set<String> ScriptSet = new HashSet<>();
|
||||
Set<String> SceneSet = new HashSet<>();
|
||||
|
||||
public LiteflowResponse execute2Resp(String chainId, Object param, Object... contextBeanArray) {
|
||||
LiteflowResponse response = flowExecutor.execute2Resp(chainId, param, contextBeanArray);
|
||||
printResponse(response);
|
||||
return response;
|
||||
}
|
||||
|
||||
public LiteflowResponse execute2RespWithRid(String chainId, Object param, String requestId, Object... contextBeanArray) {
|
||||
LiteflowResponse response = flowExecutor.execute2RespWithRid(chainId, param, requestId, contextBeanArray);
|
||||
printResponseWithRid(requestId, response);
|
||||
return response;
|
||||
}
|
||||
|
||||
public LiteflowResponse execute2Future(String chainId, Object param, Object... contextBeanArray) throws ExecutionException, InterruptedException {
|
||||
Future<LiteflowResponse> future = flowExecutor.execute2Future(chainId, param, contextBeanArray);
|
||||
LiteflowResponse response = future.get();
|
||||
printResponse(response);
|
||||
return response;
|
||||
}
|
||||
|
||||
public LiteflowResponse execute2FutureWithRid(String chainId, Object param, String requestId, Object... contextBeanArray) throws ExecutionException, InterruptedException {
|
||||
Future<LiteflowResponse> future = flowExecutor.execute2FutureWithRid(chainId, param, requestId, contextBeanArray);
|
||||
LiteflowResponse response = future.get();
|
||||
printResponseWithRid(requestId, response);
|
||||
return response;
|
||||
}
|
||||
|
||||
public void printResponse(LiteflowResponse response) {
|
||||
if (!response.isSuccess()) {
|
||||
Exception e = response.getCause();
|
||||
log.error("报错信息:{}", e.getMessage(), e);
|
||||
} else {
|
||||
//步骤详情
|
||||
Map<String, List<CmpStep>> stepMap = response.getExecuteSteps();
|
||||
stepMap.forEach((k, v) -> {
|
||||
v.forEach((step) -> {
|
||||
log.info("步骤:{}({}),执行时间:{}", step.getNodeId(), step.getNodeName(), step.getTimeSpent());
|
||||
});
|
||||
});
|
||||
//每各步骤执行时间
|
||||
String stepStr = response.getExecuteStepStrWithTime();
|
||||
log.info("步骤:{}", stepStr);
|
||||
}
|
||||
}
|
||||
|
||||
public void printResponseWithRid(String requestId, LiteflowResponse response) {
|
||||
if (!response.isSuccess()) {
|
||||
Exception e = response.getCause();
|
||||
log.error("[{}]-报错信息:{}", requestId, e.toString());
|
||||
publishLog(requestId, response.getCause().toString());
|
||||
} else {
|
||||
//步骤详情
|
||||
Map<String, List<CmpStep>> stepMap = response.getExecuteSteps();
|
||||
stepMap.forEach((k, v) -> {
|
||||
v.forEach((step) -> {
|
||||
log.info("[{}]-步骤:{}({}),执行时间:{}", requestId, step.getNodeId(), step.getNodeName(), step.getTimeSpent());
|
||||
});
|
||||
});
|
||||
//每各步骤执行时间
|
||||
String stepStr = response.getExecuteStepStrWithTime();
|
||||
log.info("[{}]-步骤:{}", requestId, stepStr);
|
||||
publishLog(requestId, stepStr);
|
||||
}
|
||||
}
|
||||
|
||||
public void openScriptLog(String scriptId) {
|
||||
ScriptSet.add(scriptId);
|
||||
}
|
||||
|
||||
public void closeScriptLog(String scriptId) {
|
||||
ScriptSet.remove(scriptId);
|
||||
}
|
||||
|
||||
public void openSceneLog(String sceneId) {
|
||||
SceneSet.add(sceneId);
|
||||
}
|
||||
|
||||
public void closeSceneLog(String sceneId) {
|
||||
SceneSet.remove(sceneId);
|
||||
}
|
||||
|
||||
//requestId : Chainid + nodeId|scriptId / sceneId + nodeId|scriptId
|
||||
public void publishLog(String requestId, String log) {
|
||||
String[] splits = requestId.split("/");
|
||||
String message = "{\"requestId\":\"" + requestId + "\",\"time\":\"" + DateUtils.getNowDate() + "\",\"log\":\"" + log + "\"}";
|
||||
for (String id : splits) {
|
||||
if (ScriptSet.contains(id)) {
|
||||
String topic = TopicsUtils.buildRuleEngineTopic(id);
|
||||
mqttClient.publish(1, false, topic, message);
|
||||
}
|
||||
if (SceneSet.contains(id)) {
|
||||
String topic = TopicsUtils.buildRuleEngineTopic(id);
|
||||
mqttClient.publish(1, false, topic, message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package com.fastbee.ruleEngine.core;
|
||||
|
||||
|
||||
import cn.hutool.core.lang.Snowflake;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import com.fastbee.ruleEngine.util.Constant;
|
||||
|
||||
public class RequestIdBuilder {
|
||||
public static String buildSnowflakeRequestId() {
|
||||
Snowflake snowflake = IdUtil.createSnowflake(1, 1);
|
||||
return Constant.REQUEST_ID_SNOWFLAKE_PREFIX + snowflake.nextId();
|
||||
}
|
||||
|
||||
public static String buildDeviceRequestId(String serialNumber) {
|
||||
return Constant.REQUEST_ID_PREFIX + serialNumber;
|
||||
}
|
||||
|
||||
public static String buildSceneRequestId(String sceneId) {
|
||||
return Constant.REQUEST_ID_PREFIX + sceneId;
|
||||
}
|
||||
|
||||
public static String buildProductRequestId(Long product, String serialNumber) {
|
||||
return Constant.REQUEST_ID_PREFIX + product + Constant.REQUEST_ID_SPLIT + serialNumber;
|
||||
}
|
||||
|
||||
public static String buildALLRequestId(String serialNumber, String product, String sceneId) {
|
||||
return Constant.REQUEST_ID_PREFIX + product + Constant.REQUEST_ID_SPLIT + serialNumber + Constant.REQUEST_ID_SPLIT + sceneId;
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package com.fastbee.ruleEngine.util;
|
||||
|
||||
public class Constant {
|
||||
public static final String REQUEST_ID_SNOWFLAKE_PREFIX = "S";
|
||||
public static final String REQUEST_ID_PREFIX = "R";
|
||||
public static final String REQUEST_ID_SPLIT = "_";
|
||||
// D=数据流,A=执行动作,T=触发器
|
||||
public static final String SCRIPT_DATA_PREFIX = "D";
|
||||
public static final String SCRIPT_ACTION_PREFIX = "A";
|
||||
public static final String SCRIPT_T_PREFIX = "T";
|
||||
}
|
Reference in New Issue
Block a user