| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335 |
- using SKMC.Api.Common.Logger;
- using SKMC.Api.Motion.Model;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Threading;
- namespace SKMC.Api.Process.Model
- {
- /// <summary>
- /// 流程步骤
- /// </summary>
- public class ProcessStep
- {
- protected static readonly ILogger log = LogFactory.Get();
- /// <summary>
- /// 活动Id
- /// </summary>
- public int Id { get; set; }
- /// <summary>
- /// 活动名称
- /// </summary>
- public string Name { get; set; }
- /// <summary>
- /// 是否活动中
- /// </summary>
- public bool InAction { get; set; }
- /// <summary>
- /// Di触发条件集合
- /// </summary>
- public List<DioCnd> TriggerDiCnds { get; set; } = new List<DioCnd>();
- /// <summary>
- /// 电机触发条件集合
- /// </summary>
- public List<AxisCnd> TriggerAxisCnds { get; set; } = new List<AxisCnd>();
- /// <summary>
- ///
- /// </summary>
- public List<DioCnd> AlarmDiCnds { get; set; } = new List<DioCnd>();
- /// <summary>
- ///
- /// </summary>
- public List<AxisCnd> AlarmAxisCnds { get; set; } = new List<AxisCnd>();
- /// <summary>
- /// 流程站点
- /// </summary>
- public ProcessStation ProcessStation { get; set; }
- // 跳过条件判断(一次)
- public bool SkipCndOnce { get; set; }
- /// <summary>
- /// 是否初始步骤
- /// </summary>
- public bool First { get; set; }
- /// <summary>
- /// 是否自动模式
- /// 自动模式下完成Actions后会自动提交下一个流程步骤(按注册顺序)
- /// 默认为自动模式
- /// 可取消自动模式, 在Actions最后调用ProcessStation的PostManual提交指定的流程步骤
- /// </summary>
- public bool AutoMode { get; set; } = true;
- /// <summary>
- /// 满足该条件函数将执行Action动作
- /// </summary>
- public Func<bool> ActionCnd { get; set; }
- /// <summary>
- /// 项目实现的动作链
- /// 通过ActionCnd判断后回调执行
- /// </summary>
- public Action Actions { get; set; }
- /// <summary>
- /// 未通过ActionCnd判断的超时时间, 单位ms, 默认-1表示不启用
- /// </summary>
- [Obsolete("此属性已暂时废弃")]
- public int Timeout { get; set; } = -1;
- /// <summary>
- /// 超过Timeout后回调执行
- /// </summary>
- [Obsolete("此属性已暂时废弃")]
- public Action Defaults { get; set; }
- /// <summary>
- /// 与ActionCnd同时判断, 如果满足AlarmCnd将执行Alarms
- /// </summary>
- public Func<bool> AlarmCnd { get; set; }
- /// <summary>
- /// 满足AlarmCnd后执行的动作
- /// </summary>
- public Action Alarms { get; set; }
- public string StepCode { get; set; }
- /// <summary>
- /// 测试模式下绕过ActionCnd, 通过该延时触发Actions
- /// </summary>
- public int TestDelay { get; set; }
- /// <summary>
- /// 是否可用
- /// </summary>
- public bool IsEnabled { get; set; } = true;
- /// <summary>
- /// 是否挂起
- /// </summary>
- public bool IsSuspend { get; set; }
- /// <summary>
- /// 是否停止
- /// </summary>
- public bool IsStoped { get; set; }
- /// <summary>
- /// 顺序号
- /// </summary>
- public int Seq { get; set; }
- /// <summary>
- /// 开始时间戳
- /// </summary>
- public long StartTicks { get; set; }
- /// <summary>
- /// 判断次数 (ActionCnd未通过后+1)
- /// </summary>
- public int CheckCount { get; set; }
- public ProcessStep()
- {
- }
- public ProcessStep(ProcessStation processStation) : this()
- {
- ProcessStation = processStation;
- }
- /// <summary>
- /// 获取流程站点的状态
- /// </summary>
- /// <param name="matType">状态类型, 可以是物料也可以是站点</param>
- /// <param name="stationId">流程站点的Id, 如果是本站则为-1</param>
- /// <param name="fromStorage">是否从持久化获取, 如果为true并且从持久化获取成功, 将覆盖掉内存中已有的数据</param>
- /// <returns></returns>
- public int GetStationStatus(int matType, int stationId = -1, bool fromStorage = false) =>
- ProcessStation.GetStationStatus(matType, stationId, fromStorage);
- /// <summary>
- /// 判断流程站点的状态是否在给定的状态值内
- /// 只要满足其中一个状态值返回true, 全部不满足返回false
- /// </summary>
- /// <param name="matType">状态类型, 可以是物料也可以是站点</param>
- /// <param name="stationId">流程站点的Id, 如果是本站则为-1</param>
- /// <param name="statusVals">需要比较的状态值集合</param>
- /// <returns></returns>
- public bool CheckStationStatus(int matType, int stationId, int[] statusVals) =>
- ProcessStation.CheckStationStatus(matType, stationId, statusVals);
- /// <summary>
- /// 设置流程站点的状态
- /// </summary>
- /// <param name="matType">状态类型, 可以是物料也可以是站点</param>
- /// <param name="status">状态值</param>
- /// <param name="stationId">站点Id, -1表示本站</param>
- /// <param name="toStorage">是否同步持久化保存, 如果为true并且持久化中已有相同matType将会覆盖</param>
- public void SetStationStatus(int matType, int status, int stationId = -1, bool toStorage = false) =>
- ProcessStation.SetStationStatus(matType, status, stationId, toStorage);
- /// <summary>
- /// 删除流程站点的状态
- /// </summary>
- /// <param name="matType"></param>
- /// <param name="stationId"></param>
- public void RemoveStationStatus(int matType, int stationId = -1) =>
- ProcessStation.RemoveStationStatus(matType, stationId);
- /// <summary>
- /// 清除流程站点的所有状态
- /// </summary>
- /// <param name="stationId"></param>
- public void ClearStationStatus(int stationId = -1) => ProcessStation.ClearStationStatus(stationId);
- /// <summary>
- /// 在ProcessStationManager中更新当前运行的Step
- /// </summary>
- public void UpdateRunningStep()
- {
- ProcessStation.ProcessStationManager.runningSteps[ProcessStation.Id] = this;
- }
- /// <summary>
- /// 等待条件满足
- /// </summary>
- /// <param name="cnd">满足的条件</param>
- /// <param name="onTimeout">超时后动作</param>
- /// <param name="timeout">超时时间, 默认60秒</param>
- /// <param name="period">条件判断间隔</param>
- public void WaitingFor(Func<bool> cnd, Action onTimeout = null, int timeout = 60000, int period = 100)
- {
- int loops = timeout / period + 1;
- for (int i = 0; i < loops; i++)
- {
- if (cnd.Invoke()) return;
- Thread.Sleep(period);
- }
- if (onTimeout != null) onTimeout.Invoke();
- }
- /// <summary>
- /// 回复Step属性状态
- /// </summary>
- public void Reset()
- {
- IsStoped = false;
- IsSuspend = false;
- InAction = false;
- }
- /// <summary>
- /// 每次执行动作前的准备
- /// </summary>
- public Action OnPrepare { get; set; }
- /// <summary>
- /// 回调方法
- /// </summary>
- public void DoActions()
- {
- double waitTime = 0;
- if (StartTicks > 0)
- {
- waitTime = (DateTime.Now.Ticks - StartTicks) / 10000d;
- }
- log.Debug($"任务[{ProcessStation.Id}-{Id}:{Name}]动作开始... 等待用时: {waitTime}");
- Stopwatch stopwatch = Stopwatch.StartNew();
- // 回复Di初始值
- foreach (var diCnd in TriggerDiCnds)
- {
- diCnd.Reset();
- }
- InAction = true;
- Actions?.Invoke();
- InAction = false;
- stopwatch.Stop();
- log.Debug($"任务[{ProcessStation.Id}-{Id}:{Name}]动作完成, 运行用时: {stopwatch.Elapsed.TotalMilliseconds}");
- }
- public void DoDefaults()
- {
- log.Debug($"任务[{ProcessStation.Id}-{Id}:{Name}]等待超时...");
- InAction = true;
- //Defaults?.Invoke();
- InAction = false;
- log.Debug($"任务[{ProcessStation.Id}-{Id}:{Name}]超时处理完成");
- }
- public void DoAlarms()
- {
- log.Debug($"任务[{ProcessStation.Id}-{Id}:{Name}]告警开始...");
- foreach (var diCnd in TriggerDiCnds)
- {
- diCnd.Reset();
- }
- InAction = true;
- Alarms?.Invoke();
- InAction = false;
- log.Debug($"任务[{ProcessStation.Id}-{Id}:{Name}]告警处理完成");
- }
- public void DoStop()
- {
- IsStoped = true;
- if (OnStop != null) OnStop?.Invoke();
- InAction = false;
- }
- /// <summary>
- /// 停止
- /// </summary>
- public Action OnStop { get; set; }
- public void DoEmgStop()
- {
- IsStoped = true;
- OnEmgStop?.Invoke();
- }
- /// <summary>
- /// 急停
- /// </summary>
- public Action OnEmgStop { get; set; }
- public void DoPause()
- {
- IsSuspend = true;
- OnPause?.Invoke();
- }
- public Action OnPause { get; set; }
- public void DoResume()
- {
- IsSuspend = false;
- StartTicks = DateTime.Now.Ticks;
- OnResume?.Invoke();
- }
- public Action OnResume { get; set; }
- public override bool Equals(object obj)
- {
- return obj is ProcessStep step && Id == step.Id;
- }
- public override int GetHashCode()
- {
- return 2108858624 + Id.GetHashCode();
- }
- }
- }
|