using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using ICSharpCode.Core;
namespace Mesnac.Action.ChemicalWeighing.ChemicalWeighingPlc
{
///
/// 料仓参数设置下传辅助类
///
public class PlcStorageBinParaSetHelper
{
#region 料仓参数下传方法
///
/// 下传料仓参数至PLC
///
/// 输出消息列表
/// PlcSchema.xml对应的节点名称
/// 收集到的要下传的数据
/// 料仓号
/// 成功返回true,失败返回false
public static bool DownloadStorageBinPara(List msgList, string plcNodeName, Entity.RT_Parameter entity,int binNum)
{
try
{
#region 定义变量
string msg = String.Empty;
List writeDataList = new List();
PropertyInfo[] ps = typeof(Entity.RT_Parameter).GetProperties();
if (msgList == null)
{
msgList = new List();
}
#endregion
#region 验证处理
PlcWriter writer = PlcSchemaHelper.Instance.GetPlcWriter(plcNodeName).FirstOrDefault();
if (writer == null)
{
msg = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_ChemicalWeighingPlc_PlcStorageBinParaSetHelper_RT_Parameter_msg10")); //料仓参数下传PLC失败:在PlcSchema.xml中没有RT_Parameter配置节!
msgList.Add(msg);
ICSharpCode.Core.LoggingService.Error(msg);
return false;
}
#endregion
#region 获取PLC品牌名称,处理西门子与其他PLC差异
string equipBrand = Mesnac.Equips.Factory.Instance.GetEquipBrandByRunName(writer.EquipRunName);
writer.EquipBrand = equipBrand;
#endregion
#region 整理数据
if (entity != null)
{
foreach (PlcWriteItem item in writer.SchemaList)
{
item.ClearData();
item.EquipBrand = equipBrand; //设置PLC设备品牌
foreach (PropertyInfo pi in ps)
{
if (item.DataFieldName == pi.Name)
{
item.SetValue = pi.GetValue(entity, null);
break;
}
}
writeDataList.AddRange(item.WriteData());
}
}
else
{
foreach (PlcWriteItem item in writer.SchemaList)
{
item.ClearData();
item.EquipBrand = equipBrand; //设置PLC设备品牌
writeDataList.AddRange(item.WriteData());
}
}
#endregion
#region 下传数据
object[] buff = new object[writeDataList.Count];
Array.Copy(writeDataList.ToArray(), buff, buff.Length);
ICSharpCode.Core.LoggingService.Debug(String.Format("开始下传" + binNum + "号料仓的参数"));
if (!BasePlcHelper.Instance.PlcWriteByRunName(writer.EquipRunName, buff))
{
msg = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_ChemicalWeighingPlc_PlcStorageBinParaSetHelper_RT_Parameter_msg2")); //下传料仓参数失败!
msgList.Add(msg);
ICSharpCode.Core.LoggingService.Error(msg);
return false;
}
ICSharpCode.Core.LoggingService.Debug(String.Format("下传料仓参数完毕!"));
#endregion
return true;
}
catch (Exception ex)
{
msgList.Add(ex.Message);
ICSharpCode.Core.LoggingService.Error(ex.Message, ex);
return false;
}
}
#endregion
}
}