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
<template>
  <div class="load-vehicle-config">
    <el-row :gutter="20">
      <el-col :span="12">
        <el-form-item label="车辆容量(mm)">
          <el-input-number
            v-model="config.vehicleCapacity"
            :min="1"
            :max="10000"
            :step="100"
            style="width: 100%;"
            @change="emitConfigUpdate"
          />
          <span class="form-tip">车辆最大容量</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="车辆速度(格/秒)">
          <el-input-number
            v-model="config.vehicleSpeed"
            :min="0.1"
            :max="10"
            :step="0.1"
            :precision="1"
            style="width: 100%;"
            @change="emitConfigUpdate"
          />
          <span class="form-tip">车辆运动速度,默认1格/秒</span>
        </el-form-item>
      </el-col>
    </el-row>
 
    <el-row :gutter="20">
      <el-col :span="12">
        <el-form-item label="玻璃间隙(mm)">
          <el-input-number
            v-model="config.glassGap"
            :min="0"
            :max="1000"
            :step="10"
            style="width: 100%;"
            @change="emitConfigUpdate"
          />
          <span class="form-tip">多块玻璃之间的物理间隔空隙,默认200mm</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="默认玻璃长度(mm)">
          <el-input-number
            v-model="config.defaultGlassLength"
            :min="100"
            :max="10000"
            :step="100"
            style="width: 100%;"
            @change="emitConfigUpdate"
          />
          <span class="form-tip">当玻璃未提供长度时使用的默认值</span>
        </el-form-item>
      </el-col>
    </el-row>
 
    <el-row :gutter="20">
      <el-col :span="12">
        <el-form-item label="初始位置(格)">
          <el-input-number
            v-model="config.homePosition"
            :min="0"
            :max="1000"
            :step="1"
            style="width: 100%;"
            @change="emitConfigUpdate"
          />
          <span class="form-tip">车辆初始位置(格子)</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="运动距离范围">
          <el-input-number
            v-model="config.minRange"
            :min="1"
            :max="1000"
            :step="1"
            style="width: 40%;"
            placeholder="最小"
            @change="emitConfigUpdate"
          />
          <span style="margin: 0 2%;">~</span>
          <el-input-number
            v-model="config.maxRange"
            :min="1"
            :max="1000"
            :step="1"
            style="width: 40%;"
            placeholder="最大"
            @change="emitConfigUpdate"
          />
          <span class="form-tip">运动距离范围(格子)</span>
        </el-form-item>
      </el-col>
    </el-row>
 
    <el-row :gutter="20">
      <el-col :span="12">
        <el-form-item label="空闲监控间隔(秒)">
          <el-input-number
            v-model="idleMonitorIntervalSeconds"
            :min="0.5"
            :max="10"
            :step="0.1"
            :precision="1"
            style="width: 100%;"
          />
          <span class="form-tip">空闲状态监控间隔,默认2秒</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="任务监控间隔(秒)">
          <el-input-number
            v-model="taskMonitorIntervalSeconds"
            :min="0.5"
            :max="10"
            :step="0.1"
            :precision="1"
            style="width: 100%;"
          />
          <span class="form-tip">任务执行监控间隔,默认1秒</span>
        </el-form-item>
      </el-col>
    </el-row>
 
    <el-row :gutter="20">
      <el-col :span="12">
        <el-form-item label="MES确认超时(秒)">
          <el-input-number
            v-model="mesConfirmTimeoutSeconds"
            :min="5"
            :max="300"
            :step="1"
            style="width: 100%;"
          />
          <span class="form-tip">等待MES确认的超时时间,默认30秒</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="自动上料">
          <el-switch v-model="config.autoFeed" @change="emitConfigUpdate" />
          <span class="form-tip">是否自动触发上料请求</span>
        </el-form-item>
      </el-col>
    </el-row>
 
    <el-row :gutter="20">
      <el-col :span="12">
        <el-form-item label="最大重试次数">
          <el-input-number
            v-model="config.maxRetryCount"
            :min="0"
            :max="10"
            :step="1"
            style="width: 100%;"
            @change="emitConfigUpdate"
          />
        </el-form-item>
      </el-col>
    </el-row>
 
    <el-form-item label="位置映射">
      <div class="position-mapping">
        <div
          v-for="(item, index) in positionList"
          :key="item.id"
          class="mapping-item"
        >
          <el-input
            v-model="item.key"
            placeholder="位置代码(如1001/1002)"
            size="small"
            style="width: 150px; margin-right: 10px;"
            @change="handlePositionKeyChange(index)"
          />
          <el-input-number
            v-model="item.value"
            :min="0"
            :max="1000"
            :step="1"
            size="small"
            style="width: 120px; margin-right: 10px;"
            placeholder="位置值(格)"
            @change="handlePositionValueChange(index)"
          />
          <el-button
            type="danger"
            size="small"
            @click="removePositionMapping(index)"
          >
            删除
          </el-button>
        </div>
        <el-button type="primary" size="small" @click="addPositionMapping">
          添加位置映射
        </el-button>
      </div>
      <span class="form-tip">将MES编号(如900/901)映射为实际位置值(格子)</span>
    </el-form-item>
  </div>
