using iWareCommon.Utils;
using iWareModel.Entity.MES;
using iWareSql.MyDbContext;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Linq;
using Admin.NET.Core.TaskModule.Enum;
using System.Configuration;
using iWareModel.Entity.WCS;
using iWareCC.Common.Helper;
using Newtonsoft.Json.Bson;
using System.Data.Entity;
using System.Web;
using iWareModel.Entity.WMS;
using iWareCommon;
using System.Threading.Tasks;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TrackBar;
using System.Windows.Forms;
using iWareSql.DataAccess;
using iWareModel.Entity;
namespace iWareCC
{
///
/// 下发任务处理的线程
///
public class IssueTasHandler
{
public static List curLaneDataObjs = new List() {
new CurLaneDataObj(){ Lane=1, LastRunTaskCategoryFun=null },
new CurLaneDataObj(){ Lane=2, LastRunTaskCategoryFun=null}
};
public static void Handler()
{
var errMsg = "";
while (true)
{
try
{
SystemWarningMsg._lbl_Alert_OutPlanTaskHandler = "循环开始...";
if (SystemValue.isAllowRuning_OutTaskIssueTaskHandler && SystemValue.isStartedModel)
{
errMsg = "";//重置
try
{
#region 查询堆垛机是否空闲
//var utl = string.Format(WcsIp + @"GetDevicesInfo?Timestamp={0}&Creator={1}", DateTime.Now.ToString(), "cc");
//var result1 = HttpHelper.Post(utl, "");
WCSDevicesOutput data1 = null;
if (FormCC.IsSimulationPLC)
{//如果是模拟
data1 = MyExtendHelper.GetSimulationSrmDevicesOutput();
Log4NetHelper.WriteInfoLog(LogType.OutPlanTask, "出库处理的线程(OutPlanTaskHandler)查询堆垛机是否空闲,模拟环境");
SystemWarningMsg._lbl_Alert_OutPlanTaskHandler = "堆垛机状态查询模拟";
}
else
{
//var utl = string.Format(@"GetDevicesInfo?Timestamp={0}&Creator={1}", DateTime.Now.ToString(), "cc");
//IDictionary parameter = new Dictionary();
//var result1 = new HTTPService(FormCC.WcsIp).postContentForString(utl, parameter, "");
//Log4NetHelper.WriteInfoLog(LogType.OutPlanTask, "出库处理的线程(OutPlanTaskHandler)查询堆垛机是否空闲,返回结果:" + result1);
//if (result1 == null)
//{
// SystemWarningMsg._lbl_Alert_OutPlanTaskHandler = "堆垛机状态查询失败,WCS返回NULL";
// continue;//继续下一次循环
//}
//data1 = JsonConvert.DeserializeObject(result1);
//if (data1 == null || !data1.Success || data1.Data == null)
//{
// SystemWarningMsg._lbl_Alert_OutPlanTaskHandler = "堆垛机状态查询失败,WCS返回结果:" + result1;
// continue;//继续下一次循环
//}
}
if (data1.Data != null && data1.Data.SRM1 != null)
{
_Do(data1.Data.SRM1, 1, ref errMsg);
}
else
{
SystemWarningMsg._lbl_Alert_OutPlanTaskHandler += "|| 1号巷道无任务或者1号堆垛机不是空闲状态!";
}
if (data1.Data != null && data1.Data.SRM2 != null)
{
_Do(data1.Data.SRM2, 2, ref errMsg);
}
else
{
SystemWarningMsg._lbl_Alert_OutPlanTaskHandler += "|| 2号巷道无任务或者2号堆垛机不是空闲状态!";
}
#endregion
// SystemWarningMsg._lbl_Alert_OutPlanTaskHandler = "循环结束...";
}
catch (Exception ex)
{
Log4NetHelper.WriteErrorLog(LogType.OutPlanTask, "处理出入库下发=>" + errMsg + ",异常:" + ex.Message, ex);
SystemWarningMsg._lbl_Alert_OutPlanTaskHandler = "处理出入库下发=>" + errMsg + ",异常:" + ex.Message;
}
}
}
catch (Exception ex)
{
SystemWarningMsg._lbl_Alert_OutPlanTaskHandler = "出现异常:" + ex.Message;
Log4NetHelper.WriteErrorLog(LogType.OutPlanTask, "OutPlanTaskHandler 出库任务 出现异常:" + ex.Message, ex);
}
Thread.Sleep(2 * 1000);//休眠2秒
}
}
private static void _Do(WDO_Data wDO_Data, int flag, ref string errMsg)
{
wms_task model = null;
if (wDO_Data != null)
{
int Lane = int.Parse(wDO_Data.SrmNo);
Handler_Issue(model, Lane, flag, ref errMsg);
}
else
{
SystemWarningMsg._lbl_Alert_OutPlanTaskHandler += "|| " + flag + "号巷道无任务或者" + flag + "号堆垛机不是空闲状态!";
}
}
///
/// 任务下发
///
///
///
///
///
private static void Handler_Issue(wms_task model, int Lane, int flag, ref string errMsg)
{
model = null;
using (MyDbContext mycontext1 = new MyDbContext())
{
model = mycontext1.wms_task.Where(x => ( x.IsDelete == false) && x.TaskStatus == (int)TaskStatusEnum.新建)
.OrderByDescending(x => x.TaskPriority).FirstOrDefault();//查询未WCS下发未完成的任务
if (model != null)
{
int result = IssueTaskHelper.Put(mycontext1, FormCC.WcsIp, Lane, model, ref errMsg);
if (!string.IsNullOrEmpty(errMsg))
{
SystemWarningMsg._lbl_Alert_OutPlanTaskHandler += "|| " + flag + "巷道下发入库任务失败," + errMsg;
}
}
}
//判断如果有错误日志,就更新任务表的消息
if (model != null && !string.IsNullOrEmpty(errMsg))
{
TaskHandler.UpdateTaskMsg(model.Id, errMsg);
}
}
}
}