FatigueTestPage.vue 15.4 KB
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 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
<script setup lang="ts">
import { ref, onBeforeMount, computed, watch, nextTick } from 'vue'
import { useI18n } from 'vue-i18n'
import { useToast } from 'vuestic-ui'
import fatigueTestApi, { FatigueTestConfig, FatigueTestStatus } from '../../services/fatigueTest'
import optionsApi from '../../services/options'

const { t } = useI18n()
const { init: notify } = useToast()

// 加载状态
const isLoading = ref(false)
const isSaving = ref(false)
const isStarting = ref(false)
const isStopping = ref(false)

// 配置数据
const config = ref<FatigueTestConfig>({
  mapId: '',
  robotIds: [],
  locationIds: [],
  taskIntervalMs: 1000,
})

// 运行状态
const status = ref<FatigueTestStatus>({
  isRunning: false,
  mapId: null,
  robotCount: 0,
  locationCount: 0,
  taskIntervalMs: 1000,
})

// 下拉选项
const robotOptions = ref<{ text: string; value: string; code?: string }[]>([])
const locationOptions = ref<{ text: string; value: string; code?: string }[]>([])
const isLoadingRobots = ref(false)
const isLoadingLocations = ref(false)

// 选中的选项对象(用于正确显示)
const selectedRobots = ref<{ text: string; value: string; code?: string }[]>([])
const selectedLocations = ref<{ text: string; value: string; code?: string }[]>([])

// 任务间隔选项(毫秒)
const intervalOptions = [
  { text: '100ms', value: 100 },
  { text: '500ms', value: 500 },
  { text: '1s', value: 1000 },
  { text: '2s', value: 2000 },
  { text: '5s', value: 5000 },
  { text: '10s', value: 10000 },
]

// 表单验证错误
const errors = ref<{
  robotIds?: string
  locationIds?: string
}>({})

// 计算属性:是否正在运行
const isRunning = computed(() => status.value?.isRunning || false)

// 计算属性:表单是否有效
const isFormValid = computed(() => {
  return config.value.robotIds.length > 0 && config.value.locationIds.length >= 2
})

// 监听选项变化,更新选中的对象
watch(
  () => config.value.robotIds,
  (newIds) => {
    selectedRobots.value = robotOptions.value.filter((opt) => newIds.includes(opt.value))
  },
  { immediate: true },
)

watch(
  () => config.value.locationIds,
  (newIds) => {
    selectedLocations.value = locationOptions.value.filter((opt) => newIds.includes(opt.value))
  },
  { immediate: true },
)

// 监听选项数据加载完成
watch(
  () => robotOptions.value,
  (newOptions) => {
    if (newOptions.length > 0 && config.value.robotIds.length > 0) {
      selectedRobots.value = newOptions.filter((opt) => config.value.robotIds.includes(opt.value))
    }
  },
  { immediate: true },
)

watch(
  () => locationOptions.value,
  (newOptions) => {
    if (newOptions.length > 0 && config.value.locationIds.length > 0) {
      selectedLocations.value = newOptions.filter((opt) => config.value.locationIds.includes(opt.value))
    }
  },
  { immediate: true },
)

// 加载机器人列表 - 使用 /api/options/robots (OptionsController)
const loadRobots = async () => {
  isLoadingRobots.value = true
  try {
    const res: any = await optionsApi.getRobots()
    // OptionsController 返回格式: { value, text, code }
    const list = Array.isArray(res) ? res : res?.data || res?.Data || []
    robotOptions.value = list.map((item: any) => ({
      text: item.text || item.name || item.label || item.RobotCode || item.id,
      value: item.value || item.id,
      code: item.code || item.RobotCode,
    }))
  } catch (err: any) {
    notify({ message: err?.message || t('fatigueTest.messages.loadRobotsFailed'), color: 'danger' })
  } finally {
    isLoadingRobots.value = false
  }
}

// 加载库位列表 - 使用 /api/options/locations (OptionsController)
const loadLocations = async () => {
  isLoadingLocations.value = true
  try {
    const res: any = await optionsApi.getLocations()
    // OptionsController 返回格式: { value, text, code }
    const list = Array.isArray(res) ? res : res?.data || res?.Data || []
    locationOptions.value = list.map((item: any) => ({
      text: item.text || item.name || item.label || item.LocationName || item.id,
      value: item.value || item.id,
      code: item.code || item.LocationCode,
    }))
  } catch (err: any) {
    notify({ message: err?.message || t('fatigueTest.messages.loadLocationsFailed'), color: 'danger' })
  } finally {
    isLoadingLocations.value = false
  }
}

// 加载配置
const loadConfig = async () => {
  try {
    const res: any = await fatigueTestApi.getConfig()
    const data = res?.data || res?.Data
    if (data) {
      config.value = {
        mapId: data.mapId || '',
        robotIds: data.robotIds || [],
        locationIds: data.locationIds || [],
        taskIntervalMs: data.taskIntervalMs || 1000,
      }
      // 更新选中的对象
      await nextTick()
      selectedRobots.value = robotOptions.value.filter((opt) => config.value.robotIds.includes(opt.value))
      selectedLocations.value = locationOptions.value.filter((opt) => config.value.locationIds.includes(opt.value))
    }
  } catch (err: any) {
    notify({ message: err?.message || t('fatigueTest.messages.loadConfigFailed'), color: 'danger' })
  }
}

