using System.Collections.Generic; using System.Windows.Forms; using System.Threading; using HNWD.Pregrant.BusinessLogic; using HNWD.Pregrant.Model; using System; using HNWD.Pregrant.Common; using System.Threading.Tasks; using System.Collections.Concurrent; namespace HNWD.LatticeScreen.Server { public class CarouselController { //private List ledLatticeScreenlist = new List(); private Dictionary> ledLatticeScreenlist1 = new Dictionary>(); //private readonly List resledLatticeScreenlist = new List(); private Dictionary> resledLatticeScreenlist1 = new Dictionary>(); private List partids = new List(); private bool bWhile = true; /// /// 构造函数 /// /// 数据库链接字符串 /// 电话停留时间 public CarouselController(string dataSource, string stayingtime) { GlobalConfigureSetting.AddConnectionSetting(dataSource); GlobalConfigureSetting.AddCallingStayingTime(stayingtime); } public void AssignLedLatticeScreen(string partid) { WD_RunProfileBusinessLogic wd_RunInfo = WD_RunProfileBusinessLogic.GetInstance(); WD_RunProfileExt wd_ext = wd_RunInfo.QueryAll(); if (wd_ext.bSuccess && wd_ext.Rows > 0) { for (int i = 0; i < wd_ext.messageDataList.Count; i++) { if (wd_ext.messageDataList[i].LEDDeviceFlag == "1") { this.InitializeList(partid); wd_RunInfo.UpdateLedFlag("0"); } else { // this.InitializeList(partid); } } } } private void InitializeList(string partid) { ///保存正在运行的led点阵屏的属性 , 如当前索引值,当前led点阵屏时间戳 foreach (LedLatticeScreen led in ledLatticeScreenlist1[partid]) { LedLatticeScreen resled3 = this.resledLatticeScreenlist1[partid].Find(f => f.DeviceID.ToString() == led.DeviceID.ToString()); resled3.CurDisIndex = led.CurDisIndex; resled3.secnow1 = led.secnow1; resled3.CallingList = led.CallingList; } this.ledLatticeScreenlist1[partid].Clear(); WD_DeviceInfoBusinessLogic wd_Device = new WD_DeviceInfoBusinessLogic("0"); List ls = wd_Device.QueryAll().messageDataList.FindAll(f => f.DEVICE_TYPE == "6" && f.DEVICE_STATUS == "1" && f.PARTID == partid);//取类型为点阵屏以及状态为激活的点阵屏数据 foreach (WD_DeviceInfo wd in ls) { LedLatticeScreen led1 = new LedLatticeScreen(wd); led1.AddProgram(wd.ID); this.ledLatticeScreenlist1[partid].Add(led1); if (!this.resledLatticeScreenlist1[partid].Exists(f => f.DeviceID == wd.ID)) { LedLatticeScreen led2 = new LedLatticeScreen(wd); this.resledLatticeScreenlist1[partid].Add(led2); } } ///还原正在运行的led点阵屏的属性 , 如当前索引值,当前led点阵屏时间戳 foreach (LedLatticeScreen led in ledLatticeScreenlist1[partid]) { LedLatticeScreen resled3 = this.resledLatticeScreenlist1[partid].Find(f => f.DeviceID.ToString() == led.DeviceID.ToString()); led.CurDisIndex = resled3.CurDisIndex; led.secnow1 = resled3.secnow1; led.CallingList = resled3.CallingList; } } public void AddCallingProgram(string CallId, string CallText, int PartID) { var partIdStr = PartID.ToString(); if (!ledLatticeScreenlist1.ContainsKey(partIdStr)) { return; } for (int i = 0; i < ledLatticeScreenlist1[partIdStr].Count; i++) { CallingProgram cp = new CallingProgram(ledLatticeScreenlist1[partIdStr][i], CallId); cp.SetEntity(new WD_ProgramInfo() { PROGRAM_TEXT = CallText, }); if (!ledLatticeScreenlist1[partIdStr][i].CallingList.Exists(f => f.CallId == CallId)) { ledLatticeScreenlist1[partIdStr][i].CallingList.Add(cp); } } } public void DeleteCallingProgram(string CallId, int PartID) { for (int i = 0; i < ledLatticeScreenlist1[PartID.ToString()].Count; i++) { ledLatticeScreenlist1[PartID.ToString()][i].CallingList.Remove(ledLatticeScreenlist1[PartID.ToString()][i].CallingList.Find(f => f.CallId == CallId)); } } public void Start() { WD_DeviceInfoBusinessLogic busloc = new WD_DeviceInfoBusinessLogic("0"); List lpartids = busloc.QueryPartIDs(); foreach (WD_DeviceInfo partid in lpartids) { this.ledLatticeScreenlist1.Add(partid.PARTID, new List()); this.resledLatticeScreenlist1.Add(partid.PARTID, new List()); this.InitializeList(partid.PARTID); Thread t1 = new Thread(new ParameterizedThreadStart(doAsync)); t1.Start((object)partid.PARTID); } } private void doAsync(object partid) { while (this.bWhile) { this.AssignLedLatticeScreen(partid.ToString()); if (ledLatticeScreenlist1[partid.ToString()].Count > 0) { Parallel.For(0, ledLatticeScreenlist1[partid.ToString()].Count, item => { if (ledLatticeScreenlist1[partid.ToString()][item].PingIpOrDomainName()) { ledLatticeScreenlist1[partid.ToString()][item].Start(); } }); } Thread.Sleep(100); } } public void End() { this.bWhile = false; } public void Restart() { this.bWhile = true; Start(); } public string GetVersion() { return "1.0.0.2"; } } }