fei.wang
7 天以前 ae7b22322555448d95fd56f505bafa325c167a26
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
/**
 * 音频管理工具
 * 负责语音播放、音频队列管理等功能
 */
 
export class AudioManager {
  constructor() {
    this.audioContext = null;
    this.audioTimer = null;
    this.audioInterval = 3000;
    this.isAudioPlaying = false;
    this.audioQueue = [];
    this.audioPlayEnabled = true;
    this.language = 'zh';
    
    // 多语言语音文件配置
    this.audioFiles = { 
      zh: {
        red: '/static/chvoice/voice2/red.mp3',
        yellow: '/static/chvoice/voice2/yellow.mp3',
        green: '/static/chvoice/voice2/green.mp3'
      },
      en: {
        red: '/static/envoice/voice2/red.mp3',
        yellow: '/static/envoice/voice2/yellow.mp3',
        green: '/static/envoice/voice2/green.mp3'
      },
      ru: {
        red: '/static/pyvoice/voice2/red.mp3',
        yellow: '/static/pyvoice/voice2/yellow.mp3',
        green: '/static/pyvoice/voice2/green.mp3'
      }
    };
  }
 
  /**
   * 初始化音频上下文
   */
  initAudioContext() {
    try {
      this.audioContext = uni.createInnerAudioContext();
      this.audioContext.onError((res) => {
        console.error('音频播放失败:', res);
        this.isAudioPlaying = false;
        this.playNextInQueue();
      });
      this.audioContext.onEnded(() => {
        this.isAudioPlaying = false;
        this.playNextInQueue();
      });
      console.log('音频上下文初始化成功');
    } catch (error) {
      console.error('音频上下文初始化失败:', error);
    }
  }
 
  /**
   * 检查音频播放状态
   */
  checkAudioPlaybackStatus() {
    try {
      if (this.audioContext) {
        this.audioContext.destroy();
      }
      this.initAudioContext();
    } catch (error) {
      console.error('检查音频播放状态失败:', error);
    }
  }
 
  /**
   * 播放语音
   */
  playVoice(layer, language = null) {
    if (!this.audioPlayEnabled || this.isAudioPlaying) {
      return;
    }
 
    const lang = language || this.language;
    const audioFile = this.audioFiles[lang]?.[layer];
    
    if (!audioFile) {
      console.error('语音文件不存在:', layer, lang);
      return;
    }
 
    this.playAudioFile(audioFile);
  }
 
  /**
   * 播放音频文件
   */
  playAudioFile(audioFile) {
    if (!this.audioContext || this.isAudioPlaying) {
      return;
    }
 
    try {
      this.isAudioPlaying = true;
      this.audioContext.src = audioFile;
      this.audioContext.play();
      console.log('开始播放音频:', audioFile);
    } catch (error) {
      console.error('播放音频失败:', error);
      this.isAudioPlaying = false;
    }
  }
 
  /**
   * 添加音频到播放队列
   */
  addToAudioQueue(layer, language = null) {
    if (!this.audioPlayEnabled) return;
    
    const lang = language || this.language;
    const audioFile = this.audioFiles[lang]?.[layer];
    
    if (audioFile) {
      this.audioQueue.push(audioFile);
      console.log('添加音频到队列:', audioFile);
      
      if (!this.isAudioPlaying) {
        this.playNextInQueue();
      }
    }
  }
 
  /**
   * 播放队列中的下一个音频
   */
  playNextInQueue() {
    if (this.audioQueue.length === 0 || this.isAudioPlaying) {
      return;
    }
 
    const nextAudio = this.audioQueue.shift();
    this.playAudioFile(nextAudio);
  }
 
  /**
   * 清空音频队列
   */
  clearAudioQueue() {
    this.audioQueue = [];
    console.log('音频队列已清空');
  }
 
  /**
   * 停止音频播放
   */
  stopAudio() {
    if (this.audioContext) {
      this.audioContext.stop();
      this.isAudioPlaying = false;
    }
    this.clearAudioQueue();
  }
 
  /**
   * 设置语言
   */
  setLanguage(language) {
    this.language = language;
    console.log('音频语言已设置为:', language);
  }
 
  /**
   * 设置音频播放开关
   */
  setAudioEnabled(enabled) {
    this.audioPlayEnabled = enabled;
    if (!enabled) {
      this.stopAudio();
    }
    console.log('音频播放开关:', enabled ? '开启' : '关闭');
  }
 
  /**
   * 设置音频播放间隔
   */
  setAudioInterval(interval) {
    this.audioInterval = interval;
    console.log('音频播放间隔已设置为:', interval, 'ms');
  }
 
  /**
   * 销毁音频上下文
   */
  destroy() {
    if (this.audioContext) {
      this.audioContext.destroy();
      this.audioContext = null;
    }
    this.isAudioPlaying = false;
    this.clearAudioQueue();
    console.log('音频管理器已销毁');
  }
}
 
/**
 * 扇形状态管理工具类
 */
export class SectorStateManager {
  constructor() {
    this.activeSectors = new Map();
    this.updateTimer = null;
    this.updateInterval = APP_CONFIG.SECTOR.UPDATE_INTERVAL;
  }
 
  /**
   * 启动扇形更新定时器
   */
  startSectorUpdateTimer() {
    // 清除之前的定时器
    if (this.updateTimer) {
      clearInterval(this.updateTimer);
    }
 
    // 启动扇形更新定时器
    this.updateTimer = setInterval(() => {
      this.updateSectorDisplay();
    }, this.updateInterval);
 
    console.log('扇形更新定时器已启动');
  }
 
