ElevatorWork.cs 6.5 KB
using System;
using System.Collections.Generic;
using System.Text;
using Model;

namespace BLL
{
    public class ElevatorWork
    {
        private Advantech.IO_Module module;

        public delegate void IOChanged(bool[] sta);
        public event IOChanged DI_Changed;
        public event IOChanged DO_Changed;
        public delegate void Connected(bool con);
        public event Connected Connect_Event;

        public ElevatorWork()
        {
            module = new Advantech.IO_Module(0, 8, 8, 8, "IO_Elevator") { IP = Common.ElevatorIOIP };
            module.Connect_Event += Module_Connect_Event;
            module.DO_Changed_Event += Module_DO_Changed_Event;
            module.DI_Changed_Event += Module_DI_Changed_Event;
        }

        public void Open()
        {
            module.Open();
        }

        public void Close()
        {
            module.Close();
        }

        public void WriteDO(int index)
        {
            int add = Common.elevatorInfos[index].Address;
            Advantech.IO_State sta = module.ReadDO(add);
            sta = module.ReverseStatus(sta);
            module.WriteDO(add, sta);
        }

        public IJob GetJob()
        {
            bool sta = GetRecycle();
            if (sta)
            {
                Common.log.Info("电梯送空架子呼叫");
                return new RecycleJob();
            }
            else
            {
                Common.log.Debug("没有找到电梯送空架子呼叫");
                return null;
            }
        }

        public bool GetRecycle()
        {
            int idx1 = Common.elevatorInfos.FindIndex(s => s.Key == Common.ELEVATOR_EMPTY_SHELF);
            if (idx1 == -1) return false;
            bool sta1 = Common.elevatorInfos[idx1].State;

            int idx2 = Common.elevatorInfos.FindIndex(s => s.Key == Common.ELEVATOR_OPEN_DOOR);
            if (idx2 == -1) return false;
            bool sta2 = Common.elevatorInfos[idx2].State;

            return sta1 && sta2;
        }

        public void UseAsk()
        {
            int idx = Common.elevatorInfos.FindIndex(s => s.Key == Common.ELEVATOR_USE_ASK);
            if (idx == -1) return;
            module.WriteDO(Common.elevatorInfos[idx].Address, Advantech.IO_State.On);
        }

        public bool UseAnswer()
        {
            int idx = Common.elevatorInfos.FindIndex(s => s.Key == Common.ELEVATOR_USE_ANSWER);
            if (idx == -1) return false;
            return Common.elevatorInfos[idx].State;
        }

        public void CallElevator(bool sta)
        {
            int idx = Common.elevatorInfos.FindIndex(s => s.Key == Common.ELEVATOR_CALL);
            if (idx == -1) return;
            module.WriteDO(Common.elevatorInfos[idx].Address, sta ? Advantech.IO_State.On : Advantech.IO_State.Off);
        }

        public bool ArriveFloor()
        {
            int idx = Common.elevatorInfos.FindIndex(s => s.Key == Common.ELEVATOR_ARRIVE_FLOOR);
            if (idx == -1) return false;
            return Common.elevatorInfos[idx].State;
        }

        public bool OpenDoor()
        {
            int idx = Common.elevatorInfos.FindIndex(s => s.Key == Common.ELEVATOR_OPEN_DOOR);
            if (idx == -1) return false;
            return Common.elevatorInfos[idx].State;
        }

        public void FullShelf()
        {
            //送满料
            int idx = Common.elevatorInfos.FindIndex(s => s.Key == Common.ELEVATOR_FULL_SHELF);
            if (idx == -1) return;
            module.WriteDO(Common.elevatorInfos[idx].Address, Advantech.IO_State.On);

            //离开信号
            idx = Common.elevatorInfos.FindIndex(s => s.Key == Common.ELEVATOR_LEAVE);
            if (idx == -1) return;
            module.WriteDO(Common.elevatorInfos[idx].Address, Advantech.IO_State.On);
        }

        public void EmptyShelf()
        {
            //离开信号
            int idx = Common.elevatorInfos.FindIndex(s => s.Key == Common.ELEVATOR_LEAVE);
            if (idx == -1) return;
            module.WriteDO(Common.elevatorInfos[idx].Address, Advantech.IO_State.On);
        }

        public void EndTask()
        {
            int idx = Common.elevatorInfos.FindIndex(s => s.Key == Common.ELEVATOR_FULL_SHELF);
            if (idx == -1) return;
            module.WriteDO(Common.elevatorInfos[idx].Address, Advantech.IO_State.Off);

            idx = Common.elevatorInfos.FindIndex(s => s.Key == Common.ELEVATOR_LEAVE);
            if (idx == -1) return;
            module.WriteDO(Common.elevatorInfos[idx].Address, Advantech.IO_State.Off);

            idx = Common.elevatorInfos.FindIndex(s => s.Key == Common.ELEVATOR_USE_ASK);
            if (idx == -1) return;
            module.WriteDO(Common.elevatorInfos[idx].Address, Advantech.IO_State.Off);

        }

        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)
        {
            List<bool> work = new List<bool>();
            for (int i = 0; i < Common.elevatorInfos.Count; i++)
            {
                if (Common.elevatorInfos[i].Type == "DO")
                {
                    int idx = Common.elevatorInfos[i].Address;
                    if (idx < sta.Length)
                    {
                        Common.elevatorInfos[i].State = sta[idx] == Advantech.IO_State.On;
                    }
                    work.Add(Common.elevatorInfos[i].State);
                }
            }
            DO_Changed?.Invoke(work.ToArray());
        }

        private void Module_DI_Changed_Event(Advantech.IO_Module box, Advantech.IO_State[] sta)
        {
            List<bool> work = new List<bool>();
            List<string> content = new List<string>();

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

    }
}