JobStep.cs 2.8 KB
using Common;
using System;

namespace DeviceLib.Model.AGV
{
    public class JobStep<T> where T : Enum
    {
        private T step;
        private T preStep;
        public string Msg
        {
            get; set;
        }
        public string WarnMsg
        {
            get; set;
        }
        public DateTime StartTime { get; set; } = DateTime.Now;
        public DateTime EndTime { get; set; } = DateTime.Now;
        public string RunTime { get { return FormUtil.GetSpanStr(EndTime - StartTime); } }
        public JobStep(T initStep)
        {
            this.step = initStep;
            this.preStep = initStep;
        }
        /// <summary>
        /// 当前步骤是否是某个步骤
        /// </summary>
        /// <param name="step"></param>
        /// <returns></returns>
        public bool IsStep(T step)
        {
            return this.step.Equals(step);
        }

        /// <summary>
        /// 是否超时(秒)
        /// </summary>
        /// <param name="timeOutValue"></param>
        /// <returns></returns>
        public bool IsTimeOut(double timeOutSeconds, out double timeOutValue)
        {
            TimeSpan span = DateTime.Now - StartTime;
            timeOutValue = span.TotalSeconds;
            if (span.TotalSeconds > timeOutSeconds)
            {
                return true;
            }
            return false;
        }
        public bool IsTimeOut(double timeOutSeconds, out TimeSpan timeOutValue)
        {
            timeOutValue = DateTime.Now - StartTime;
            if (timeOutValue.TotalSeconds > timeOutSeconds)
            {
                return true;
            }
            return false;
        }
        public bool IsTimeOut(double timeOutSeconds)
        {
            TimeSpan span = DateTime.Now - StartTime;
            if (span.TotalSeconds > timeOutSeconds)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 当前步骤结束
        /// </summary>
        public void StepFinish()
        {
            EndTime = DateTime.Now;
        }
        /// <summary>
        /// 进入下一个步骤
        /// </summary>
        /// <param name="nextStep"></param>
        public void ToNextStep(T nextStep, string msg)
        {
            WarnMsg = "";
            Msg = msg;
            preStep = step;
            step = nextStep;
            StartTime = DateTime.Now;
        }

        public T CurStep
        {
            get { return step; }
        }
        public string CurStepStr
        {
            get { return step.ToString(); }
        }
        public T PreStep
        {
            get { return preStep; }
        }
        public string PreStepStr
        {
            get { return preStep.ToString(); }
        }
    }
}