using System;
using System.Collections.Generic;
using System.Net.NetworkInformation;
using System.Text;
using System.Linq;
using System.Data;
using System.Threading;
using Mesnac.Action.Base;
using Mesnac.Controls.Base;
using System.Windows.Forms;
using Mesnac.Codd.Session;
using System.IO;
using Mesnac.Action.Feeding.BasicInfo;
using Mesnac.Action.Feeding.FeedingPlc;
using Mesnac.Basic;
namespace Mesnac.Action.Feeding.FinishBatch
{
///
/// 自动执行下一个计划
///
public class AutuExecuteNextPlan
{
private bool hasWeightType(List weightList, RecipeData.WeightType weightType)
{
foreach (RecipeData.RecipeWeightInfo w in weightList)
{
if (w.WeightType == (int)weightType)
{
return true;
}
}
return false;
}
///
/// 自动下传下一个计划的称量信息
///
public void Run()
{
//判断是否连续执行
if (Mesnac.Basic.RunSchema.Instance.GetConfigValue("PlanExecuteType", "0").Trim() == "0")
{
return;
}
RecipeData recipe = new RecipeData();
//当前计划设定数量
int allche = PlcData.Instance.RecipeSetNumber.LastValue.ToInt();
//当前计划称量信息
List weightList = recipe.GetCurrentRecipeWeightInfo();
if (PlcData.Instance.MixingFinishedCount.NowValue.ToInt() >= allche)
{
return;
}
if (PlcData.Instance.PloyFinishedCount.NowValue.ToInt() < allche
&& hasWeightType(weightList, RecipeData.WeightType.胶料))
{
return;
}
if (PlcData.Instance.CarbonFinishedCount.NowValue.ToInt() < allche
&& hasWeightType(weightList, RecipeData.WeightType.炭黑))
{
return;
}
if (PlcData.Instance.OilFinishedCount.NowValue.ToInt() < allche
&& hasWeightType(weightList, RecipeData.WeightType.油))
{
return;
}
string planid = recipe.GetNextPlanID();
if (string.IsNullOrWhiteSpace(planid))
{
return;
}
if (new BaseAction().NetType == BaseAction.NetTypes.Net)
{
new NetRecipe().GetRecipeByPlanID(planid);
}
string recipeid = recipe.GetRecipeID(planid);
#region 下传称量信息
try
{
//1、获取配方称量信息
DataTable dt = recipe.GetRecipeWeigthData(recipeid);
dt.TableName = planid;
RecipeToDatabase recipeToDatabase = new RecipeToDatabase();
//2、清空PLCData表数据,防止多下传信息
//recipeToDatabase.TruncatePlcData(); //现在不用PLCData表了
//2、
List errorList = null;
RecipeData recipeData = new RecipeData();
List nextWeightList = recipeData.FillRecipeWeightInfo(dt);
if (!recipeToDatabase.ValidateJarSerial(nextWeightList, out errorList))
{
return;
}
//3、把配方称量信息写入PLCData表
if (recipeToDatabase.DownloadRecipeWeightInfo(dt, true) == 1)
{
//4、把称量设定数写入PLCData表
//if (!PlcData.Instance.DataWrite(PlcData.Instance.WeightSetNumber, new object[] { new BasicInfo.RecipeData().GetPlanLotCount(planid) }))
//{
// ICSharpCode.Core.LoggingService.Warn("下传称量信息:把称量设定数写入PLCData表失败!");
// return;
//}
//4、改为把称量设定数直接写入PLC设备
if (!PlcData.Instance.PlcWriteByDataKey(PlcData.Instance.WeightSetNumber, new object[] { new BasicInfo.RecipeData().GetPlanLotCount(planid) }))
{
ICSharpCode.Core.LoggingService.Warn("下传称量信息:把称量信息下传至PLC失败!");
return;
}
//5、把PLCData表中的数据写入PLC,现不存PLCData表,直接写入PLC设备
//if (!new BasicInfo.DatabaseToPlc().Run())
//{
// ICSharpCode.Core.LoggingService.Warn("下传称量信息:把称量信息下传至PLC失败!");
// return;
//}
//6、把开始称量信息下传至PLC
if (!new StartWeightToPlc().DownLoadStartWeight())
{
ICSharpCode.Core.LoggingService.Warn("下传称量信息:把开始称量信号下传至PLC失败!");
return;
}
//7、缓存称量信息
this.CacheRecipeWeigh(recipe);
}
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService.Warn("执行下一个计划提前下传称量信息失败:" + ex.Message);
}
#endregion
}
///
/// 缓存配方称量信息 并保存提前称量缓存信息至SysKeyValue表
///
/// 配方数据对象
public void CacheRecipeWeigh(RecipeData recipeData)
{
try
{
RecipeWeighCache.Instance.CacheFlag = true;
RecipeWeighCache.Instance.CacheAllWeightInfo(recipeData.GetCurrentRecipeWeightInfo());
FeedingAction action = new FeedingAction();
string xmlRecipeWeightCache = SerializeHandler.SerializeObject>(RecipeWeighCache.Instance.AllWeightInfo);
action.UpdateSysValue("RecipeWeightCache", xmlRecipeWeightCache);
action.UpdateSysValue("RecipeWeightCache_CacheFlag", RecipeWeighCache.Instance.CacheFlag.ToString());
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService.Error("保存提前称量缓存信息至SysKeyValue表失败:" + ex.Message, ex);
}
}
}
}