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