PosInfos.cs 6.0 KB
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Windows.Forms.LinkLabel;
using System.Windows.Forms;
using System.IO;
using OnlineStore.Common;

namespace CtuDeviceLib
{

    public class PosInfos
    {
        static List<PosInfo> _PosInfos = new List<PosInfo>();

        public static bool Load(string linefilePath)
        {
            if (!File.Exists(linefilePath))
            {
                LogUtil.error($"未找到料架配置路径:{linefilePath}");
                return false;
            }
            LogUtil.info($"加载料架配置:{linefilePath}");
            string[] lines = File.ReadAllLines(linefilePath);
            List<PosInfo> _tempPosInfos = new List<PosInfo>();
            for (int i = 1; i < lines.Length; i++)
            {
                string[] fields = lines[i].Split(',');
                if (fields.Length < 8)
                    continue;
                string posId = fields[0];
                string pointCode = fields[1];
                int.TryParse(fields[2], out int updown);
                int.TryParse(fields[3], out int inout);
                int.TryParse(fields[4], out int scancode);
                int.TryParse(fields[5], out int containerWidth);
                var shelve = new Shelves(posId, pointCode, updown, inout, scancode, containerWidth);
                string side = fields[6].Trim();
                if (!string.IsNullOrEmpty(side))
                {
                    shelve.Side = side[0];
                }
                shelve.Desc = fields[7];
                if (_tempPosInfos.Find(s => s.ShelveCode == posId) != null)
                {
                    LogUtil.error($"{posId} 已存在,请确保货架码唯一");
                    MessageBox.Show($"{posId} 已存在,请确保货架码唯一");
                    return false;
                }
                var pos = new PosInfo()
                {
                    Name = posId.Trim(),
                    Dir = (byte)(shelve.Side == 'L' ? 1 : 2),
                    InoutDepth = shelve.InoutDepth,
                    PointCode = shelve.PointCode,
                    ScanCodeShift = shelve.ScanCodeShift,
                    ShelveCode = posId.Trim(),
                    UpDownHeight = shelve.UpDownHeight,
                    Lanway = shelve.Lanway,
                    Row = shelve.Row,
                };
                if (!string.IsNullOrEmpty(shelve.Desc))
                {
                    pos.Name = shelve.Desc.Trim();
                }
                _tempPosInfos.Add(pos);
            }
            _PosInfos = _tempPosInfos.ToList();
            return true;
        }

        public static List<PosInfo> GetAllPosInfos()
        {
            return _PosInfos;
        }

        /// <summary>
        /// 根据名称获取其位置信息
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static PosInfo GetPosInfoByName(string name)
        {
            return _PosInfos.Find(s => s.Name.Equals(name));
        }
        public static PosInfo GetPosInfoByPtCode(uint pt)
        {
            return _PosInfos.Find(s => s.PointCode.Equals(pt));
        }


    }
    /// <summary>
    /// 货架
    /// </summary>
    public class Shelves
    {
        /// <summary>
        /// 货架号
        /// </summary>
        public string PosId { get; set; }

        /// <summary>
        /// 楼层
        /// </summary>
        public int Floor { get; set; }
        /// <summary>
        /// 巷道编码
        /// </summary>
        public string Lanway { get; set; }
        /// <summary>
        /// 库位在车头的左侧/右侧
        /// </summary>
        public char Side { get; set; }
        /// <summary>
        /// 地面点位编码
        /// </summary>
        public uint PointCode { get; set; }
        /// <summary>
        /// 所在层
        /// </summary>
        public int Row { get; set; }
        /// <summary>
        /// 升降高度
        /// </summary>
        public int UpDownHeight { get; set; }
        /// <summary>
        /// 进出深度
        /// </summary>
        public int InoutDepth { get; set; }
        /// <summary>
        /// 料箱码读码偏移距离
        /// </summary>
        public int ScanCodeShift { get; set; }
        /// <summary>
        /// 料箱宽度
        /// </summary>
        public int ContainerWidth { get; set; }
        //描述
        public string Desc { get; set; }
        /// <summary>
        /// 货架码
        /// 16位
        /// </summary>
        /// <param name="posid"></param>
        public Shelves(string posid, string pointcode, int updown, int inout, int scancode, int containerWidth)
        {
            if (string.IsNullOrEmpty(posid))
                return;
            if (posid.Length < 16)
            {
                posid = "";
                Lanway = "";
                if (!string.IsNullOrEmpty(pointcode))
                {
                    PointCode = uint.Parse(pointcode);
                }
                InoutDepth = inout;
                ScanCodeShift = scancode;
                ContainerWidth = containerWidth;
                return;
            }
            PosId = posid;
            string floor = posid.Substring(0, 1);
            string laneway = posid.Substring(1, 3);
            string side = posid.Substring(4, 1);
            string pointCode = posid.Substring(5, 8);
            if (!string.IsNullOrEmpty(pointcode))
            {
                pointCode = pointcode;
            }
            string row = posid.Substring(14);

            int.TryParse(floor, out int val);
            Floor = val;
            Lanway = laneway;
            Side = side[0];
            PointCode = uint.Parse(pointCode);
            Row = int.Parse(row);
            UpDownHeight = updown;
            InoutDepth = inout;
            ScanCodeShift = scancode;
            ContainerWidth = containerWidth;
        }
    }
}