| | |
| | | import { ref } from 'vue' |
| | | |
| | | export type CabinetOnlineStatus = '在线' | '离线' |
| | | export type CabinetTestStatus = '待检测' | '测试中' | '空闲' |
| | | export type CabinetStatus = '启用' | '禁用' |
| | |
| | | createdAt: string |
| | | } |
| | | |
| | | export type CabinetPayload = Pick<DeviceCabinetRecord, |
| | | 'deviceCode' | 'ip' | 'mac' | 'name' | 'manufacturer' | 'model' | 'channelCount' | |
| | | 'specs' | 'remark' | 'onlineStatus' | 'testStatus' | 'currentTest' | 'status'> |
| | | export type CabinetPayload = Pick<DeviceCabinetRecord, 'deviceCode' | 'ip' | 'mac' | 'name' | 'manufacturer' | 'model' | 'channelCount' | 'specs' | 'remark' | 'onlineStatus' | 'testStatus' | 'currentTest' | 'status'> |
| | | |
| | | const STORAGE_KEY = 'battery_detection_device_cabinets_v1' |
| | | |
| | | const seedRecords: DeviceCabinetRecord[] = [ |
| | | { id: 4012, deviceCode: '45643166', ip: '192.1.1.152', mac: '41:F5:D6:N1', name: '测试柜1', manufacturer: 'XXX', model: 'A1', channelCount: 12, specs: 120, remark: '一号实验区', onlineStatus: '在线', testStatus: '待检测', currentTest: 'SP2电池寿命循环', status: '启用', createdAt: '2026-08-05 09:26' }, |
| | | { id: 4011, deviceCode: '45643167', ip: '192.1.1.153', mac: '41:F5:D6:N2', name: '测试柜2', manufacturer: 'XXX', model: 'A1', channelCount: 12, specs: 120, remark: '一号实验区', onlineStatus: '在线', testStatus: '测试中', currentTest: '高温循环性能测试', status: '启用', createdAt: '2026-08-04 16:18' }, |
| | | { id: 4010, deviceCode: '45643168', ip: '192.1.1.154', mac: '41:F5:D6:N3', name: '测试柜3', manufacturer: 'XXX', model: 'A2', channelCount: 24, specs: 240, remark: '二号实验区', onlineStatus: '在线', testStatus: '空闲', currentTest: '-', status: '启用', createdAt: '2026-08-04 10:42' }, |
| | | { id: 4009, deviceCode: '45643169', ip: '192.1.1.155', mac: '41:F5:D6:N4', name: '测试柜4', manufacturer: 'XXX', model: 'A2', channelCount: 24, specs: 240, remark: '二号实验区', onlineStatus: '离线', testStatus: '空闲', currentTest: '-', status: '禁用', createdAt: '2026-08-03 15:06' }, |
| | | { id: 4008, deviceCode: '45643170', ip: '192.1.1.156', mac: '41:F5:D6:N5', name: '测试柜5', manufacturer: 'YYY', model: 'B1', channelCount: 8, specs: 80, remark: '三号实验区', onlineStatus: '在线', testStatus: '待检测', currentTest: '-', status: '启用', createdAt: '2026-08-02 11:35' }, |
| | | { id: 4007, deviceCode: '45643171', ip: '192.1.1.157', mac: '41:F5:D6:N6', name: '测试柜6', manufacturer: 'YYY', model: 'B1', channelCount: 8, specs: 80, remark: '三号实验区', onlineStatus: '在线', testStatus: '空闲', currentTest: '-', status: '启用', createdAt: '2026-08-01 09:18' }, |
| | | { id: 4006, deviceCode: '45643172', ip: '192.1.1.158', mac: '41:F5:D6:N7', name: '测试柜7', manufacturer: 'YYY', model: 'B2', channelCount: 16, specs: 160, remark: '四号实验区', onlineStatus: '离线', testStatus: '待检测', currentTest: '-', status: '禁用', createdAt: '2026-07-30 14:20' }, |
| | | { id: 4005, deviceCode: '45643173', ip: '192.1.1.159', mac: '41:F5:D6:N8', name: '测试柜8', manufacturer: 'YYY', model: 'B2', channelCount: 16, specs: 160, remark: '四号实验区', onlineStatus: '在线', testStatus: '测试中', currentTest: '低温充放电测试', status: '启用', createdAt: '2026-07-29 17:12' }, |
| | | { id: 4004, deviceCode: '45643174', ip: '192.1.1.160', mac: '41:F5:D6:N9', name: '测试柜9', manufacturer: 'ZZZ', model: 'C1', channelCount: 32, specs: 320, remark: '五号实验区', onlineStatus: '在线', testStatus: '空闲', currentTest: '-', status: '启用', createdAt: '2026-07-28 13:46' }, |
| | | { id: 4003, deviceCode: '45643175', ip: '192.1.1.161', mac: '41:F5:D6:N0', name: '测试柜10', manufacturer: 'ZZZ', model: 'C1', channelCount: 32, specs: 320, remark: '五号实验区', onlineStatus: '离线', testStatus: '空闲', currentTest: '-', status: '禁用', createdAt: '2026-07-26 10:16' }, |
| | | ] |
| | | |
| | | function loadRecords() { |
| | | try { |
| | | const stored = localStorage.getItem(STORAGE_KEY) |
| | | if (stored) { |
| | | const parsed = JSON.parse(stored) as Array<Omit<DeviceCabinetRecord, 'specs'> & { specs: number | string }> |
| | | return parsed.map(item => ({ |
| | | ...item, |
| | | specs: typeof item.specs === 'number' ? item.specs : Number(item.specs) || item.channelCount * 10, |
| | | })) |
| | | } |
| | | } catch { |
| | | localStorage.removeItem(STORAGE_KEY) |
| | | } |
| | | return seedRecords |
| | | } |
| | | |
| | | const records = ref<DeviceCabinetRecord[]>(loadRecords()) |
| | | |
| | | function persist() { |
| | | localStorage.setItem(STORAGE_KEY, JSON.stringify(records.value)) |
| | | } |
| | | |
| | | function nowString() { |
| | | const date = new Date() |
| | | const pad = (value: number) => String(value).padStart(2, '0') |
| | | return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}` |
| | | } |
| | | |
| | | export function useDeviceCabinets() { |
| | | function createCabinet(data: CabinetPayload) { |
| | | const id = Math.max(4000, ...records.value.map(item => item.id)) + 1 |
| | | records.value.unshift({ ...data, id, createdAt: nowString() }) |
| | | persist() |
| | | } |
| | | |
| | | function updateCabinet(id: number, data: CabinetPayload) { |
| | | const target = records.value.find(item => item.id === id) |
| | | if (!target) return false |
| | | Object.assign(target, data) |
| | | persist() |
| | | return true |
| | | } |
| | | |
| | | function removeCabinet(id: number) { |
| | | records.value = records.value.filter(item => item.id !== id) |
| | | persist() |
| | | } |
| | | |
| | | function saveChanges() { |
| | | persist() |
| | | } |
| | | |
| | | return { records, createCabinet, updateCabinet, removeCabinet, saveChanges } |
| | | } |