CtuStandbys.cs 7.5 KB
using DeviceLibrary;
using DeviceLibrary.CtuService;
using Mushiny;
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.PeerResolvers;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms.VisualStyles;

namespace CtuDeviceLib
{

    public class CtuStandbys
    {

        public static List<CtuStandby> ctuStandbies
        {
            get
            {
                var posInfos = PosInfos.GetAllPosInfos()?.FindAll(s => s.Name.StartsWith(Setting_Init.Standby));
                if (posInfos == null || posInfos.Count == 0)
                {
                    Msg.add($"未查询到待机点信息,请配置待机点", MsgLevel.warning);
                    return new List<CtuStandby>();
                }
                else
                {
                    var standbys = new List<CtuStandby>();
                    posInfos.ForEach(s => standbys.Add(new CtuStandby() { Name = s.Name }));
                    return standbys;
                }
            }
        }
        //待机点格式:
        //入料待机点 Standby-In-1
        //出料待机点 Standby-Out-1
        //临时待机点 Standby-Tmp-1

        /// <summary>
        /// 获取空闲待机点
        /// </summary>
        /// <param name="ctuName"></param>
        /// <returns></returns>
        public static List<CtuStandby> GetIdleStandbies(CTU ctu)
        {
            var filterStands = new List<CtuStandby>();
            if (CTUManager.InletGroup.IsInGroup(ctu.Name))
            {
                filterStands = ctuStandbies.FindAll(s =>
                {
                    var names = s.Name.Split('-');
                    if (names.Length > 2)
                    {
                        if (Setting_Init.In.Equals(names[1]))
                            return true;
                        return false;
                    }
                    else
                    {
                        return false;
                    }
                });
            }
            else
            {
                filterStands = ctuStandbies.FindAll(s =>
                {
                    var names = s.Name.Split('-');
                    if (names.Length > 2)
                    {
                        if (Setting_Init.Out.Equals(names[1]))
                            return true;
                        return false;
                    }
                    else
                    {
                        return false;
                    }
                });
            }
            var ctuIsInStand = filterStands.Find(s =>
            {
                var stand = PosInfos.GetPosInfoByName(s.Name);
                if (stand != null)
                {
                    if (stand.PointCode == ctu.CurLandMark)//有车在待机点位置
                    {
                        return true;
                    }

                }
                return false;
            });

            if(ctuIsInStand!=null)
            {
                return new List<CtuStandby>() { ctuIsInStand };
            }
            foreach (var item in CTUManager.CTUs)
            {
                if (!item.Enabled) continue;
                if (item.CtuTask != null && (item.CtuTask is StandbyTask))
                {
                    var hasOccupied = filterStands?.Find(s => s.Name.Equals(item.CtuTask.DstName));
                    if (hasOccupied != null)
                    {
                        filterStands.Remove(hasOccupied);
                    }
                }
                else
                {
                    filterStands = filterStands.FindAll(s =>
                    {
                        var stand = PosInfos.GetPosInfoByName(s.Name);
                        if (stand != null)
                        {
                            if (stand.PointCode == item.CurLandMark)//有车在待机点位置
                            {
                                return false;
                            }

                        }
                        return true;
                    });
                }
            }
            return filterStands;
        }


        public static CtuStandby GetNearestStandby(CTU ctu)
        {
            var filterStands = new List<CtuStandby>();
            if (CTUManager.InletGroup.IsInGroup(ctu.Name))
            {
                filterStands = ctuStandbies.FindAll(s =>
                {
                    var names = s.Name.Split('-');
                    if (names.Length > 2)
                    {
                        if (Setting_Init.In.Equals(names[1]))
                            return true;
                        return false;
                    }
                    else
                    {
                        return false;
                    }
                });
            }
            else
            {
                filterStands = ctuStandbies.FindAll(s =>
                {
                    var names = s.Name.Split('-');
                    if (names.Length > 2)
                    {
                        if (Setting_Init.Out.Equals(names[1]))
                            return true;
                        return false;
                    }
                    else
                    {
                        return false;
                    }
                });
            }
            var ctuIsInStand = filterStands.Find(s =>
            {
                var stand = PosInfos.GetPosInfoByName(s.Name);
                if (stand != null)
                {
                    if (stand.PointCode == ctu.CurLandMark)//有车在待机点位置
                    {
                        return true;
                    }

                }
                return false;
            });

            if (ctuIsInStand != null)
            {
                return ctuIsInStand;
            }
            foreach (var item in CTUManager.CTUs)
            {
                if (!item.Enabled) continue;
                if (item.CtuTask != null && (item.CtuTask is StandbyTask))
                {
                    var hasOccupied = filterStands?.Find(s => s.Name.Equals(item.CtuTask.DstName));
                    if (hasOccupied != null)
                    {
                        filterStands.Remove(hasOccupied);
                    }
                }
                else
                {
                    filterStands = filterStands.FindAll(s =>
                    {
                        var stand = PosInfos.GetPosInfoByName(s.Name);
                        if (stand != null)
                        {
                            if (stand.PointCode == item.CurLandMark)//有车在待机点位置
                            {
                                return false;
                            }

                        }
                        return true;
                    });
                }
            }

            if (filterStands?.Count > 0)
                return filterStands[0];
            else
            {
                return new CtuStandby();
            }
        }
        /// <summary>
        /// 获取临时待机点
        /// </summary>
        /// <returns></returns>
        public static CtuStandby GetTmpStandby()
        {
            return new CtuStandby() { Name = "" };
        }
    }
    /// <summary>
    /// CTU待机点
    /// </summary>
    public class CtuStandby
    {
        /// <summary>
        /// 待机点名称
        /// </summary>
        public string Name { get; set; } = "";
    }
}