using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using Gumming.Execute.Workflow; namespace Gumming.Execute.Station { public class StationUnit { public string StationName { get; set; } public int Time { get; set; } public int[] Pos { get; set; } public Context Context { get; set; } public bool StopFlag { get; set; } private readonly object _lock = new object(); public void Work(string stationName, Wafer wafer) { Console.WriteLine( $"{wafer.Name} do [{wafer.CurrentFlow.Index}:{wafer.CurrentFlow.Name}] at {stationName} begin {DateTime.Now}"); Context.AppendLog( $"{wafer.Name} do [{wafer.CurrentFlow.Index}:{wafer.CurrentFlow.Name}] at {stationName} begin {DateTime.Now}"); //机器人作业 try { Thread.Sleep(Time * 1000); } catch (ThreadInterruptedException) { } Console.WriteLine( $"{wafer.Name} do [{wafer.CurrentFlow.Index}:{wafer.CurrentFlow.Name}] at {stationName} end {DateTime.Now}"); Context.AppendLog( $"{wafer.Name} do [{wafer.CurrentFlow.Index}:{wafer.CurrentFlow.Name}] at {stationName} end {DateTime.Now}"); } public int GetPos() { lock (_lock) { for (int i = 0; i < Pos.Length; i++) { if (Pos[i] == 0) { Pos[i] = 1; return i; } } return -1; } } public int GetFinishedPos() { lock (_lock) { for (int i = 0; i < Pos.Length; i++) { if (Pos[i] == 2) { return i; } } return -1; } } public void ReleasePos(int i) { lock (_lock) { Pos[i] = 0; } } public void FinishPos(int i) { lock (_lock) { Pos[i] = 2; } } public void Process(string stationName) { FlowUnit todoFlow = Context.GetFlowUnitByStation(stationName); while (!StopFlag) { int pos = GetPos(); if (pos >= 0) { RobotHand todoWaferRobot = Context.GetTodoWaferByFlow(todoFlow, pos); if (todoWaferRobot != null) { Wafer todoWafer = todoWaferRobot.CurrentWafer; todoWafer.CurrentFlow = todoFlow; int currentPos = todoWafer.CurrentPos; if (todoWafer.Works.Count > 0) { StationWork lastWork = todoWafer.Works[todoWafer.Works.Count - 1]; string log = $"{stationName}{pos}拿到 wafer:{todoWafer.Name} 释放:{lastWork.StationName}"; Console.WriteLine(log); Context.AppendLog(log); Context.StationUnitMap[lastWork.FlowUnit.Name].ReleasePos(currentPos); } else { string log = $"{stationName}{pos}拿到 wafer:{todoWafer.Name}"; Console.WriteLine(log); Context.AppendLog(log); } todoWafer.CurrentPos = pos; Context.Robot.ReleaseFreeHand(todoWaferRobot); StationWork stationWork = new StationWork(); todoWafer.Works.Add(stationWork); stationWork.FlowUnit = todoFlow; stationWork.StationName = stationName + pos; stationWork.StationNo = pos; stationWork.StartTime = DateTime.Now; todoWafer.CurrentStationWork = stationWork; new Thread(() => { Work(stationName + pos, todoWafer); stationWork.EndTime = DateTime.Now; stationWork.Done = true; FinishPos(pos); Context.DoneWafer(stationWork, todoWafer); GetFinishedWafer(stationName); }).Start(); } else { ReleasePos(pos); } } } } /// /// 送料:当本工位wafer结束时取料 /// private void GetFinishedWafer(string stationName) { var finishedPos = GetFinishedPos(); //取到已完成的工位 if (finishedPos >= 0) { RobotHand robotHand = Context.Robot.GetFreeHand(2); //留一个空位 if (robotHand != null) { Wafer currentWafer = Context.GetCurrentWaferByFlow(stationName, finishedPos, robotHand); if (currentWafer != null) { //清除当前工位 /*currentWafer.CurrentFlow = null; currentWafer.CurrentStationWork = null;*/ } else { Context.Robot.ReleaseFreeHand(robotHand); } //置为空闲 ReleasePos(finishedPos); } } } } }