LineWork.cs 6.0 KB
using System;
using System.Collections.Generic;
using System.Text;
using Model;

namespace BLL
{
    public class LineWork
    {
        private Advantech.IO_Module[] module;
        
        public delegate void IOChanged();
        public event IOChanged DI_Changed;
        public delegate void Connected(bool con);
        public event Connected Connect_Event;

        public LineWork()
        {
            Load();
            module = new Advantech.IO_Module[Common.lineInfos.Count];
            for (int i = 0; i < module.Length; i++)
            {
                module[i] = new Advantech.IO_Module(0, 4, 4, 4, "IO_Line") { IP = Common.lineInfos[i].IP };
                module[i].DI_Changed_Event += Module_DI_Changed_Event;
            }
        }

        public void Open()
        {
            for (int i = 0; i < module.Length; i++)
                module[i].Open();
        }

        public void Close()
        {
            for (int i = 0; i < module.Length; i++)
                module[i].Close();
        }

        public IJob GetJob()
        {
            if (Common.FirstFloorCurr < Common.FirstFloorCount)
            {
                int index = Common.lineInfos.FindIndex(s => s.LineCall && s.ShelfExist);
                if (index > -1)
                {
                    Common.log.Info(Common.lineInfos[index].Name + "呼叫送满架");
                    return new FullShelfJob();
                }
                else
                {
                    Common.log.Debug("没有找到任何产线呼叫送满架");
                    return null;
                }
            }
            else
            {
                Common.log.Debug("一楼货架已满" + Common.FirstFloorCount + "个");
                return null;
            }
        }

        public void TakeAway(string key)
        {
            int index = Common.lineInfos.FindIndex(s => s.Key == key);
            if (index == -1) return;
            Common.lineInfos[index].LineCall = false;
            Common.lineInfos[index].ShelfExist = false;
            Common.lineInfos[index].FinalDate = DateTime.Now;
            Save();
            DI_Changed?.Invoke();
        }

        public void SendTo(string key)
        {
            int index = Common.lineInfos.FindIndex(s => s.Key == key);
            if (index == -1) return;
            Common.lineInfos[index].LineCall = false;
            Common.lineInfos[index].ShelfExist = true;
            Save();
            DI_Changed?.Invoke();
        }




        private void Load()
        {
            if (System.IO.File.Exists(Common.PATH_LINE_WORK))
            {
                string[] lines = System.IO.File.ReadAllLines(Common.PATH_LINE_WORK, Encoding.UTF8);
                for (int i = 0; i < lines.Length; i++)
                {
                    if (i == 0) continue;  //第一行是标题
                    string[] str = lines[i].Split(',');
                    if (str.Length != 4) continue;
                    int index = Common.lineInfos.FindIndex(s => s.Key == str[0]);
                    if (index == -1) continue;
                    Common.lineInfos[index].LineCall = Convert.ToBoolean(str[1]);
                    Common.lineInfos[index].ShelfExist = Convert.ToBoolean(str[2]);
                    Common.lineInfos[index].FinalDate = Convert.ToDateTime(str[3]);
                }
                Common.log.Info("加载缓存文件 " + Common.PATH_LINE_WORK);
            }
            else
            {
                Save();
            }
          
        }

        public void Save()
        {
            string[] content = new string[Common.lineInfos.Count + 1];
            content[0] = "产线名,呼叫,架子,时间";
            for (int i = 0; i < Common.lineInfos.Count; i++)
                content[i + 1] = string.Format("{0},{1},{2},{3:yyyy-MM-dd HH:mm:ss}", Common.lineInfos[i].Key, Common.lineInfos[i].LineCall, Common.lineInfos[i].ShelfExist, Common.lineInfos[i].FinalDate);
            System.IO.File.WriteAllLines(Common.PATH_LINE_WORK, content, Encoding.UTF8);
            Common.log.Info("保存缓存文件 " + Common.PATH_LINE_WORK);
        }

        private void Module_Connect_Event(Advantech.IO_Module box)
        {
            Connect_Event?.Invoke(box.IsConn);
        }

        private void Module_DO_Changed_Event(Advantech.IO_Module box, Advantech.IO_State[] sta)
        {
        }

        private void Module_DI_Changed_Event(Advantech.IO_Module box, Advantech.IO_State[] sta)
        {
            int lineIdx = Common.lineInfos.FindIndex(s => s.IP == box.IP);
            if (lineIdx == -1) return;
            int add = Common.lineInfos[lineIdx].Address;
            if (add >= sta.Length) return;

            bool change = false;
            if (sta[add] == Advantech.IO_State.On)
            {
                if (!Common.lineInfos[lineIdx].LineCall)  //已经呼叫了就不在更新时间了
                {
                    Common.lineInfos[lineIdx].LineCall = true;
                    Common.lineInfos[lineIdx].FinalDate = DateTime.Now;
                    change = true;
                }
            }
            if (change) Save();
            DI_Changed?.Invoke();



            //List<bool> work = new List<bool>();
            //List<string> content = new List<string>();

            //for (int i = 0; i < Common.lineInfos.Count; i++)
            //{
            //    if (Common.lineInfos[i].Type == "DI")
            //    {
            //        int idx = Common.lineInfos[i].Address;
            //        if (idx < sta.Length)
            //        {
            //            if (sta[idx] == Advantech.IO_State.On)
            //                Common.lineInfos[i].State = true;
            //        }
            //        work.Add(Common.lineInfos[i].State);
            //        content.Add(Common.lineInfos[i].ToText());
            //    }
            //}
            //System.IO.File.WriteAllLines(Common.PATH_LINE_WORK, content, Encoding.UTF8);
            //DI_Changed?.Invoke(work.ToArray());
        }


    }
}