| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677 | using System;using System.Collections.Generic;using System.Linq;using System.Windows.Forms;using Aitex.Core.Common;using Aitex.Core.RT.DataCenter;using Aitex.Core.RT.Event;using Aitex.Core.RT.Log;using Aitex.Core.RT.OperationCenter;using Aitex.Core.Util;using MECF.Framework.Common.DBCore;using MECF.Framework.Common.Equipment;using MECF.Framework.Common.Utilities;namespace MECF.Framework.Common.SubstrateTrackings{    public class WaferManager : Singleton<WaferManager>    {         Dictionary<string, WaferInfo> _dict = new Dictionary<string, WaferInfo>();        object _lockerWaferList = new object();        Dictionary<ModuleName, Dictionary<int, WaferInfo>> _locationWafers = new Dictionary<ModuleName, Dictionary<int, WaferInfo>>();        private const string EventWaferLeft = "WaferLeftPosition";        private const string EventWaferArrive = "WaferArrivePosition";        public WaferManager()        {        }        public void Serialize()        {            try            {                if (_locationWafers != null)                {                    BinarySerializer<Dictionary<ModuleName, Dictionary<int, WaferInfo>>>.ToStream(_locationWafers, "WaferManager");                }            }            catch (Exception ex)            {                LOG.WriteExeption(ex);            }        }        public void Deserialize()        {            try            {                var ccc = BinarySerializer<Dictionary<ModuleName, Dictionary<int, WaferInfo>>>.FromStream("WaferManager");                if (ccc != null)                {                    foreach (var moduleWafers in ccc)                    {                        if (ModuleHelper.IsLoadPort(moduleWafers.Key))                        {                            foreach (var waferInfo in moduleWafers.Value)                            {                                waferInfo.Value.SetEmpty();                            }                        }                    }                    _locationWafers = ccc;                }            }            catch (Exception ex)            {                LOG.WriteExeption(ex);            }        }        public void Initialize()        {            EV.Subscribe(new EventItem("Event", EventWaferLeft, "Wafer Left"));            EV.Subscribe(new EventItem("Event", EventWaferArrive, "Wafer Arrived"));            Deserialize();        }         public void SubscribeLocation(string module, int slotNumber)        {            ModuleName mod;            if (Enum.TryParse(module, out mod))            {                SubscribeLocation(mod, slotNumber);            }            else            {                LOG.Write(eEvent.ERR_WAFER_MANAGER_FAILED, ModuleName.System, string.Format("Failed SubscribeLocation, module name invalid, {0} ", module));            }        }        public void SubscribeLocation(ModuleName module, int slotNumber)        {            if (!_locationWafers.ContainsKey(module))            {                _locationWafers[module] = new Dictionary<int, WaferInfo>();                for (int i = 0; i < slotNumber; i++)                {                    _locationWafers[module][i] = new WaferInfo();                }            }            DATA.Subscribe(module.ToString(), "ModuleWaferList", () => _locationWafers[module].Values.ToArray());            DATA.Subscribe(module.ToString(), "HasWafer", () => CheckHasWafer(module,0));        }        public void WaferMoved(ModuleName moduleFrom, int slotFrom, ModuleName moduleTo, int slotTo)        {            if (_locationWafers[moduleFrom][slotFrom].IsEmpty)            {                LOG.Write(eEvent.ERR_WAFER_MANAGER_FAILED, ModuleName.System, string.Format("Invalid wafer move, no wafer at source, {0}{1}=>{2}{3}", moduleFrom, slotFrom+1, moduleTo, slotTo+1));                return;            }            if (!_locationWafers[moduleTo][slotTo].IsEmpty)            {                LOG.Write(eEvent.ERR_WAFER_MANAGER_FAILED, ModuleName.System, string.Format("Invalid wafer move, destination has wafer, {0}{1}=>{2}{3}", moduleFrom, slotFrom + 1, moduleTo, slotTo + 1));                return;            }            string waferOrigin = _locationWafers[moduleFrom][slotFrom].WaferOrigin;            WaferInfo wafer = CopyWaferInfo(moduleTo, slotTo, _locationWafers[moduleFrom][slotFrom]);            DeleteWafer(moduleFrom, slotFrom);            //EV.PostMessage(ModuleName.System.ToString(), EventEnum.WaferMoved, waferOrigin, moduleFrom.ToString(), slotFrom + 1, moduleTo.ToString(), slotTo + 1);            LOG.Write(eEvent.EV_WAFER_MOVED, ModuleName.System, waferOrigin, moduleFrom.ToString(), (slotFrom + 1).ToString(), moduleTo.ToString(), (slotTo + 1).ToString());            WaferMoveHistoryRecorder.WaferMoved(wafer.InnerId.ToString(), moduleTo.ToString(), slotTo, wafer.Status.ToString());            if (ModuleHelper.IsLoadPort(moduleFrom))            {                EV.Notify(EventWaferLeft,  new SerializableDictionary<string, string>()                {                    {"SLOT_NO", (slotFrom+1).ToString("D2")},                    {"WAFER_ID", wafer.WaferID},                    {"LOT_ID", wafer.LotId},                    { "CAR_ID", CarrierManager.Instance.GetCarrier(moduleFrom.ToString()).CarrierId ?? ""},                    { "LEFT_POS_NAME", $"{moduleFrom}.{(slotFrom+1).ToString("D2")}" },                    {"PortID", (wafer.OriginStation-(int)ModuleName.LP1+1).ToString()},                    {"SlotID", (wafer.OriginSlot+1).ToString()},                    {"StationName", moduleFrom.ToString()},                                    });            }            else            {                EV.Notify(EventWaferLeft, new SerializableDictionary<string, string>()                {                    {"PortID", (wafer.OriginStation-(int)ModuleName.LP1+1).ToString()},                    {"SlotID", (wafer.OriginSlot+1).ToString()},                    {"StationName", moduleFrom.ToString()},                });            }            if (ModuleHelper.IsLoadPort(moduleTo))            {                EV.Notify(EventWaferArrive, new SerializableDictionary<string, string>()                {                    {"SLOT_NO", (slotTo+1).ToString("D2")},                    {"WAFER_ID", wafer.WaferID},                    {"LOT_ID", wafer.LotId},                    { "CAR_ID", CarrierManager.Instance.GetCarrier(moduleTo.ToString()).CarrierId ?? ""},                    { "ARRIVE_POS_NAME", $"{moduleTo}.{(slotTo+1).ToString("D2")}" },                    {"PortID", (wafer.OriginStation-(int)ModuleName.LP1+1).ToString()},                    {"SlotID", (wafer.OriginSlot+1).ToString()},                    {"StationName", moduleTo.ToString()},                });            }            else            {                EV.Notify(EventWaferArrive, new SerializableDictionary<string, string>()                {                    {"PortID", (wafer.OriginStation-(int)ModuleName.LP1+1).ToString()},                    {"SlotID", (wafer.OriginSlot+1).ToString()},                    {"StationName", moduleTo.ToString()},                });            }            Serialize();        }        public bool IsWaferSlotLocationValid(ModuleName module, int slot)        {            return _locationWafers.ContainsKey(module) && _locationWafers[module].ContainsKey(slot);        }        public WaferInfo[] GetWafers(ModuleName module)        {            return _locationWafers[module].Values.ToArray() ;        }        public WaferInfo[] GetWaferByProcessJob(string jobName)        {            List<WaferInfo> wafers = new List<WaferInfo>();            foreach (var moduleWafer in _locationWafers)            {                foreach (var waferInfo in moduleWafer.Value)                {                    if (waferInfo.Value!=null && !waferInfo.Value.IsEmpty                                               && (waferInfo.Value.ProcessJob!=null)                         && waferInfo.Value.ProcessJob.Name == jobName)                        wafers.Add(waferInfo.Value);                }            }            return wafers.ToArray();        }        public WaferInfo[] GetWafer(string waferID)        {            List<WaferInfo> result = new List<WaferInfo>();            foreach (var locationWafer in _locationWafers)            {                foreach (var wafer in locationWafer.Value)                {                    if (wafer.Value.WaferID == waferID)                        result.Add(wafer.Value);                }            }            return result.ToArray();        }        public WaferInfo  GetWafer(ModuleName module, int slot)        {            return _locationWafers[module][slot];        }        public string GetWaferID(ModuleName module, int slot)        {            return IsWaferSlotLocationValid(module, slot) ? _locationWafers[module][slot].WaferID: "";        }        public bool CheckNoWafer(ModuleName module, int slot)        {            return IsWaferSlotLocationValid(module, slot) && _locationWafers[module][slot].IsEmpty;        }        public bool CheckNoWafer(string module, int slot)        {            return CheckNoWafer((ModuleName)Enum.Parse(typeof(ModuleName), module), slot);        }        public bool CheckHasWafer(ModuleName module, int slot)        {            return IsWaferSlotLocationValid(module, slot) && !_locationWafers[module][slot].IsEmpty;        }        public bool CheckWaferIsDummy(ModuleName module, int slot)        {            return IsWaferSlotLocationValid(module, slot) && !_locationWafers[module][slot].IsEmpty                                                           && _locationWafers[module][slot].Status == WaferStatus.Dummy;        }        /// <summary>        /// Verify the slot map in string[] format, if there's any mis-match it will return false.        /// </summary>        /// <param name="moduleNo">LP No</param>        /// <param name="flagStrings">Flag input</param>        /// <returns></returns>        public bool CheckWaferExistFlag(string moduleNo, string[] flagStrings, out string reason)        {            var i = 0;            reason = string.Empty;            if (!Enum.TryParse($"LP{moduleNo}", out ModuleName module))            {                reason = "Port Number Error";                return false;            }            foreach (var slot in flagStrings)            {                if (slot == "1")                {                    if (IsWaferSlotLocationValid(module, i) && _locationWafers[module][i].IsEmpty)                    {                        reason = "Flag Mis-Match";                        return false;                    }                }                else                {                    if (IsWaferSlotLocationValid(module, i) && !_locationWafers[module][i].IsEmpty)                    {                        reason = "Flag Mis-Match";                        return false;                    }                }                i++;            }            return true;        }        public bool CheckHasWafer(string module, int slot)        {            return CheckHasWafer((ModuleName)Enum.Parse(typeof(ModuleName), module), slot);        }        public bool CheckWaferFull(ModuleName module)        {            var wafers = _locationWafers[module];            foreach (var waferInfo in wafers)            {                if (waferInfo.Value.IsEmpty)                    return false;            }            return true;        }        public bool CheckWaferEmpty(ModuleName module)        {            var wafers = _locationWafers[module];            foreach (var waferInfo in wafers)            {                if (!waferInfo.Value.IsEmpty)                    return false;            }            return true;        }        public bool CheckWafer(ModuleName module, int slot, WaferStatus state)        {            if (module==ModuleName.Robot && slot == 2)            {                return IsWaferSlotLocationValid(module, 0) && (_locationWafers[module][0].Status==state)                    && IsWaferSlotLocationValid(module, 1) && (_locationWafers[module][1].Status==state);            }            return IsWaferSlotLocationValid(module, slot) && (_locationWafers[module][slot].Status==state);        }        public WaferInfo CreateWafer(ModuleName module, int slot, WaferStatus state)        {            if (!IsWaferSlotLocationValid(module, slot))            {                LOG.Write(eEvent.ERR_WAFER_MANAGER_FAILED, ModuleName.System, string.Format("Invalid wafer create, invalid parameter, {0},{1}", module, slot+1));                return null;            }            string carrierInnerId = "";            string carrierID = string.Empty;            if (ModuleHelper.IsLoadPort(module))            {                CarrierInfo carrier = CarrierManager.Instance.GetCarrier(module.ToString());                if (carrier == null)                {                    LOG.Write(eEvent.ERR_WAFER_MANAGER_FAILED, ModuleName.System,                       string.Format("No carrier at {0}, can not create wafer", module));                    return null;                }                carrierInnerId = carrier.InnerId.ToString();                carrierID = carrier.CarrierId;            }            lock (_lockerWaferList)            {                _locationWafers[module][slot].Status = state;                _locationWafers[module][slot].ProcessState = EnumWaferProcessStatus.Idle;                _locationWafers[module][slot].ChuckState = EnumWaferChuckStatus.Init;                _locationWafers[module][slot].WaferID = GenerateWaferId(module, slot,"");                _locationWafers[module][slot].WaferOrigin = GenerateOrigin(module, slot);                _locationWafers[module][slot].Station = (int)module;                _locationWafers[module][slot].Slot = slot;                _locationWafers[module][slot].InnerId = Guid.NewGuid();                _locationWafers[module][slot].OriginStation = (int)module;                _locationWafers[module][slot].OriginSlot = slot;                _locationWafers[module][slot].OriginCarrierID = carrierID;                _dict[_locationWafers[module][slot].WaferID] = _locationWafers[module][slot];            }            WaferDataRecorder.CreateWafer(_locationWafers[module][slot].InnerId.ToString(), carrierInnerId, module.ToString(), slot, _locationWafers[module][slot].WaferID);            Serialize();            return _locationWafers[module][slot];        }        public WaferInfo CreateWafer(ModuleName module, int slot, WaferStatus state,WaferSize wz)        {            if (!IsWaferSlotLocationValid(module, slot))            {                LOG.Write(eEvent.ERR_WAFER_MANAGER_FAILED, ModuleName.System, string.Format("Invalid wafer create, invalid parameter, {0},{1}", module, slot + 1));                return null;            }            string carrierInnerId = "";            string carrierID = string.Empty;            if (ModuleHelper.IsLoadPort(module) )            {                CarrierInfo carrier = CarrierManager.Instance.GetCarrier(module.ToString());                if (carrier == null)                {                    LOG.Write(eEvent.ERR_WAFER_MANAGER_FAILED, ModuleName.System, string.Format("No carrier at {0}, can not create wafer.", module));                    return null;                }                carrierInnerId = carrier.InnerId.ToString();                carrierID = carrier.CarrierId;            }            lock (_lockerWaferList)            {                _locationWafers[module][slot].Status = state;                _locationWafers[module][slot].ProcessState = EnumWaferProcessStatus.Idle;                _locationWafers[module][slot].ChuckState = EnumWaferChuckStatus.Init;                _locationWafers[module][slot].WaferID = GenerateWaferId(module, slot, carrierID);                _locationWafers[module][slot].WaferOrigin = GenerateOrigin(module, slot);                _locationWafers[module][slot].Station = (int)module;                _locationWafers[module][slot].Slot = slot;                _locationWafers[module][slot].InnerId = Guid.NewGuid();                _locationWafers[module][slot].OriginStation = (int)module;                _locationWafers[module][slot].OriginSlot = slot;                _locationWafers[module][slot].OriginCarrierID = carrierID;                _locationWafers[module][slot].LotId = "";                 _locationWafers[module][slot].LaserMarker = "";                _locationWafers[module][slot].T7Code = "";                 _locationWafers[module][slot].Size = wz;                 _dict[_locationWafers[module][slot].WaferID] = _locationWafers[module][slot];                            }             LOG.Write(eEvent.EV_WAFER_MANAGER_NOTIFY, ModuleName.System, "System", $"Create wafer successfully on {module} slot:{slot+1} wafersize:{wz}.");            WaferDataRecorder.CreateWafer(_locationWafers[module][slot].InnerId.ToString(), carrierInnerId, module.ToString(), slot, _locationWafers[module][slot].WaferID );            Serialize();             return _locationWafers[module][slot];        }        public void DeleteWafer(ModuleName module, int slotFrom, int count = 1)        {            lock (_lockerWaferList)            {                for (int i = 0; i < count; i++)                {                    int slot = slotFrom+i;                    if (!IsWaferSlotLocationValid(module, slot))                    {                        LOG.Write(eEvent.ERR_WAFER_MANAGER_FAILED, ModuleName.System, string.Format("Invalid wafer delete, invalid parameter, {0},{1}", module, slot + 1));                        continue;                    }                    WaferDataRecorder.DeleteWafer(_locationWafers[module][slot].InnerId.ToString());                    _locationWafers[module][slot].SetEmpty();                    _dict.Remove(_locationWafers[module][slot].WaferID);                }            }            Serialize();        }        public void UpdateWaferLaser(ModuleName module, int slot, string laserMarker)        {            if (!IsWaferSlotLocationValid(module, slot))            {                LOG.Write(eEvent.ERR_WAFER_MANAGER_FAILED, ModuleName.System, string.Format("Failed UpdateWaferLaser, invalid parameter, {0},{1}", module, slot + 1));                return;            }            lock (_lockerWaferList)            {                _locationWafers[module][slot].LaserMarker = laserMarker;                WaferDataRecorder.SetWaferMarker(_locationWafers[module][slot].InnerId.ToString(), laserMarker);            }            Serialize();        }        public void UpdateWaferT7Code(ModuleName module, int slot, string T7Code)        {            if (!IsWaferSlotLocationValid(module, slot))            {                LOG.Write(eEvent.ERR_WAFER_MANAGER_FAILED, ModuleName.System, string.Format("Failed UpdateWaferT7Code, invalid parameter, {0},{1}", module, slot + 1));                return;            }            lock (_lockerWaferList)            {                _locationWafers[module][slot].T7Code = T7Code;                WaferDataRecorder.SetWaferT7Code(_locationWafers[module][slot].InnerId.ToString(), T7Code);            }            Serialize();        }        public void UpdateWaferTransFlag(ModuleName module, int slot, string flag)        {            if (!IsWaferSlotLocationValid(module, slot))            {                LOG.Write(eEvent.ERR_WAFER_MANAGER_FAILED, ModuleName.System, string.Format("Failed UpdateWaferTransFlag, invalid parameter, {0},{1}", module, slot + 1));                return;            }            lock (_lockerWaferList)            {                _locationWafers[module][slot].TransFlag = flag;            }            Serialize();        }        public void UpdateWaferNotch(ModuleName module, int slot, int angle)        {            if (!IsWaferSlotLocationValid(module, slot))            {                LOG.Write(eEvent.ERR_WAFER_MANAGER_FAILED, ModuleName.System, string.Format("Failed UpdateWaferNotch, invalid parameter, {0},{1}", module, slot + 1));                return;            }            lock (_lockerWaferList)            {                _locationWafers[module][slot].Notch = angle;            }            Serialize();        }        public void UpdateWaferProcessStatus(ModuleName module, int slot, EnumWaferProcessStatus status)        {            if (!IsWaferSlotLocationValid(module, slot))            {                LOG.Write(eEvent.ERR_WAFER_MANAGER_FAILED, ModuleName.System, string.Format("Failed UpdateWaferProcessStatus, invalid parameter, {0},{1}", module, slot + 1));                return;            }            lock (_lockerWaferList)            {                _locationWafers[module][slot].ProcessState = status;            }            Serialize();        }         public void UpdateWaferChuckStatus(ModuleName module, int slot, EnumWaferChuckStatus status)        {            if (!IsWaferSlotLocationValid(module, slot))            {                LOG.Write(eEvent.ERR_WAFER_MANAGER_FAILED, ModuleName.System, string.Format("Failed UpdateWaferChuckStatus, invalid parameter, {0},{1}", module, slot + 1));                return;            }            lock (_lockerWaferList)            {                _locationWafers[module][slot].ChuckState = status;            }            Serialize();        }        public void UpdateWaferId(ModuleName module, int slot, string waferId)        {            if (!IsWaferSlotLocationValid(module, slot))            {                LOG.Write(eEvent.ERR_WAFER_MANAGER_FAILED, ModuleName.System, string.Format("Failed UpdateWaferId, invalid parameter, {0},{1}", module, slot + 1));                return;            }            lock (_lockerWaferList)            {                _locationWafers[module][slot].WaferID = waferId;            }            Serialize();        }        public void UpdateWaferLotId(ModuleName module, int slot, string lotId)        {            if (!IsWaferSlotLocationValid(module, slot))            {                LOG.Write(eEvent.ERR_WAFER_MANAGER_FAILED, ModuleName.System, string.Format("Failed UpdateWaferLotId, invalid parameter, {0},{1}", module, slot + 1));                return;            }            lock (_lockerWaferList)            {                _locationWafers[module][slot].LotId = lotId;            }            Serialize();        }        public void UpdateWaferProcessStatus(string waferID, EnumWaferProcessStatus status)        {            WaferInfo[] wafers = GetWafer(waferID);            lock (_lockerWaferList)            {                foreach (var waferInfo in wafers)                {                    waferInfo.ProcessState = status;                }            }            Serialize();        }        public void UpdateWaferProcess(string waferID, string processId)        {            WaferInfo[] wafers = GetWafer(waferID);            lock (_lockerWaferList)            {                foreach (var waferInfo in wafers)                {                    WaferDataRecorder.SetProcessInfo(waferInfo.InnerId.ToString(), processId);                }            }            Serialize();        }        public WaferInfo CopyWaferInfo(ModuleName module, int slot, WaferInfo wafer)        {            if (!IsWaferSlotLocationValid(module, slot))            {                LOG.Write(eEvent.ERR_WAFER_MANAGER_FAILED, ModuleName.System, string.Format("Failed CopyWaferInfo, invalid parameter, {0},{1}", module, slot + 1));                return null;            }            lock (_lockerWaferList)            {                _locationWafers[module][slot].Update(wafer);                _locationWafers[module][slot].Station = (int)module;                _locationWafers[module][slot].Slot = slot;            }            return _locationWafers[module][slot];        }        public bool UpdateWaferSize(ModuleName module, int slot, WaferSize size)        {            if (!IsWaferSlotLocationValid(module, slot))            {                LOG.Write(eEvent.ERR_WAFER_MANAGER_FAILED, ModuleName.System, string.Format("Failed UpdateWaferSize, invalid parameter, {0},{1}", module, slot + 1));                return false;            }            lock (_lockerWaferList)            {                _locationWafers[module][slot].Size = size;            }            Serialize();            return true;        }        private string GenerateWaferId(ModuleName module, int slot,string carrierID)        {                        string carrierinfor = "";            //5 + 2(unit) + 2(slot) + time(18) + index{5}                 if (string.IsNullOrEmpty(carrierID))                carrierinfor = module.ToString() + DateTime.Now.ToString("yyyyMMddHHmmssffff");            else carrierinfor = carrierID;            return string.Format($"{carrierinfor}.{(slot+1).ToString("00")}");        }        private string GenerateOrigin(ModuleName module, int slot)        {            return string.Format("{0}.{1:D2}", ModuleHelper.GetAbbr(module), slot + 1);        }    }}
 |