|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
|
using System.Text;
|
|
|
|
|
|
namespace Mesnac.Action.ChemicalWeighing
|
|
|
{
|
|
|
/// <summary>
|
|
|
/// PLCSchema.xml中的存盘数据项类 - 对应add节点
|
|
|
/// </summary>
|
|
|
public class PlcRecipeReaderItem
|
|
|
{
|
|
|
#region 构造方法
|
|
|
|
|
|
/// <summary>
|
|
|
/// 默认构造方法
|
|
|
/// </summary>
|
|
|
public PlcRecipeReaderItem()
|
|
|
{
|
|
|
this.EquipBrand = String.Empty;
|
|
|
this.DataFieldName = string.Empty;
|
|
|
this.PlcFiledName = string.Empty;
|
|
|
this.DataType = string.Empty;
|
|
|
this.PlcShifting = 0;
|
|
|
this.DefaultValue = 0;
|
|
|
this.ValueLen = 1;
|
|
|
this.Multiply = 1;
|
|
|
this.SetValue = null;
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
#region 属性定义
|
|
|
|
|
|
/// <summary>
|
|
|
/// 所属PLC设备品牌
|
|
|
/// </summary>
|
|
|
public string EquipBrand { get; set; }
|
|
|
/// <summary>
|
|
|
/// 数据库字段名
|
|
|
/// </summary>
|
|
|
public string DataFieldName { get; set; }
|
|
|
/// <summary>
|
|
|
/// Plc别名
|
|
|
/// </summary>
|
|
|
public string PlcFiledName { get; set; }
|
|
|
/// <summary>
|
|
|
/// 偏移值
|
|
|
/// </summary>
|
|
|
public int PlcShifting { get; set; }
|
|
|
/// <summary>
|
|
|
/// 默认值
|
|
|
/// </summary>
|
|
|
public int DefaultValue { get; set; }
|
|
|
/// <summary>
|
|
|
/// 长度
|
|
|
/// </summary>
|
|
|
public int ValueLen { get; set; }
|
|
|
/// <summary>
|
|
|
/// 数据处理方式
|
|
|
/// </summary>
|
|
|
public string DataType { get; set; }
|
|
|
/// <summary>
|
|
|
/// 乘数
|
|
|
/// </summary>
|
|
|
public int Multiply { get; set; }
|
|
|
/// <summary>
|
|
|
/// 原始值
|
|
|
/// </summary>
|
|
|
public int[] SetValue { get; set; }
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
#region 方法定义
|
|
|
|
|
|
/// <summary>
|
|
|
/// 数据转化值
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
public object GetValue()
|
|
|
{
|
|
|
if (string.IsNullOrWhiteSpace(DataType))
|
|
|
{
|
|
|
return GetValue_Null();
|
|
|
}
|
|
|
else if (DataType.Equals("string", StringComparison.CurrentCultureIgnoreCase))
|
|
|
{
|
|
|
return this.GetValue_String1();
|
|
|
}
|
|
|
else if (DataType.Equals("DateTime", StringComparison.CurrentCultureIgnoreCase))
|
|
|
{
|
|
|
return GetValue_DateTime();
|
|
|
}
|
|
|
else if (DataType.Equals("Int32", StringComparison.CurrentCultureIgnoreCase))
|
|
|
{
|
|
|
return GetValue_Int32();
|
|
|
}
|
|
|
else if (DataType.Equals("float", StringComparison.CurrentCultureIgnoreCase))
|
|
|
{
|
|
|
return GetValue_Float();
|
|
|
}
|
|
|
else if (DataType.Equals("Int16", StringComparison.CurrentCultureIgnoreCase))
|
|
|
{
|
|
|
return GetValue_Int16();
|
|
|
}
|
|
|
return GetValue_Null();
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
#region 辅助方法定义
|
|
|
|
|
|
/// <summary>
|
|
|
/// 对节点值进行除法运算
|
|
|
/// </summary>
|
|
|
/// <param name="buff">要处理的值</param>
|
|
|
/// <returns>返回除法运算后的结果</returns>
|
|
|
private double MultiplyData(double buff)
|
|
|
{
|
|
|
double result = buff;
|
|
|
if (this.Multiply != 0)
|
|
|
{
|
|
|
result = result / (double)this.Multiply;
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 获取对应的设备值
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
private double GetValue_Null()
|
|
|
{
|
|
|
if (this.SetValue.Length == 0)
|
|
|
{
|
|
|
return DefaultValue;
|
|
|
}
|
|
|
return MultiplyData(this.SetValue[0]);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 获取字符串值
|
|
|
/// </summary>
|
|
|
/// <returns>返回当前读取项的字符串值</returns>
|
|
|
private string GetValue_String()
|
|
|
{
|
|
|
if (this.SetValue == null)
|
|
|
{
|
|
|
ICSharpCode.Core.LoggingService<PlcRecipeReaderItem>.Warn(String.Format("把PLC数据解析为字符串{0}失败,原因SetValue为NULL!", this.DataFieldName));
|
|
|
return String.Empty;
|
|
|
}
|
|
|
else if (this.SetValue.Length != this.ValueLen)
|
|
|
{
|
|
|
ICSharpCode.Core.LoggingService<PlcRecipeReaderItem>.Warn(String.Format("把PLC数据解析为字符串{0}失败,原因SetValue.Length!=ValueLen!", this.DataFieldName));
|
|
|
return String.Empty;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
return Mesnac.Basic.DataProcessor.ToString(this.SetValue);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 获取字符串值
|
|
|
/// </summary>
|
|
|
/// <returns>返回当前读取项的字符串值</returns>
|
|
|
private string GetValue_String1()
|
|
|
{
|
|
|
if (this.SetValue == null)
|
|
|
{
|
|
|
ICSharpCode.Core.LoggingService<PlcRecipeReaderItem>.Warn(String.Format("把PLC数据解析为字符串{0}失败,原因SetValue为NULL!", this.DataFieldName));
|
|
|
return String.Empty;
|
|
|
}
|
|
|
else if (this.SetValue.Length != this.ValueLen)
|
|
|
{
|
|
|
ICSharpCode.Core.LoggingService<PlcRecipeReaderItem>.Warn(String.Format("把PLC数据解析为字符串{0}失败,原因SetValue.Length!=ValueLen!", this.DataFieldName));
|
|
|
return String.Empty;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
return Mesnac.Basic.DataProcessor.ToString1(this.SetValue);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 获取日期值
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
private DateTime GetValue_DateTime()
|
|
|
{
|
|
|
if (this.SetValue == null)
|
|
|
{
|
|
|
ICSharpCode.Core.LoggingService<PlcRecipeReaderItem>.Warn(String.Format("把PLC数据解析为DateTime:{0}失败,原因SetValue为NULL!", this.DataFieldName));
|
|
|
return new DateTime(1900, 1, 1, 0, 0, 0);
|
|
|
}
|
|
|
else if (this.SetValue.Length != this.ValueLen)
|
|
|
{
|
|
|
ICSharpCode.Core.LoggingService<PlcRecipeReaderItem>.Warn(String.Format("把PLC数据解析为DateTime:{0}失败,原因SetValue.Length!=ValueLen!", this.DataFieldName));
|
|
|
return new DateTime(1900, 1, 1, 0, 0, 0);
|
|
|
}
|
|
|
else if (this.SetValue.Length != 6)
|
|
|
{
|
|
|
ICSharpCode.Core.LoggingService<PlcRecipeReaderItem>.Warn(String.Format("把PLC数据解析为DateTime:{0}失败,原因SetValue.Length!=6!", this.DataFieldName));
|
|
|
return new DateTime(1900, 1, 1, 0, 0, 0);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
DateTime dateResult = DateTime.Now;
|
|
|
string strDate = String.Format("{0}-{1}-{2} {3}:{4}:{5}", this.SetValue[0], this.SetValue[1], this.SetValue[2], this.SetValue[3], this.SetValue[4], this.SetValue[5]);
|
|
|
if (!DateTime.TryParse(strDate, out dateResult))
|
|
|
{
|
|
|
dateResult = DateTime.Now;
|
|
|
ICSharpCode.Core.LoggingService<PlcRecipeReaderItem>.Warn(String.Format("字段[{0}],把PLC数据解析为DateTime:{1}失败!", this.DataFieldName, strDate));
|
|
|
}
|
|
|
return dateResult;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 获取Int32类型的值
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
private Int32 GetValue_Int32()
|
|
|
{
|
|
|
if (this.SetValue == null)
|
|
|
{
|
|
|
ICSharpCode.Core.LoggingService<PlcRecipeReaderItem>.Warn(String.Format("把PLC数据解析为Int32:{0}失败,原因SetValue为NULL!", this.DataFieldName));
|
|
|
return 0;
|
|
|
}
|
|
|
else if (this.SetValue.Length != this.ValueLen)
|
|
|
{
|
|
|
ICSharpCode.Core.LoggingService<PlcRecipeReaderItem>.Warn(String.Format("把PLC数据解析为Int32:{0}失败,原因SetValue.Length!=ValueLen!", this.DataFieldName));
|
|
|
return 0;
|
|
|
}
|
|
|
else if (this.SetValue.Length != 2)
|
|
|
{
|
|
|
ICSharpCode.Core.LoggingService<PlcRecipeReaderItem>.Warn(String.Format("把PLC数据解析为Int32:{0}失败,原因SetValue.Length!=2!", this.DataFieldName));
|
|
|
return 0;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
Int32 result = 0;
|
|
|
if (this.EquipBrand.Equals("Siemens", StringComparison.CurrentCultureIgnoreCase))
|
|
|
{
|
|
|
result = Mesnac.Basic.DataProcessor.ToSiemensInt32(this.SetValue);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
result = Mesnac.Basic.DataProcessor.ToInt32(this.SetValue);
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 获取Int16类型的值
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
private object GetValue_Int16()
|
|
|
{
|
|
|
if (this.SetValue == null)
|
|
|
{
|
|
|
object[] result = new object[this.ValueLen];
|
|
|
for (int i = 0; i < result.Length;i++ )
|
|
|
{
|
|
|
result[i] = 0;
|
|
|
}
|
|
|
ICSharpCode.Core.LoggingService<PlcRecipeReaderItem>.Warn(String.Format("把PLC数据解析为Int16:{0}失败,原因SetValue为NULL!", this.DataFieldName));
|
|
|
return result;
|
|
|
}
|
|
|
else if (this.SetValue.Length != this.ValueLen)
|
|
|
{
|
|
|
ICSharpCode.Core.LoggingService<PlcRecipeReaderItem>.Warn(String.Format("把PLC数据解析为Int16:{0}失败,原因SetValue.Length!=ValueLen!", this.DataFieldName));
|
|
|
return 0;
|
|
|
}
|
|
|
else if (this.SetValue.Length == 1)
|
|
|
{
|
|
|
return this.SetValue[0];
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
object[] result = new object[this.ValueLen];
|
|
|
for (int i = 0; i < result.Length; i++)
|
|
|
{
|
|
|
result[i] = this.SetValue[i];
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 获取Float类型的值
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
private float GetValue_Float()
|
|
|
{
|
|
|
if (this.SetValue == null)
|
|
|
{
|
|
|
ICSharpCode.Core.LoggingService<PlcRecipeReaderItem>.Warn(String.Format("把PLC数据解析为float:{0}失败,原因SetValue为NULL!", this.DataFieldName));
|
|
|
return 0.0f;
|
|
|
}
|
|
|
else if (this.SetValue.Length != this.ValueLen)
|
|
|
{
|
|
|
ICSharpCode.Core.LoggingService<PlcRecipeReaderItem>.Warn(String.Format("把PLC数据解析为float:{0}失败,原因SetValue.Length!=ValueLen!", this.DataFieldName));
|
|
|
return 0.0f;
|
|
|
}
|
|
|
else if (this.SetValue.Length != 2)
|
|
|
{
|
|
|
ICSharpCode.Core.LoggingService<PlcRecipeReaderItem>.Warn(String.Format("把PLC数据解析为float:{0}失败,原因SetValue.Length!=2!", this.DataFieldName));
|
|
|
return 0.0f;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
float result = 0.0f;
|
|
|
if (this.EquipBrand.Equals("Siemens", StringComparison.CurrentCultureIgnoreCase))
|
|
|
{
|
|
|
result = Mesnac.Basic.DataProcessor.ToSiemensFloat(this.SetValue);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
result = Mesnac.Basic.DataProcessor.ToFloat(this.SetValue);
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
}
|
|
|
}
|