ManageWork.cs 7.7 KB
using System;
using System.Text;
using Model;

namespace BLL
{
    public static class ManageWork
    {
        public static LineWork line;
        public static ElevatorWork elevator;
        public static bool PauseFull;
        public static string frontLine;

        public static void Init()
        {
            frontLine = "";
            PauseFull = false;
            line = new LineWork();
            elevator = new ElevatorWork();
        }

        public static void Start()
        {
            line.Open();
            elevator.Open();
        }

        public static void Stop()
        {
            line.Close();
            elevator.Close();
        }

        /// <summary>
        /// 获取产线或电梯任务
        /// </summary>
        /// <returns></returns>
        public static IJob GetJob()
        {
            IJob job = elevator.GetJob();
            if (job == null)
                job = line.GetJob();
            return job;
        }



        /// <summary>
        /// 查找产线呼叫
        /// </summary>
        /// <param name="key"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static bool FindLineWork(out string key, out string name)
        {
            //按时间的先后顺序
            int index = -1;
            DateTime dt = DateTime.Now;
            for (int i = 0; i < Common.lineInfos.Count; i++)
            {
                if (Common.lineInfos[i].LineCall && Common.lineInfos[i].ShelfExist)
                {
                    if (dt > Common.lineInfos[i].FinalDate)
                    {
                        dt = Common.lineInfos[i].FinalDate;
                        index = i;
                    }
                }
            }

            //int index = Common.lineInfos.FindIndex(s => s.Call && s.Exist);
            if (index == -1)
            {
                key = "";
                name = "";
                return false;
            }
            else
            {
                key = Common.lineInfos[index].Key;
                name = Common.lineInfos[index].Name;
                return true;
            }
        }

        /// <summary>
        /// 查找电梯回收呼叫
        /// </summary>
        /// <returns></returns>
        public static bool FindElevatorWork()
        {
            return elevator.GetRecycle();
        }

        /// <summary>
        /// 查找不带货架的产线
        /// </summary>
        /// <param name="key"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static bool FindLineWithoutShelf(out string key, out string name)
        {
            int index;
            int startIndex = Common.lineInfos.FindIndex(s => s.Key == frontLine);
            if (startIndex == -1)  //没有关键字
            {
                index = Common.lineInfos.FindIndex(s => s.ShelfExist == false);
            }
            else if (startIndex == Common.lineInfos.Count - 1)  //最后一行
            {
                index = Common.lineInfos.FindIndex(s => s.ShelfExist == false);
            }
            else
            {
                index = Common.lineInfos.FindIndex(++startIndex, s => s.ShelfExist == false);
                if (index == -1)  //从头开始
                    index = Common.lineInfos.FindIndex(s => s.ShelfExist == false);
            }

            if (index == -1)
            {
                key = "";
                name = "";
                frontLine = "";
                return false;
            }
            else
            {
                key = Common.lineInfos[index].Key;
                name = Common.lineInfos[index].Name;
                frontLine = key;
                return true;
            }






            //int index = Common.lineInfos.FindIndex(s => s.Exist == false);
            //if (index == -1)
            //{
            //    key = "";
            //    name = "";
            //    return false;
            //}
            //else
            //{
            //    key = Common.lineInfos[index].Key;
            //    name = Common.lineInfos[index].Name;
            //    return true;
            //}
        }

        /// <summary>
        /// 小车从产线取走货架
        /// </summary>
        /// <param name="key"></param>
        public static void LineTakeAway(string key)
        {
            line.TakeAway(key);
        }

        /// <summary>
        /// 小车拉取货架送到产线
        /// </summary>
        /// <param name="key"></param>
        public static void LineSendTo(string key)
        {
            line.SendTo(key);
        }

        /// <summary>
        /// 发送电梯使用请求
        /// </summary>
        public static void SendElevatorUseAsk()
        {
            elevator.UseAsk();
        }

        /// <summary>
        /// 电梯使用应答
        /// </summary>
        /// <returns></returns>
        public static bool ElevatorUseAnswer()
        {
            return elevator.UseAnswer();
        }

        /// <summary>
        /// 电梯呼叫
        /// </summary>
        /// <param name="sta"></param>
        public static void ElevatorCall(bool sta)
        {
            elevator.CallElevator(sta);
        }

        /// <summary>
        /// 电梯已准备好(到达4楼并开门)
        /// </summary>
        /// <returns></returns>
        public static bool ElevatorReady()
        {
            bool rtn1 = elevator.ArriveFloor();
            bool rtn2 = elevator.OpenDoor();
            return rtn1 && rtn2;
        }

        /// <summary>
        /// 电梯送满架子
        /// </summary>
        public static void ElevatorFullShelf()
        {
            elevator.FullShelfLeave();
        }

        /// <summary>
        /// 电梯回收空架子
        /// </summary>
        public static void ElevatorEmptyShelf()
        {
            elevator.EmptyShelfLeave();
        }

        /// <summary>
        /// 电梯任务结束,可以离开
        /// </summary>
        public static void ElevatorEnd()
        {
            elevator.EndTask();
        }





        public static void FirstFloorAdd()
        {
            Common.FirstFloorCurr++;
            if (Common.FirstFloorCurr > Common.FirstFloorCount)
                Common.FirstFloorCurr = Common.FirstFloorCount;
            System.IO.File.WriteAllText(Common.PATH_FIRST_FLOOR_CURR, Common.FirstFloorCurr.ToString(), Encoding.UTF8);
        }

        public static void FirstFloorMinus()
        {
            Common.FirstFloorCurr--;
            if (Common.FirstFloorCurr < 0)
                Common.FirstFloorCurr = 0;
            System.IO.File.WriteAllText(Common.PATH_FIRST_FLOOR_CURR, Common.FirstFloorCurr.ToString(), Encoding.UTF8);
        }

        public static void FirstFloorSave()
        {
            if (Common.FirstFloorCurr < 0)
                Common.FirstFloorCurr = 0;
            else if (Common.FirstFloorCurr > Common.FirstFloorCount)
                Common.FirstFloorCurr = Common.FirstFloorCount;
            System.IO.File.WriteAllText(Common.PATH_FIRST_FLOOR_CURR, Common.FirstFloorCurr.ToString(), Encoding.UTF8);
        }



        /// <summary>
        /// 回收空货架错误处理
        /// </summary>
        public static void EmptyErrorDispose()
        {
            elevator.EmptyShelfLeave();
            System.Threading.Thread.Sleep(3000);
            elevator.EndTask();
            FirstFloorMinus();
        }

        /// <summary>
        /// 送满货架错误处理
        /// </summary>
        public static void FullErrorDispose()
        {
            elevator.FullShelfLeave();
            System.Threading.Thread.Sleep(3000);
            elevator.EndTask();
            FirstFloorAdd();
        }


    }
}