Эх сурвалжийг харах

feat: 增加规则引擎中对 rugo 的处理,并在.env环境文件中配置 使用 rugo 还是 node-red

yanglzh 11 сар өмнө
parent
commit
788d054a59

+ 5 - 0
.env

@@ -30,5 +30,10 @@ VITE_MODBUS_API = '/base-api/modbus'
 # ice104协议网关服务
 VITE_ICE104_API = '/base-api/ice104'
 
+# 规则引擎模式 rulego node-red
+VITE_RULE_MODEL = 'node-red'
+# rulego api
+VITE_RULEGO_SERVER_URL = 'http://127.0.0.1:9090/api/v1'
+
 # 加密公钥, 用双引号,换行符用 \n
 VITE_PUBLIC_KEY = "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwrJzCAJ0aart82Y2B5qo\nsZRv8p1dGX2oLFr1sArJxevW3a1v7cVA0U4WVFJdifDVFpsuich9nsfhUp7CNOZn\na+rNveglzYlrtMhqynYU+bKUBBAmYaVyDHOpxkp86fhp0q7qoX8YoeSvYRaVaPoF\nHRYeahy0d3L+gL8pRhr0k70RZMraC3zzXbuUcM7GNibiKbFiQllhlGlfbV0bmOH8\nLZcwWwv40Ptdd4x2gihn5vmzGdQ1OAf3D6YmtsXf7iMj0H1g5svyHs17ncSN7h9i\nWTrVKcNDxrl1dm4BRsxDJsWenwrIM1WUHuonlbE6OoIJEO25T3ucymzWDzMSWxe3\nsQIDAQAB\n-----END PUBLIC KEY-----"

+ 71 - 35
src/views/iot/rule-engine/edit.vue

@@ -1,12 +1,5 @@
 <template>
-	<el-dialog
-		class="api-edit"
-		v-model="showDialog"
-		:title="`${formData.id ? '编辑' : '新增'}`"
-		width="600px"
-		:close-on-click-modal="false"
-		:close-on-press-escape="false"
-	>
+	<el-dialog class="api-edit" v-model="showDialog" :title="`${formData.id ? '编辑' : '新增'}`" width="600px" :close-on-click-modal="false" :close-on-press-escape="false">
 		<el-form ref="formRef" :model="formData" :rules="ruleForm" label-width="80px">
 			<el-form-item label="名称" prop="name">
 				<el-input v-model.trim="formData.name" placeholder="输入名称" />
@@ -31,6 +24,7 @@ import axios from 'axios';
 import { ruleRequired } from '/@/utils/validator';
 import { ElMessage } from 'element-plus';
 import { getToken } from "/@/utils/auth";
+import { v4 as uuid } from 'uuid'
 
 const emit = defineEmits(['getList']);
 
