using System; using System.Collections.Generic; using System.Text; using System.Threading; using System.Timers; namespace Mesnac.Basic { [Serializable] public class Variable { #region 定义委托 public delegate void RefreshControlValue(Variable sender); public event RefreshControlValue Refresh; #endregion #region value define private string mName; private string mAddr; private string mDescription; private object mValue; private object mOldValue; private string mDevice; private TypeCode mValuetype; private float mStep; private string mVarway; private string mDateStamp; private float mMinvalue; private float mMaxvalue; private uint mTime; private uint counter; private bool bUse; private string _sExpress1; private string _sExpress2; private string _sType; private bool _isRun = false; //是否运行 [NonSerialized] private System.Timers.Timer mTimer; #endregion public string sExpress1 { get { return _sExpress1; } set { _sExpress1 = value; } } public string sExpress2 { get { return _sExpress2; } set { _sExpress2 = value; } } public string sType { get { return _sType; } set { _sType = value; } } private string _sVbs; public string sVbs { get { return _sVbs; } set { _sVbs = value; } } public Variable() { Name = ""; Addr = ""; Description = ""; Device = ""; Varway = ""; Value = 0; OldValue = 0; Valuetype = TypeCode.Empty; nCounter = 0; MTime = 0; _sExpress1 = ""; _sExpress2 = ""; _sType = ""; mbUse = false; _sVbs = ""; } public bool mbUse { get { return bUse; } set { bUse = value; } } public void Stop() { try { this.mTimer.Stop(); this._isRun = false; this.mTimer.Elapsed -= new ElapsedEventHandler(mTimer_Elapsed); } catch { } } public void Start(uint ntime) { if (this._isRun) { this.Stop(); } this.mTimer = new System.Timers.Timer(); if (ntime < 10) ntime = 10; this.mTimer.Interval = ntime; this.mTimer.Elapsed += new ElapsedEventHandler(mTimer_Elapsed); this.mTimer.Start(); this._isRun = true; } //public event EventHandler OnVirDataChanged; void mTimer_Elapsed(object sender, ElapsedEventArgs e) { GenerateVarValue(); } public void GenerateVarValue() { //Monitor.Enter(this); try { DateTime dt = new DateTime(); dt = DateTime.Now; DateStamp = dt.ToString(); switch (Varway) { case "循环变化": if (!mbUse) { Value = Minvalue; mbUse = true; } else { Value = Convert.ToSingle(Value) + Step; if (Convert.ToSingle(Value) >= Maxvalue) { Value = Maxvalue; mbUse = false; } } break; case "正向一周": Valuetype = TypeCode.Single; if (!mbUse) { Value = Minvalue; mbUse = true; } else { Value = Convert.ToSingle(Value) + Step; if (Convert.ToSingle(Value) >= Maxvalue) { Value = Maxvalue; //mbUse = false; } } break; case "BOOL变化": if (!mbUse) { Value = 0; mbUse = true; } else { Value = 1; mbUse = false; } break; case "X^2变化": Valuetype = TypeCode.Single; if (!mbUse) { mOldValue = Minvalue; mbUse = true; } else { mOldValue = Convert.ToSingle(mOldValue) + Step; Value = Convert.ToSingle(mOldValue) * Convert.ToSingle(mOldValue); if (Convert.ToSingle(mOldValue) >= Maxvalue) { mOldValue = Maxvalue; mbUse = false; } } break; case "SIN变化": Valuetype = TypeCode.Single; if (!mbUse) { mOldValue = Minvalue; mbUse = true; } else { mOldValue = Convert.ToSingle(mOldValue) + Step; Value = Math.Sin(Convert.ToSingle(mOldValue)); if (Convert.ToSingle(mOldValue) >= Maxvalue) { mOldValue = Maxvalue; mbUse = false; } } break; case "随机变化": Random d = new Random(); Value = d.Next(Convert.ToInt32(Maxvalue)); Valuetype = TypeCode.UInt32; break; case "中间变量": Valuetype = TypeCode.Single; break; } //激活时间 if (Refresh != null) { Refresh(this); } } finally { //Monitor.Exit(this); } } public uint nCounter { get { return counter; } set { counter = value; } } public uint MTime { get { return mTime; } set { mTime = value; } } public string Name { get { return this.mName; } set { this.mName = value; } } public string Addr { get { return this.mAddr; } set { this.mAddr = value; } } public string Description { get { return this.mDescription; } set { this.mDescription = value; } } public object Value { get { return this.mValue; } set { this.mValue = value; } } public object OldValue { get { return this.mOldValue; } set { this.mOldValue = value; } } public string Device { get { return this.mDevice; } set { this.mDevice = value; } } public TypeCode Valuetype { get { return this.mValuetype; } set { this.mValuetype = value; } } public float Step { get { return this.mStep; } set { this.mStep = value; } } public string Varway { get { return this.mVarway; } set { this.mVarway = value; } } public string DateStamp { get { return this.mDateStamp; } set { this.mDateStamp = value; } } public float Minvalue { get { return this.mMinvalue; } set { this.mMinvalue = value; } } public float Maxvalue { get { return this.mMaxvalue; } set { this.mMaxvalue = value; } } /// /// 虚拟设备是否处于运行状态 /// public bool IsRun { get { return this._isRun; } } } }