Files
YuPu-OrpaonEMS/OrpaonEMS.App/Com/TrigTimeModel.cs
2025-02-28 22:23:13 +08:00

184 lines
4.7 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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
{
/// <summary>
/// 触发模型
/// </summary>
public class TrigTimeModel
{
/// <summary>
/// 周期定时器
/// </summary>
private System.Timers.Timer CycleTimer { get; set; }
/// <summary>
/// 触发时间达到阈值
/// </summary>
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;
}
/// <summary>
/// 周期调用这个方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
/// <exception cref="NotImplementedException"></exception>
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(); //执行完毕后再开启器
}
}
/// <summary>
/// 更新值
/// 大于一个值
/// </summary>
/// <returns></returns>
public bool UpdateByCompareOver(double Value)
{
if (Value >= TrigThValue)
{
TrigState = true;
}
else
{
TrigState = false;
}
return TrigState;
}
/// <summary>
/// 更新值
/// 小于一个值
/// </summary>
/// <returns></returns>
public bool UpdateByCompareLess(double Value)
{
if (Value <= TrigThValue)
{
TrigState = true;
}
else
{
TrigState = false;
}
return TrigState;
}
/// <summary>
/// 更新状态等使用
/// 为False时代表关注点被触发
/// </summary>
/// <param name="Value"></param>
/// <returns></returns>
public bool UpdateByBool(bool Value)
{
if (Value==false)//关注False。为False时代表关注点被触发
{
TrigState = true;
}
else
{
TrigState = false;
}
return TrigState;
}
/// <summary>
/// 触发成功开始的时间
/// </summary>
public DateTime TrigStartTime { get; set; }
private bool _TrigState = false;
/// <summary>
/// 触发时间
/// </summary>
public bool TrigState
{
get { return _TrigState; }
set
{
if (value != _TrigState)//状态改变
{
if (value)//触发击中
{
TrigStartTime = DateTime.Now;
CycleTimer.Start();
}
else//触发消失
{
CycleTimer.Stop();
}
_TrigState = value;
}
}
}
/// <summary>
/// 达到触发值实时时间-秒
/// </summary>
public double TrigRtTimeSec { get; set; }
/// <summary>
/// 达到触发值的值
/// </summary>
public int TrigThValue { get; set; }
/// <summary>
/// 达到触发值的值时长阈值-秒
/// </summary>
public int TrigThSecTime { get; set; }
}
}