</template>
 
<script setup>
import { ref, watch } from 'vue'
 
const props = defineProps({
  modelValue: {
    type: Object,
    default: () => ({})
  }
})
 
const emit = defineEmits(['update:modelValue'])
 
// 配置数据
const config = ref({
  vehicleCapacity: 6000,
  vehicleSpeed: 1.0,
  glassGap: 200,
  defaultGlassLength: 2000,
  homePosition: 0,
  minRange: 1,
  maxRange: 100,
  idleMonitorIntervalMs: 2000,
  taskMonitorIntervalMs: 1000,
  mesConfirmTimeoutMs: 30000,
  autoFeed: true,
  maxRetryCount: 5,
  positionMapping: {}
})
 
// 位置映射编辑列表
const positionList = ref([])
let suppressPositionSync = false
let suppressEmit = false
 
const emitConfigUpdate = () => {
  if (suppressEmit) return
  emit('update:modelValue', { ...config.value })
}
 
const syncPositionListFromConfig = () => {
  suppressPositionSync = true
  const entries = Object.entries(config.value.positionMapping || {})
  positionList.value = entries.map(([key, value], idx) => ({
    id: `${key}_${idx}_${Date.now()}`,
    key,
    value: value ?? 1
  }))
  suppressPositionSync = false
}
 
const applyPositionListToConfig = () => {
  if (suppressPositionSync) return
  const mapping = {}
  positionList.value.forEach((item) => {
    if (item.key && item.key.trim()) {
      mapping[item.key.trim()] = item.value ?? 1
    }
  })
  config.value.positionMapping = mapping
  emitConfigUpdate()
}
 
syncPositionListFromConfig()
 
// 时间字段(秒)- 用于前端显示和输入
const idleMonitorIntervalSeconds = ref(2.0)
const taskMonitorIntervalSeconds = ref(1.0)
const mesConfirmTimeoutSeconds = ref(30)
 
// 监听props变化
watch(() => props.modelValue, (newVal) => {
  if (newVal && Object.keys(newVal).length > 0) {
    suppressEmit = true
    config.value = {
      vehicleCapacity: newVal.vehicleCapacity ?? 6000,
      vehicleSpeed: newVal.vehicleSpeed ?? 1.0,
      glassGap: newVal.glassGap ?? 200,
      defaultGlassLength: newVal.defaultGlassLength ?? 2000,
      homePosition: newVal.homePosition ?? 0,
      minRange: newVal.minRange ?? 1,
      maxRange: newVal.maxRange ?? 100,
      idleMonitorIntervalMs: newVal.idleMonitorIntervalMs ?? 2000,
      taskMonitorIntervalMs: newVal.taskMonitorIntervalMs ?? 1000,
      mesConfirmTimeoutMs: newVal.mesConfirmTimeoutMs ?? 30000,
      autoFeed: newVal.autoFeed ?? true,
      maxRetryCount: newVal.maxRetryCount ?? 5,
      positionMapping: newVal.positionMapping || {}
    }
    // 将毫秒转换为秒用于显示
    idleMonitorIntervalSeconds.value = (config.value.idleMonitorIntervalMs ?? 2000) / 1000
    taskMonitorIntervalSeconds.value = (config.value.taskMonitorIntervalMs ?? 1000) / 1000
    mesConfirmTimeoutSeconds.value = (config.value.mesConfirmTimeoutMs ?? 30000) / 1000
    syncPositionListFromConfig()
    suppressEmit = false
  }
}, { immediate: true, deep: true })
 
// 监听秒字段变化,转换为毫秒并更新config
watch(idleMonitorIntervalSeconds, (val) => {
  config.value.idleMonitorIntervalMs = Math.round(val * 1000)
  emitConfigUpdate()
})
 
watch(taskMonitorIntervalSeconds, (val) => {
  config.value.taskMonitorIntervalMs = Math.round(val * 1000)
  emitConfigUpdate()
})
 
watch(mesConfirmTimeoutSeconds, (val) => {
  config.value.mesConfirmTimeoutMs = Math.round(val * 1000)
  emitConfigUpdate()
})
 
// 监听config其他字段变化,同步到父组件
// 位置映射相关方法
const addPositionMapping = () => {
  const nextIndex = positionList.value.length + 1
  positionList.value.push({
    id: `POS_${Date.now()}_${nextIndex}`,
    key: '',
    value: 1
  })
}
 
const removePositionMapping = (idx) => {
  positionList.value.splice(idx, 1)
  applyPositionListToConfig()
}
 
const handlePositionKeyChange = () => {
  applyPositionListToConfig()
}
 
const handlePositionValueChange = () => {
  applyPositionListToConfig()
}
</script>
 
<style scoped>
.form-tip {
  margin-left: 10px;
  font-size: 12px;
  color: #909399;
}
 
.position-mapping {
  width: 100%;
}
 
.mapping-item {
  display: flex;
  align-items: center;
  margin-bottom: 12px;
  padding: 12px;
  border: 1px solid #ebeef5;
  border-radius: 6px;
  background-color: #fafafa;
}
</style>