LandMarks.cs 8.2 KB
using log4net;
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CtuDeviceLib
{

    public class LandMarks
    {
        /// <summary>
        /// 所有地标
        /// </summary>
        static List<PointCode> _pointCodes = new List<PointCode>();

        public static bool Load(string linefilePath)
        {
            if (!File.Exists(linefilePath))
            {
                LogUtil.error($"未找到路标配置路径:{linefilePath}");
                return false;
            }
            LogUtil.info($"加载路标配置:{linefilePath}");
            string[] lines = File.ReadAllLines(linefilePath);
            List<PointCode> _tempPointCodes = new List<PointCode>();
            for (int i = 1; i < lines.Length; i++)
            {
                string[] fields = lines[i].Split(',');
                if (fields.Length < 10)
                    continue;
                var landmark = new PointCode();
                uint.TryParse(fields[0], out uint id);
                landmark.Id = id;
                landmark.Name = fields[1];
                landmark.Type = (LandmarkType)Enum.Parse(typeof(LandmarkType), fields[2]);
                landmark.CanTurning = bool.Parse(fields[3]);
                string above = fields[4];
                if (!string.IsNullOrEmpty(above))
                {
                    landmark.Above = new LandmarkNeighboor();
                }
                if (!setNeighboringLand(landmark.Above, above, 0))
                {
                    LogUtil.error($"加载异常:{above}");
                    return false;
                }
                string right = fields[5];
                if (!string.IsNullOrEmpty(right))
                {
                    landmark.Right = new LandmarkNeighboor();
                }
                if (!setNeighboringLand(landmark.Right, right, 90))
                {
                    LogUtil.error(($"加载异常:{right}"));
                    return false;
                }
                string below = fields[6];
                if (!string.IsNullOrEmpty(below))
                {
                    landmark.Below = new LandmarkNeighboor();
                }
                if (!setNeighboringLand(landmark.Below, below, 180))
                {
                    LogUtil.error($"加载异常:{below}");
                    return false;
                }
                string left = fields[7];
                if (!string.IsNullOrEmpty(left))
                {
                    landmark.Left = new LandmarkNeighboor();
                }
                if (!setNeighboringLand(landmark.Left, left, 270))
                {
                    LogUtil.error($"加载异常:{left}");
                    return false;
                }
                landmark.X = int.Parse(fields[8]);
                landmark.Y = int.Parse(fields[9]);
                //landmark.Reserved = fields[8];
                _tempPointCodes.Add(landmark);
            }
            _pointCodes = _tempPointCodes.ToList();
            return true;
        }

        /// <summary>
        /// 获取地标码信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static PointCode GetPointCode(uint id)
        {
            return _pointCodes.Find(s => s.Id == id);
        }
        public static List<PointCode> GetPointCodes()
        {
            return _pointCodes;
        }
        static short disBtwLandmark = ConfigHelper.Config.Get<short>(Mushiny.Common.Mushiny_ + "DefaultDisBtwLandmark", 1000);

        static bool setNeighboringLand(LandmarkNeighboor landmark, string line, short angle = 0)
        {
            if (string.IsNullOrEmpty(line))
            {
                return true;
            }
            string[] tmp1 = line.Split('#');
            landmark.Andle = angle;
            landmark.Distance = disBtwLandmark;
            if (tmp1.Length == 3)
            {
                var id = uint.Parse(tmp1[0]);
                var dis = short.Parse(tmp1[1]);
                var angl = short.Parse(tmp1[2]);
                landmark.Id = id;
                landmark.Distance = dis;
                landmark.Andle = angl;
                return true;
            }
            else if (tmp1.Length == 2)
            {
                var id = uint.Parse(tmp1[0]);
                var dis = short.Parse(tmp1[1]);
                landmark.Distance = dis;
                landmark.Id = id;
                return true;
            }
            else if (tmp1.Length == 1)
            {
                var id = uint.Parse(tmp1[0]);
                landmark.Id = id;
                return true;
            }
            return false;
        }
    }
    /// <summary>
    /// 地面点位编码
    /// </summary>
    public class PointCode
    {
        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 编号
        /// </summary>
        public uint Id { get; set; }
        /// <summary>
        /// 类型
        /// </summary>
        public LandmarkType Type { get; set; }
        /// <summary>
        /// 是否可转弯
        /// </summary>
        public bool CanTurning { get; set; }
        /// <summary>
        /// 上方路标
        /// </summary>
        public LandmarkNeighboor Above { get; set; }
        /// <summary>
        /// 右侧路标
        /// </summary>
        public LandmarkNeighboor Right { get; set; }
        /// <summary>
        /// 下方路标
        /// </summary>
        public LandmarkNeighboor Below { get; set; }
        /// <summary>
        /// 左侧路标
        /// </summary>
        public LandmarkNeighboor Left { get; set; }


        public short GetDis(uint id)
        {
            if (Above != null && Above.Id == id)
            {
                return Above.Distance;
            }
            else if (Right != null && Right.Id == id)
            {
                return Right.Distance;
            }
            else if (Below != null && Below.Id == id)
            {
                return Below.Distance;
            }
            else if (Left != null && Left.Id == id) { return Left.Distance; }
            else
            {
                return 0;
            }
        }

        public short GetAngle(uint id)
        {
            if (Above != null && Above.Id == id)
            {
                return Above.Andle;
            }
            else if (Right != null && Right.Id == id)
            {
                return Right.Andle;
            }
            else if (Below != null && Below.Id == id)
            {
                return Below.Andle;
            }
            else if (Left != null && Left.Id == id) { return Left.Andle; }
            else
            {
                return 0;
            }
        }
        public int X { get; set; } = -1;
        public int Y { get; set; } = -1;
        /// <summary>
        /// 预留
        /// </summary>
        public string Reserved { get; set; }
    }
    /// <summary>
    /// 邻接地标
    /// </summary>
    public class LandmarkNeighboor
    {
        /// <summary>
        /// 编号
        /// </summary>
        public uint Id { get; set; }
        /// <summary>
        /// 距离
        /// </summary>
        public short Distance { get; set; }
        /// <summary>
        /// 角度
        /// </summary>
        public short Andle { get; set; }
    }
    public enum LandmarkType
    {
        /// <summary>
        /// 普通点
        /// </summary>
        None,
        /// <summary>
        /// 转弯点
        /// </summary>
        Turning,
        /// <summary>
        /// 货架点/巷道点
        /// </summary>
        Shelves,
        /// <summary>
        /// 设备对接点-默认车头0
        /// </summary>
        Docking_0,
        /// <summary>
        /// 设备对接点-默认车头90
        /// </summary>
        Docking_90,
        /// <summary>
        /// 设备对接点-默认车头180
        /// </summary>
        Docking_180,
        /// <summary>
        /// 设备对接点-默认车头270
        /// </summary>
        Docking_270,

    }
}