// 加载状态
const loadStatus = async () => {
  try {
    const res: any = await fatigueTestApi.getStatus()
    const data = res?.data || res?.Data
    if (data) {
      status.value = {
        isRunning: data.isRunning || false,
        mapId: data.mapId || null,
        robotCount: data.robotCount || 0,
        locationCount: data.locationCount || 0,
        taskIntervalMs: data.taskIntervalMs || 1000,
      }
    }
  } catch (err: any) {
    console.error('Failed to load status:', err)
  }
}

// 保存配置
const handleSave = async () => {
  // 验证表单
  errors.value = {}

  if (!config.value.robotIds || config.value.robotIds.length === 0) {
    errors.value.robotIds = t('fatigueTest.validation.robotRequired')
  }
  if (!config.value.locationIds || config.value.locationIds.length < 2) {
    errors.value.locationIds = t('fatigueTest.validation.locationRequired')
  }

  if (Object.keys(errors.value).length > 0) {
    return
  }

  isSaving.value = true
  try {
    const res: any = await fatigueTestApi.saveConfig({
      mapId: config.value.mapId,
      robotIds: config.value.robotIds,
      locationIds: config.value.locationIds,
      taskIntervalMs: config.value.taskIntervalMs,
    })

    if (res && res.success === false) {
      notify({ message: res.message || t('fatigueTest.messages.saveFailed'), color: 'danger' })
    } else {
      notify({ message: res?.message || t('fatigueTest.messages.saveSuccess'), color: 'success' })
      await loadStatus()
    }
  } catch (err: any) {
    notify({ message: err?.message || t('fatigueTest.messages.saveFailed'), color: 'danger' })
  } finally {
    isSaving.value = false
  }
}

// 启动测试
const handleStart = async () => {
  if (!isFormValid.value) {
    notify({ message: t('fatigueTest.messages.configIncomplete'), color: 'warning' })
    return
  }

  isStarting.value = true
  try {
    const res: any = await fatigueTestApi.start()
    if (res && res.success === false) {
      notify({ message: res.message || t('fatigueTest.messages.startFailed'), color: 'danger' })
    } else {
      notify({ message: res?.message || t('fatigueTest.messages.startSuccess'), color: 'success' })
      await loadStatus()
    }
  } catch (err: any) {
    notify({ message: err?.message || t('fatigueTest.messages.startFailed'), color: 'danger' })
  } finally {
    isStarting.value = false
  }
}

// 停止测试
const handleStop = async () => {
  isStopping.value = true
  try {
    const res: any = await fatigueTestApi.stop()
    if (res && res.success === false) {
      notify({ message: res.message || t('fatigueTest.messages.stopFailed'), color: 'danger' })
    } else {
      notify({ message: res?.message || t('fatigueTest.messages.stopSuccess'), color: 'success' })
      await loadStatus()
    }
  } catch (err: any) {
    notify({ message: err?.message || t('fatigueTest.messages.stopFailed'), color: 'danger' })
  } finally {
    isStopping.value = false
  }
}

// 页面加载时获取数据 - 先加载选项,再加载配置
onBeforeMount(async () => {
  isLoading.value = true
  try {
    // 先加载下拉选项
    await Promise.all([loadRobots(), loadLocations()])
    // 再加载配置(这样选项已存在,可以正确匹配)
    await Promise.all([loadConfig(), loadStatus()])
  } finally {
    isLoading.value = false
  }
})
</script>

