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
| // PLC测试工具类 - 用于模拟PLC设备行为和提供测试场景
| class PlcTestUtil {
| constructor() {
| // PLC状态数据
| this.plcState = {
| gantryStorage: {
| onlineStatus: true,
| plcRequest: 0,
| plcReport: 0,
| complete1A: false,
| complete1B: false,
| taskStatus: 0,
| shuttlePosition: 1,
| suctionPosition: 0,
| mesSend: 0,
| mesConfirm: 0,
| start: 1,
| target: 5,
| glassCount: 0,
| edge1Margin: 0,
| edge1Long: 0,
| edge1Short: 0,
| edge1Thick: 0,
| edge2Margin: 0,
| edge2Long: 0,
| edge2Short: 0,
| edge2Thick: 0,
| alarm: 0
| },
| upperModule: {
| onlineStatus: true,
| plcRequest: 0,
| plcReport: 0,
| complete1A: false,
| complete1B: false,
| taskStatus: 0,
| mesSend: 0,
| mesConfirm: 0,
| start: 1,
| target: 5,
| glassCount: 0,
| alarm: 0
| },
| storageModule: {
| onlineStatus: true,
| plcRequest: 0,
| plcReport: 0,
| taskStatus: 0,
| shuttlePosition: 1,
| suctionPosition: 0,
| mesSend: 0,
| mesConfirm: 0,
| start: 1,
| target: 5,
| alarm: 0
| }
| };
|
| // 测试场景配置
| this.testScenarios = {
| normal: 'normal', // 正常执行
| timeout: 'timeout', // 超时测试
| communicationError: 'communicationError', // 通信错误
| partialDataLoss: 'partialDataLoss', // 部分数据丢失
| incorrectData: 'incorrectData', // 数据错误
| alarmTrigger: 'alarmTrigger' // 触发报警
| };
|
| // 任务执行模拟器
| this.taskSimulator = {
| runningTasks: {},
| intervals: {}
| };
| }
|
| /**
| * 获取模块状态
| * @param {string} module - 模块名称
| * @returns {object} 模块状态对象
| */
| getModuleStatus(module) {
| if (!this.plcState[module]) {
| throw new Error(`不支持的模块: ${module}`);
| }
| return { ...this.plcState[module] };
| }
|
| /**
| * 重置模块状态
| * @param {string} module - 模块名称
| */
| resetModuleStatus(module) {
| if (!this.plcState[module]) {
| throw new Error(`不支持的模块: ${module}`);
| }
|
| // 停止该模块的所有运行中任务
| this.stopAllTasks(module);
|
| // 重置状态
| const initialState = this.getInitialState(module);
| this.plcState[module] = { ...initialState };
| }
|
| /**
| * 执行PLC任务
| * @param {string} module - 模块名称
| * @param {object} taskData - 任务数据
| * @param {string} scenario - 测试场景类型
| * @param {function} callback - 回调函数
| * @returns {string} 任务ID
| */
| executeTask(module, taskData, scenario = this.testScenarios.normal, callback) {
| if (!this.plcState[module]) {
| throw new Error(`不支持的模块: ${module}`);
| }
|
| // 生成任务ID
| const taskId = `task_${Date.now()}_${Math.floor(Math.random() * 1000)}`;
|
| // 更新PLC状态为任务开始
| this.updatePlcState(module, {
| mesSend: 1,
| taskStatus: 1, // 任务进行中
| ...taskData
| });
|
| // 存储任务信息
| this.taskSimulator.runningTasks[taskId] = {
| module,
| taskData,
| scenario,
| startTime: Date.now(),
| status: 'running'
| };
|
| // 根据场景模拟任务执行
| this.simulateTaskExecution(taskId, callback);
|
| return taskId;
| }
|
| /**
| * 模拟任务执行
| * @param {string} taskId - 任务ID
| * @param {function} callback - 回调函数
| */
| simulateTaskExecution(taskId, callback) {
| const task = this.taskSimulator.runningTasks[taskId];
| if (!task) return;
|
| const { module, scenario } = task;
|
| // 根据场景设置不同的执行时间和行为
| let executionTime = 2000; // 默认2秒
| let success = true;
| let errorMessage = '';
|
| switch (scenario) {
| case this.testScenarios.normal:
| executionTime = 2000 + Math.random() * 3000; // 2-5秒随机
| break;
| case this.testScenarios.timeout:
| executionTime = 10000; // 10秒超时
| break;
| case this.testScenarios.communicationError:
| executionTime = 1000;
| success = false;
| errorMessage = 'PLC通信错误';
| break;
| case this.testScenarios.partialDataLoss:
| executionTime = 3000;
| // 模拟部分数据丢失
| this.updatePlcState(module, { plcReport: Math.floor(Math.random() * 100) });
| break;
| case this.testScenarios.incorrectData:
| executionTime = 3000;
| // 模拟数据错误
| this.updatePlcState(module, {
| shuttlePosition: 999,
| alarm: 1
| });
| break;
| case this.testScenarios.alarmTrigger:
| executionTime = 1500;
| // 触发报警
| this.updatePlcState(module, { alarm: 1 });
| success = false;
| errorMessage = '设备报警触发';
| break;
| default:
| break;
| }
|
| // 设置定时器模拟任务执行
| this.taskSimulator.intervals[taskId] = setTimeout(() => {
| if (success) {
| // 任务成功完成
| this.updatePlcState(module, {
| taskStatus: 2, // 任务完成
| plcReport: 1,
| mesConfirm: 1,
| complete1A: true,
| complete1B: true,
| shuttlePosition: task.taskData.target || 5
| });
|
| task.status = 'completed';
| task.endTime = Date.now();
|
| if (callback) {
| callback(null, {
| success: true,
| taskId,
| module,
| duration: Math.round((task.endTime - task.startTime) / 1000)
| });
| }
| } else {
| // 任务失败
| this.updatePlcState(module, {
| taskStatus: 3, // 任务失败
| alarm: 1
| });
|
| task.status = 'failed';
| task.endTime = Date.now();
| task.error = errorMessage;
|
| if (callback) {
| callback(new Error(errorMessage), {
| success: false,
| taskId,
| module,
| error: errorMessage
| });
| }
| }
|
| // 清理定时器
| delete this.taskSimulator.intervals[taskId];
| }, executionTime);
| }
|
| /**
| * 停止特定任务
| * @param {string} taskId - 任务ID
| */
| stopTask(taskId) {
| if (this.taskSimulator.intervals[taskId]) {
| clearTimeout(this.taskSimulator.intervals[taskId]);
| delete this.taskSimulator.intervals[taskId];
| }
|
| if (this.taskSimulator.runningTasks[taskId]) {
| const task = this.taskSimulator.runningTasks[taskId];
| // 更新状态为已停止
| this.updatePlcState(task.module, {
| taskStatus: 0,
| mesSend: 0
| });
|
| task.status = 'stopped';
| task.endTime = Date.now();
| }
| }
|
| /**
| * 停止模块的所有任务
| * @param {string} module - 模块名称
| */
| stopAllTasks(module) {
| Object.keys(this.taskSimulator.runningTasks).forEach(taskId => {
| if (this.taskSimulator.runningTasks[taskId].module === module) {
| this.stopTask(taskId);
| }
| });
| }
|
| /**
| * 更新PLC状态
| * @param {string} module - 模块名称
| * @param {object} updates - 更新的字段
| */
| updatePlcState(module, updates) {
| if (!this.plcState[module]) {
| throw new Error(`不支持的模块: ${module}`);
| }
|
| this.plcState[module] = {
| ...this.plcState[module],
| ...updates
| };
| }
|
| /**
| * 获取模块的初始状态
| * @param {string} module - 模块名称
| * @returns {object} 初始状态
| */
| getInitialState(module) {
| switch (module) {
| case 'gantryStorage':
| return {
| onlineStatus: true,
| plcRequest: 0,
| plcReport: 0,
| complete1A: false,
| complete1B: false,
| taskStatus: 0,
| shuttlePosition: 1,
| suctionPosition: 0,
| mesSend: 0,
| mesConfirm: 0,
| start: 1,
| target: 5,
| glassCount: 0,
| edge1Margin: 0,
| edge1Long: 0,
| edge1Short: 0,
| edge1Thick: 0,
| edge2Margin: 0,
| edge2Long: 0,
| edge2Short: 0,
| edge2Thick: 0,
| alarm: 0
| };
| case 'upperModule':
| return {
| onlineStatus: true,
| plcRequest: 0,
| plcReport: 0,
| complete1A: false,
| complete1B: false,
| taskStatus: 0,
| mesSend: 0,
| mesConfirm: 0,
| start: 1,
| target: 5,
| glassCount: 0,
| alarm: 0
| };
| case 'storageModule':
| return {
| onlineStatus: true,
| plcRequest: 0,
| plcReport: 0,
| taskStatus: 0,
| shuttlePosition: 1,
| suctionPosition: 0,
| mesSend: 0,
| mesConfirm: 0,
| start: 1,
| target: 5,
| alarm: 0
| };
| default:
| return {};
| }
| }
|
| /**
| * 生成模拟测试数据
| * @param {string} module - 模块名称
| * @param {number} count - 数据条目数
| * @returns {array} 测试数据数组
| */
| generateTestData(module, count = 10) {
| const data = [];
| const moduleMap = {
| 'gantryStorage': '龙门仓储',
| 'upperModule': '上片模块',
| 'storageModule': '穿梭仓储'
| };
|
| for (let i = 0; i < count; i++) {
| const start = Math.floor(Math.random() * 10) + 1;
| const target = Math.floor(Math.random() * 10) + 1;
| const duration = Math.floor(Math.random() * 10) + 1;
| const status = Math.random() > 0.2 ? '通过' : '失败';
| const startTime = new Date(Date.now() - i * 60000).toLocaleString();
| const endTime = new Date(Date.now() - (i - duration) * 60000).toLocaleString();
|
| data.push({
| id: `test_${Date.now()}_${i}`,
| module: moduleMap[module],
| moduleKey: module,
| startTime,
| endTime,
| status,
| duration,
| retryCount: 0,
| errorMsg: status === '失败' ? '模拟随机故障' : '',
| timeline: [
| { time: startTime.split(' ')[1], status: 'primary', desc: '测试任务启动' },
| { time: new Date(Date.now() - (i - 0.5) * 60000).toLocaleTimeString(), status: 'primary', desc: '开始初始化测试环境' },
| { time: new Date(Date.now() - (i - 1) * 60000).toLocaleTimeString(), status: status === '通过' ? 'success' : 'error', desc: status === '通过' ? '任务执行完成' : '任务执行失败' }
| ]
| });
| }
|
| return data;
| }
|
| /**
| * 模拟网络延迟
| * @param {number} minDelay - 最小延迟(ms)
| * @param {number} maxDelay - 最大延迟(ms)
| * @returns {Promise} 延迟Promise
| */
| simulateNetworkDelay(minDelay = 100, maxDelay = 1000) {
| const delay = minDelay + Math.random() * (maxDelay - minDelay);
| return new Promise(resolve => setTimeout(resolve, delay));
| }
| }
|
| // 导出单例实例
| export default new PlcTestUtil();
|
|