123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using BroadcastModule;
- namespace allInOneMachine
- {
- public class BroadcastLinker
- {
- public static BroadcastLinker ins = new BroadcastLinker();
- static BroadcastBoard board;
- static string ip;
- static int port;
- static int minimumInterval = 5;
- static DisplayedMsg[] displayedMsg = new DisplayedMsg[4];
- static Queue<DisplayedMsg>[] msgQueue = new Queue<DisplayedMsg>[4];
- static object msgLock = new object();
- /// <summary>
- /// 初始化单例
- /// </summary>
- public static void Start()
- {
- Task.Factory.StartNew(() =>
- {
- try
- {
- ip = ConfigurationManager.AppSettings["allInOneMachineIP"];
- port = Int32.Parse(ConfigurationManager.AppSettings["allInOneMachinePort"]);
- board = new BroadcastBoard(ip, port, 5);
- board.UpdateTime();
- //ins.VolumeControl(100);
- board.Play(0, BroadcastBoard.PlayMode.download, "欢迎光临");
- board.Play(1, BroadcastBoard.PlayMode.download, "智象停车");
- board.Play(2, BroadcastBoard.PlayMode.download, "date");
- board.Play(3, BroadcastBoard.PlayMode.download, "time");
- //ins.Refresh();
- board.Play(0, BroadcastBoard.PlayMode.audio, "欢迎光临");
- OriginState();
- for (int i = 0; i < 4; i++)
- {
- displayedMsg[i] = null;
- msgQueue[i] = new Queue<DisplayedMsg>();
- }
- }
- catch (Exception e) { Console.WriteLine(e.Message); ins = null; }
- Run();
- });
- }
- static void Run()
- {
- Task.Factory.StartNew(() =>
- {
- while (true)
- {
- lock (msgLock)
- {
- for (int i = 0; i < 4; i++)
- {
- try
- {
- //若存在信息显示,则更新剩余时间,超时恢复默认状态
- if (displayedMsg[i] != null)
- {
- int existTime = (int)(DateTime.Now - displayedMsg[i].enteringTime).TotalSeconds;
- //有消息等待且最短时间间隔已过
- if (msgQueue[i].Count != 0 && existTime > minimumInterval)
- {
- displayedMsg[i] = msgQueue[i].Dequeue();
- displayedMsg[i].enteringTime = DateTime.Now;
- board.Play(displayedMsg[i].winID, BroadcastBoard.PlayMode.temporary, displayedMsg[i].str, 3);
- if (displayedMsg[i].audio != "")
- {
- board.Play(0, BroadcastBoard.PlayMode.audio, displayedMsg[i].audio);
- }
- }
- //判断消息剩余时间,不足则恢复默认
- else
- {
- int timeLeft = displayedMsg[i].delay - existTime;
- if (timeLeft <= 0)
- {
- if (displayedMsg[i].switchMode == 1)
- board.Play(displayedMsg[i].winID, BroadcastBoard.PlayMode.readBuffer, "", 0, 0);
- else if (displayedMsg[i].switchMode == 2)
- OriginState();
- displayedMsg[i] = null;
- }
- }
- }
- else if (msgQueue[i].Count != 0)
- {
- displayedMsg[i] = msgQueue[i].Dequeue();
- displayedMsg[i].enteringTime = DateTime.Now;
- board.Play(displayedMsg[i].winID, BroadcastBoard.PlayMode.temporary, displayedMsg[i].str, 3);
- if (displayedMsg[i].audio != "")
- {
- board.Play(0, BroadcastBoard.PlayMode.audio, displayedMsg[i].audio);
- }
- }
- }
- catch (Exception e) { Console.WriteLine("更新异常," + e.Message); }
- }
- }
- Thread.Sleep(500);
- }
- });
- }
- public void DispForAWhile(int winID, string str, int delay, int switchMode, string audio)
- {
- if (ins != null)
- {
- lock (msgLock)
- {
- DisplayedMsg msg = new DisplayedMsg(winID, str, delay, switchMode, audio);
- try
- {
- if (msgQueue[winID] != null)
- {
- msgQueue[winID].Enqueue(msg);
- }
- }
- catch { Console.WriteLine("入队异常"); }
- }
- //ins.Play(winID, BroadcastBoard.PlayMode.temporary, str, 1);
- //Task.Factory.StartNew(() =>
- //{
- // if (switchMode == 1)
- // {
- // Thread.Sleep(delay * 1000);
- // ins.Play(winID, BroadcastBoard.PlayMode.readBuffer, "", 0, 0);
- // }
- // else if (switchMode == 2)
- // {
- // Thread.Sleep(delay * 1000);
- // OriginState();
- // }
- //});
- }
- }
- static void OriginState()
- {
- if (ins != null)
- {
- board.Play(0, BroadcastBoard.PlayMode.readBuffer, "", 0, 0);
- board.Play(1, BroadcastBoard.PlayMode.readBuffer, "", 0, 0);
- board.Play(2, BroadcastBoard.PlayMode.readBuffer, "", 0, 0);
- board.Play(3, BroadcastBoard.PlayMode.readBuffer, "", 0, 0);
- }
- }
- private class DisplayedMsg
- {
- public string str;
- public string audio;
- public int winID;
- public int delay;
- /// <summary>
- /// 播放完成后处理方式,0不做操作,1当前恢复默认,2整体恢复默认
- /// </summary>
- public int switchMode;
- public DateTime enteringTime;
- public DisplayedMsg(int winID, string str, int delay, int switchMode, string audio)
- {
- this.winID = winID;
- this.str = str;
- this.delay = delay;
- this.switchMode = switchMode;
- this.audio = audio;
- enteringTime = DateTime.Now;
- }
- }
- }
- }
|