using OrpaonEMS.App.Services; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Timers; namespace OrpaonEMS.App.Com { /// /// 触发模型 /// public class TrigTimeModel { /// /// 周期定时器 /// private System.Timers.Timer CycleTimer { get; set; } /// /// 触发时间达到阈值 /// public event EventHandler TrigTimeOutHandler; public TrigTimeModel(int trigThValue, int trigThSecTime) { TrigThValue = trigThValue; TrigThSecTime = trigThSecTime; //10秒触发一次 CycleTimer = new System.Timers.Timer(1000); CycleTimer.Elapsed += CycleAction; CycleTimer.AutoReset = true; CycleTimer.Enabled = true; } /// /// 周期调用这个方法 /// /// /// /// private void CycleAction(object? sender, ElapsedEventArgs e) { try { //如果Execute执行的是一个很耗时的方法,会导致方法未执行完毕,定时器又启动了一个线程来执行Execute方法 //CycleTimer.Stop(); //先关闭定时器 if (TrigState)//触发击中时统计时间 { TrigRtTimeSec = (DateTime.Now - TrigStartTime).TotalSeconds; if (TrigRtTimeSec >= TrigThSecTime) { CycleTimer.Stop(); //先关闭定时器,不再触发后续的事件 TrigTimeOutHandler(this, null); } } else//触发消失 { TrigRtTimeSec = 0; } //CycleTimer.Start(); //执行完毕后再开启器 } catch (Exception ex) { CycleTimer.Start(); //执行完毕后再开启器 } } /// /// 更新值 /// 大于一个值 /// /// public bool UpdateByCompareOver(double Value) { if (Value >= TrigThValue) { TrigState = true; } else { TrigState = false; } return TrigState; } /// /// 更新值 /// 小于一个值 /// /// public bool UpdateByCompareLess(double Value) { if (Value <= TrigThValue) { TrigState = true; } else { TrigState = false; } return TrigState; } /// /// 更新状态等使用 /// 为False时代表关注点被触发 /// /// /// public bool UpdateByBool(bool Value) { if (Value==false)//关注False。为False时代表关注点被触发 { TrigState = true; } else { TrigState = false; } return TrigState; } /// /// 触发成功开始的时间 /// public DateTime TrigStartTime { get; set; } private bool _TrigState = false; /// /// 触发时间 /// public bool TrigState { get { return _TrigState; } set { if (value != _TrigState)//状态改变 { if (value)//触发击中 { TrigStartTime = DateTime.Now; CycleTimer.Start(); } else//触发消失 { CycleTimer.Stop(); } _TrigState = value; } } } /// /// 达到触发值实时时间-秒 /// public double TrigRtTimeSec { get; set; } /// /// 达到触发值的值 /// public int TrigThValue { get; set; } /// /// 达到触发值的值时长阈值-秒 /// public int TrigThSecTime { get; set; } } }