SteelManage.cs 8.6 KB
using System;
using Model;
using AGVControl_Steel;
using System.Collections.Generic;

namespace BLL
{
    public class SteelManage
    {
        private List<SteelWork> oldSteelWork;
        private List<SteelWork> newSteelWork;

        public SteelManage()
        {
            oldSteelWork = new List<SteelWork>();
            newSteelWork = new List<SteelWork>();
        }

        //public int OldSteelWorkCount
        //{
        //    get
        //    {
        //        return oldSteelWork.Count;
        //    }
        //}

        //public int NewSteelWorkCount
        //{
        //    get
        //    {
        //        return newSteelWork.Count;
        //    }
        //}



        public void OldSteelWorkAdd(string place)
        {
            int index = oldSteelWork.FindIndex(sw => sw.Place == place);
            if (index == -1)
            {
                SteelWork sw = new SteelWork() { Place = place };
                oldSteelWork.Add(sw);
                string[] content = new string[oldSteelWork.Count];
                for (int i = 0; i < content.Length; i++)
                    content[i] = oldSteelWork[i].Place + "," + oldSteelWork[i].DateTime.ToString();
                if (Common.txtOldSteel != null) Common.txtOldSteel.Text = string.Join("\r\n", content);
                System.IO.File.WriteAllLines(Common.PATH_OLD_STEEL_WORK, content, System.Text.Encoding.UTF8);
            }
            Common.log.Debug("添加(" + place + ")旧钢板任务,保存到" + Common.PATH_OLD_STEEL_WORK);
        }

        public void OldSteelWorkDel(string place)
        {
            int index = oldSteelWork.FindIndex(sw => sw.Place == place);
            if (index > -1)
            {
                oldSteelWork.RemoveAt(index);
                string[] content = new string[oldSteelWork.Count];
                for (int i = 0; i < content.Length; i++)
                    content[i] = oldSteelWork[i].Place + "," + oldSteelWork[i].DateTime.ToString();
                if (Common.txtOldSteel != null) Common.txtOldSteel.Text = string.Join("\r\n", content);
                System.IO.File.WriteAllLines(Common.PATH_OLD_STEEL_WORK, content, System.Text.Encoding.UTF8);
            }
            Common.log.Debug("删除(" + place + ")旧钢板任务,保存到" + Common.PATH_OLD_STEEL_WORK);
        }

        public void OldSteelWorkLoad()
        {
            oldSteelWork = new List<SteelWork>();
            if (!System.IO.File.Exists(Common.PATH_OLD_STEEL_WORK)) return;
            string[] lines = System.IO.File.ReadAllLines(Common.PATH_OLD_STEEL_WORK, System.Text.Encoding.UTF8);
            if (Common.txtOldSteel != null) Common.txtOldSteel.Text = string.Join("\r\n", lines);
            for (int i = 0; i < lines.Length; i++)
            {
                string[] arr = lines[i].Split(',');
                SteelWork sw = new SteelWork() { Place = arr[0], DateTime = Convert.ToDateTime(arr[1]) };
                oldSteelWork.Add(sw);
            }
            Common.log.Debug("加载旧钢板任务,来自" + Common.PATH_OLD_STEEL_WORK);
        }

        public bool FindOldSteelWork(AgvInfo info, out string place)
        {
            int index = -1;
            string name = "";
            bool find = false;

            string s = info.Workshop.Substring(1, 1);
            for (int i = 0; i < Common.PLACE_NAME.Length; i++)
            {
                name = Common.PLACE_NAME[i];
                if (name.StartsWith(s))
                {
                    index = oldSteelWork.FindIndex(s => s.Place == name);
                    if (index > -1)
                    {
                        find = true;
                        break;
                    }
                }
            }

            place = name;
            return find;
        }

        public void NewSteelWorkAdd(string from, string place)
        {
            int index = newSteelWork.FindIndex(sw => sw.From == from && sw.Place == place);
            if (index == -1)
            {
                SteelWork sw = new SteelWork() { From = from, Place = place };
                newSteelWork.Add(sw);
                string[] content = new string[newSteelWork.Count];
                for (int i = 0; i < content.Length; i++)
                    content[i] = newSteelWork[i].From + "," + newSteelWork[i].Place + "," + newSteelWork[i].DateTime.ToString();
                if (Common.txtNewSteel != null) Common.txtNewSteel.Text = string.Join("\r\n", content);
                System.IO.File.WriteAllLines(Common.PATH_NEW_STEEL_WORK, content, System.Text.Encoding.UTF8);
            }
            Common.log.Debug("添加(" + from + "," + place + ")新钢板任务,保存到" + Common.PATH_NEW_STEEL_WORK);

        }

