using System;
using System.Collections.Generic;
using System.Net.NetworkInformation;
using System.Text;
using System.Linq;
using System.Data;
using System.Threading;
using Mesnac.Controls.Base;
using System.Windows.Forms;
using Mesnac.Codd.Session;
using System.IO;
using Mesnac.Action.Feeding.Qingquan.BasicInfo;
using Mesnac.Action.Base;
namespace Mesnac.Action.Feeding.Qingquan.FeedingPlc
{
///
/// 暂停计划,不能作为命令使用,因为要用到当班计划中的控件
///
public class PausePlan : FeedingAction, IAction
{
#region 事件定义
///
/// 刷新计划事件
///
public static event EventHandler OnRefreshPlan;
#endregion
public void Run(RuntimeParameter runtime)
{
base.RunIni(runtime);
try
{
base.LogDebug("暂停计划...");
string msg = String.Empty;
string currentPlanID = String.Empty; //保存当前计划
#region 检验执行暂停计划的条件
#region 判断当班计划界面是否打开
if (PlanCommon.IsInit == false)
{
msg = "当班计划还为初始化,请先打开当班计划,再进行此操作!";
ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
#endregion
#region 检验本地数据连接
DbHelper localHelper = base.NewDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local);
if (localHelper == null)
{
base.LogError("获取本地数据连接失败...");
msg = base.Language(140); //"本地数据库连接异常,不能终止计划\r\n请在数据库连接正常后再进行“终止”操作。";
ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
#endregion
#region 检验网络数据连接
//判断是单机版还是网络版
if (base.NetType == NetTypes.Net)
{
//网络版
//bool isConnect = PlanCommon.PingIpOrDomainName(base.GetConfigValue("ServerIP", "127.0.0.1"));
bool isConnect = PlanCommon.IsCanConnectServer();
if (!isConnect)
{
//连接服务器数据库失败...
ShowMsg(Language(33), Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else
{
DbHelper serverHelper = base.NewDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Server);
if (serverHelper == null)
{
base.LogError("获取网络数据连接失败...");
msg = base.Language(141); //"网络数据库连接异常,不能终止计划\r\n请在数据库连接正常后再进行“终止”操作。";
ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
serverHelper.ClearParameter();
serverHelper.CommandType = CommandType.Text;
}
}
#endregion
#region 检验PLC连接状态与密炼机中有无料标志
int mixerHasRub = this.ReadMixAutoAndRubExists(); //判断密炼机中有无料
if (mixerHasRub == -1)
{
msg = base.Language(142); //"PLC连接异常,不能终止计划\r\n请在PLC连接正常后再进行“终止”操作。";
ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else if (mixerHasRub == 1)
{
msg = base.Language(143); //"密炼机内有料,不能暂停计划!";
ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
#endregion
#region 检验班次、日期是否合法
DbMCControl shiftControl = this.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, "PptShift").FirstOrDefault();
DbMCControl planDateControl = this.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, "[ppt_plan].[down_date]").FirstOrDefault();
if (shiftControl == null)
{
base.LogError("缺少班次控件...(班次控件为组合框,数据源要绑定PptShift)");
return;
}
if (planDateControl == null)
{
base.LogError("缺少日期控件...(日期控件为日历,数据源要绑定down_date)");
return;
}
PlanLog log = PlanCommon.PlanLog;
if (log != null)
{
if (log.LastSelectShiftID != Convert.ToInt32(shiftControl.BaseControl.MCValue) || String.Format("{0:yyyyMMdd}", log.LastSelectDate) != String.Format("{0:yyyyMMdd}", Convert.ToDateTime(planDateControl.BaseControl.MCValue)))
{
msg = base.Language(144); //"请把计划调整到正确的日期和班组后再进行终止!";
ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
currentPlanID = log.LastPlanID; //记录当前计划
}
else
{
msg = base.Language(145); //"还没有正在执行的计划,无法暂停!";
ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
#endregion
#endregion
#region 暂停执行业务
PlanStates planStates = PlanCommon.GetPlanState(currentPlanID);
if (planStates != PlanStates.Producting)
{
msg = "当前计划未处于生产状态,不能暂停!";
ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
msg = Language(146); //"当前计划{0}设定{1}车,\r\n您确认暂停?";
DataRow planDataRow = PlanCommon.GetPlanData(Basic.DataSourceFactory.MCDbType.Local, currentPlanID); //获取当前计划数据行
msg = String.Format(msg, currentPlanID, planDataRow["PlanNum"]);
DialogResult result = MessageBox.Show(msg, Language(1), MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
//LogData('基本操作','暂停计划','配方名称'+MemName[nowplan]);
string recipeMaterialName = planDataRow["RecipeMaterialName"] as string;
recipeMaterialName = String.IsNullOrEmpty(recipeMaterialName) ? String.Empty : recipeMaterialName;
base.DBLog("基本操作", "暂停计划", "配方名称" + recipeMaterialName);
DbMCControl clientGridControl = this.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, "ppt_plan").FirstOrDefault(); //获取本机台计划网格控件
if (clientGridControl == null)
{
base.LogError("{当班计划——“修改”本机台计划}缺少本机台计划网格控件...");
return;
}
DataGridView clientGridView = clientGridControl.BaseControl as DataGridView;
if (clientGridView == null)
{
base.LogError("{当班计划——“修改”本机台计划}网格控件类型错误...");
return;
}
foreach (DataGridViewRow rowView in clientGridView.Rows)
{
if (currentPlanID == rowView.Cells["PlanID"].Value as string)
{
//rowView.Cells["RealNum"].Value = rowView.Cells["RealNum"].Value + "终止";
break;
}
}
int result1 = this.DownLoadAbortMixing();
if (result1 == -1)
{
msg = base.Language(147); //"终止密炼失败(PLC读取失败)。";
ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (result1 == 0)
{
msg = base.Language(148); //"终止密炼失败(PLC写入失败)。";
ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
int result2 = this.DownLoadAbortWeight();
if (result1 == -1)
{
msg = base.Language(149); //"终止称量失败(PLC读取失败)。";
ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (result1 == 0)
{
msg = base.Language(150); //"终止称量失败(PLC写入失败)。";
ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
//如果有胶料称客户端
if (base.FarControlType == 1)
{
this.DownLoadFlagStopPlan(1);
}
PlanCommon.SendPlanId(base.NetType, currentPlanID); //增加计划非正常终止时刻计算
#region 如果合并一次法计划标志为1,则更新一次法本地库计划状态,终止计划把一次法计划状态改为4
if (base.GetConfigValue("IsCombinedOne", "0") == "1")
{
PlanCommon.UpdatePlanOne(currentPlanID.Trim(), 0, 4, false);
}
#endregion
if (base.NetType == NetTypes.Net)
{
PlanCommon.FlushShiftConfig(Basic.DataSourceFactory.MCDbType.Server, Global.PublicVar.Instance.FirstLotBarCode, Global.PublicVar.Instance.FirstLotSerialId, Global.PublicVar.Instance.LastLotNum); //增加计划非正常终止时刻计算
if (base.FarControlType == 1)
{
string netMsg = "{0}:1/";
netMsg = String.Format(netMsg, Global.ProtocalHeader.ReceivePlanExecInfo);
Mesnac.Communication.TcpService.Instance.NetSendMsg(netMsg);
}
}
PlanCommon.StopChanYongPlan();
msg = base.Language(151); //"当前计划已经终止,需要运行请重新调整计划!";
ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information);
}
#endregion
//触发事件
if (PausePlan.OnRefreshPlan != null)
{
PausePlan.OnRefreshPlan(null, System.EventArgs.Empty);
}
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService.Error("暂停计划失败:" + ex.Message, ex);
ShowMsg(ex.Message, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
///
/// 计划暂停简化业务,负责处理胶料称客户端的Socket指令
///
public void ExecPause()
{
string currentPlanID = String.Empty;
string msg = String.Empty;
PlanLog planLog = PlanCommon.PlanLog;
if (planLog == null)
{
return;
}
try
{
currentPlanID = planLog.LastPlanID;
PlanStates planStates = PlanCommon.GetPlanState(currentPlanID);
if (planStates != PlanStates.Producting)
{
msg = "当前计划未处于生产状态,不能暂停!";
ICSharpCode.Core.LoggingService.Info(msg);
return;
}
DataRow planDataRow = PlanCommon.GetPlanData(Basic.DataSourceFactory.MCDbType.Local, currentPlanID); //获取当前计划数据行
//LogData('基本操作','暂停计划','配方名称'+MemName[nowplan]);
string recipeMaterialName = planDataRow["RecipeMaterialName"] as string;
recipeMaterialName = String.IsNullOrEmpty(recipeMaterialName) ? String.Empty : recipeMaterialName;
base.DBLog("基本操作", "暂停计划", "配方名称" + recipeMaterialName);
int result1 = this.DownLoadAbortMixing();
if (result1 == -1)
{
msg = base.Language(147); //"终止密炼失败(PLC读取失败)。";
ICSharpCode.Core.LoggingService.Error(msg);
return;
}
if (result1 == 0)
{
msg = base.Language(148); //"终止密炼失败(PLC写入失败)。";
ICSharpCode.Core.LoggingService.Error(msg);
return;
}
int result2 = this.DownLoadAbortWeight();
if (result1 == -1)
{
msg = base.Language(149); //"终止称量失败(PLC读取失败)。";
ICSharpCode.Core.LoggingService.Error(msg);
return;
}
if (result1 == 0)
{
msg = base.Language(150); //"终止称量失败(PLC写入失败)。";
ICSharpCode.Core.LoggingService.Error(msg);
return;
}
//如果有胶料称客户端
if (base.FarControlType == 1)
{
this.DownLoadFlagStopPlan(1);
}
PlanCommon.SendPlanId(base.NetType, currentPlanID); //增加计划非正常终止时刻计算
if (base.NetType == NetTypes.Net)
{
PlanCommon.FlushShiftConfig(Basic.DataSourceFactory.MCDbType.Server, Global.PublicVar.Instance.FirstLotBarCode, Global.PublicVar.Instance.FirstLotSerialId, Global.PublicVar.Instance.LastLotNum); //增加计划非正常终止时刻计算
if (base.FarControlType == 1)
{
string netMsg = "{0}:1/";
netMsg = String.Format(netMsg, Global.ProtocalHeader.ReceivePlanExecInfo);
Mesnac.Communication.TcpService.Instance.NetSendMsg(netMsg);
}
}
PlanCommon.StopChanYongPlan();
msg = base.Language(151); //"当前计划已经终止,需要运行请重新调整计划!";
ICSharpCode.Core.LoggingService.Info(msg);
//触发事件
if (PausePlan.OnRefreshPlan != null)
{
PausePlan.OnRefreshPlan(null, System.EventArgs.Empty);
}
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService.Error("计划暂停失败:" + ex.Message, ex);
}
}
///
/// 判断密炼机中有无料
///
/// 如果PLC连接失败返回-1,否则返回密炼机中有料标志(1-有料,0-无料)
public int ReadMixAutoAndRubExists()
{
int iExistsMaterial = PlcData.Instance.HasMaterial.LastValue.ToInt();
iExistsMaterial = iExistsMaterial & 0x0001; //取最低位
return iExistsMaterial;
}
///
/// 终止密炼
///
/// PLC读取失败返回-1,PLC写入失败返回0,成功返回1
public int DownLoadAbortMixing()
{
if (StopMixing.ExecStopMixing())
{
return 1;
}
else
{
return 0;
}
}
///
/// 终止称量
///
/// PLC读取失败返回-1,PLC写入失败返回0,成功返回1
public int DownLoadAbortWeight()
{
if (StopWeight.ExecStopWeight())
{
return 1;
}
else
{
return 0;
}
}
///
/// 更新停止计划标志
///
/// 标志值
/// 成功返回true,否则返回false
public bool DownLoadFlagStopPlan(int flag)
{
return (PlcData.Instance.PlcWriteByDataKey(PlcData.Instance.StopPlan, new object[] { flag }));
}
}
}