From 5e2434c46bcc4d8ed1bdaa7f9fea8c64451e01cc Mon Sep 17 00:00:00 2001 From: luoyifan Date: Thu, 3 Jul 2025 10:32:41 +0800 Subject: [PATCH 1/3] =?UTF-8?q?EnvManager=20=E5=90=AF=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/manager/EnvManager.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/manager/EnvManager.ts b/src/core/manager/EnvManager.ts index 192e2ac..66261e9 100644 --- a/src/core/manager/EnvManager.ts +++ b/src/core/manager/EnvManager.ts @@ -60,7 +60,7 @@ export default class EnvManager { system.showLoading() worldModel.state.runState.isLoading = true - worldModel.state.runState.currentEnv = Object.freeze(env) + worldModel.state.runState.currentEnv = env try { await LCC.serverStart() await LCC.loadExecutor() From 7e5e46ec70748a540334dd406ad67eab34e0edf6 Mon Sep 17 00:00:00 2001 From: luoyifan Date: Thu, 3 Jul 2025 13:18:07 +0800 Subject: [PATCH 2/3] =?UTF-8?q?Script=20=E4=BF=9D=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/script/LCCScript.ts | 12 +++++ src/editor/ModelMain.vue | 2 +- src/editor/widgets/script/ScriptView.vue | 86 ++++++++++++++++++++++++++++++-- src/types/LCC.d.ts | 6 +++ 4 files changed, 102 insertions(+), 4 deletions(-) diff --git a/src/core/script/LCCScript.ts b/src/core/script/LCCScript.ts index 5fb5ee1..f99204c 100644 --- a/src/core/script/LCCScript.ts +++ b/src/core/script/LCCScript.ts @@ -54,6 +54,18 @@ export default class LCCScript implements LCC { return res.data } + saveAndSyncScripts(scriptList: { name: string; content: string }[]): Promise> { + if (!worldModel.state.project_uuid || !worldModel.state.runState.currentEnvId) { + return Promise.reject(new Error('Project UUID or Environment ID is not set.')) + } + + return Request.request.post('/api/workbench/LccController@saveAndSyncScripts', { + projectUUID: worldModel.state.project_uuid, + envId: worldModel.state.runState.currentEnvId, + scriptList: scriptList + }) + } + // 从后台读取所有车 async loadExecutor(): Promise> { const res = await Request.request.post('/api/workbench/LccController@loadExecutor', { diff --git a/src/editor/ModelMain.vue b/src/editor/ModelMain.vue index 4759006..d11a4ee 100644 --- a/src/editor/ModelMain.vue +++ b/src/editor/ModelMain.vue @@ -23,7 +23,7 @@
- + 保存并同步 + 添加 @@ -25,8 +28,9 @@ - + + @@ -44,6 +48,7 @@ import localforage from 'localforage' import YvSrcEditor from '@/components/YvSrcEditor.vue' import IWidgets from '../IWidgets.js' import CodeDropper from '@/core/manager/CodeDropper.js' +import { escByKeyboard } from '@/core/ModelUtils.js' export default { name: 'ScriptView', @@ -52,11 +57,59 @@ export default { }, mixins: [IWidgets], data() { + const me = this + return { scriptIsRunning: false, scriptIndex: 0, scriptList: [], - searchKeyword: '' + searchKeyword: '', + scriptMenu: [ + { + name: 'removeScript', label: '删除', + click(menu, { class: classJoinStr }) { + // 拿到 classJoinStr 最后一截字符串, 取到 scriptIndex + const index = parseInt(classJoinStr.split('_').pop()) + if (isNaN(index) || index < 0 || index >= me.scriptList.length) { + system.msg('脚本索引错误', 'error') + return + } + const script = me.scriptList[index] + if (!script) { + system.msg('脚本不存在', 'error') + return + } + if (confirm(`确定要删除脚本 "${script.name}" 吗?`)) { + _.remove(me.scriptList, (item, idx) => idx === index) + if (me.scriptList.length === 0) { + me.scriptIndex = 0 + } else if (me.scriptIndex >= me.scriptList.length) { + me.scriptIndex = me.scriptList.length - 1 + } + } + } + }, + { + name: 'rename', label: '重命名', + click(menu, { class: classJoinStr }) { + // 拿到 classJoinStr 最后一截字符串, 取到 scriptIndex + const index = parseInt(classJoinStr.split('_').pop()) + if (isNaN(index) || index < 0 || index >= me.scriptList.length) { + system.msg('脚本索引错误', 'error') + return + } + const script = me.scriptList[index] + if (!script) { + system.msg('脚本不存在', 'error') + return + } + const newName = prompt('请输入新的脚本名称', script.name) + if (newName && newName.trim()) { + script.name = newName.trim() + } + } + } + ] } }, watch: { @@ -71,6 +124,33 @@ export default { this.loadFromLocal() }, methods: { + async saveAndSyncScripts() { + // 保存当前脚本索引 + const originName = this.scriptList[this.scriptIndex]?.name + system.showLoading() + try { + const serverResponse = await LCC.saveAndSyncScripts(this.scriptList) + if (serverResponse.success) { + this.scriptList = serverResponse.data || [] + + // 从 originName 中找到对应的脚本索引 + if (originName) { + const index = this.scriptList.findIndex(item => item.name === originName) + if (index >= 0) { + this.scriptIndex = index + return + } + } + + this.scriptIndex = 0 + } + + system.msg('脚本已保存并同步到服务器') + + } finally { + system.clearLoading() + } + }, runScript() { this.scriptIsRunning = true this.viewport.modelManager.executestring(this.currentScript) diff --git a/src/types/LCC.d.ts b/src/types/LCC.d.ts index 3a9e071..cbb5476 100644 --- a/src/types/LCC.d.ts +++ b/src/types/LCC.d.ts @@ -26,6 +26,12 @@ declare interface LCC { * 获取所有车,并放到 Model 上 */ loadExecutor(): Promise> + + /** + * 保存并同步当前项目所有脚本 + * @param scriptList + */ + saveAndSyncScripts(scriptList: { name: string, content: string }[]): Promise> } type ContainerT = 'pallet' | 'tote' | 'carton' | 'box' From a1bed4579bbdee979bfa1587f23cce9c296f0516 Mon Sep 17 00:00:00 2001 From: luoyifan Date: Thu, 3 Jul 2025 13:30:06 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=90=AF=E5=8A=A8=E9=A1=BA=E5=BA=8F?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/manager/EnvManager.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/core/manager/EnvManager.ts b/src/core/manager/EnvManager.ts index 66261e9..bbb2703 100644 --- a/src/core/manager/EnvManager.ts +++ b/src/core/manager/EnvManager.ts @@ -63,7 +63,7 @@ export default class EnvManager { worldModel.state.runState.currentEnv = env try { await LCC.serverStart() - await LCC.loadExecutor() + await worldModel.lccMqttManager.start(env.envConfig.frontendMqtt) await LCC.loadInv() this.client = mqtt.connect(env.envConfig.mqtt.websocket, { @@ -77,7 +77,8 @@ export default class EnvManager { keepalive: 60 }) - await worldModel.lccMqttManager.start(env.envConfig.frontendMqtt) + await LCC.loadExecutor() + this.client.on('connect', this.onMqttConnect) this.client.on('message', this.onMqttMessage) this.client.on('error', this.onMqttError)