using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using System.Reflection;
using System.Xml.Serialization;
using ICSharpCode.Core;
using Mesnac.Basic;
using Mesnac.Codd.Session;
using Mesnac.Action.ChemicalWeighing.Entity;
namespace Mesnac.Action.ChemicalWeighing.ChemicalWeighingPlc
{
///
/// 配方下传PLC辅助类
///
public class PlcRecipeHelper
{
#region 读取HMI配方查询请求,并将配方列表写入到PLC
///
/// 读取HMI配方查询请求,并将配方列表写入到PLC
///
public static void DownloadRecipeNameListToPLC()
{
try
{
bool flag = false;
List writeDataList = new List();
#region 读取并处理配方查询请求,并写入。
if (BasePlcHelper.Instance.ChemicalWeighing_HMI_Request_Recipe_Name_ShakeHand.NowValue.ToInt() == 1 && (BasePlcHelper.Instance.ChemicalWeighing_HMI_Request_Recipe_Name_FeedBack.NowValue.ToInt() <= 0 || BasePlcHelper.Instance.ChemicalWeighing_HMI_Request_Recipe_Name_FeedBack.NowValue.ToInt() == 65535))
{
#region 从PLC中获取筛选条件:机台号和配方名称
int[] NamedData;
BasePlcHelper.Instance.PlcRead(BasePlcHelper.Instance.ChemicalWeighing_Plan_Code, out NamedData);
string recipeName = Mesnac.Basic.DataProcessor.ToString1(NamedData);
List recipeNameList = new List();
if (recipeName != null && recipeName != "")
{
#region 从数据库中查询配方名称List
List allList = Technical.XlRecipe.TechnicalHelper.GetPmt_recipeList();
if (allList == null || allList.Count == 0)
{
string msg = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_ChemicalWeighingPlc_PlcRecipeHelper_Mix_msg2")); //HMI请求配方列表:配方数据表为空!
ICSharpCode.Core.LoggingService.Error(msg);
}
else
{
foreach (xl_recipe pmtRecipe in allList)
{
if (pmtRecipe.Recipe_Name.Contains(recipeName))
{
recipeNameList.Add(pmtRecipe.Recipe_Name);
}
}
}
#endregion
#region 数据整理
if (recipeNameList != null && recipeNameList.Count > 0)
{
#region 待写入PLC项整理
List writerList = PlcSchemaHelper.Instance.GetPlcWriter("RecipeNameDataList"); //配方名称数据列表项
if (writerList == null || writerList.Count == 0)
{
string msg = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_ChemicalWeighingPlc_PlcRecipeHelper_Mix_msg4")); //HMI请求配方列表:获取PLC配方名称列表项时出错!
ICSharpCode.Core.LoggingService.Error(msg);
}
#endregion
#region 数据组成及下传业务处理
foreach (PlcWriter writer in writerList)
{
writeDataList.Clear();
#region 验证处理
if (writer.EquipRunName != "RecipeNameList_Info")
{
continue;
}
#endregion
#region 获取PLC品牌名称,处理西门子与其他PLC差异
string equipBrand = Mesnac.Equips.Factory.Instance.GetEquipBrandByRunName(writer.EquipRunName);
writer.EquipBrand = equipBrand;
#endregion
#region 整理数据
if (writer.EquipRunName == "RecipeNameList_Info")
{
for (int i = 0; i < writer.ThisCount; i++)
{
if (recipeNameList != null && i < recipeNameList.Count)
{
foreach (PlcWriteItem item in writer.SchemaList)
{
item.ClearData();
item.EquipBrand = equipBrand; //设置PLC设备品牌
if (item.EquipRunName == "DownLoad_PlanCode")
{
if (recipeNameList[i] != null)
{
item.SetValue = recipeNameList[i];
writeDataList.AddRange(item.WriteData());
}
}
}
}
}
}
#endregion
if (writeDataList.Count < 400)
{
int fillNum = 400 - writeDataList.Count;
for (int i = 0; i < fillNum; i++)
{
writeDataList.AddRange(new short[]{0});
}
}
#region 下传数据
object[] buff = new object[writeDataList.Count];
Array.Copy(writeDataList.ToArray(), buff, buff.Length);
ICSharpCode.Core.LoggingService.Debug(String.Format("[{0}]开始下传计划信息...", writer.EquipRunName));
if (!BasePlcHelper.Instance.PlcWriteByRunName(writer.EquipRunName, buff))
{
string msg = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_ChemicalWeighingPlc_PlcRecipeHelper_Mix_msg5")); //HMI请求配方列表:配方名称列表数据下传失败!
ICSharpCode.Core.LoggingService.Error(msg);
}
else
{
flag = true;
}
ICSharpCode.Core.LoggingService.Debug(String.Format("[{0}]计划信息下传完毕...", writer.EquipRunName));
#endregion
}
#endregion
}
else
{
#region 下传异常反馈信号
bool result = BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.ChemicalWeighing_HMI_Request_Recipe_Name_FeedBack, new object[] { 3 });
if (!result)
{
string msg1 = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_ChemicalWeighingPlc_PlcPlanHelper_Mix_msg30")); //小料-计划信息下传PLC:向PLC下传计划状态请求信号失败!
ICSharpCode.Core.LoggingService.Error(msg1);
}
#endregion
string msg = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_ChemicalWeighingPlc_PlcRecipeHelper_Mix_msg3")); //HMI请求配方列表:没有相关的配方数据!
ICSharpCode.Core.LoggingService.Error(msg);
}
#endregion
}
else
{
string msg = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_ChemicalWeighingPlc_PlcRecipeHelper_Mix_msg1")); //HMI请求配方列表时关键字为空!
ICSharpCode.Core.LoggingService.Error(msg);
}
#endregion
}
#endregion
#region 请求交互
if (flag)
{
#region 下传请求信号
bool result = BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.ChemicalWeighing_HMI_Request_Recipe_Name_FeedBack, new object[] { 1 });
if (!result)
{
string msg = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_ChemicalWeighingPlc_PlcPlanHelper_Mix_msg30")); //小料-计划信息下传PLC:向PLC下传计划状态请求信号失败!
ICSharpCode.Core.LoggingService.Error(msg);
}
#endregion
}
#endregion
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService.Error("把PLC的计划状态更新至数据库异常:" + ex.Message, ex);
}
finally
{
Global.PublicVar.Instance.LocalPlanIsRefresh = false;
}
}
#endregion
}
}