You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

210 lines
6.1 KiB
C#

1 year ago
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Mesnac.Equips;
namespace Mesnac.Action.ChemicalWeighing.Entity
{
/// <summary>
/// PLC数据键值类
/// </summary>
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 属性定义
/// <summary>
/// 配置名称 = 配置key值
/// </summary>
public string Name { get; private set; }
/// <summary>
/// 配置key值
/// </summary>
public string FieldKey { get; private set; }
/// <summary>
/// 设备设置值
/// </summary>
public string EquipKey { get; private set; }
/// <summary>
/// 设备设置值
/// </summary>
public string EquipRunName { get; private set; }
/// <summary>
/// 设备设置值
/// </summary>
public Mesnac.Equips.BaseInfo.Data EquipData { get; private set; }
/// <summary>
/// 说明
/// </summary>
public string Remark { get; private set; }
/// <summary>
/// 上一次读取的值
/// </summary>
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);
}
}
/// <summary>
/// 读取当前值
/// </summary>
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 方法定义
/// <summary>
/// 初始化方法
/// </summary>
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;
}
}
}
}
}
/// <summary>
/// 遍历设备、设备块、设备变量获取设备变量最后一次读取PLC后的值
/// </summary>
/// <returns></returns>
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;
}
/// <summary>
/// 遍历设备、设备块、设备变量、获取设备变量当前的值
/// </summary>
/// <returns></returns>
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
}
}