        public void NewSteelWorkDel(string from, string place)
        {
            int index = newSteelWork.FindIndex(sw => sw.From == from && sw.Place == place);
            if (index > -1)
            {
                newSteelWork.RemoveAt(index);
                string[] content = new string[newSteelWork.Count];
                for (int i = 0; i < content.Length; i++)
                    content[i] = newSteelWork[i].From + "," + newSteelWork[i].Place + "," + newSteelWork[i].DateTime.ToString();
                if (Common.txtNewSteel != null) Common.txtNewSteel.Text = string.Join("\r\n", content);
                System.IO.File.WriteAllLines(Common.PATH_NEW_STEEL_WORK, content, System.Text.Encoding.UTF8);
            }
            Common.log.Debug("删除(" + from + "," + place + ")新钢板任务,保存到" + Common.PATH_NEW_STEEL_WORK);
        }

        public void NewSteelWorkLoad()
        {
            newSteelWork = new List<SteelWork>();
            if (!System.IO.File.Exists(Common.PATH_NEW_STEEL_WORK)) return;
            string[] lines = System.IO.File.ReadAllLines(Common.PATH_NEW_STEEL_WORK, System.Text.Encoding.UTF8);
            if (Common.txtNewSteel != null) Common.txtNewSteel.Text = string.Join("\r\n", lines);
            for (int i = 0; i < lines.Length; i++)
            {
                string[] arr = lines[i].Split(',');
                SteelWork sw = new SteelWork() { From = arr[0], Place = arr[1], DateTime = Convert.ToDateTime(arr[2]) };
                newSteelWork.Add(sw);
            }
            Common.log.Debug("加载新钢板任务,来自" + Common.PATH_NEW_STEEL_WORK);
        }

        public bool FindNewSteelWork(AgvInfo info, out string place)
        {
            int index = -1;
            string name = "";
            bool find = false;

            for (int i = 0; i < Common.PLACE_NAME.Length; i++)
            {
                name = Common.PLACE_NAME[i];
                index = newSteelWork.FindIndex(s => s.From == info.SteelFrom && s.Place == name);
                if (index > -1)
                {
                    find = true;
                    break;
                }
            }

            place = name;
            return find;
        }

        public bool FindNewSteelWork(AgvInfo info)
        {
            int index = newSteelWork.FindIndex(s => s.From == info.SteelFrom);
            if (index > -1)
                return true;
            else
                return false;
        }





        public Job GetSteelJob(AgvInfo info)
        {

            //TEST
            string s = "";
            for (int i = 0; i < oldSteelWork.Count; i++)
                s += oldSteelWork[i].Place + ";";
            Common.log.Debug("TEST oldSteelWork=" + s);
            s = "";
            for (int i = 0; i < newSteelWork.Count; i++)
                s += newSteelWork[i].From + "," + newSteelWork[i].Place + "; ";
            Common.log.Debug("TEST newSteelWork=" + s);






            //回收旧钢板
            string name = info.Workshop.Substring(1, 1);
            int index = oldSteelWork.FindIndex(s => s.Place.StartsWith(name));
            if (index > -1)
            {
                Common.log.Info(info.Workshop + "旧钢板回收");
                return new TakeOldJob();
            }

            //清洗点呼叫
            index = newSteelWork.FindIndex(s => s.From.StartsWith(info.Workshop.ToLower()) && s.From == s.Place);
            if (index > -1)
            {
                info.SteelFrom = newSteelWork[index].From;
                Common.log.Info(info.Workshop + "清洗点呼叫");
                return new WashPointJob();
            }

            //仓库呼叫
            index = newSteelWork.FindIndex(s => s.From == "storage" && s.From == s.Place);
            if (index > -1)
            {
                info.SteelFrom = newSteelWork[index].From;
                Common.log.Info("4D仓库呼叫");
                return new StorageJob();
            }

            return null;
        }

    }
}