using iWareCC.Common.Helper;
using iWareCC.WCF;
using iWareCommon;
using iWareCommon.Utils;
using iWareModel.Entity.MES;
using iWareSql.DataAccess;
using iWareSql.MyDbContext;
using Newtonsoft.Json;
using System;
using System.Configuration;
using System.Drawing;
using System.Net;
using System.ServiceModel;
using System.Threading;
using System.Windows.Forms;
namespace iWareCC
{
public partial class FormCC : Form
{
///
/// WCS的IP地址
///
public static string WcsIp = ConfigurationSettings.AppSettings["WcsApiHost"].Trim();
public static string WcsSysUserName = "调度系统";
///
/// 是否模拟PLC
///
public static bool IsSimulationPLC = ConfigHelper.GetConfigBool("IsSimulationPLC");
private string currentCheckModelText = "";
const int cycleDelay_Device = 1000;//各个线程的延迟毫秒数
const int cycleDelay_Task = 1000;
Thread startModelTipsThread;//启动线程提示
private bool isStartModelTipsThread = false;//是否启动了线程提示
private Color default_btn_Start_Color = Color.FromArgb(0, 192, 0);//默认的开始模拟按钮颜色
private Color ss_btn_Start_Color1 = Color.FromArgb(255, 255, 192);//开始模拟按钮后闪烁的颜色1
private Color ss_btn_Start_Color2 = Color.FromArgb(192, 192, 255);//开始模拟按钮后闪烁的颜色1
public FormCC()
{
InitializeComponent();
//暂时注释
//tp_Thread.Parent = null;//隐藏标签[线程控制]
CheckForIllegalCrossThreadCalls = false;
//设置界面初始化的选项
//暂时 默认就选中自动模式
this.rBNormal.Checked = SystemValue.rBNormal_Checked = true;
this.rbHand.Checked = SystemValue.rbHand_Checked = false;
this.rBDebug.Checked = SystemValue.rBDebug_Checked = false;
SetModelCheckBoxToSystemValue();
startModelTipsThread = new Thread(ShowTipsWhenStartModel);
startModelTipsThread.Start();
var tskSetSrmLable = new Thread(SrmRead_Label);
tskSetSrmLable.Start();
#region 服务线程启动
//处理 1、2巷道的任务下发
new Thread(IssueTasHandler.Handler).Start();
//平库WCS任务完成线程
new Thread(TaskFinishHandler.Handler).Start();
new Thread(DeleteData).Start();
new Thread(PrintHandler.Handler).Start();
new Thread(ReceivingPoOrderHandler.Handler).Start();
#endregion
}
private void FormCC_Load(object sender, EventArgs e)
{
#region 对外发布WCF形式数据访问服务
try
{
#region 对外发布WCF形式数据访问服务
var lineServiceHost = new ServiceHost(typeof(CCWcfService));
lineServiceHost.Open();
lbl_WCFMsg.Text = "发布WCF成功," + lineServiceHost.BaseAddresses[0].AbsoluteUri;
lbl_WCFMsg.ForeColor = Color.Green;
#endregion
}
catch (Exception ex)
{
Log4NetHelper.WriteErrorLog(iWareCommon.Utils.LogType.CCWCFService, "发布WCF失败", ex);
lbl_WCFMsg.Text = "发布WCF失败:" + ex.Message;
lbl_WCFMsg.ForeColor = Color.Red;
}
#endregion
this.lbl_IsSimulationPLC.Text = "是否模拟PLC:" + (IsSimulationPLC ? "是" : "否");
if (IsSimulationPLC)
{
//暂时注释
this.panel_Model.BackColor = Color.Tomato;
}
else
{//正式模式
}
this.lbl_IsSimulationPLC.BackColor = IsSimulationPLC ? Color.Red : Color.Green;
//此处暂时快速启动 调试模式,用于开发用
//btn_Start_Click(null, null);
}
#region 其他线程
///
/// 设置1/2号堆垛机的前端显示
///
private void SrmRead_Label()
{
while (true)
{
Thread.Sleep(1000);
try
{
var showNowTime = "(" + DateTimeHelper.ConvertToStringForOnlyShowTime(DateTime.Now) + ")";
this.tb_Alert_OutPlanTaskHandler.Text = showNowTime + SystemWarningMsg._lbl_Alert_OutPlanTaskHandler;
this.tb__Alert_OutPlanTaskHandler3.Text = showNowTime + SystemWarningMsg._lbl_Alert_OutPlanTaskHandler3;
this.lbl_Alert_SrmTaskFinish.Text = showNowTime + SystemWarningMsg._lbl_Alert_SrmTaskFinish;
this.lbl_Alert_PingKuTaskFinish.Text = showNowTime + SystemWarningMsg._lbl_Alert_PingKuTaskFinish;
this.lbl_Alert_库存邮箱预警线程.Text = showNowTime + SystemWarningMsg._lbl_Alert_StoreWaringMailHandler;
this.lbl_Alert_邮件发送处理线程.Text = showNowTime + SystemWarningMsg._lbl_Alert_PoDeliveryHandler;
this.lbl_Alert_InventorySnapshotHandler.Text = showNowTime + SystemWarningMsg._lbl_Alert_noti;
this.lbl_Alert_PrintHandler.Text = showNowTime + SystemWarningMsg.lbl_Alert_ReceivingPoOrderHandler;
this.lbl_Alert_InventorySnapshotHandler.Text = showNowTime + SystemWarningMsg._lbl_Alert_InventorySnapshotHandler;
}
catch (Exception ex)
{
Log4NetHelper.WriteErrorLog(iWareCommon.Utils.LogType.SrmTheadService, "方法SrmRead_Label出现异常:" + ex.Message, ex);
continue;
}
}
}
#region 定时删除数据
///
/// 定时删除数据
///
public void DeleteData()
{
while (true)
{
try
{
if (SystemValue.isAllowRuning_DeleteData && SystemValue.isStartedModel)
{
ClearDataHandler.Delete_wms_stock_snapshot(360);
ClearDataHandler.Delete_wms_stock_snapshot_details(360);
ClearDataHandler.Delete_wms_stock_snapshot_group(360);
ClearDataHandler.Delete_wms_stock_quan_zero(60);
ClearDataHandler.Delete_wms_stock_quan_out(60);
ClearDataHandler.Delete_HttpRequestRecord(14);
//ClearDataHandler.Delete_Task_Part(10);
//ClearDataHandler.Delete_Device_Warning(30);
//ClearDataHandler.Delete_HttpRequestRecord(14);
//ClearDataHandler.Delete_MES_BTJReceiveReq(30);
//ClearDataHandler.Delete_MES_OnLineReq(30);
LogTextHelper.BatchDeleteLog();
Thread.Sleep(8 * 60 * 60 * 1000);//每天8小时一次
}
else
{
Thread.Sleep(6 * 1000);
}
}
catch (Exception ex)
{
SystemWarningMsg._lbl_Alert_DeleteData = "出现异常:" + ex.Message;
Log4NetHelper.WriteErrorLog(iWareCommon.Utils.LogType.CCWCFService, "定时删除数据 出现异常", ex);
}
finally
{
}
}
}
#endregion
#endregion
private void rBNormal_CheckedChanged(object sender, EventArgs e)
{
SetModelCheckBoxToSystemValue();
}
private void rBDebug_CheckedChanged(object sender, EventArgs e)
{
SetModelCheckBoxToSystemValue();
}
///
/// 根据选择的模式配置不同的值
///
private void SetModelCheckBoxToSystemValue()
{
SystemValue.rBNormal_Checked = this.rBNormal.Checked ? true : false;
SystemValue.rbHand_Checked = this.rbHand.Checked ? true : false;
SystemValue.rBDebug_Checked = this.rBDebug.Checked ? true : false;
var title = "当前模式: ";
if (SystemValue.rBNormal_Checked)
{//选中了 自动模式
currentCheckModelText = "自动模式";
this.lbl_ModelText.Text = title + currentCheckModelText;
this.gBoxThread.Enabled = false;
SetAllCheckBoxChecked(true);
}
else if (SystemValue.rbHand_Checked)
{
currentCheckModelText = "手动模式";
this.lbl_ModelText.Text = title + currentCheckModelText;
this.gBoxThread.Enabled = false;
SetAllCheckBoxChecked(false);
}
else if (SystemValue.rBDebug_Checked)
{
currentCheckModelText = "调试模式";
this.lbl_ModelText.Text = title + currentCheckModelText;
SetAllCheckBoxChecked(false);
this.gBoxThread.Enabled = true;
}
else if (SystemValue.rBOnlyIn_Checked)
{
currentCheckModelText = "入库模式";
this.lbl_ModelText.Text = title + currentCheckModelText;
SetAllCheckBoxChecked(true);
this.gBoxThread.Enabled = true;
}
else
{
currentCheckModelText = "无效模式";
}
}
///
/// 遍历所有的Checkbox,并配置是否被选中
///
///
private void SetAllCheckBoxChecked(bool _checked)
{
var contorls = this.groupBox1.Controls;
foreach (var control in contorls)
{
if (control is CheckBox)
{
CheckBox c = (CheckBox)control;
c.Checked = _checked;
}
}
}
///
/// 通用的checkbox改变事件
///
///
///
private void CommonCheckedChanged(object sender, EventArgs e)
{
DoCommonCheckedChanged(ref SystemValue.isAllowRuning_OutTaskIssueTaskHandler, ck_OutPlanTaskHandler);
DoCommonCheckedChanged(ref SystemValue.isAllowRuning_OutTaskIssueTaskHandler3, ck_OutPlanTaskHandler3);
DoCommonCheckedChanged(ref SystemValue.isAllowRuning_DeleteData, ckDeleteData);
DoCommonCheckedChanged(ref SystemValue.isAllowRuning_SrmTaskFinish, ckSrmTaskFinish);
DoCommonCheckedChanged(ref SystemValue.isAllowRuning_PingKuTaskFinish, ckPingKuTaskFinish);
DoCommonCheckedChanged(ref SystemValue.isAllowRuning_PrintHandler, ck_PrintHandler);
DoCommonCheckedChanged(ref SystemValue.isAllowRuning_PoDeliveryHandler, ck_PoDeliveryHandler);
DoCommonCheckedChanged(ref SystemValue.isAllowRuning_ReceivingPoOrderHandler, ck_ReceivingPoOrderHandler);
DoCommonCheckedChanged(ref SystemValue.isAllowRuning_InventorySnapshotHandler, ck_InventorySnapshotHandler);
DoCommonCheckedChanged(ref SystemValue.isAllowRuning_StoreWaringMailHandler, ck_库存邮箱预警线程);
DoCommonCheckedChanged(ref SystemValue.isAllowRuning_PoDeliveryHandler, ck_邮件发送处理线程);
}
private void DoCommonCheckedChanged(ref bool isAllowValue, CheckBox cb)
{
isAllowValue = cb.Checked ? true : false;
}
private void FormCC_SizeChanged(object sender, EventArgs e)
{
}
///
/// 停止
///
///
///
private void btn_End_Click(object sender, EventArgs e)
{
//首先确认下,是不是选择的是 自动模式或者是 提前配板模式
//注意:这里增加模式时,要处理的 !!!!
if (!(SystemValue.rBNormal_Checked
|| SystemValue.rbHand_Checked
|| SystemValue.rBDebug_Checked
|| SystemValue.rBOnlyIn_Checked
))
{
WZ.Useful.Commons.MessageUtil.ShowError("当前没有选择模式,停止动作无效!");
return;
}
if (isStartModelTipsThread == false)
{
WZ.Useful.Commons.MessageUtil.ShowError("当前没有模式在运行,停止动作无效!");
return;
}
MessageBoxButtons messButton = MessageBoxButtons.OKCancel;
DialogResult dr = MessageBox.Show("确定要停止【" + currentCheckModelText + "】模式吗?", "停止模式", messButton);
if (dr == DialogResult.OK)//如果点击“确定”按钮
{
var doResult = CommonStopService();
if (!doResult)
{
return;
}
this.btn_Start.Text = "启动模式";
this.btn_Start.Enabled = true;
SystemValue.isStartedModel = false;
//启用选择模式的checkbox
SetEnableForModelRadioButton(true);
//this.rBNormal.Enabled = true;
//this.rBClose.Enabled = true;
//this.rBDebug.Enabled = true;
//this.rBPrepare.Enabled = true;
//this.rBPersonRaw.Enabled = true;
//this.rBInOutStore.Enabled = true;
//this.rBCancelBatchBackToStore.Enabled = true;
//不需要重新关闭checkbox
//SetAllCheckBoxChecked(false);
isStartModelTipsThread = false;
this.btn_Start.BackColor = default_btn_Start_Color;//还原颜色
}
else//如果点击“取消”按钮
{
}
}
///
/// 控制模式中的RadioButton禁启用
///
///
private void SetEnableForModelRadioButton(bool enabled)
{
var ctls = this.panel_Model.Controls;
foreach (var item in ctls)
{
if (item is RadioButton)
{
RadioButton rb = (RadioButton)item;
rb.Enabled = enabled;
}
}
}
///
/// 启动
///
///
///
private void btn_Start_Click(object sender, EventArgs e)
{
//首先确认下,是不是选择的是 自动模式或者是 提前配板模式
if (!(SystemValue.rBNormal_Checked
|| SystemValue.rbHand_Checked
|| SystemValue.rBDebug_Checked
|| SystemValue.rBOnlyIn_Checked
))
{
WZ.Useful.Commons.MessageUtil.ShowError("请选择其中一个模式!");
return;
}
MessageBoxButtons messButton = MessageBoxButtons.OKCancel;
DialogResult dr = MessageBox.Show("确定要启动【" + currentCheckModelText + "】模式吗?", "启动模式", messButton);
if (dr == DialogResult.OK)//如果点击“确定”按钮
{
using (MyDbContext edm = new MyDbContext())
{
var ccSystem = BasicDataSetHandler.GetCCSystem(edm);
if (ccSystem == null)
{
WZ.Useful.Commons.MessageUtil.ShowError("请联系管理员维护系统调度相关的数据!");
return;
};
//判断是否是自己开启的软件
if (!string.IsNullOrEmpty(ccSystem.Machine) && ccSystem.Machine != Dns.GetHostName())
{//默认的空值
WZ.Useful.Commons.MessageUtil.ShowError(string.Format("调度系统已经在其他地方启动模式,机器名:{0},启动时间:{1}!"
, ccSystem.Machine, DateTimeHelper.ConvertToString(ccSystem.OnLineTime)));
return;
}
ccSystem.BasicDataValue = SystemValue.SystemStartGuid = Guid.NewGuid().ToString();
ccSystem.OnLineTime = DateTime.Now;
ccSystem.Machine = Dns.GetHostName();
int i = edm.SaveChanges();
if (i <= 0)
{
WZ.Useful.Commons.MessageUtil.ShowError("保存系统调度相关的数据失败!");
return;
}
//*/
}
this.btn_Start.Text = "【" + currentCheckModelText + "】模式运行中...";
this.btn_Start.Enabled = false;
this.btn_End.Enabled = true;
SystemValue.isStartedModel = true;
//禁用选择模式的checkbox
SetEnableForModelRadioButton(false);
//this.rBNormal.Enabled = false;
//this.rBClose.Enabled = false;
//this.rBDebug.Enabled = false;
//this.rBPersonRaw.Enabled = false;
//this.rBPrepare.Enabled = false;
//this.rBInOutStore.Enabled = false;
isStartModelTipsThread = true;
}
else//如果点击“取消”按钮
{
}
}
///
/// 公共停止服务的处理
///
///
private bool CommonStopService()
{
using (MyDbContext edm = new MyDbContext())
{
var ccSystem = BasicDataSetHandler.GetCCSystem(edm);
if (ccSystem == null)
{
WZ.Useful.Commons.MessageUtil.ShowError("请联系管理员维护系统调度相关的数据!");
return false;
};
//判断是否是自己开启的软件模式
if (SystemValue.SystemStartGuid == ccSystem.BasicDataValue)
{
ccSystem.BasicDataValue = "000000";//默认的空值
ccSystem.OnLineTime = null;
ccSystem.Machine = string.Empty;
ccSystem.LastMachine = Dns.GetHostName();
ccSystem.LastUnLineTime = DateTime.Now;
ccSystem.UpdateTime = DateTime.Now;
int i = edm.SaveChanges();
if (i <= 0)
{
WZ.Useful.Commons.MessageUtil.ShowError("保存系统调度相关的数据失败!");
return false;
}
}
}
return true;
}
private void FormCC_FormClosing(object sender, FormClosingEventArgs e)
{
MessageBoxButtons messButton = MessageBoxButtons.OKCancel;
DialogResult dr = MessageBox.Show("确定要退出吗?", "确认", messButton);
if (dr == DialogResult.OK)//如果点击“确定”按钮
{
try
{
var doResult = CommonStopService();
if (!doResult)
{
e.Cancel = true;
return;
}
e.Cancel = false;
}
catch (Exception ex)
{
WZ.Useful.Commons.MessageUtil.ShowError("出现错误!" + ex.Message);
e.Cancel = true;
}
}
else//如果点击“取消”按钮
{
e.Cancel = true;
}
}
private void FormCC_FormClosed(object sender, FormClosedEventArgs e)
{
try
{
Dispose();
}
catch (Exception)
{
}
Environment.Exit(0);
}
///
/// 模式启动后,按钮闪烁
///
///
private void ShowTipsWhenStartModel(object obj)
{
while (true)
{
Thread.Sleep(1000);
if (isStartModelTipsThread)
{
try
{
this.btn_Start.BackColor = ss_btn_Start_Color2;
Thread.Sleep(1000);
this.btn_Start.BackColor = ss_btn_Start_Color1;
}
catch (Exception)
{
continue;
}
}
else
{
if (this.btn_Start.BackColor != default_btn_Start_Color)
{
this.btn_Start.BackColor = default_btn_Start_Color;//还原颜色
}
}
}
}
private void panel27_Paint(object sender, PaintEventArgs e)
{
}
private void rBOnlyIn_CheckedChanged(object sender, EventArgs e)
{
SetModelCheckBoxToSystemValue();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
CriterionContainerOutput outs = WMSRequestHelper.FindEmptyLocationForLane3(1, "SLTP-3", 0);
MessageBox.Show("返回:" + JsonConvert.SerializeObject(outs));
}
catch (Exception ex)
{
MessageBox.Show("异常:" + ex.Message);
}
}
///
/// 获取单个PO单
///
///
///
private void button2_Click(object sender, EventArgs e)
{
// this.tb_Msg.Text = StringHelper.GetLog("PO收货单:" + poNummber + ",返回结果:" + JsonConvert.SerializeObject(purchaseOrderEntity) + ",错误消息:" + msg);
}
///
/// PO收货单并保存数据
///
///
///
private void button3_Click(object sender, EventArgs e)
{
//var poNummber = this.tb_PoNumber.Text.Trim();
//if (string.IsNullOrEmpty(poNummber))
//{
// MessageBox.Show("请输入PO单号");
// return;
//}
//var msg = "";
//iWareCC.WuhanMakinoSapWcfService.PurchaseOrderEntity purchaseOrderEntity = null;
//try
//{
// using (WuhanMakinoSapWcfService.SapWcfServiceClient sapWcfServiceClient = new WuhanMakinoSapWcfService.SapWcfServiceClient())
// {
// purchaseOrderEntity = sapWcfServiceClient.GetPurchaseOrder(poNummber, out msg);
// }
//}
//catch (Exception ex)
//{
// msg = "异常:" + ex.Message;
//}
//this.tb_Msg.Text = StringHelper.GetLog("PO收货单:" + poNummber + ",返回结果:" + JsonConvert.SerializeObject(purchaseOrderEntity) + ",错误消息:" + msg);
//if (purchaseOrderEntity != null)
//{
// try
// {
// using (MyDbContext context = new MyDbContext())
// {
// var _list = new System.Collections.Generic.List()
// {
// purchaseOrderEntity
// };
// Sap_PurchaseOrderTaskHandler.SavePurchaseOrder(context, _list);
// }
// }
// catch (Exception ex)
// {
// msg = "异常:" + ex.Message;
// }
//}
//this.tb_Msg.Text += StringHelper.GetLog("PO收货单:" + poNummber + ",错误消息:" + msg);
}
private void tabPage1_Click(object sender, EventArgs e)
{
}
private void ck_ReceivingPoOrderHandler_CheckedChanged(object sender, EventArgs e)
{
}
private void lbl_Alert_ReceivingPoOrderHandler_Click(object sender, EventArgs e)
{
}
private void lbl_Alert_PrintHandler_Click(object sender, EventArgs e)
{
}
private void lbl_Alert_PoDeliveryHandler_Click(object sender, EventArgs e)
{
}
private void ck_PoDeliveryHandler_CheckedChanged(object sender, EventArgs e)
{
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
//private void button1_Click(object sender, EventArgs e)
//{
// IDictionary hs = new System.Collections.Generic.Dictionary();
// hs.Add("1-1-4-18", "MYH03711");
// hs.Add("3-12-1-4", "MYST000273");
// hs.Add("1-11-4-11", "MYH02720");
// hs.Add("1-11-4-8", "MYS02366");
// hs.Add("1-11-4-12", "MYH02712");
// hs.Add("1-11-4-13", "MYH02722");
// hs.Add("1-11-4-17", "MYH03663");
// hs.Add("1-11-4-18", "MYH00297");
// hs.Add("1-12-1-11", "MYH00168");
// hs.Add("1-12-1-13", "MYH00163");
// hs.Add("1-12-4-1", "MYS03325");
// hs.Add("1-12-4-3", "MYS00196");
// hs.Add("1-12-1-14", "MYH02710");
// hs.Add("1-12-4-15", "MYH00892");
// hs.Add("1-12-4-16", "MYH00973");
// hs.Add("1-12-4-17", "MYH00490");
// hs.Add("1-12-4-18", "MYH00349");
// hs.Add("1-1-2-2", "MYS00698");
// hs.Add("1-1-1-3", "MYS04090");
// hs.Add("2-6-2-4", "MYS00822");
// hs.Add("1-1-1-4", "MYS00510");
// hs.Add("2-39-2-2", "MYS00829");
// hs.Add("1-1-1-6", "MYS00566");
// hs.Add("1-1-4-12", "MYH00246");
// hs.Add("1-10-1-13", "MYH01680");
// hs.Add("1-1-4-15", "MYH02675");
// try
// {
// using (MyDbContext context = new MyDbContext())
// {
// foreach (var item in hs)
// {
// var isEist = context.ware_location_vs_container.Where(x => x.WareLocationCode == item.Key && (x.IsDeleted == null || x.IsDeleted == false)).FirstOrDefault();
// if (isEist == null)
// {
// ware_location_vs_container ware_Location_Vs_Container = new ware_location_vs_container()
// {
// Id = Yitter.IdGenerator.YitIdHelper.NextId(),
// WareLocationCode = item.Key,
// WareContainerCode = item.Value,
// IsEmptyContainer = false,
// CreatedTime = DateTime.Now,
// IsDeleted = false,
// Status = 0,
// TenantId = 0,
// CreatedUserId = 0,
// CreatedUserName = "cc",
// UpdatedUserId = 0
// };
// context.ware_location_vs_container.Add(ware_Location_Vs_Container);
// }
// }
// context.SaveChanges();
// MessageBox.Show("成功");
// }
// }
// catch (Exception ex)
// {
// MessageBox.Show("异常:" + ex.Message);
// }
//}
}
}