wanshenmean
2026-03-17 737dec3c384f394fd6f9849b4480b697d1ba35d5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
<template>
  <div>
    <div v-if="loading" class="loading-container">
      <el-icon class="loading-icon" :size="40"><Loading /></el-icon>
      <p>加载中...</p>
    </div>
 
    <div v-else-if="errorMsg">
      <el-result icon="error" :title="errorMsg">
        <template #extra>
          <el-button type="primary" @click="$router.push('/')">返回列表</el-button>
        </template>
      </el-result>
    </div>
 
    <div v-else>
      <div class="page-header">
        <div class="header-left">
          <h2>
            <el-icon :size="24"><Edit /></el-icon>
            编辑实例
          </h2>
          <p class="text-muted">编辑实例配置: {{ form.id }}</p>
        </div>
        <el-button @click="$router.push('/')">
          <el-icon><Back /></el-icon>
          返回列表
        </el-button>
      </div>
 
      <el-alert
        v-if="isRunning"
        type="warning"
        :closable="false"
        class="mb-4"
        show-icon
      >
        实例正在运行中,修改配置后需要重启实例才能生效
      </el-alert>
 
      <el-row justify="center">
        <el-col :lg="24">
          <el-card shadow="never">
            <el-form :model="form" :rules="rules" ref="formRef" label-width="120px">
              <!-- 基本信息 -->
              <el-divider content-position="left">
                <h3>基本信息</h3>
              </el-divider>
 
              <el-row :gutter="20">
                <el-col :span="12">
                  <el-form-item label="实例ID">
                    <el-input v-model="form.id" disabled />
                    <div class="form-tip">实例ID创建后不可修改</div>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="实例名称" prop="name">
                    <el-input v-model="form.name" />
                  </el-form-item>
                </el-col>
              </el-row>
 
              <el-row :gutter="20">
                <el-col :span="12">
                  <el-form-item label="PLC型号" prop="plcType">
                    <el-select v-model="form.plcType" style="width: 100%">
                      <el-option label="S7-200 Smart" value="S7200Smart" />
                      <el-option label="S7-1200" value="S71200" />
                      <el-option label="S7-1500" value="S71500" />
                      <el-option label="S7-300" value="S7300" />
                      <el-option label="S7-400" value="S7400" />
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="监听端口" prop="port">
                    <el-input-number
                      v-model="form.port"
                      :min="1"
                      :max="65535"
                      style="width: 100%"
                    />
                  </el-form-item>
                </el-col>
              </el-row>
 
              <el-row :gutter="20">
                <el-col :span="12">
                  <el-form-item label="HSL激活码">
                    <el-input v-model="form.activationKey" />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="自动启动">
                    <el-switch v-model="form.autoStart" />
                  </el-form-item>
                </el-col>
              </el-row>
 
              <el-row :gutter="20">
                <el-col :span="12">
                  <el-form-item label="协议模板" prop="protocolTemplateId">
                    <el-select v-model="form.protocolTemplateId" style="width: 100%">
                      <el-option
                        v-for="tpl in protocolTemplates"
                        :key="tpl.id"
                        :label="`${tpl.name} (${tpl.id})`"
                        :value="tpl.id"
                      />
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
 
              <!-- 内存配置 -->
              <el-divider content-position="left">
                <h3>内存配置</h3>
              </el-divider>
 
              <el-row :gutter="20">
                <el-col :span="8">
                  <el-form-item label="M区大小">
                    <el-input-number
                      v-model="form.mRegionSize"
                      :min="0"
                      style="width: 100%"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="I区大小">
                    <el-input-number
                      v-model="form.iRegionSize"
                      :min="0"
                      style="width: 100%"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="Q区大小">
                    <el-input-number
                      v-model="form.qRegionSize"
                      :min="0"
                      style="width: 100%"
                    />
                  </el-form-item>
                </el-col>
              </el-row>
 
              <el-row :gutter="20">
                <el-col :span="8">
                  <el-form-item label="DB块列表">
                    <el-select
                      v-model="form.dbBlockNumbers"
                      multiple
                      filterable
                      allow-create
                      default-first-option
                      :reserve-keyword="false"
                      style="width: 100%"
                      placeholder="输入块号后回车,例如 50、900、901"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="DB块大小">
                    <el-input-number
                      v-model="form.dbBlockSize"
                      :min="0"
                      style="width: 100%"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="4">
                  <el-form-item label="定时器数量">
                    <el-input-number
                      v-model="form.tRegionCount"
                      :min="0"
                      style="width: 100%"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="4">
                  <el-form-item label="计数器数量">
                    <el-input-number
                      v-model="form.cRegionCount"
                      :min="0"
                      style="width: 100%"
                    />
                  </el-form-item>
                </el-col>
              </el-row>
 
              <!-- 提交按钮 -->
              <el-form-item>
                <el-button type="primary" @click="handleSubmit" :loading="submitting">
                  {{ submitting ? '保存中...' : '保存更改' }}
                </el-button>
                <el-button @click="$router.push('/')">取消</el-button>
              </el-form-item>
            </el-form>
          </el-card>
        </el-col>
      </el-row>
    </div>
  </div>
</template>
 
<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
import { Edit, Back, Loading } from '@element-plus/icons-vue'
import * as api from '../api'
import type { InstanceConfig, MemoryRegionConfig, ProtocolTemplate, SiemensPLCType } from '../types'
 
const router = useRouter()
const route = useRoute()
const formRef = ref<FormInstance>()
 
const form = ref({
  id: '',
  name: '',
  plcType: 'S71200' as SiemensPLCType,
  port: 102,
  activationKey: '',
  autoStart: false,
  protocolTemplateId: '',
  mRegionSize: 1024,
  dbBlockCount: 0,
  dbBlockNumbers: [] as Array<number | string>,
  dbBlockSize: 65536,
  iRegionSize: 256,
  qRegionSize: 256,
  tRegionCount: 64,
  cRegionCount: 64
})
 