@@ -39,6 +33,10 @@ const props = defineProps({
 		type: Number,
 		default: 0, // 规则编排是0 数据转发是1
 	},
+	model: {
+		type: String,
+		default: 'rulego', // rulego node-red
+	},
 });
 
 const headers = {
@@ -47,7 +45,6 @@ const headers = {
 const flowsUrl = window.location.origin + '/rule-engine/flow';
 // const flowsUrl = 'http://zhgy.sagoo.cn/rule-engine/flow';
 
-
 const showDialog = ref(false);
 const formRef = ref();
 
@@ -71,36 +68,75 @@ const ruleForm = {
 const onSubmit = async () => {
 	await formRef.value.validate();
 
-	if (!formData.id) {
-		const { data } = await axios.post(
-			flowsUrl,
-			{
-				label: formData.name,
-				disabled: true,
-				info: '',
-				env: [],
-				nodes: [],
-			},
-			{
-				headers,
+	// 不同引擎,用不同处理方式
+	if (props.model === 'rulego') {
+		if (!formData.id) {
+			const id = uuid()
+			await axios.post(
+				import.meta.env.VITE_RULEGO_SERVER_URL + '/rule/' + id,
+				{
+					"ruleChain": {
+						"id": id,
+						"name": formData.name,
+						"root": false,
+						"additionalInfo": {
+							"description": formData.expound
+						}
+					},
+					"metadata": {
+						"endpoints": [],
+						"nodes": [],
+						"connections": []
+					}
+				},
+				{ headers }
+			);
+			formData.flowId = id;
+		} else {
+			// 找到规则
+			const { data } = await axios.get(import.meta.env.VITE_RULEGO_SERVER_URL + '/rule/' + formData.flowId, { headers }).catch(() => {
+				ElMessage.error('规则不存在')
+			}) as any
+
+			// 修改名称和说明
+			data.ruleChain.name = formData.name
+			data.ruleChain.additionalInfo.description = formData.expound
+
+			// 保存
+			await axios.post(import.meta.env.VITE_RULEGO_SERVER_URL + '/rule/' + formData.flowId, data, { headers });
+		}
+	} else if (props.model === 'node-red') {
+		if (!formData.id) {
+			const { data } = await axios.post(
+				flowsUrl,
+				{
+					label: formData.name,
+					disabled: true,
+					info: '',
+					env: [],
+					nodes: [],
+				},
+				{
+					headers,
+				}
+			);
+			formData.flowId = data.id;
+		} else {
+			// 找到所有规则
+			const { data: flows } = await axios.get(flowsUrl + 's', { headers });
+
+			const flow = flows.find((item: any) => item.id === formData.flowId);
+
+			if (!flow) {
+				ElMessage.error('规则不存在');
+				return;
 			}
-		);
-		formData.flowId = data.id;
-	} else {
-		// 找到所有规则
-		const { data: flows } = await axios.get(flowsUrl + 's', { headers });
 
-		const flow = flows.find((item: any) => item.id === formData.flowId);
+			flow.label = formData.name;
 
-		if (!flow) {
-			ElMessage.error('规则不存在');
-			return;
+			// 设置规则状态
+			await axios.post(flowsUrl + 's', flows, { headers });
 		}
-
-		flow.label = formData.name;
-
-		// 设置规则状态
-		await axios.post(flowsUrl + 's', flows, { headers });
 	}
 
 	const theApi = formData.id ? api.edit : api.add;

+ 41 - 24
src/views/iot/rule-engine/index.vue

@@ -33,7 +33,7 @@
 				</el-table-column>
 			</el-table>
 			<pagination v-if="params.total" :total="params.total" v-model:page="params.pageNum" v-model:limit="params.pageSize" @pagination="getList()" />
-			<EditForm ref="editFormRef" @getList="getList(1)" :types="0"></EditForm>
+			<EditForm :model="model" ref="editFormRef" @getList="getList(1)" :types="0"></EditForm>
 		</el-card>
 	</div>
 </template>
@@ -49,6 +49,9 @@ import { getToken } from "/@/utils/auth";
 
 const editFormRef = ref();
 
+// 规则引擎模式 node-red rulego
+const model = import.meta.env.VITE_RULE_MODEL
+
 const { params, tableData, getList, loading } = useSearch<any[]>(api.getList, 'Data', { types: 0 });
 
 const headers = {
@@ -98,9 +101,16 @@ const setStatus = async (row: any, status: number) => {
 };
 
 const edit = async (row: any) => {
-	localStorage.setItem('auth-tokens', `{"access_token":"${getToken()}"}`);
-	const url = '/rule-engine/#flow/' + row.flowId;
-	window.open(url);
+	if (model == 'rulego') {
+		localStorage.setItem('auth-tokens', `{"access_token":"${getToken()}"}`);
+		const url = 'http://localhost:3000/plugin/rule/?id=' + row.flowId;
+		window.open(url);
+
+	} else if (model == 'node-red') {
+		localStorage.setItem('auth-tokens', `{"access_token":"${getToken()}"}`);
+		const url = '/rule-engine/#flow/' + row.flowId;
+		window.open(url);
+	}
 };
 
 const onDel = (row: any) => {
@@ -109,26 +119,33 @@ const onDel = (row: any) => {
 		cancelButtonText: '取消',
 		type: 'warning',
 	}).then(async () => {
-		// 找到所有规则
-		const { data: flows } = await axios.get(flowsUrl, { headers });
-
-		const flowIndex = flows.findIndex((item: any) => item.id === row.flowId);
-
-		if (flowIndex >= 0) {
-			// 删除指定规则
-			flows.splice(flowIndex, 1);
-
-			// 删除当前规则下的各个节点信息
-			const newFlows = flows.filter((item: any) => {
-				if (item.z === row.flowId) {
-					return false;
-				} else {
-					return true;
-				}
-			});
-
-			// 设置规则状态
-			await axios.post(flowsUrl, newFlows, { headers });
+
+		if (model == 'rulego') {
+			await axios.delete(import.meta.env.VITE_RULEGO_SERVER_URL + '/rule/' + row.flowId, { headers }).catch(() => {
+				ElMessage.error('规则不存在')
+			})
+		} else if (model == 'node-red') {
+			// 找到所有规则
+			const { data: flows } = await axios.get(flowsUrl, { headers });
+
+			const flowIndex = flows.findIndex((item: any) => item.id === row.flowId);
+
+			if (flowIndex >= 0) {
+				// 删除指定规则
+				flows.splice(flowIndex, 1);
+
+				// 删除当前规则下的各个节点信息
+				const newFlows = flows.filter((item: any) => {
+					if (item.z === row.flowId) {
+						return false;
+					} else {
+						return true;
+					}
+				});
+
+				// 设置规则状态
+				await axios.post(flowsUrl, newFlows, { headers });
+			}
 		}
 
 		await api.del([row.id as string]);

+ 40 - 24
src/views/iot/rule-engine/send.vue

@@ -39,7 +39,7 @@
         </el-table-column>
       </el-table>
       <pagination v-if="params.total" :total="params.total" v-model:page="params.pageNum" v-model:limit="params.pageSize" @pagination="getList()" />
-      <EditForm ref="editFormRef" @getList="getList(1)" :types="1"></EditForm>
+      <EditForm :model="model" ref="editFormRef" @getList="getList(1)" :types="1"></EditForm>
     </el-card>
   </div>
 </template>
@@ -55,6 +55,9 @@ import { getToken } from "/@/utils/auth";
 
 const editFormRef = ref();
 
+// 规则引擎模式 node-red rulego
+const model = import.meta.env.VITE_RULE_MODEL
+
 const { params, tableData, getList, loading } = useSearch<any[]>(api.getList, 'Data', { types: 1 });
 
 const headers = {
@@ -102,9 +105,16 @@ const setStatus = async (row: any, status: number) => {
 };
 
 const edit = async (row: any) => {
-  localStorage.setItem('auth-tokens', `{"access_token":"${getToken()}"}`);
-  const url = '/rule-engine/#flow/' + row.flowId;
-  window.open(url);
+  if (model == 'rulego') {
+    localStorage.setItem('auth-tokens', `{"access_token":"${getToken()}"}`);
+    const url = 'http://localhost:3000/plugin/rule/?id=' + row.flowId;
+    window.open(url);
+
+  } else if (model == 'node-red') {
+    localStorage.setItem('auth-tokens', `{"access_token":"${getToken()}"}`);
+    const url = '/rule-engine/#flow/' + row.flowId;
+    window.open(url);
+  }
 };
 
 const onDel = (row: any) => {
@@ -113,26 +123,32 @@ const onDel = (row: any) => {
     cancelButtonText: '取消',
     type: 'warning',
   }).then(async () => {
-    // 找到所有规则
-    const { data: flows } = await axios.get(flowsUrl, { headers });
-
-    const flowIndex = flows.findIndex((item: any) => item.id === row.flowId);
-
-    if (flowIndex >= 0) {
-      // 删除指定规则
-      flows.splice(flowIndex, 1);
-
-      // 删除当前规则下的各个节点信息
-      const newFlows = flows.filter((item: any) => {
-        if (item.z === row.flowId) {
-          return false;
-        } else {
-          return true;
-        }
-      });
-
-      // 设置规则状态
-      await axios.post(flowsUrl, newFlows, { headers });
+    if (model == 'rulego') {
+      await axios.delete(import.meta.env.VITE_RULEGO_SERVER_URL + '/rule/' + row.flowId, { headers }).catch(() => {
+        ElMessage.error('规则不存在')
+      })
+    } else if (model == 'node-red') {
+      // 找到所有规则
+      const { data: flows } = await axios.get(flowsUrl, { headers });
+
+      const flowIndex = flows.findIndex((item: any) => item.id === row.flowId);
+
+      if (flowIndex >= 0) {
+        // 删除指定规则
+        flows.splice(flowIndex, 1);
+
+        // 删除当前规则下的各个节点信息
+        const newFlows = flows.filter((item: any) => {
+          if (item.z === row.flowId) {
+            return false;
+          } else {
+            return true;
+          }
+        });
+
+        // 设置规则状态
+        await axios.post(flowsUrl, newFlows, { headers });
+      }
     }
 
     await api.del([row.id as string]);