123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- 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 { }
- }
- }
- }
|