wanshenmean
2026-03-13 d18970b451b863b465e356e2a5e4576707a17355
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
<template>
  <div>
    <div v-if="loading" class="text-center py-5">
      <div class="spinner-border text-primary" role="status">
        <span class="visually-hidden">加载中...</span>
      </div>
    </div>
 
    <div v-else-if="errorMsg">
      <div class="alert alert-danger">{{ errorMsg }}</div>
      <router-link to="/" class="btn btn-primary">返回列表</router-link>
    </div>
 
    <div v-else>
      <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
          <h2 class="mb-0">
            <i class="bi bi-pencil me-2"></i>编辑实例
          </h2>
          <p class="text-muted mb-0 mt-1">编辑实例配置: {{ form.id }}</p>
        </div>
        <router-link to="/" class="btn btn-outline-secondary">
          <i class="bi bi-arrow-left me-1"></i>返回列表
        </router-link>
      </div>
 
      <div v-if="isRunning" class="alert alert-warning">
        <i class="bi bi-exclamation-triangle-fill me-2"></i>
        实例正在运行中,修改配置后需要重启实例才能生效
      </div>
 
      <div class="row justify-content-center">
        <div class="col-lg-8">
          <div class="card">
            <div class="card-body">
              <form @submit.prevent="handleSubmit">
                <!-- 基本信息 -->
                <h5 class="card-title mb-3">基本信息</h5>
                <div class="row mb-3">
                  <div class="col-md-6">
                    <label for="id" class="form-label">实例ID</label>
                    <input
                      type="text"
                      class="form-control"
                      id="id"
                      v-model="form.id"
                      disabled
                    >
                    <div class="form-text">实例ID创建后不可修改</div>
                  </div>
                  <div class="col-md-6">
                    <label for="name" class="form-label">实例名称 *</label>
                    <input
                      type="text"
                      class="form-control"
                      id="name"
                      v-model="form.name"
                      required
                    >
                  </div>
                </div>
 
                <div class="row mb-3">
                  <div class="col-md-6">
                    <label for="plcType" class="form-label">PLC型号 *</label>
                    <select class="form-select" id="plcType" v-model="form.plcType" required>
                      <option value="S7200Smart">S7-200 Smart</option>
                      <option value="S71200">S7-1200</option>
                      <option value="S71500">S7-1500</option>
                      <option value="S7300">S7-300</option>
                      <option value="S7400">S7-400</option>
                    </select>
                  </div>
                  <div class="col-md-6">
                    <label for="port" class="form-label">监听端口 *</label>
                    <input
                      type="number"
                      class="form-control"
                      id="port"
                      v-model.number="form.port"
                      min="1"
                      max="65535"
                      required
                    >
                  </div>
                </div>
 
                <div class="row mb-3">
                  <div class="col-md-6">
                    <label for="activationKey" class="form-label">HSL激活码</label>
                    <input
                      type="text"
                      class="form-control"
                      id="activationKey"
                      v-model="form.activationKey"
                    >
                  </div>
                  <div class="col-md-6 d-flex align-items-center">
                    <div class="form-check">
                      <input class="form-check-input" type="checkbox" id="autoStart" v-model="form.autoStart">
                      <label class="form-check-label" for="autoStart">
                        自动启动
                      </label>
                    </div>
                  </div>
                </div>
 
                <!-- 内存配置 -->
                <h5 class="card-title mb-3 mt-4">内存配置</h5>
                <div class="row mb-3">
                  <div class="col-md-4">
                    <label for="mRegionSize" class="form-label">M区域大小</label>
                    <input
                      type="number"
                      class="form-control"
                      id="mRegionSize"
                      v-model.number="form.mRegionSize"
                      min="0"
                    >
                  </div>
                  <div class="col-md-4">
                    <label for="iRegionSize" class="form-label">I区域大小</label>
                    <input
                      type="number"
                      class="form-control"
                      id="iRegionSize"
                      v-model.number="form.iRegionSize"
                      min="0"
                    >
                  </div>
                  <div class="col-md-4">
                    <label for="qRegionSize" class="form-label">Q区域大小</label>
                    <input
                      type="number"
                      class="form-control"
                      id="qRegionSize"
                      v-model.number="form.qRegionSize"
                      min="0"
                    >
                  </div>
                </div>
 
                <div class="row mb-3">
                  <div class="col-md-4">
                    <label for="dbBlockCount" class="form-label">DB块数量</label>
                    <input
                      type="number"
                      class="form-control"
                      id="dbBlockCount"
                      v-model.number="form.dbBlockCount"
                      min="0"
                    >
                  </div>
                  <div class="col-md-4">
                    <label for="dbBlockSize" class="form-label">DB块大小</label>
                    <input
                      type="number"
                      class="form-control"
                      id="dbBlockSize"
                      v-model.number="form.dbBlockSize"
                      min="0"
                    >
                  </div>
                  <div class="col-md-2">
                    <label for="tRegionCount" class="form-label">定时器数量</label>
                    <input
                      type="number"
                      class="form-control"
                      id="tRegionCount"
                      v-model.number="form.tRegionCount"
                      min="0"
                    >
                  </div>
                  <div class="col-md-2">
                    <label for="cRegionCount" class="form-label">计数器数量</label>
                    <input
                      type="number"
                      class="form-control"
                      id="cRegionCount"
                      v-model.number="form.cRegionCount"
                      min="0"
                    >
                  </div>
                </div>
 
                <!-- 提交按钮 -->
                <div class="d-flex gap-2 mt-4">
                  <button type="submit" class="btn btn-primary" :disabled="submitting">
                    <span v-if="submitting" class="spinner-border spinner-border-sm me-1"></span>
                    <i v-else class="bi bi-check-lg me-1"></i>
                    {{ submitting ? '保存中...' : '保存更改' }}
                  </button>
                  <router-link to="/" class="btn btn-outline-secondary">取消</router-link>
                </div>
              </form>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
 
