Control.cs 4.2 KB
using System;
using System.Collections.Generic;
using System.Threading;
using AGVControl_Steel;
using Model;

namespace BLL
{
    /// <summary>
    /// 控制类
    /// </summary>
    public class Control
    {
        private Timer timerCall;

        public delegate void AgvChangedEvent(int agvIndex);
        public event AgvChangedEvent AgvChanged;
        public event AgvChangedEvent AgvOnline;



        /// <summary>
        /// 控制类
        /// </summary>
        public Control()
        {
            ThreadPool.SetMaxThreads(5, 5);  //线程池最大数量
        }

        /// <summary>
        /// 打开服务
        /// </summary>
        public void Start()
        {
            timerCall = new Timer(CallProcess, null, 0, 3000);
        }

        /// <summary>
        /// 关闭服务
        /// </summary>
        public void Stop()
        {
            timerCall.Dispose();
        }



        private void CallProcess(object obj)
        {
            for (int i = 0; i < Common.agvInfos.Count; i++)
            {
                AgvInfo info = Common.agvInfos[i];
                if (info.IsCall) continue;

                try
                {
                    info.IsCall = true;
                    CheckAgvOnline(ref info);
                    if (!info.IsOnline) continue;   //脱机
                    GetAgvState(ref info);
                    if (!info.IsAuto) continue;     //手动

                    if (info.CurrentJob == null)
                        info.CurrentJob = new StandbyJob();
                    else
                        info.CurrentJob = info.CurrentJob.Execute(info);
                }
                catch (Exception ex)
                {
                    Common.log.Error("CallProcess " + info.FullName, ex);
                }
                finally
                {
                    info.IsCall = false;
                }
            }
        }

        /// <summary>
        /// 获取小车的状态
        /// </summary>
        /// <param name="info"></param>
        private void GetAgvState(ref AgvInfo info)
        {
            if (!info.IsOnline) return;
            bool rtn = Common.mir.Get_State(info.IP, info.Authorization, out int stateID, out string stateText, out int battery, out string missionText, out System.Drawing.PointF position);
            Common.log.Debug(string.Format("{1} GetAgvState[return={0}] stateID={2}, stateText={3}, battery={4}, missionText={5}, position={6}", rtn, info.FullName, stateID, stateText, battery, missionText, position));

            if (rtn)
            {
                info.StateID = stateID;
                info.StateText = stateText;
                info.Battery = battery;
                info.MissionText = missionText;
                info.Position = position;

                string ip = info.IP;
                int idx = Common.agvInfos.FindIndex(s => s.IP == ip);
                if (idx > -1) AgvChanged?.Invoke(idx);
            }
        }

        /// <summary>
        /// 检查小车是否在线
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        private bool CheckAgvOnline(ref AgvInfo info)
        {
            bool rtn = Common.mir.CheckIP(info.IP);
            if (rtn)
            {
                if (!info.IsOnline)
                {
                    info.IsOnline = true;
                    string ip = info.IP;
                    int idx = Common.agvInfos.FindIndex(s => s.IP == ip);
                    if (idx > -1) AgvOnline?.Invoke(idx);
                    rtn = Common.mir.Get_IO_Modules(info.IP, info.Authorization, out string[] guid);
                    if (rtn) info.IOGuid = guid[0];
                }
                Common.log.Debug(string.Format("{0}[{1}] 在线", info.Name, info.IP));
            }
            else
            {
                if (info.IsOnline)
                {
                    info.IsOnline = false;
                    string ip = info.IP;
                    int idx = Common.agvInfos.FindIndex(s => s.IP == ip);
                    if (idx > -1) AgvOnline?.Invoke(idx);
                }
                Common.log.Debug(string.Format("{0}[{1}] 脱机", info.Name, info.IP));
            }
            return rtn;
        }




    }
}