<template>
  <div class="fatigue-test-page">
    <div class="page-header">
      <h1 class="page-title">{{ t('fatigueTest.title') }}</h1>
      <div class="status-badge" :class="{ running: isRunning }">
        <span class="status-dot"></span>
        {{ isRunning ? t('fatigueTest.status.running') : t('fatigueTest.status.stopped') }}
      </div>
    </div>

    <div class="content-wrapper">
      <VaCard class="config-card">
        <VaCardTitle>{{ t('fatigueTest.config.title') }}</VaCardTitle>
        <VaCardContent>
          <div class="form-grid">
            <!-- 任务间隔 -->
            <div class="form-item">
              <VaSelect
                v-model="config.taskIntervalMs"
                :label="t('fatigueTest.config.interval')"
                :options="intervalOptions"
                text-by="text"
                value-by="value"
                class="w-full"
              />
            </div>

            <!-- 机器人多选 -->
            <div class="form-item full-width">
              <VaSelect
                v-model="config.robotIds"
                :label="t('fatigueTest.config.robots')"
                :options="robotOptions"
                text-by="text"
                value-by="value"
                track-by="value"
                :loading="isLoadingRobots"
                :error="!!errors.robotIds"
                :error-messages="errors.robotIds"
                multiple
                searchable
                clearable
                class="w-full"
              >
                <template #content="{ valueArray }">
                  <template v-if="valueArray?.length">
                    <VaChip
                      v-for="item in valueArray.slice(0, 3)"
                      :key="item.value"
                      size="small"
                      class="mr-1"
                      closeable
                      @close="config.robotIds = config.robotIds.filter((id) => id !== item.value)"
                    >
                      {{ item.text }}
                    </VaChip>
                    <span v-if="valueArray.length > 3" class="text-secondary ml-1"> +{{ valueArray.length - 3 }} </span>
                  </template>
                </template>
              </VaSelect>
            </div>

            <!-- 库位多选 -->
            <div class="form-item full-width">
              <VaSelect
                v-model="config.locationIds"
                :label="t('fatigueTest.config.locations')"
                :options="locationOptions"
                text-by="text"
                value-by="value"
                track-by="value"
                :loading="isLoadingLocations"
                :error="!!errors.locationIds"
                :error-messages="errors.locationIds"
                multiple
                searchable
                clearable
                class="w-full"
              >
                <template #content="{ valueArray }">
                  <template v-if="valueArray?.length">
                    <VaChip
                      v-for="item in valueArray.slice(0, 3)"
                      :key="item.value"
                      size="small"
                      class="mr-1"
                      closeable
                      @close="config.locationIds = config.locationIds.filter((id) => id !== item.value)"
                    >
                      {{ item.text }}
                    </VaChip>
                    <span v-if="valueArray.length > 3" class="text-secondary ml-1"> +{{ valueArray.length - 3 }} </span>
                  </template>
                </template>
              </VaSelect>
              <div class="hint-text">
                {{ t('fatigueTest.config.locationHint') }}
              </div>
            </div>
          </div>

          <!-- 当前配置信息 -->
          <div v-if="status.robotCount > 0 || status.locationCount > 0" class="current-config">
            <VaDivider />
            <h4>{{ t('fatigueTest.config.currentConfig') }}</h4>
            <div class="config-info">
              <div class="info-item">
                <span class="label">{{ t('fatigueTest.config.robotCount') }}:</span>
                <span class="value">{{ status.robotCount }}</span>
              </div>
              <div class="info-item">
                <span class="label">{{ t('fatigueTest.config.locationCount') }}:</span>
                <span class="value">{{ status.locationCount }}</span>
              </div>
              <div class="info-item">
                <span class="label">{{ t('fatigueTest.config.interval') }}:</span>
                <span class="value">{{ status.taskIntervalMs }}ms</span>
              </div>
            </div>
          </div>
        </VaCardContent>
      </VaCard>

      <!-- 操作按钮 -->
      <div class="action-buttons">
        <VaButton color="primary" :loading="isSaving" :disabled="isRunning" @click="handleSave">
          {{ t('fatigueTest.actions.save') }}
        </VaButton>

        <VaButton v-if="!isRunning" color="success" :loading="isStarting" :disabled="!isFormValid" @click="handleStart">
          {{ t('fatigueTest.actions.start') }}
        </VaButton>

        <VaButton v-else color="danger" :loading="isStopping" @click="handleStop">
          {{ t('fatigueTest.actions.stop') }}
        </VaButton>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.fatigue-test-page {
  padding: 1.5rem;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1.5rem;
}

.page-title {
  font-size: 1.75rem;
  font-weight: 600;
  margin: 0;
}

.status-badge {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 1rem;
  border-radius: 9999px;
  background-color: var(--va-background-element);
  font-weight: 500;

  &.running {
    background-color: var(--va-success);
    color: white;
  }
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background-color: var(--va-secondary);

  .running & {
    background-color: white;
    animation: pulse 2s infinite;
  }
}

@keyframes pulse {
  0%,
  100% {
    opacity: 1;
  }
  50% {
    opacity: 0.5;
  }
}

.content-wrapper {
  max-width: 800px;
}

.config-card {
  margin-bottom: 1.5rem;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 1.5rem;
}

.form-item {
  &.full-width {
    grid-column: span 2;
  }
}

.hint-text {
  font-size: 0.875rem;
  color: var(--va-secondary);
  margin-top: 0.5rem;
}

.current-config {
  margin-top: 1.5rem;

  h4 {
    margin: 0 0 1rem 0;
    font-size: 1rem;
    font-weight: 600;
  }
}

.config-info {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.75rem;
}

.info-item {
  display: flex;
  gap: 0.5rem;

  .label {
    color: var(--va-secondary);
  }

  .value {
    font-weight: 500;
  }
}

.action-buttons {
  display: flex;
  gap: 1rem;
}

.w-full {
  width: 100%;
}

.mr-1 {
  margin-right: 0.25rem;
}

.ml-1 {
  margin-left: 0.25rem;
}

.text-secondary {
  color: var(--va-secondary);
}

@media (max-width: 768px) {
  .form-grid {
    grid-template-columns: 1fr;
  }

  .form-item.full-width {
    grid-column: span 1;
  }

  .config-info {
    grid-template-columns: 1fr;
  }

  .action-buttons {
    flex-direction: column;
  }
}
</style>