JobList.cs 3.4 KB
using OnlineStore.Common;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DeviceLibrary
{
    class StoreJobList
    {
        public ConcurrentQueue<JobInfo> jobInfos = new ConcurrentQueue<JobInfo>();
        string lastoutpos = "";
        string jobname = "";
        public StoreJobList(string jn) {
            jobname = jn;
        }
        public void Enqueue(JobInfo jobInfo)
        {
            lock (jobInfos)
            {
                if (lastoutpos == jobInfo.PosId)
                {
                    LogUtil.info($"[{jobname}] 出库任务正在执行:" + jobInfo.ToStr());
                    return;
                }
                var l = jobInfos.ToList().Find((x) => { return x.PosId == jobInfo.PosId; });
                if (l == null)
                {
                    jobInfos.Enqueue(jobInfo);
                    LogUtil.info($"[{jobname}] 出库任务加入队列:" + jobInfo.ToStr());
                }
                else
                    LogUtil.info($"[{jobname}] 出库任务已存在队列中:" + jobInfo.ToStr());
            }
        }
        public bool Dequeue(out JobInfo jobInfo)
        {
            if (jobInfos.TryDequeue(out jobInfo)) {
                lastoutpos = jobInfo.PosId;
                return true;
            }
            return false;
        }
        public int Count {
            get => jobInfos.Count;
        }
        public void ClearLastPosid(string posid) {
            LogUtil.info($"[{jobname}] 清除正在执行的任务:cur:" + posid + ",last:" + lastoutpos);
            if (lastoutpos == posid)
                lastoutpos = "";

            lock (jobInfos)
            {
                List<JobInfo> tempList = new List<JobInfo>();
                JobInfo item;
                while (jobInfos.TryDequeue(out item))
                {
                    if (item.PosId != posid)
                    {
                        tempList.Add(item);
                    }
                }

                // 重新将剩余的元素加入队列中
                foreach (JobInfo i in tempList)
                {
                    jobInfos.Enqueue(i);
                }
            }
        }
    }
    
    public class JobInfo
    {
        public JobInfo(string wareNum, string posId, int platew, int plateh)
        {
            this.WareNum = wareNum;
            this.PosId = posId;
            this.plateW = platew;
            this.plateH = plateh;
            LogUtil.info($"new JobInfo:{wareNum},{posId},{platew},{plateh}");
        }
        /// <summary>
        /// 夹具编码值(1-6)
        /// </summary>
        public int TrayCode { get; set; }
        /// <summary>
        /// 物品二维码
        /// </summary>
        public string WareNum { get; set; }
        /// <summary>
        /// 库位号编码
        /// </summary>
        public string PosId { get; set; }
        /// <summary>
        /// 料盘宽
        /// </summary>
        public int plateW { get; set; }
        /// <summary>
        /// 料盘高
        /// </summary>
        public int plateH { get; set; }

        public string ToStr()
        {
            return "TrayCode【" + TrayCode + "】,WareNum=【" + WareNum + "】,PosId=【" + PosId + "】,plateW=【" + plateW + "】,plateH=【" + plateH + "】";
        }
    }


}