using System.Linq; //using log4net; using System; using System.Media; using System.Configuration; //using EDosApiWrapper; namespace IntelligentControlForsx { public class MediaManager { private bool isPlaying; private bool isPlaying2; //private Uri mediaUri; //private MediaElement mediaPlayer; private SoundPlayer soundPlayer; private SoundPlayer soundPlayer2; private DateTime lastPlayTime; private int repeatPlayInterval; private bool isActive = true; // private EDosProxy eDos; //private string keyIsActive = "CALC_ALERTACTIVE"; public bool IsPlaying { get { return isPlaying || isPlaying2; } } #region 单例模式 private MediaManager() { string str = ConfigurationManager.AppSettings["AlertMediaFile"]; soundPlayer = new SoundPlayer(str); string str2 = ConfigurationManager.AppSettings["CriticalMediaFile"]; soundPlayer2 = new SoundPlayer(str2); lastPlayTime = DateTime.Now.AddDays(-1); string sts = ConfigurationManager.AppSettings["MediaPlayInterval"]; if (int.TryParse(sts, out repeatPlayInterval) == false) { repeatPlayInterval = 5; //默认5分钟 } //string edosIp = ConfigurationManager.AppSettings["EDosIP"]; //string strPort = ConfigurationManager.AppSettings["EDosPort"]; //ushort edosPort = 0; //if (!ushort.TryParse(strPort, out edosPort)) //{ // edosPort = 10010; //} //eDos = new EDosProxy(edosIp, edosPort); //mediaUri = new Uri(str); //mediaPlayer = new MediaElement(); //mediaPlayer.LoadedBehavior = MediaState.Manual; //mediaPlayer.UnloadedBehavior = MediaState.Manual; //mediaPlayer.MediaEnded += new RoutedEventHandler(media_MediaEnded); } public static MediaManager Instance { get { return SingletonCreator.instance; } } class SingletonCreator { internal static readonly MediaManager instance = new MediaManager(); } #endregion public void Silence() { lastPlayTime = DateTime.Now; this.Stop(); } public void Play() { if (isPlaying ) return; if (isPlaying2) { if (soundPlayer2 != null) { soundPlayer2.Stop(); } isPlaying2 = false; } TimeSpan ts = DateTime.Now.Subtract(lastPlayTime); if (ts.TotalMinutes < repeatPlayInterval) return; if (soundPlayer != null) { soundPlayer.PlayLooping(); //mediaPlayer.Source = mediaUri; //mediaPlayer.Play(); isPlaying = true; //lastPlayTime = DateTime.Now; } } public void Play2() { if (isPlaying2) return; if (isPlaying) { if (soundPlayer != null) { soundPlayer.Stop(); } isPlaying = false; } TimeSpan ts = DateTime.Now.Subtract(lastPlayTime); if (ts.TotalMinutes < repeatPlayInterval) return; if (soundPlayer2 != null) { soundPlayer2.PlayLooping(); //mediaPlayer.Source = mediaUri; //mediaPlayer.Play(); isPlaying2 = true; //lastPlayTime = DateTime.Now; } } public void Stop() { if (soundPlayer != null) { soundPlayer.Stop(); } if (soundPlayer2 != null) { soundPlayer2.Stop(); } isPlaying = false; isPlaying2 = false; } public void UpdateAlertMediaStatus() { UpdateActiveStatus(); if (isActive == false) return; try { TimeSpan ts = DateTime.Now.Subtract(lastPlayTime); if (ts.TotalMinutes < repeatPlayInterval) return; var lst = ClientCache.Instance.AlertSnaps; if (lst != null && lst.Count > 0) { var tmp = lst.Where(q => q.IsConfirmed == false && (q.Rank == "5")).OrderByDescending(q=>q.LastUpdateTime).First(); if (tmp != null) { TimeSpan ts1 = DateTime.Now.Subtract(tmp.LastUpdateTime.Value); if (ts1.TotalMinutes < repeatPlayInterval) { this.Play2(); return; } } var tmp2 = lst.Where(q => q.IsConfirmed == false && (q.Rank == "4")).OrderByDescending(q => q.LastUpdateTime).First(); if (tmp2 != null ) { TimeSpan ts1 = DateTime.Now.Subtract(tmp2.LastUpdateTime.Value); if (ts1.TotalMinutes < repeatPlayInterval) { this.Play(); return; } } } this.Stop(); } catch { } } public void UpdateActiveStatus() { try { // isActive = !eDos.GetDIRTValue(keyIsActive); } catch { }; } public void Active() { try { this.Stop(); isActive = false; // eDos.SendSinglePoint(keyIsActive, 0); } catch { } } public void DeActive() { try { isActive = true; //eDos.SendSinglePoint(keyIsActive, 1); } catch { } } } }