MediaManager.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. using System.Linq;
  2. //using log4net;
  3. using System;
  4. using System.Media;
  5. using System.Configuration;
  6. //using EDosApiWrapper;
  7. namespace IntelligentControlForsx
  8. {
  9. public class MediaManager
  10. {
  11. private bool isPlaying;
  12. private bool isPlaying2;
  13. //private Uri mediaUri;
  14. //private MediaElement mediaPlayer;
  15. private SoundPlayer soundPlayer;
  16. private SoundPlayer soundPlayer2;
  17. private DateTime lastPlayTime;
  18. private int repeatPlayInterval;
  19. private bool isActive = true;
  20. // private EDosProxy eDos;
  21. //private string keyIsActive = "CALC_ALERTACTIVE";
  22. public bool IsPlaying
  23. {
  24. get { return isPlaying || isPlaying2; }
  25. }
  26. #region 单例模式
  27. private MediaManager()
  28. {
  29. string str = ConfigurationManager.AppSettings["AlertMediaFile"];
  30. soundPlayer = new SoundPlayer(str);
  31. string str2 = ConfigurationManager.AppSettings["CriticalMediaFile"];
  32. soundPlayer2 = new SoundPlayer(str2);
  33. lastPlayTime = DateTime.Now.AddDays(-1);
  34. string sts = ConfigurationManager.AppSettings["MediaPlayInterval"];
  35. if (int.TryParse(sts, out repeatPlayInterval) == false)
  36. {
  37. repeatPlayInterval = 5; //默认5分钟
  38. }
  39. //string edosIp = ConfigurationManager.AppSettings["EDosIP"];
  40. //string strPort = ConfigurationManager.AppSettings["EDosPort"];
  41. //ushort edosPort = 0;
  42. //if (!ushort.TryParse(strPort, out edosPort))
  43. //{
  44. // edosPort = 10010;
  45. //}
  46. //eDos = new EDosProxy(edosIp, edosPort);
  47. //mediaUri = new Uri(str);
  48. //mediaPlayer = new MediaElement();
  49. //mediaPlayer.LoadedBehavior = MediaState.Manual;
  50. //mediaPlayer.UnloadedBehavior = MediaState.Manual;
  51. //mediaPlayer.MediaEnded += new RoutedEventHandler(media_MediaEnded);
  52. }
  53. public static MediaManager Instance
  54. {
  55. get { return SingletonCreator.instance; }
  56. }
  57. class SingletonCreator
  58. {
  59. internal static readonly MediaManager instance = new MediaManager();
  60. }
  61. #endregion
  62. public void Silence()
  63. {
  64. lastPlayTime = DateTime.Now;
  65. this.Stop();
  66. }
  67. public void Play()
  68. {
  69. if (isPlaying )
  70. return;
  71. if (isPlaying2)
  72. {
  73. if (soundPlayer2 != null)
  74. {
  75. soundPlayer2.Stop();
  76. }
  77. isPlaying2 = false;
  78. }
  79. TimeSpan ts = DateTime.Now.Subtract(lastPlayTime);
  80. if (ts.TotalMinutes < repeatPlayInterval)
  81. return;
  82. if (soundPlayer != null)
  83. {
  84. soundPlayer.PlayLooping();
  85. //mediaPlayer.Source = mediaUri;
  86. //mediaPlayer.Play();
  87. isPlaying = true;
  88. //lastPlayTime = DateTime.Now;
  89. }
  90. }
  91. public void Play2()
  92. {
  93. if (isPlaying2)
  94. return;
  95. if (isPlaying)
  96. {
  97. if (soundPlayer != null)
  98. {
  99. soundPlayer.Stop();
  100. }
  101. isPlaying = false;
  102. }
  103. TimeSpan ts = DateTime.Now.Subtract(lastPlayTime);
  104. if (ts.TotalMinutes < repeatPlayInterval)
  105. return;
  106. if (soundPlayer2 != null)
  107. {
  108. soundPlayer2.PlayLooping();
  109. //mediaPlayer.Source = mediaUri;
  110. //mediaPlayer.Play();
  111. isPlaying2 = true;
  112. //lastPlayTime = DateTime.Now;
  113. }
  114. }
  115. public void Stop()
  116. {
  117. if (soundPlayer != null)
  118. {
  119. soundPlayer.Stop();
  120. }
  121. if (soundPlayer2 != null)
  122. {
  123. soundPlayer2.Stop();
  124. }
  125. isPlaying = false;
  126. isPlaying2 = false;
  127. }
  128. public void UpdateAlertMediaStatus()
  129. {
  130. UpdateActiveStatus();
  131. if (isActive == false)
  132. return;
  133. try
  134. {
  135. TimeSpan ts = DateTime.Now.Subtract(lastPlayTime);
  136. if (ts.TotalMinutes < repeatPlayInterval)
  137. return;
  138. var lst = ClientCache.Instance.AlertSnaps;
  139. if (lst != null && lst.Count > 0)
  140. {
  141. var tmp = lst.Where(q => q.IsConfirmed == false &&
  142. (q.Rank == "5")).OrderByDescending(q=>q.LastUpdateTime).First();
  143. if (tmp != null)
  144. {
  145. TimeSpan ts1 = DateTime.Now.Subtract(tmp.LastUpdateTime.Value);
  146. if (ts1.TotalMinutes < repeatPlayInterval)
  147. {
  148. this.Play2();
  149. return;
  150. }
  151. }
  152. var tmp2 = lst.Where(q => q.IsConfirmed == false &&
  153. (q.Rank == "4")).OrderByDescending(q => q.LastUpdateTime).First();
  154. if (tmp2 != null )
  155. {
  156. TimeSpan ts1 = DateTime.Now.Subtract(tmp2.LastUpdateTime.Value);
  157. if (ts1.TotalMinutes < repeatPlayInterval)
  158. {
  159. this.Play();
  160. return;
  161. }
  162. }
  163. }
  164. this.Stop();
  165. }
  166. catch
  167. {
  168. }
  169. }
  170. public void UpdateActiveStatus()
  171. {
  172. try
  173. {
  174. // isActive = !eDos.GetDIRTValue(keyIsActive);
  175. }
  176. catch { };
  177. }
  178. public void Active()
  179. {
  180. try
  181. {
  182. this.Stop();
  183. isActive = false;
  184. // eDos.SendSinglePoint(keyIsActive, 0);
  185. }
  186. catch { }
  187. }
  188. public void DeActive()
  189. {
  190. try
  191. {
  192. isActive = true;
  193. //eDos.SendSinglePoint(keyIsActive, 1);
  194. }
  195. catch { }
  196. }
  197. }
  198. }