using System.Collections.Generic; using System.Linq; using System.Text; namespace M423project { public class ProductStep { public int DetectID { get; set; } public int Step { get; set; } public ProductStep(int detectID) { DetectID = detectID; Step = 0; } } public class StepContol { static object _stepLock = new object(); private volatile List _stepList = new List(); public int Count { get { return _stepList.Count(); } } public void RemoveUnload() { lock (_stepLock) { //List listRemove = new List(); //for (int i = 0; i < _stepList.Count; i++) //{ // if (_stepList[i].Step == 3) // { // listRemove.Add(i); // } //} //foreach (int i in listRemove) // _stepList.RemoveAt(i); _stepList.RemoveAll(u => u.Step == 3); } } public void IncStep() { //lock (this) //{ // foreach (var x in _stepList) // { // x.Step++; // } //} lock (_stepLock) { _stepList.ForEach(u => u.Step++); } } public void AddProductStep(int detectID) { //lock (this) //{ // _stepList.Add(new ProductStep(detectID)); //} lock (_stepLock) { _stepList.Add(new ProductStep(detectID)); } } public int GetDetectHeightID() { //lock (this) //{ // int id = -1; // for (int i = 0; i < _stepList.Count; i++) // { // if (_stepList[i].Step == 0) // { // id = _stepList[i].DetectID; // break; // } // } // return id; //} return _stepList.FirstOrDefault(u => u.Step == 0)?.DetectID ?? -1; } public int GetDetectSizeStepID() { //lock (this) //{ // int id = -1; // for (int i = 0; i < _stepList.Count; i++) // { // if (_stepList[i].Step == 2) // { // id = _stepList[i].DetectID; // break; // } // } // return id; //} return _stepList.FirstOrDefault(u => u.Step == 2)?.DetectID ?? -1; } public int GetUnloadStepID() { //lock (this) //{ // int id = -1; // for (int i = 0; i < _stepList.Count; i++) // { // if (_stepList[i].Step == 3) // { // id = _stepList[i].DetectID; // break; // } // } // return id; //} return _stepList.FirstOrDefault(u => u.Step == 3)?.DetectID ?? -1; } public void Clear() { _stepList.Clear(); } public override string ToString() { StringBuilder sb = new StringBuilder(); sb.Append(string.Format("StepControl Count:{0}", _stepList.Count)); foreach (var x in _stepList) { sb.Append(" ["); sb.Append(x.DetectID.ToString()); sb.Append(","); sb.Append(x.Step.ToString()); sb.Append("]"); } return sb.ToString(); } #region Add by Patrick 2018-07-12 /// /// 获取下料机械手臂下料完成时的两块电池的ID号 /// /// public List GetUnloadStep2IDs() { //lock (this) { return _stepList.OrderByDescending(p => p.Step).Take(2).Select(p => p.DetectID).ToList(); } } /// /// 获取下料机械手臂下料完成时的电池的ID号 /// /// public int GetUnloadBatteryID() { ////lock (this) //{ // var st = _stepList.OrderByDescending(p => p.Step).FirstOrDefault(); // if (st != null) // { // return st.DetectID; // } // else // { // return 0; // } // //return _stepList.OrderByDescending(p => p.Step).FirstOrDefault().DetectID; //} return _stepList.OrderByDescending(p => p.Step).FirstOrDefault().DetectID; } public void RemoveById(int id) { lock (_stepLock) { _stepList.RemoveAll(p => p.DetectID == id); } } #endregion } }