broadcastModule.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using BroadcastModule;
  9. namespace allInOneMachine
  10. {
  11. public class BroadcastLinker
  12. {
  13. public static BroadcastLinker ins = new BroadcastLinker();
  14. static BroadcastBoard board;
  15. static string ip;
  16. static int port;
  17. static int minimumInterval = 5;
  18. static DisplayedMsg[] displayedMsg = new DisplayedMsg[4];
  19. static Queue<DisplayedMsg>[] msgQueue = new Queue<DisplayedMsg>[4];
  20. static object msgLock = new object();
  21. /// <summary>
  22. /// 初始化单例
  23. /// </summary>
  24. public static void Start()
  25. {
  26. Task.Factory.StartNew(() =>
  27. {
  28. try
  29. {
  30. ip = ConfigurationManager.AppSettings["allInOneMachineIP"];
  31. port = Int32.Parse(ConfigurationManager.AppSettings["allInOneMachinePort"]);
  32. board = new BroadcastBoard(ip, port, 5);
  33. board.UpdateTime();
  34. //ins.VolumeControl(100);
  35. board.Play(0, BroadcastBoard.PlayMode.download, "欢迎光临");
  36. board.Play(1, BroadcastBoard.PlayMode.download, "智象停车");
  37. board.Play(2, BroadcastBoard.PlayMode.download, "date");
  38. board.Play(3, BroadcastBoard.PlayMode.download, "time");
  39. //ins.Refresh();
  40. board.Play(0, BroadcastBoard.PlayMode.audio, "欢迎光临");
  41. OriginState();
  42. for (int i = 0; i < 4; i++)
  43. {
  44. displayedMsg[i] = null;
  45. msgQueue[i] = new Queue<DisplayedMsg>();
  46. }
  47. }
  48. catch (Exception e) { Console.WriteLine(e.Message); ins = null; }
  49. Run();
  50. });
  51. }
  52. static void Run()
  53. {
  54. Task.Factory.StartNew(() =>
  55. {
  56. while (true)
  57. {
  58. lock (msgLock)
  59. {
  60. for (int i = 0; i < 4; i++)
  61. {
  62. try
  63. {
  64. //若存在信息显示,则更新剩余时间,超时恢复默认状态
  65. if (displayedMsg[i] != null)
  66. {
  67. int existTime = (int)(DateTime.Now - displayedMsg[i].enteringTime).TotalSeconds;
  68. //有消息等待且最短时间间隔已过
  69. if (msgQueue[i].Count != 0 && existTime > minimumInterval)
  70. {
  71. displayedMsg[i] = msgQueue[i].Dequeue();
  72. displayedMsg[i].enteringTime = DateTime.Now;
  73. board.Play(displayedMsg[i].winID, BroadcastBoard.PlayMode.temporary, displayedMsg[i].str, 3);
  74. if (displayedMsg[i].audio != "")
  75. {
  76. board.Play(0, BroadcastBoard.PlayMode.audio, displayedMsg[i].audio);
  77. }
  78. }
  79. //判断消息剩余时间,不足则恢复默认
  80. else
  81. {
  82. int timeLeft = displayedMsg[i].delay - existTime;
  83. if (timeLeft <= 0)
  84. {
  85. if (displayedMsg[i].switchMode == 1)
  86. board.Play(displayedMsg[i].winID, BroadcastBoard.PlayMode.readBuffer, "", 0, 0);
  87. else if (displayedMsg[i].switchMode == 2)
  88. OriginState();
  89. displayedMsg[i] = null;
  90. }
  91. }
  92. }
  93. else if (msgQueue[i].Count != 0)
  94. {
  95. displayedMsg[i] = msgQueue[i].Dequeue();
  96. displayedMsg[i].enteringTime = DateTime.Now;
  97. board.Play(displayedMsg[i].winID, BroadcastBoard.PlayMode.temporary, displayedMsg[i].str, 3);
  98. if (displayedMsg[i].audio != "")
  99. {
  100. board.Play(0, BroadcastBoard.PlayMode.audio, displayedMsg[i].audio);
  101. }
  102. }
  103. }
  104. catch (Exception e) { Console.WriteLine("更新异常," + e.Message); }
  105. }
  106. }
  107. Thread.Sleep(500);
  108. }
  109. });
  110. }
  111. public void DispForAWhile(int winID, string str, int delay, int switchMode, string audio)
  112. {
  113. if (ins != null)
  114. {
  115. lock (msgLock)
  116. {
  117. DisplayedMsg msg = new DisplayedMsg(winID, str, delay, switchMode, audio);
  118. try
  119. {
  120. if (msgQueue[winID] != null)
  121. {
  122. msgQueue[winID].Enqueue(msg);
  123. }
  124. }
  125. catch { Console.WriteLine("入队异常"); }
  126. }
  127. //ins.Play(winID, BroadcastBoard.PlayMode.temporary, str, 1);
  128. //Task.Factory.StartNew(() =>
  129. //{
  130. // if (switchMode == 1)
  131. // {
  132. // Thread.Sleep(delay * 1000);
  133. // ins.Play(winID, BroadcastBoard.PlayMode.readBuffer, "", 0, 0);
  134. // }
  135. // else if (switchMode == 2)
  136. // {
  137. // Thread.Sleep(delay * 1000);
  138. // OriginState();
  139. // }
  140. //});
  141. }
  142. }
  143. static void OriginState()
  144. {
  145. if (ins != null)
  146. {
  147. board.Play(0, BroadcastBoard.PlayMode.readBuffer, "", 0, 0);
  148. board.Play(1, BroadcastBoard.PlayMode.readBuffer, "", 0, 0);
  149. board.Play(2, BroadcastBoard.PlayMode.readBuffer, "", 0, 0);
  150. board.Play(3, BroadcastBoard.PlayMode.readBuffer, "", 0, 0);
  151. }
  152. }
  153. private class DisplayedMsg
  154. {
  155. public string str;
  156. public string audio;
  157. public int winID;
  158. public int delay;
  159. /// <summary>
  160. /// 播放完成后处理方式,0不做操作,1当前恢复默认,2整体恢复默认
  161. /// </summary>
  162. public int switchMode;
  163. public DateTime enteringTime;
  164. public DisplayedMsg(int winID, string str, int delay, int switchMode, string audio)
  165. {
  166. this.winID = winID;
  167. this.str = str;
  168. this.delay = delay;
  169. this.switchMode = switchMode;
  170. this.audio = audio;
  171. enteringTime = DateTime.Now;
  172. }
  173. }
  174. }
  175. }