<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import * as api from '../api'
import type { InstanceConfig, MemoryRegionConfig, SiemensPLCType } from '../types'
 
const router = useRouter()
const route = useRoute()
 
const form = ref({
  id: '',
  name: '',
  plcType: 'S71200' as SiemensPLCType,
  port: 102,
  activationKey: '',
  autoStart: false,
  mRegionSize: 1024,
  dbBlockCount: 100,
  dbBlockSize: 1024,
  iRegionSize: 256,
  qRegionSize: 256,
  tRegionCount: 64,
  cRegionCount: 64
})
 
const loading = ref(true)
const errorMsg = ref('')
const submitting = ref(false)
const isRunning = ref(false)
 
const id = route.params.id as string
 
onMounted(async () => {
  try {
    // 获取实例状态
    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,
      mRegionSize: config.memoryConfig.mRegionSize,
      dbBlockCount: config.memoryConfig.dBBBlockCount,
      dbBlockSize: config.memoryConfig.dBBBlockSize,
      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() {
  submitting.value = true
 
  try {
    const memoryConfig: MemoryRegionConfig = {
      mRegionSize: form.value.mRegionSize > 0 ? form.value.mRegionSize : 1024,
      dBBBlockCount: form.value.dbBlockCount > 0 ? form.value.dbBlockCount : 100,
      dBBBlockSize: 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,
      memoryConfig
    }
 
    const result = await api.updateInstance(form.value.id, config)
 
    if (result) {
      alert(`实例 "${form.value.id}" 更新成功!`)
      router.push('/')
    } else {
      alert('更新实例失败')
    }
  } catch (err) {
    console.error('更新实例失败:', err)
    alert('更新实例失败,请查看控制台')
  } finally {
    submitting.value = false
  }
}
</script>
 
<style scoped>
.card-title {
  color: #495057;
  font-weight: 600;
}
 
.form-label {
  font-weight: 500;
  color: #495057;
}
</style>