  /**
   * 更新活跃扇形状态
   * @param {Map} newSectors - 新的扇形状态
   */
  updateActiveSectors(newSectors) {
    // 清空当前活跃扇形
    this.activeSectors.clear();
 
    // 添加新的活跃扇形
    newSectors.forEach((sectorInfo, sectorKey) => {
      this.activeSectors.set(sectorKey, sectorInfo);
    });
 
    console.log('活跃扇形状态已更新,当前活跃扇形数量:', this.activeSectors.size);
  }
 
  /**
   * 更新扇形显示
   * @param {Array} circleColors - 扇形颜色数组
   * @param {boolean} isOffline - 是否离线
   * @param {Function} updateCallback - 更新回调函数
   */
  updateSectorDisplay(circleColors, isOffline, updateCallback) {
    // 先重置所有扇形为灰色
    this.resetAllColors(circleColors);
 
    // 如果处于离线状态,保持灰色
    if (isOffline) {
      console.log('离线状态,保持扇形灰色');
      return;
    }
 
    // 显示当前活跃的扇形
    let updatedCount = 0;
    this.activeSectors.forEach((sectorInfo, sectorKey) => {
      const { layerIndex, sectorIndex, color } = sectorInfo;
 
      // 验证索引有效性
      if (layerIndex >= 0 && layerIndex < circleColors.length && 
          sectorIndex >= 0 && sectorIndex < circleColors[layerIndex].length) {
 
        // 更新扇形颜色
        updateCallback(layerIndex, sectorIndex, color);
        updatedCount++;
      } else {
        console.warn('扇形索引无效:', { layerIndex, sectorIndex, sectorKey });
      }
    });
 
    if (updatedCount > 0) {
      console.log(`扇形显示已更新,更新了${updatedCount}个扇形`);
    }
  }
 
  /**
   * 重置所有扇形为灰色
   * @param {Array} circleColors - 扇形颜色数组
   */
  resetAllColors(circleColors) {
    for (let layerIndex = 0; layerIndex < circleColors.length; layerIndex++) {
      for (let sectorIndex = 0; sectorIndex < circleColors[layerIndex].length; sectorIndex++) {
        circleColors[layerIndex][sectorIndex] = APP_CONFIG.COLORS.DEFAULT_SECTOR;
      }
    }
  }
 
  /**
   * 清空所有扇形
   */
  clearAllSectors() {
    // 清空活跃扇形状态
    this.activeSectors.clear();
    console.log('所有扇形已清空');
  }
 
  /**
   * 强制触发扇形更新
   * @param {Array} circleColors - 扇形颜色数组
   * @param {boolean} isOffline - 是否离线
   * @param {Function} updateCallback - 更新回调函数
   */
  forceUpdateSectors(circleColors, isOffline, updateCallback) {
    console.log('强制触发扇形更新');
    this.updateSectorDisplay(circleColors, isOffline, updateCallback);
  }
 
  /**
   * 销毁扇形状态管理器
   */
  destroy() {
    if (this.updateTimer) {
      clearInterval(this.updateTimer);
      this.updateTimer = null;
    }
    this.activeSectors.clear();
  }
}
 
/**
 * 离线检测管理工具类
 */
export class OfflineDetectionManager {
  constructor() {
    this.lastDataTime = 0;
    this.offlineTimer = null;
    this.offlineTimeout = APP_CONFIG.OFFLINE.TIMEOUT;
    this.checkInterval = APP_CONFIG.OFFLINE.CHECK_INTERVAL;
    this.isOffline = false;
  }
 
  /**
   * 启动离线检测机制
   */
  startOfflineDetection() {
    // 清除之前的定时器
    if (this.offlineTimer) {
      clearInterval(this.offlineTimer);
    }
 
    // 启动离线检测定时器
    this.offlineTimer = setInterval(() => {
      const currentTime = Date.now();
      const timeSinceLastData = currentTime - this.lastDataTime;
 
      // 如果超过超时时间没有收到数据,判定为离线
      if (timeSinceLastData > this.offlineTimeout && !this.isOffline) {
        console.log('检测到离线状态,清空所有扇形');
        this.isOffline = true;
        this.onOfflineDetected();
      }
    }, this.checkInterval);
 
    console.log('离线检测机制已启动');
  }
 
  /**
   * 更新最后数据接收时间
   */
  updateLastDataTime() {
    this.lastDataTime = Date.now();
    this.isOffline = false;
  }
 
  /**
   * 离线检测回调
   * @param {Function} callback - 离线检测回调函数
   */
  setOfflineCallback(callback) {
    this.onOfflineDetected = callback;
  }
 
  /**
   * 重置离线状态
   */
  resetOfflineState() {
    this.isOffline = false;
    this.lastDataTime = Date.now();
  }
 
  /**
   * 销毁离线检测管理器
   */
  destroy() {
    if (this.offlineTimer) {
      clearInterval(this.offlineTimer);
      this.offlineTimer = null;
    }
  }
}
 
/**
 * 获取当前时间字符串
 * @returns {string} 时间字符串
 */
export function getTime() {
  const now = new Date();
  const hour = String(now.getHours()).padStart(2, '0');
  const minute = String(now.getMinutes()).padStart(2, '0');
  const second = String(now.getSeconds()).padStart(2, '0');
  return `${hour}:${minute}:${second}`;