using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Threading; using System.Threading; using OpenSEMI.Core.Msg; namespace OpenSEMI.ClientBase.Handlers { public class WaferStatusHandler : IHandler { public static readonly List ReverseModules = new List() { "LP1", "LP2", "LP3", "LP4", "Cassette", }; public WaferStatusHandler(Func> funcGetWaferStatus, List allModules) { this.WaferStatusDic = new Dictionary(); this.looper = new MsgPool(500, Do); this.GetWaferStatusByModule = funcGetWaferStatus; //init wafer status dictionary allModules.ForEach(key => { InitWafersByModule(key); }); } public void Handle() { this.looper.Run(); } private void InitWafersByModule(string moduleID) { lock (lockObj) { if (!this.WaferStatusDic.ContainsKey(moduleID)) { this.WaferStatusDic.Add(moduleID, new ModuleWaferManager(moduleID)); List mWafers = GetWaferStatusByModule(moduleID); if (mWafers != null) { ModuleWaferManager modinfowithwafer = this.WaferStatusDic[moduleID]; for (int i = 0; i < mWafers.Count; i++) { modinfowithwafer.Wafers.Add(mWafers[i]); } if (ReverseModules.Contains(moduleID)) { modinfowithwafer.Wafers.Reverse(); } } } } } private void Do(MsgPool pool) { lock (lockObj) { if (Application.Current == null || Application.Current.Dispatcher == null) return; foreach (KeyValuePair pair in WaferStatusDic) { List mWafers = GetWaferStatusByModule(pair.Key); Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate { if (mWafers != null && mWafers.Count == pair.Value.Wafers.Count) { int index; for (int i = 0; i < mWafers.Count; i++) { //status used in UI control if (ReverseModules.Contains(pair.Key)) index = mWafers.Count - i - 1; else index = i; pair.Value.Wafers[i].WaferStatus = mWafers[index].WaferStatus; pair.Value.Wafers[i].WaferID = mWafers[index].WaferID; pair.Value.Wafers[i].SourceName = mWafers[index].SourceName; } } }); } } } public Dictionary WaferStatusDic { get; private set; } public MsgPool looper { get; private set; } private Func> GetWaferStatusByModule; private static object lockObj = new object(); } }