mock-device-types.ts
5.01 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
import type { DeviceType, DeviceTypePayload } from '../../app/types/device-type'
interface MutationResult {
success: boolean
errorCode: string | null
message: string
}
function now() {
return new Date().toISOString()
}
let nextId = 1005
const mockDeviceTypes: DeviceType[] = [{
id: 1001,
name: '协作机械臂控制柜',
model: 'RPS-CB-500',
category: 'controller',
lengthMm: 680,
widthMm: 520,
heightMm: 1480,
weightKg: 92,
hasBattery: true,
batterySpec: '24V 15Ah 锂电',
description: '用于协作机械臂整机控制与供电管理。',
updatedAt: now()
}, {
id: 1002,
name: '末端视觉模组',
model: 'RPS-VM-220',
category: 'vision',
lengthMm: 210,
widthMm: 135,
heightMm: 118,
weightKg: 2.8,
hasBattery: false,
batterySpec: undefined,
description: '安装于末端执行器位置,用于识别与定位。',
updatedAt: now()
}, {
id: 1003,
name: '底盘驱动单元',
model: 'RPS-CH-920',
category: 'chassis',
lengthMm: 1020,
widthMm: 740,
heightMm: 360,
weightKg: 118,
hasBattery: true,
batterySpec: '48V 40Ah 磷酸铁锂',
description: 'AGV 底盘驱动与转向一体化单元。',
updatedAt: now()
}, {
id: 1004,
name: '工业网关',
model: 'RPS-GW-110',
category: 'communication',
lengthMm: 280,
widthMm: 180,
heightMm: 86,
weightKg: 3.2,
hasBattery: false,
batterySpec: undefined,
description: '用于产线网络与设备数据转发。',
updatedAt: now()
}]
function normalizeText(value: string | undefined) {
if (!value) {
return undefined
}
const trimmed = value.trim()
return trimmed.length > 0 ? trimmed : undefined
}
function cloneDeviceType(item: DeviceType): DeviceType {
return {
...item
}
}
function byUpdatedAtDesc(a: DeviceType, b: DeviceType) {
return b.updatedAt.localeCompare(a.updatedAt)
}
function hasDuplicateNameModel(payload: DeviceTypePayload, ignoreId?: number) {
return mockDeviceTypes.some((item) => {
if (typeof ignoreId === 'number' && item.id === ignoreId) {
return false
}
return item.name.trim().toLowerCase() === payload.name.trim().toLowerCase()
&& item.model.trim().toLowerCase() === payload.model.trim().toLowerCase()
})
}
function withMutation<T extends Record<string, unknown>>(base: MutationResult, extra?: T) {
return {
...base,
...(extra || {})
}
}
export function getMockDeviceTypes() {
return mockDeviceTypes
.slice()
.sort(byUpdatedAtDesc)
.map(item => cloneDeviceType(item))
}
export function createMockDeviceType(payload: DeviceTypePayload) {
if (hasDuplicateNameModel(payload)) {
return withMutation({
success: false,
errorCode: 'DEVICE_TYPE_EXISTS',
message: '设备名称与型号组合已存在。'
})
}
const created: DeviceType = {
id: nextId++,
name: payload.name.trim(),
model: payload.model.trim(),
category: payload.category,
lengthMm: payload.lengthMm,
widthMm: payload.widthMm,
heightMm: payload.heightMm,
weightKg: payload.weightKg,
hasBattery: payload.hasBattery,
batterySpec: payload.hasBattery ? normalizeText(payload.batterySpec) : undefined,
description: normalizeText(payload.description),
updatedAt: now()
}
mockDeviceTypes.unshift(created)
return withMutation({
success: true,
errorCode: null,
message: '设备类型已创建。'
}, {
item: cloneDeviceType(created)
})
}
export function updateMockDeviceType(id: number, payload: DeviceTypePayload) {
const index = mockDeviceTypes.findIndex(item => item.id === id)
if (index < 0) {
return withMutation({
success: false,
errorCode: 'DEVICE_TYPE_NOT_FOUND',
message: '设备类型不存在。'
})
}
if (hasDuplicateNameModel(payload, id)) {
return withMutation({
success: false,
errorCode: 'DEVICE_TYPE_EXISTS',
message: '设备名称与型号组合已存在。'
})
}
const updated: DeviceType = {
id,
name: payload.name.trim(),
model: payload.model.trim(),
category: payload.category,
lengthMm: payload.lengthMm,
widthMm: payload.widthMm,
heightMm: payload.heightMm,
weightKg: payload.weightKg,
hasBattery: payload.hasBattery,
batterySpec: payload.hasBattery ? normalizeText(payload.batterySpec) : undefined,
description: normalizeText(payload.description),
updatedAt: now()
}
mockDeviceTypes[index] = updated
return withMutation({
success: true,
errorCode: null,
message: '设备类型已更新。'
}, {
item: cloneDeviceType(updated)
})
}
export function removeMockDeviceType(id: number) {
const index = mockDeviceTypes.findIndex(item => item.id === id)
if (index < 0) {
return withMutation({
success: false,
errorCode: 'DEVICE_TYPE_NOT_FOUND',
message: '设备类型不存在。'
})
}
const [removed] = mockDeviceTypes.splice(index, 1)
return withMutation({
success: true,
errorCode: null,
message: '设备类型已删除。'
}, {
item: removed ? cloneDeviceType(removed) : undefined
})
}