const loading = ref(true)
const errorMsg = ref('')
const submitting = ref(false)
const isRunning = ref(false)
const protocolTemplates = ref<ProtocolTemplate[]>([])
 
const id = route.params.id as string
 
const rules: FormRules = {
  name: [
    { required: true, message: '请输入实例名称', trigger: 'blur' }
  ],
  plcType: [
    { required: true, message: '请选择PLC型号', trigger: 'change' }
  ],
  port: [
    { required: true, message: '请输入监听端口', trigger: 'blur' },
    { type: 'number', min: 1, max: 65535, message: '端口必须在 1-65535 之间', trigger: 'blur' }
  ],
  protocolTemplateId: [
    { required: true, message: '请选择协议模板', trigger: 'change' }
  ]
}
 
onMounted(async () => {
  try {
    protocolTemplates.value = await api.getProtocolTemplates()
 
    // 获取实例状态
    const state = await api.getInstance(id)
    if (!state) {
      errorMsg.value = `实例 "${id}" 不存在`
      loading.value = false
      return
    }
 
    isRunning.value = state.status === 'Running'
 
    // 获取实例配置
    const config = await api.getInstanceConfig(id)
    if (!config) {
      errorMsg.value = `无法加载实例 "${id}" 的配置`
      loading.value = false
      return
    }
 
    form.value = {
      id: config.id,
      name: config.name,
      plcType: config.plcType,
      port: config.port,
      activationKey: config.activationKey,
      autoStart: config.autoStart,
      protocolTemplateId: config.protocolTemplateId || '',
      mRegionSize: config.memoryConfig.mRegionSize,
      dbBlockCount: 0,
      dbBlockNumbers: toDbBlockNumbers(config.memoryConfig.dbBlockNumbers, config.memoryConfig.dbBlockCount),
      dbBlockSize: config.memoryConfig.dbBlockSize,
      iRegionSize: config.memoryConfig.iRegionSize,
      qRegionSize: config.memoryConfig.qRegionSize,
      tRegionCount: config.memoryConfig.tRegionCount,
      cRegionCount: config.memoryConfig.cRegionCount
    }
  } catch (err) {
    console.error('加载实例配置失败:', err)
    errorMsg.value = '加载实例配置失败,请查看控制台'
  } finally {
    loading.value = false
  }
})
 
async function handleSubmit() {
  if (!formRef.value) return
 
  await formRef.value.validate(async (valid) => {
    if (!valid) return
 
    submitting.value = true
 
    try {
      const dbBlockNumbers = normalizeDbBlockNumbers(form.value.dbBlockNumbers)
      if (dbBlockNumbers.length === 0) {
        ElMessage.error('请至少配置一个DB块号,例如 50,900,901')
        return
      }
 
      const memoryConfig: MemoryRegionConfig = {
        mRegionSize: form.value.mRegionSize > 0 ? form.value.mRegionSize : 1024,
        dbBlockCount: 0,
        dbBlockNumbers,
        dbBlockSize: form.value.dbBlockSize > 0 ? form.value.dbBlockSize : 1024,
        iRegionSize: form.value.iRegionSize > 0 ? form.value.iRegionSize : 256,
        qRegionSize: form.value.qRegionSize > 0 ? form.value.qRegionSize : 256,
        tRegionCount: form.value.tRegionCount > 0 ? form.value.tRegionCount : 64,
        cRegionCount: form.value.cRegionCount > 0 ? form.value.cRegionCount : 64
      }
 
      const config: InstanceConfig = {
        id: form.value.id,
        name: form.value.name,
        plcType: form.value.plcType,
        port: form.value.port,
        activationKey: form.value.activationKey,
        autoStart: form.value.autoStart,
        protocolTemplateId: form.value.protocolTemplateId,
        memoryConfig
      }
 
      const result = await api.updateInstance(form.value.id, config)
 
      if (result) {
        ElMessage.success(`实例 "${form.value.id}" 更新成功!`)
        router.push('/')
      } else {
        ElMessage.error('更新实例失败')
      }
    } catch (err) {
      console.error('更新实例失败:', err)
      ElMessage.error('更新实例失败,请查看控制台')
    } finally {
      submitting.value = false
    }
  })
}
 
function normalizeDbBlockNumbers(input: Array<number | string>): number[] {
  return Array.from(new Set(
    input
      .map(x => Number(String(x).trim()))
      .filter(x => Number.isInteger(x) && x > 0)
  )).sort((a, b) => a - b)
}
 
function toDbBlockNumbers(dbBlockNumbers: number[] | undefined, dbBlockCount: number): Array<number | string> {
  if (dbBlockNumbers && dbBlockNumbers.length > 0) {
    return dbBlockNumbers
  }
 
  if (dbBlockCount > 0) {
    return Array.from({ length: dbBlockCount }, (_, idx) => idx + 1)
  }
 
  return []
}
</script>
 
<style scoped>
.page-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 20px;
  flex-wrap: wrap;
  gap: 16px;
}
 
.header-left h2 {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0 0 8px 0;
}
 
.text-muted {
  color: #909399;
  margin: 0;
}
 
.loading-container {
  text-align: center;
  padding: 60px 0;
  color: #909399;
}
 
.loading-icon {
  animation: spin 1s linear infinite;
}
 
@keyframes spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}
 
.form-tip {
  font-size: 12px;
  color: #909399;
  margin-top: 4px;
}
 
.el-divider h3 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  color: #303133;
}
 
.mb-4 {
  margin-bottom: 16px;
}
</style>