using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Mesnac.Equips; namespace Mesnac.Action.ChemicalWeighing.Entity { /// /// PLC数据键值类 /// public class DataKeyValue { #region 字段定义 private Value setLastValue = null; private Value setNowValue = null; #endregion #region 构造方法 public DataKeyValue(string name) { this.Name = name; this.FieldKey = name; this.Init(); } #endregion #region 属性定义 /// /// 配置名称 = 配置key值 /// public string Name { get; private set; } /// /// 配置key值 /// public string FieldKey { get; private set; } /// /// 设备设置值 /// public string EquipKey { get; private set; } /// /// 设备设置值 /// public string EquipRunName { get; private set; } /// /// 设备设置值 /// public Mesnac.Equips.BaseInfo.Data EquipData { get; private set; } /// /// 说明 /// public string Remark { get; private set; } /// /// 上一次读取的值 /// public Value LastValue { get { Value value = this.getLastValue(); if (value == null || value.ToObject() == null) { value = this.getNowValue(); } if (value == null || value.ToObject() == null) { value = setLastValue; } if (value == null) { value = new Value(null); } setLastValue = value; return setLastValue; } set { this.setLastValue = value; this.EquipData.Value = value.ToObject(); this.EquipData.Group.Equip.RefreshEventArgsValue(this.EquipData); } } /// /// 读取当前值 /// public Value NowValue { get { Value value = this.getNowValue(); if (value == null) { value = setNowValue; } if (value == null) { value = setLastValue; } if (value == null) { value = new Value(null); } setNowValue = value; return setNowValue; } set { this.setNowValue = value; //this.EquipData.Value = value.ToObject(); //this.EquipData.Group.Equip.RefreshEventArgsValue(this.EquipData); } } #endregion #region 方法定义 /// /// 初始化方法 /// private void Init() { foreach (Mesnac.Equips.BaseEquip equip in Factory.Instance.AllEquips.Values) { foreach (Mesnac.Equips.BaseInfo.Group group in equip.Group.Values) { foreach (Mesnac.Equips.BaseInfo.Data data in group.Data.Values) { if (data.RunName.Equals(this.FieldKey, StringComparison.CurrentCultureIgnoreCase)) { this.EquipKey = data.KeyName; this.EquipRunName = data.RunName; this.EquipData = data; } } } } } /// /// 遍历设备、设备块、设备变量,获取设备变量最后一次读取PLC后的值 /// /// private Value getLastValue() { Value Result = null; foreach (Mesnac.Equips.BaseEquip equip in Factory.Instance.AllEquips.Values) { foreach (Mesnac.Equips.BaseInfo.Group group in equip.Group.Values) { if (group.Access == System.IO.FileAccess.Read || group.Access == System.IO.FileAccess.ReadWrite) { foreach (Mesnac.Equips.BaseInfo.Data data in group.Data.Values) { if (data.KeyName == this.EquipKey) { return new Value(data.Value); } } } } } return Result; } /// /// 遍历设备、设备块、设备变量、获取设备变量当前的值 /// /// private Value getNowValue() { Value Result = null; foreach (Mesnac.Equips.BaseEquip equip in Factory.Instance.AllEquips.Values) { foreach (Mesnac.Equips.BaseInfo.Group group in equip.Group.Values) { if (group.Access == System.IO.FileAccess.Read || group.Access == System.IO.FileAccess.ReadWrite) { foreach (Mesnac.Equips.BaseInfo.Data data in group.Data.Values) { if (data.KeyName == this.EquipKey) { return new Value(equip.ReadData(data)); } } } } } return Result; } #endregion } }