PosDebug.cs 6.3 KB
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OnlineStore.DeviceLibrary
{
    public class PosDebug
    {
        static string filePath = $"{Application.StartupPath}//StoreConfig//pos_debug.json";
        static object locker = new object();
        /// <summary>
        /// 左侧库位结果表
        /// </summary>
        public List<PosPos> LPos { get; set; }
        /// <summary>
        /// 右侧库位结果表
        /// </summary>
        public List<PosPos> RPos { get; set; }
        public int Total { get; set; }
        public int Ok { get; set; }
        public void Start()
        {
            Total++;
            ClearPosState();
        }
        public int GetRows()
        {
            return LPos.Select(s=>s.Row).Max();
        }
        public int GetCols()
        {
            return LPos.Select(s => s.Col).Max();
        }
        public bool Exist(string posId,out PosPos posPos)
        {
            posPos = null;
            int idx = LPos.FindIndex(s => s.PosId.Equals(posId));
            if (idx == -1)
            {
                idx = RPos.FindIndex(s => s.PosId.Equals(posId));
                if (idx == -1)
                    return false;
                else
                {
                    posPos = RPos[idx];
                }
            }
            else
            {
                posPos = LPos[idx];
            }
            return true;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="posId"></param>
        /// <param name="posPos"></param>
        /// <param name="dir">0:左侧</param>
        /// <returns></returns>
        public bool Exist(string posId, out PosPos posPos,out int dir)
        {
            posPos = null;
            dir = 0;
            int idx = LPos.FindIndex(s => s.PosId.Equals(posId));
            if (idx == -1)
            {
                idx = RPos.FindIndex(s => s.PosId.Equals(posId));
                if (idx == -1)
                    return false;
                else
                {
                    posPos = RPos[idx];
                    dir = 1;
                }
            }
            else
            {
                posPos = LPos[idx];
            }
            return true;
        }
        void SetPosState(string posId,StoreMoveType moveType)
        {
            int idx = LPos.FindIndex(s=>s.PosId.Equals(posId));
            if(idx == -1)
            {
                idx= RPos.FindIndex(s=>s.PosId.Equals(posId));
                if(idx != -1)
                {
                    if (moveType.Equals(StoreMoveType.InStore))
                    {
                        RPos[idx].PosState.InStoreCheck = 1;
                    }
                    else if (moveType.Equals(StoreMoveType.OutStore))
                    {
                        RPos[idx].PosState.OutStoreCheck = 1;

                    }
                }
            }
            else
            {
                if (moveType.Equals(StoreMoveType.InStore))
                {
                    LPos[idx].PosState.InStoreCheck = 1;
                }
                else if (moveType.Equals(StoreMoveType.OutStore))
                {
                    LPos[idx].PosState.OutStoreCheck = 1;
                }
            }
        }
        public void AddResult(string posId, StoreMoveType movetype, bool isLast)
        {
            if (Monitor.TryEnter(locker, 500))
            {
                try
                {
                    if (isLast)
                        Ok++;
                    SetPosState(posId, movetype);
                    File.WriteAllText(filePath, JsonHelper.SerializeObject(this));
                }
                finally
                {
                    Monitor.Exit(locker);
                }
            }
        }
        private void ClearPosState()
        {
            foreach (var item in LPos)
            {
                item.PosState.InStoreCheck = 0;
                item.PosState.OutStoreCheck = 0;
            }
            foreach (var item in RPos)
            {
                item.PosState.InStoreCheck = 0;
                item.PosState.OutStoreCheck = 0;
            }
        }
        public static PosDebug Init(List<string> posNumList)
        {
            PosDebug posDebug;
            if (File.Exists(filePath))
            {
                string txt = File.ReadAllText(filePath);
                posDebug = JsonHelper.DeserializeJsonToObject<PosDebug>(txt);
                if (posDebug != null)
                    return posDebug;
            }
            posDebug = new PosDebug();
            posDebug.LPos = new List<PosPos>();
            posDebug.RPos = new List<PosPos>();
            for (int i = 0; i < posNumList.Count; i++)
            {
                //AC1#R1_1_11
                //AC1#L1_1_11
                string pos = posNumList[i];
                string[] dir = pos.Split('#');
                if (dir[1].StartsWith("L"))
                {
                    posDebug.LPos.Add(new PosPos { PosId = pos, Index = i });
                }
                else
                {
                    posDebug.RPos.Add(new PosPos { PosId = pos, Index = i });
                }
            }
            return posDebug;
        }

    }
    //AC1#L1_1_11
    public class PosPos
    {
        /// <summary>
        /// 库位ID
        /// </summary>
        public string PosId { get; set; }
        /// <summary>
        /// 库位名
        /// </summary>
        public string PosName { get { return PosId.Split('#')[1]; } }
        /// <summary>
        /// 所在库位列表的索引
        /// </summary>
        public int Index { get; set; }
        /// <summary>
        /// 库位所在层
        /// </summary>
        public int Row { get { return int.Parse(PosId.Split('#')[1].Split('_')[2]); } }
        /// <summary>
        /// 库位所在列
        /// </summary>
        public int Col { get { return int.Parse(PosId.Split('#')[1].Split('_')[0].Substring(1)); } }
        public PosState PosState { get; set; } = new PosState();
    }
    /// <summary>
    /// 库位状态
    /// </summary>
    public class PosState
    {
        public int InStoreCheck = 0;
        public int OutStoreCheck = 0;
    }
}