// using System; // using System.Collections.Generic; // public class AlarmService : IAlarmService // { // private readonly List _records = new List(); // private int _nextId = 1; // public event Action OnAlarmRaised; // public bool IsMuted { get; private set; } // public AlarmService() // { // // 延迟获取AudioService,因为它可能还未初始化 // UnityEngine.MonoBehaviour.print("AlarmService: 初始化"); // } // public void Mute(bool mute) // { // IsMuted = mute; // // 通知音频服务静音状态变化 // NotifyAudioServiceMute(mute); // } // /// // /// 通知音频服务静音状态 // /// // private void NotifyAudioServiceMute(bool mute) // { // try // { // // 通过反射获取AudioService,避免编译时依赖 // var audioServiceType = System.Type.GetType("AudioService"); // if (audioServiceType != null) // { // var instanceProperty = audioServiceType.GetProperty("Instance"); // if (instanceProperty != null) // { // var audioServiceInstance = instanceProperty.GetValue(null); // if (audioServiceInstance != null) // { // var setMuteMethod = audioServiceType.GetMethod("SetMute", new System.Type[] { typeof(bool) }); // setMuteMethod?.Invoke(audioServiceInstance, new object[] { mute }); // UnityEngine.Debug.Log($"AlarmService: 同步静音状态到音频服务 - {mute}"); // } // } // } // } // catch (System.Exception ex) // { // UnityEngine.Debug.LogWarning($"AlarmService: 无法同步静音状态到音频服务 - {ex.Message}"); // } // } // public void RaiseAlarm(AlarmPriority priority, string reason) // { // var evt = new AlarmEvent // { // Id = _nextId++, // Priority = priority, // Reason = reason, // Time = DateTime.Now // }; // _records.Add(evt); // // 检查静音状态:高优先级报警即使在静音状态下也要显示 // bool shouldShowAlarm = !IsMuted; // if (shouldShowAlarm) // { // OnAlarmRaised?.Invoke(evt); // if (IsMuted && priority == AlarmPriority.High) // { // UnityEngine.Debug.Log($"AlarmService: 高优先级报警覆盖静音状态 [{priority}] {reason}"); // } // else // { // UnityEngine.Debug.Log($"AlarmService: 触发报警 [{priority}] {reason}"); // } // } // else // { // UnityEngine.Debug.Log($"AlarmService: 报警被静音阻止 [{priority}] {reason}"); // } // } // public IReadOnlyList GetRecords(int maxCount = 100) // { // if (_records.Count <= maxCount) return _records; // return _records.GetRange(_records.Count - maxCount, maxCount); // } // public void ClearRecords() // { // _records.Clear(); // UnityEngine.Debug.Log("AlarmService: 报警记录已清空"); // } // public void Mute(bool mute, int durationInMinutes) // { // IsMuted = mute; // // 同步到音频服务 // NotifyAudioServiceMute(mute, durationInMinutes); // if (mute && durationInMinutes > 0) // { // // 启动一个定时器,在指定时间后取消静音 // System.Timers.Timer timer = new System.Timers.Timer(durationInMinutes * 60 * 1000); // timer.AutoReset = false; // 只执行一次 // timer.Elapsed += (sender, e) => // { // IsMuted = false; // NotifyAudioServiceMute(false); // 通知音频服务恢复 // timer.Dispose(); // }; // timer.Start(); // } // } // /// // /// 通知音频服务定时静音 // /// // private void NotifyAudioServiceMute(bool mute, int durationInMinutes) // { // try // { // var audioServiceType = System.Type.GetType("AudioService"); // if (audioServiceType != null) // { // var instanceProperty = audioServiceType.GetProperty("Instance"); // if (instanceProperty != null) // { // var audioServiceInstance = instanceProperty.GetValue(null); // if (audioServiceInstance != null) // { // var setMuteMethodWithDuration = audioServiceType.GetMethod("SetMute", new System.Type[] { typeof(bool), typeof(int) }); // if (setMuteMethodWithDuration != null) // { // setMuteMethodWithDuration.Invoke(audioServiceInstance, new object[] { mute, durationInMinutes }); // } // else // { // // 回退到基本的SetMute方法 // var setMuteMethod = audioServiceType.GetMethod("SetMute", new System.Type[] { typeof(bool) }); // setMuteMethod?.Invoke(audioServiceInstance, new object[] { mute }); // } // UnityEngine.Debug.Log($"AlarmService: 同步定时静音状态到音频服务 - {mute}, {durationInMinutes}分钟"); // } // } // } // } // catch (System.Exception ex) // { // UnityEngine.Debug.LogWarning($"AlarmService: 无法同步定时静音状态到音频服务 - {ex.Message}"); // } // } // }