PathWays.cs 5.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;
using static System.Windows.Forms.LinkLabel;

namespace CtuDeviceLib
{
    public class PathWays
    {
        /// <summary>
        /// 所有的路线
        /// </summary>
        static List<PathWay> _pathWays { get; set; }

        public static bool Load(string linefilePath)
        {
            if (!File.Exists(linefilePath))
            {
                LogUtil.error($"未找到路线配置路径:{linefilePath}");
                return false;
            }
            LogUtil.info($"加载路线配置:{linefilePath}");
            string[] lines = File.ReadAllLines(linefilePath);
            List<PathWay> _tempPathWays = new List<PathWay>();
            for (int i = 1; i < lines.Length; i++)
            {
                string[] fields = lines[i].Split(',');
                if (fields.Length < 5)
                    continue;
                var pathway = new PathWay();
                uint.TryParse(fields[0], out uint id);
                pathway.Id = id;
                pathway.Name = fields[1];
                string[] paths = fields[2].Split('#');
                pathway.Paths = new List<uint>();
                foreach (string s in paths)
                {
                    uint landId = uint.Parse(s);
                    pathway.Paths.Add(landId);
                }
                pathway.IsTwoWayLanes = bool.Parse(fields[3]);
                string isLimitOneCtu = fields[4];
                if (string.IsNullOrEmpty(isLimitOneCtu))
                {
                    pathway.IsLimitOneCtu = false;
                }
                else
                {
                    if (bool.TryParse(isLimitOneCtu, out bool isLimitCtu))
                    {
                        pathway.IsLimitOneCtu = isLimitCtu;
                    }
                }
                _tempPathWays.Add(pathway);
                LogUtil.debug($"【加载路线:Id={pathway.Id},Name={pathway.Name}," +
                    $"【{(pathway.IsTwoWayLanes ? string.Join("<->", pathway.Paths) : string.Join(">", pathway.Paths))}】");
            }
            _pathWays = _tempPathWays.ToList();
            NeighboringPathWays.Reload();
            return true;
        }

        public static List<PathWay> GetAllPathWays()
        {
            return _pathWays;
        }
        public static PathWay GetPathWay(uint pointCode)
        {
            return _pathWays.Find(s => s.Paths.Contains(pointCode));
        }
        public static PathWay GetPathWay(uint pointCode1, uint pointCode2)
        {
            return _pathWays.Find(s => s.Paths.Contains(pointCode1) && s.Paths.Contains(pointCode2));
        }
        /// <summary>
        /// 邻接点是否满足方向
        /// </summary>
        /// <param name="srcLand"></param>
        /// <param name="dstLand"></param>
        /// <returns></returns>
        public static bool IsTwoWay(uint srcLand, uint dstLand)
        {
            if (_pathWays == null || _pathWays.Count == 0) return true;
            var pathway = _pathWays.Find(s => s.Paths.Contains(srcLand) && s.Paths.Contains(dstLand));
            if (pathway != null)
            {
                if (pathway.IsTwoWayLanes)
                {
                    return true;
                }
                var srcIdx = pathway.Paths.IndexOf(srcLand);
                var dstIdx = pathway.Paths.IndexOf(dstLand);
                if (srcIdx < dstIdx)
                {
                    return true;
                }
            }
            return false;
        }
        public static bool IsInSamePath(uint land1, uint land2)
        {
            var pathway = _pathWays.Find(s => s.Paths.Contains(land1) && s.Paths.Contains(land2));
            if (pathway != null)
            {
                return true;
            }
            else
                return false;
        }
        /// <summary>
        /// 该点所在路径是否限制单辆车
        /// </summary>
        /// <param name="land1"></param>
        /// <returns></returns>
        public static bool IsPathwayLimitOneCtu(uint land1)
        {
            var pathways = _pathWays.FindAll(s => s.Paths.Contains(land1));
            if (pathways != null)
            {
                var find = pathways.Find(s => !s.IsLimitOneCtu);
                if (find == null)//未找到不限制单辆车的路线
                    return true;
            }
            return false;
        }

    }
    /// <summary>
    /// 路线
    /// </summary>
    public class PathWay
    {
        /// <summary>
        /// 路线编号
        /// </summary>
        public uint Id { get; set; }
        /// <summary>
        /// 路线名
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 线路
        /// 路标集合
        /// </summary>
        public List<uint> Paths { get; set; }
        /// <summary>
        /// 是否是双向路线
        /// </summary>

        public bool IsTwoWayLanes { get; set; }
        /// <summary>
        /// 是否限制单辆车在此路线
        /// 死胡同
        /// </summary>
        public bool IsLimitOneCtu { get; set; } = false;
    }
}