LineManager.cs 4.1 KB
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;

namespace DeviceLibrary
{
    public class LineManager
    {
        Timer lineTimer = null;
        Dictionary<string, DateTime> linrunlist = new Dictionary<string, DateTime>();

        string iotype;
        DeviceConfig deviceConfig;
        string Name;
        public LineManager(string IOTYPE, DeviceConfig config, string name) {
            iotype = IOTYPE;
            deviceConfig = config;
            Name = name;
        }
        ~LineManager()
        {
            if (lineTimer != null)
            { lineTimer.Stop();
            }
        }
        void LineInit()
        {
            if (lineTimer == null)
            {
                lineTimer = new Timer(300);
                lineTimer.Elapsed += LineTimer_Elapsed;
                lineTimer.Start();
                GC.KeepAlive(lineTimer);
            }
        }

        private void LineTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            lock (linrunlist)
            {
                if (canStopLine(out _) && IOManager.IOValue(iotype, deviceConfig).Equals(IO_VALUE.HIGH))
                {
                    IOManager.IOMove(iotype, IO_VALUE.LOW, deviceConfig);
                    LogUtil.info(Name + $" 线体管理器 停止线体.");
                }
            }
        }

        /// <summary>
        /// 控制线体运转
        /// </summary>
        /// <param name="id">需求方标识</param>
        /// <param name="seconds">秒数</param>
        public void LineRun(string id="n") {
            LineRun(id, 888, "");
        }
        /// <summary>
        /// 控制线体运转
        /// </summary>
        /// <param name="id">需求方标识</param>
        /// <param name="seconds">秒数</param>
        public void LineRun(string id, int seconds, string parentname = "")
        {
            LineInit();

            IOManager.IOMove(iotype, IO_VALUE.HIGH, deviceConfig);
            lock (linrunlist)
            {
                IOManager.IOMove(iotype, IO_VALUE.HIGH, deviceConfig);
                if (!string.IsNullOrEmpty(id) && seconds > 0)
                {
                    if (linrunlist.ContainsKey(id))
                        linrunlist[id] = DateTime.Now.AddSeconds(seconds);
                    else
                    {
                        linrunlist.Add(id, DateTime.Now.AddSeconds(seconds));
                    }
                    LogUtil.info(Name + $" 线体管理器 {id},{parentname} 请求链条运行 {seconds}秒.");
                }
            }
        }
        public void LineStop(string id = "n", string parentname = "")
        {
            lock (linrunlist)
            {
                if (!string.IsNullOrEmpty(id))
                {
                    if (linrunlist.ContainsKey(id))
                        linrunlist.Remove(id);
                    LogUtil.info(Name + $" 线体管理器 {id},{parentname} 请求立刻停止线体.");
                }
            }
            if (!canStopLine(out string msg))
                LogUtil.info(Name + $" {Name}");
            //    IOMove(IO_Type.Line_Run, IO_VALUE.LOW);
        }
        bool canStopLine(out string msg)
        {
            msg = "";
            bool canStop = true;
            lock (linrunlist)
            {
                foreach (var x in linrunlist.ToList())
                {
                    if (x.Value > DateTime.Now)
                    {
                        canStop = false;
                        msg = Name + $" 线体管理器 {x.Key} 不允许停止线体 需求停止时间 {x.Value.ToString()}.";
                        //LogUtil.info(Name + $" {x.Key} 不允许停止线体 需求停止时间 {x.Value.ToString()}.");
                    }
                    else
                    {
                        LogUtil.info(Name + $" 线体管理器 {x.Key} 请求时间已过期,删除.");
                        linrunlist.Remove(x.Key);

                    }
                }
            }
            return canStop;
        }
    }
}