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

添加ai,ao xml,添加删除逻辑

一只小菜杨 2 сар өмнө
parent
commit
4a7119a4e4

+ 1 - 0
src/main.ts

@@ -1,4 +1,5 @@
 import './assets/main.css'
+import 'vant/es/dialog/style'
 
 import { createApp } from 'vue'
 import { createPinia } from 'pinia'

+ 2 - 2
src/stores/global.ts

@@ -6,12 +6,12 @@ export const useGlobalStore = defineStore(
   () => {
     const projectList = ref<ProjectType[]>([
       {
-        name: '项目1',
+        name: '工程1',
         id: '1',
         time: '2025-01-09 16:00:00',
         commandList: [
           {
-            name: '命令1',
+            name: '任务1',
             id: '11',
             time: '2025-01-09 16:00:00',
             parentId: '1',

+ 26 - 3
src/views/CommandInfo.vue

@@ -1,13 +1,14 @@
 <template>
   <div class="command-info">
     <div class="header">
-      <div class="go-back" @click="router.go(-1)">
+      <div class="go-back" @click="router.push(`/project-info/${crtProject!.id}`)">
         <van-button size="small"><van-icon name="arrow-left" />返回</van-button>
       </div>
       <div class="btn-group">
         <van-button size="small" type="primary" @click="handleAddCommand('condition')">增加条件</van-button>
         <van-button size="small" type="primary" @click="handleAddCommand('delay')">增加延时</van-button>
         <van-button size="small" type="primary" @click="handleAddCommand('exec')">增加执行</van-button>
+        <van-button size="small" type="danger" @click="handleAddCommand('del')">删除任务</van-button>
         <van-icon name="question-o" size="24" @click="showHelpDoc = true" />
       </div>
     </div>
@@ -167,6 +168,7 @@ import type { CommandType, ConditionType, ProjectType } from '@/types/global'
 import { deepClone } from '@/utils/tools'
 import { storeToRefs } from 'pinia'
 import { v4 as uuid4 } from 'uuid'
+import { showConfirmDialog } from 'vant'
 
 const router = useRouter()
 const route = useRoute()
@@ -344,6 +346,26 @@ const handleEditCommand = (type: string, index: number, cIndex?: number) => {
 
 const handleAddCommand = (type: string) => {
   if (!crtCommand.value) return
+  if (type === 'del') {
+    if (crtProject.value && crtCommand.value) {
+      showConfirmDialog({
+        title: '提示',
+        message: '确定要删除这个任务吗'
+      })
+        .then(() => {
+          // on confirm
+          console.log(crtProject.value, crtCommand.value)
+          crtProject.value!.commandList = crtProject.value!.commandList.filter(
+            (item) => crtCommand.value?.id !== item.id
+          )
+          router.push(`/project-info/${crtProject.value!.id}`)
+        })
+        .catch(() => {
+          // on cancel
+        })
+    }
+    return
+  }
   const endCMD = crtCommand.value.stepList[crtCommand.value.stepList.length - 1]
   const isExistTime = endCMD?.list?.some((item) => item.type === 'time')
   const x = endCMD?.x || crtCommand.value.x
@@ -527,11 +549,12 @@ const showHelpDoc = ref(false)
   width: 70%;
 }
 .help-label {
-  margin: 16px 8px;
+  margin: 24px 16px 16px;
   font-size: 18px;
   font-weight: 600;
 }
 .help-content {
-  padding: 0 16px 16px;
+  padding: 0 24px 24px;
+  line-height: 2;
 }
 </style>

+ 340 - 57
src/views/ProjectInfo.vue

@@ -1,7 +1,7 @@
 <template>
   <div class="project-info">
     <div class="header">
-      <div class="go-back" @click="router.go(-1)">
+      <div class="go-back" @click="router.replace({ name: 'Automate' })">
         <van-button size="small"><van-icon name="arrow-left" />返回</van-button>
       </div>
       <div class="project-name">{{ crtProject!.name }}</div>
@@ -9,13 +9,16 @@
     </div>
 
     <div class="command-container">
-      <van-swipe-cell v-for="item in commandList" :key="item.id" disabled>
+      <van-swipe-cell v-for="item in commandList" :key="item.id" :disabled="touchStatus">
         <van-cell
           clickable
           :border="false"
           :title="item.name"
           :label="item.time"
-          @click="router.push(`/command-info/${crtProject!.id}/${item.id}`)"
+          @click="!touchStatus && router.push(`/command-info/${crtProject!.id}/${item.id}`)"
+          @touchstart="handleTouchStart"
+          @touchmove="handleTouchMove"
+          @touchend="handleTouchEnd"
         >
         </van-cell>
         <template #right>
@@ -31,6 +34,7 @@
       <van-cell-group :style="{ marginTop: '16px' }">
         <van-cell title="编辑工程名称" clickable @click="handleEditName" />
         <van-cell title="下载到设备" @click="getXMLFile" clickable />
+        <van-cell title="删除工程" @click="delProject" clickable />
         <wx-open-launch-weapp username="小程序的原始ID(gh_开头)" path="pages/index/index?param1=xxx&param2=xxx">
           <template>
             <!-- 自定义跳转按钮样式 -->
@@ -63,17 +67,18 @@
 
 <script setup lang="ts">
 import { useGlobalStore } from '@/stores/global'
-import type { ProjectType } from '@/types/global'
+import type { CommandType, ProjectType } from '@/types/global'
 import { formatDateTime } from '@/utils/tools'
 import { storeToRefs } from 'pinia'
 import { v4 as uuid4 } from 'uuid'
+import { showConfirmDialog } from 'vant'
 
 const route = useRoute()
 const router = useRouter()
 
 const { projectList } = storeToRefs(useGlobalStore())
 const crtProject = ref<ProjectType>()
-const commandList = ref()
+const commandList = ref<CommandType[]>([])
 
 watch(
   () => route,
@@ -88,6 +93,26 @@ watch(
   }
 )
 
+let touchTimer: number
+const touchStatus = ref(false)
+const handleTouchStart = () => {
+  touchTimer = setTimeout(() => {
+    touchStatus.value = true
+  }, 700)
+}
+const handleTouchMove = () => {
+  if (touchTimer) {
+    clearTimeout(touchTimer)
+    touchTimer = 0
+  }
+}
+const handleTouchEnd = () => {
+  if (touchTimer) {
+    clearTimeout(touchTimer)
+    touchTimer = 0
+  }
+}
+
 const showMorePopup = ref(false)
 const handleEditName = () => {
   showEditPopup.value = true
@@ -114,16 +139,29 @@ const handleCancel = async () => {
   newProjectName.value = ''
 }
 
-const handleDelete = (val: string) => {}
+const handleDelete = (val: string) => {
+  console.log(val)
+  console.log(commandList.value)
+  if (val) {
+    commandList.value = commandList.value.filter((item) => item.id !== val)
+  }
+}
 
 const handleAddCommond = () => {
+  let name = `任务${commandList.value.length + 1}`
+  if (commandList.value.length) {
+    const beforeName = commandList.value[commandList.value.length - 1].name
+    const index = beforeName.split('任务')[1]
+    name = `任务${+index + 1}`
+  }
   commandList.value.push({
     id: '' + Date.now(),
-    name: `任务${commandList.value.length + 1}`,
+    name,
     time: new Date().toLocaleString(),
     stepList: [],
     x: 100,
-    y: 100 + crtProject.value!.commandList.length * 2000
+    y: 100 + crtProject.value!.commandList.length * 2000,
+    parentId: crtProject.value!.id
   })
 }
 
@@ -167,34 +205,38 @@ const getXMLFile = () => {
     周日: 3
   }
 
+  let instanceXml = ''
   crtProject.value.commandList.forEach((item, index) => {
+    instanceXml += `<pouInstance name="instance${index}" typeName="program${index}"/>`
     xml += `<pou name="program${index}" pouType="program">
               <interface>
-                <localVars>
+                <externalVars>
                   <variable name="DI1">
-                    <type><BOOL /></type>
-                  </variable>
-                  <variable name="DI2">
-                    <type><BOOL /></type>
-                  </variable>
-                  <variable name="AI1">
-                    <type><INT /></type>
-                  </variable>
-                  <variable name="AI2">
-                    <type><INT /></type>
-                  </variable>
-                  <variable name="DO1">
-                    <type><BOOL /></type>
-                  </variable>
-                  <variable name="DO2">
-                    <type><BOOL /></type>
-                  </variable>
-                  <variable name="AO1">
-                    <type><INT /></type>
-                  </variable>
-                  <variable name="AO2">
-                    <type><INT /></type>
-                  </variable>
+                      <type><BOOL /></type>
+                    </variable>
+                    <variable name="DI2">
+                      <type><BOOL /></type>
+                    </variable>
+                    <variable name="AI1">
+                      <type><INT /></type>
+                    </variable>
+                    <variable name="AI2">
+                      <type><INT /></type>
+                    </variable>
+                    <variable name="DO1">
+                      <type><BOOL /></type>
+                    </variable>
+                    <variable name="DO2">
+                      <type><BOOL /></type>
+                    </variable>
+                    <variable name="AO1">
+                      <type><INT /></type>
+                    </variable>
+                    <variable name="AO2">
+                      <type><INT /></type>
+                    </variable>
+                </externalVars>
+                <localVars>
                   <variable name="MOD_HOUR">
                     <type><ULINT /></type>
                     <initialValue>
@@ -225,12 +267,18 @@ const getXMLFile = () => {
                       <simpleValue value="60" />
                     </initialValue>
                   </variable>
-                  <variable name="STARTDATE">
+                  <variable name="CRT_DATA">
                     <type><DT /></type>
                     <initialValue>
                       <simpleValue value="DT#${formatDateTime(new Date(), '-')}" />
                     </initialValue>
                   </variable>
+                  <variable name="MUL_NUM">
+                    <type><INT /></type>
+                    <initialValue>
+                      <simpleValue value="1" />
+                    </initialValue>
+                  </variable>
                 `
     // 临时变量
     // const tempVarNum = item.stepList.filter((cItem) => cItem.type === 'condition').length
@@ -240,11 +288,26 @@ const getXMLFile = () => {
                 <type><BOOL /></type>
               </variable>`
     }
-
-    // RTC 数量
+    // 定义所需变量
     item.stepList.forEach((item, index) => {
-      if ('list' in item) {
+      if (item.type === 'delay') {
+        // RTC 数量
+        xml += `<variable name="TON${index}">
+                  <type>
+                    <derived name="TON" />
+                  </type>
+                </variable>
+                <variable name="PT${index}">
+                  <type>
+                    <TIME />
+                  </type>
+                  <initialValue>
+                    <simpleValue value="T#${item.value}s" />
+                  </initialValue>
+                </variable>`
+      } else {
         item.list?.forEach((cItem, cIndex) => {
+          // PT 数量
           if (cItem.type === 'time') {
             xml += `<variable name="RTC${index}_${cIndex}">
                       <type>
@@ -286,29 +349,27 @@ const getXMLFile = () => {
                         </initialValue>
                       </variable>`
             }
+          } else if (cItem.type === 'input' && cItem.label.includes('AI')) {
+            // 判断AI变量
+            xml += `<variable name="COMPARE_AI${index}_${cIndex}">
+                      <type><INT /></type>
+                      <initialValue>
+                        <simpleValue value="${cItem.value}" />
+                      </initialValue>
+                    </variable>`
+          } else if (cItem.type === 'input' && cItem.label.includes('AO')) {
+            // 赋值AO变量
+            xml += `<variable name="SET_AI${index}_${cIndex}">
+                      <type><INT /></type>
+                      <initialValue>
+                        <simpleValue value="${cItem.value}" />
+                      </initialValue>
+                    </variable>`
           }
         })
       }
     })
 
-    // PT 数量
-    item.stepList.forEach((cItem, sIndex) => {
-      if (cItem.type === 'delay') {
-        xml += `<variable name="TON${sIndex}">
-                  <type>
-                    <derived name="TON" />
-                  </type>
-                </variable>
-                <variable name="PT${sIndex}">
-                  <type>
-                    <TIME />
-                  </type>
-                  <initialValue>
-                    <simpleValue value="T#${cItem.value}s" />
-                  </initialValue>
-                </variable>`
-      }
-    })
     xml += `</localVars></interface>`
     xml += `<body><LD>`
 
@@ -368,7 +429,7 @@ const getXMLFile = () => {
               <connectionPointOut>
                 <relPosition x="60" y="20" />
               </connectionPointOut>
-              <expression>STARTDATE</expression>
+              <expression>CRT_DATA</expression>
             </inVariable>`
             let RTCID1 = `${index + 1}${sIndex}${i}12`
             // 定义RTC变量
@@ -1018,6 +1079,158 @@ const getXMLFile = () => {
                 <position x="${cItem.x + 2090}" y="${cItem.y + 20}" />
                 <position x="${sItem.x - 120}" y="${sItem.y + 20}" />
               </connection>`
+          } else if (cItem.label.includes('AI')) {
+            let compareVarID = `${index + 1}${sIndex}${i}33`
+            // 对AI进行比较的变量
+            xml += `
+            <inVariable localId="${compareVarID}" width="40" height="40" negated="false">
+              <position x="${cItem.x - 100}" y="${cItem.y + 200}" />
+              <connectionPointOut>
+                <relPosition x="60" y="20" />
+              </connectionPointOut>
+              <expression>COMPARE_AI${sIndex}_${i}</expression>
+            </inVariable>`
+            // AI变量
+            let AIID = `${index + 1}${sIndex}${i}34`
+            xml += `
+            <inVariable localId="${AIID}" width="40" height="40" negated="false">
+              <position x="${cItem.x - 100}" y="${cItem.y + 200}" />
+              <connectionPointOut>
+                <relPosition x="60" y="20" />
+              </connectionPointOut>
+              <expression>${cItem.label}</expression>
+            </inVariable>`
+            let compareBlockID = `${index + 1}${sIndex}${i}35`
+            let operationMap: Record<string, string> = {
+              '≥': 'GE',
+              '≤': 'LE',
+              '>': 'GT',
+              '<': 'LT',
+              '=': 'EQ',
+              '≠': 'NE'
+            }
+            xml += `<block localId="${compareBlockID}" typeName="${operationMap[cItem.operation!]}" width="70" height="60">
+                  <position x="${cItem.x}" y="${cItem.y + 200}" />
+                  <inputVariables>
+                    <variable formalParameter="IN1">
+                      <connectionPointIn>
+                        <relPosition x="-20" y="40" />
+                        <connection refLocalId="${compareVarID}" formalParameter="OUT">
+                          <position x="${cItem.x - 40}" y="${cItem.y + 220}" />
+                          <position x="${cItem.x - 20}" y="${cItem.y + 240}" />
+                        </connection>
+                      </connectionPointIn>
+                    </variable>
+                    <variable formalParameter="IN2">
+                      <connectionPointIn>
+                        <relPosition x="-20" y="60" />
+                        <connection refLocalId="${AIID}" formalParameter="OUT">
+                          <position x="${cItem.x - 40}" y="${cItem.y + 220}" />
+                          <position x="${cItem.x - 20}" y="${cItem.y + 260}" />
+                        </connection>
+                      </connectionPointIn>
+                    </variable>
+                  </inputVariables>
+                  <inOutVariables />
+                  <outputVariables>
+                    <variable formalParameter="OUT">
+                      <connectionPointOut>
+                        <relPosition x="90" y="20" />
+                      </connectionPointOut>
+                    </variable>
+                  </outputVariables>
+                </block>`
+
+            tempConnectXML += `
+              <connection refLocalId="${compareBlockID}" formalParameter="OUT">
+                <position x="${cItem.x + 690}" y="${cItem.y + 20}" />
+                <position x="${sItem.x - 120}" y="${sItem.y + 20}" />
+              </connection>`
+          } else if (cItem.label.includes('AO')) {
+            // 乘数id
+            let mulID = `${index + 1}${sIndex}${i}36`
+            xml += `
+            <inVariable localId="${mulID}" width="40" height="40" negated="false">
+              <position x="${cItem.x - 100}" y="${cItem.y + 200}" />
+              <connectionPointOut>
+                <relPosition x="60" y="20" />
+              </connectionPointOut>
+              <expression>MUL_NUM</expression>
+            </inVariable>`
+            // 赋值id
+            let setID = `${index + 1}${sIndex}${i}37`
+            xml += `
+            <inVariable localId="${setID}" width="40" height="40" negated="false">
+              <position x="${cItem.x - 100}" y="${cItem.y + 200}" />
+              <connectionPointOut>
+                <relPosition x="60" y="20" />
+              </connectionPointOut>
+              <expression>SET_AI${sIndex}_${i}</expression>
+            </inVariable>`
+
+            let mulBlockId = `${index + 1}${sIndex}${i}38`
+            xml += `
+            <block localId="${mulBlockId}" typeName="DIV" width="70" height="60">
+              <position x="${cItem.x}" y="${cItem.y}" />
+              <inputVariables>
+              <variable formalParameter="EN">
+                  <connectionPointIn>
+                    <relPosition x="-20" y="20"/>
+                    <connection refLocalId="${sIndex === 0 ? item.id : tempVarId}" formalParameter="OUT">
+                      <position x="${sIndex === 0 ? item.x + 40 : sItem.x - 40}" y="${sItem.y + 20}"/>
+                      <position x="${cItem.x - 20}" y="${cItem.y + 20}"/>
+                    </connection>
+                  </connectionPointIn>
+                </variable>
+                      <variable formalParameter="IN1">
+                      <connectionPointIn>
+                      <relPosition x="-20" y="40" />
+                      <connection refLocalId="${mulID}" formalParameter="OUT">
+                      <position x="${cItem.x - 40}" y="${cItem.y + 220}" />
+                      <position x="${cItem.x - 20}" y="${cItem.y + 40}" />
+                    </connection>
+                  </connectionPointIn>
+                </variable>
+                <variable formalParameter="IN2">
+                  <connectionPointIn>
+                    <relPosition x="-20" y="60" />
+                    <connection refLocalId="${setID}">
+                      <position x="${cItem.x - 40}" y="${cItem.y + 220}" />
+                      <position x="${cItem.x - 20}" y="${cItem.y + 60}" />
+                    </connection>
+                  </connectionPointIn>
+                </variable>
+              </inputVariables>
+              <inOutVariables />
+              <outputVariables>
+              <variable formalParameter="ENO">
+                <connectionPointOut>
+                  <relPosition x="90" y="20"/>
+                </connectionPointOut>
+              </variable>
+                <variable formalParameter="OUT">
+                  <connectionPointOut>
+                    <relPosition x="90" y="40" />
+                  </connectionPointOut>
+                </variable>
+              </outputVariables>
+            </block>
+            `
+
+            // 赋值AO变量
+            let AOID = `${index + 1}${sIndex}${i}38`
+            xml += `
+            <outVariable localId="${AOID}" width="40" height="40" negated="false">
+              <position x="${cItem.x - 400}" y="${cItem.y + 200}" />
+              <connectionPointIn>
+                <relPosition x="60" y="20" />
+                <connection refLocalId="${mulBlockId}" formalParameter="OUT">
+                  <position x="${cItem.x + 90}" y="${cItem.y + 4}" />
+                  <position x="${cItem.x - 340}" y="${cItem.y + 20}" />
+                </connection>
+              </connectionPointIn>
+              <expression>${cItem.label}</expression>
+            </outVariable>`
           }
         }
       } else if (sItem.type === 'delay') {
@@ -1117,16 +1330,65 @@ const getXMLFile = () => {
           <configuration name="Config0">
             <resource name="Res0">
               <task name="task0" priority="0" interval="T#20ms">
-                <pouInstance name="instance0" typeName="program0"/>
+              ${instanceXml}
               </task>
             </resource>
+            <globalVars>
+              <variable name="DI1">
+                <type><BOOL /></type>
+                <initialValue>
+                  <simpleValue value="false" />
+                </initialValue>
+              </variable>
+              <variable name="DI2">
+                <type><BOOL /></type>
+                <initialValue>
+                  <simpleValue value="false" />
+                </initialValue>
+              </variable>
+              <variable name="AI1">
+                <type><INT /></type>
+                <initialValue>
+                  <simpleValue value="4" />
+                </initialValue>
+              </variable>
+              <variable name="AI2">
+                <type><INT /></type>
+                <initialValue>
+                  <simpleValue value="4" />
+                </initialValue>
+              </variable>
+              <variable name="DO1">
+                <type><BOOL /></type>
+                <initialValue>
+                  <simpleValue value="false" />
+                </initialValue>
+              </variable>
+              <variable name="DO2">
+                <type><BOOL /></type>
+                <initialValue>
+                  <simpleValue value="false" />
+                </initialValue>
+              </variable>
+              <variable name="AO1">
+                <type><INT /></type>
+                <initialValue>
+                  <simpleValue value="2" />
+                </initialValue>
+              </variable>
+              <variable name="AO2">
+                <type><INT /></type>
+                <initialValue>
+                  <simpleValue value="2" />
+                </initialValue>
+              </variable>
+            </globalVars>
           </configuration>
         </configurations>
       </instances>
     </project>`
 
   console.log(xml)
-
   // 生成文件下载
   const blob = new Blob([xml], { type: 'text/plain;charset=utf-8' })
 
@@ -1137,6 +1399,24 @@ const getXMLFile = () => {
   URL.revokeObjectURL(link.href)
   link.remove()
 }
+
+const delProject = () => {
+  if (crtProject.value) {
+    showConfirmDialog({
+      title: '提示',
+      message: '确定要删除这个工程吗'
+    })
+      .then(() => {
+        // on confirm
+        console.log(projectList.value)
+        projectList.value = projectList.value.filter((item) => crtProject.value?.id !== item.id)
+        router.replace({ name: 'Automate' })
+      })
+      .catch(() => {
+        // on cancel
+      })
+  }
+}
 const getSTFile = () => {
   if (!crtProject.value) return
   let st = ''
@@ -1193,7 +1473,10 @@ const getSTFile = () => {
   white-space: nowrap;
   text-overflow: ellipsis;
 }
+.command-container :deep(.van-swipe-cell__right) {
+}
 .del-button {
+  width: 64px;
   height: 100%;
 }
 .van-cell {