using Hanhe.iWCS.Business;
using Hanhe.iWCS.Common;
using Hanhe.iWCS.Event;
using Hanhe.iWCS.Interface;
using Hanhe.iWCS.MData;
using Hanhe.iWCS.Model;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using static Hanhe.iWCS.TaizhouGEMTwoProtocol.ERPService;
using static Hanhe.iWCS.TaizhouGEMTwoProtocol.PLCControl;
using static Hanhe.iWCS.TaizhouGEMTwoProtocol.ProcessHelper;
namespace Hanhe.iWCS.TaizhouGEMTwoProtocol
{
///
/// 事件监听处理和触发事件回调
///
public class EnentListen : IEnentListen
{
private EquipmentCommandEQBLL CommandEQBLL = new EquipmentCommandEQBLL();
private ICallTaskProcessing iCallTask = new ICallTaskProcessing();
static object locker = new object();
static object lock1027 = new object();
static object lock10001 = new object();
static object lock10014 = new object();
static object lock10015 = new object();
static object lock10017 = new object();
static EnentListen() {
}
///
/// 事件订阅
///
/// 当前动作模型
///
public bool EnentSubscribe(TN_I_TASK_DTL_ACTION currActionModel) {
try {
EventDelegate eventDelegate = new EventDelegate();
eventDelegate.CurrActionModel = currActionModel;
//eventDelegate.iSleep = 3000;
eventDelegate.StartListening();
return true;
}
catch (Exception ex) {
throw new Exception(ex.Message);
}
}
private SortByDocument sortBy = new SortByDocument { { "createTime", 1 } };
///
/// 事件监听处理
///
/// 当前动作模型
/// true:条件成立 false:条件不成立
public bool MonitorProcess(TN_I_TASK_DTL_ACTION currActionModel) {
bool bResult = false;
string msg = string.Empty;
#region 处理任务状态
if (currActionModel.CN_N_ACTION_CODE == 1027)
{
lock (lock1027)
{
var request = MongoDBSingleton.Instance.FindAll();
if (request.Count > 0)
{
request.ForEach(a =>
{
try
{
CMMLog.Info($"进入workflowToDeviceDriver,CN_S_TASK_NO:{a.TaskNo},CN_N_ACTION_CODE:{a.State},CN_S_DEVICE_CODE:{a.ForkliftNo},Ext2:{a.LockNo},Ext3:{a.ExtData}");
new ProtocolAnalysis().workflowToDeviceDriver(new TN_I_TASK_DTL_ACTION { CN_S_TASK_NO = a.TaskNo, CN_N_ACTION_CODE = a.State, CN_S_DEVICE_CODE = a.ForkliftNo, Ext2 = a.LockNo, Ext3 = a.ExtData });
MongoDBSingleton.Instance.Remove(Query.EQ("_id", a._id), RemoveFlags.Single);
}
catch (Exception ex)
{
CMMLog.Error(ex.Message, ex);
}
});
}
Thread.Sleep(1000);
}
}
#endregion
#region 3楼设备任务
#region 10001-读取包装线信号-3楼包装取料--已完成(待测试)
if (currActionModel.CN_N_ACTION_CODE == 10001)
{
lock (lock10001)
{
var list = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.deviceType == "1").ToList();
if (list.Count > 0)
{
list.ForEach(a =>
{
// 包装机启动时读取【翻页通道】,根据页号获取五条数据,然后写入包装机
ERPService.WriteItemInfo(a);
PLCControl.CheckPackingMachine(a);
});
}
Thread.Sleep(1500);
}
}
#endregion
#region 10002-读取包装线信号-3楼复称入缓存架--弃用
//if (currActionModel.CN_N_ACTION_CODE == 10002)
//{
// var list = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.deviceType == "2").ToList();
// if (list.Count > 0)
// {
// list.ForEach(a =>
// {
// PLCControl.SecondWeightInCache(a);
// });
// }
// //Thread.Sleep(3000);
//}
#endregion
#region 10003-读取叠包机信号(三楼双层缓存架入叠盘机流程)-3楼缓存架入叠托--已完成(待测试)
if (currActionModel.CN_N_ACTION_CODE == 10003)
{
var list = Settings.GetPlcInfo().Where(a => a.enable == 1 && (a.deviceType == "3" || a.deviceType == "23")).ToList();
if (list.Count > 0)
{
list.ForEach(a =>
{
PLCControl.CacheStackingMouth(a);
});
}
Thread.Sleep(1500);
}
#endregion
#region 10004-读包装机信号(包装机补空托流程)-3楼包装补空--已完成(待测试)
if (currActionModel.CN_N_ACTION_CODE == 10004)
{
bool action = false; //2025/03/19 变更开关 包装机补空流程生成从包装机前得拆盘位到指定包装机
if (action)
{
var list = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.deviceType == "1").ToList();
if (list.Count > 0)
{
list.ForEach(a =>
{
PLCControl.PickUpBlank(a);
});
}
}
else
{
var list = Settings.GetPlcInfo().Where(a => a.enable == 1 && (a.deviceType == "1" || a.deviceType == "21")).ToList();
if (list.Count > 0)
{
list.ForEach(a =>
{
PLCControl.PickUpBlankTwo(a);
});
}
}
Thread.Sleep(1500);
}
#endregion
#region 10005-打包线空托补料请求(3楼拆盘机补空托)-3楼拆盘补空--已完成(待测试)
if (currActionModel.CN_N_ACTION_CODE == 10005)
{
var plc = Settings.GetPlcInfo().Where(a => a.enable == 1 && (a.deviceType == "6" || a.deviceType == "24")).FirstOrDefault();
if (plc != null)
{
if (PickUpEndFree(plc.location))
{
PLCControl.CheckPackingLineEmpty(plc);
}
}
//Thread.Sleep(500);
}
#endregion
#region 10006-叠盘机满托下线(3楼叠盘下线)-3楼叠盘下线--已完成(待测试)
if (currActionModel.CN_N_ACTION_CODE == 10006)
{
var plc = Settings.GetPlcInfo().Where(a => a.enable == 1 && (a.deviceType == "15" || a.deviceType == "25")).ToList();
if (plc.Count >0 )
{
foreach(var a in plc)
{
if (PickUpStartFree(a.location))
{
PLCControl.StackingLineEmpty(a);
}
}
}
//Thread.Sleep(500);
}
#endregion
#region 10007-打包线下线-3楼打包下线--已完成(待测试)
if (currActionModel.CN_N_ACTION_CODE == 10007)
{
var plc = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.deviceType == "4").FirstOrDefault();
if (plc != null)
{
if (CheckStartFree(plc.location))
{
PLCControl.CheckPackingLineFull(plc);
}
}
//Thread.Sleep(500);
}
#endregion
#endregion
#region 1000-电梯任务处理--待确认
if (currActionModel.CN_N_ACTION_CODE == 1000)
{
ProcessHelper.CheckElevatorTask();
//Thread.Sleep(500);
}
#endregion
#region 2000-充电任务--无需改动
if (currActionModel.CN_N_ACTION_CODE == 2000)
{
ProcessHelper.AGVBatteryTime();
Thread.Sleep(500);
ERPService.deletePackageInfo();
}
#endregion
#region 2001-包装机写电量--已完成(待测试)
if (currActionModel.CN_N_ACTION_CODE == 2001)
{
PLCControl.WriteBattery();
}
#endregion
#region 10010-任务动作线程
if (currActionModel.CN_N_ACTION_CODE == 10010)
{
lock (locker)
{
var request = MongoDBSingleton.Instance.FindAll();
if (request.Count > 0)
{
request.ForEach(a =>
{
try
{
if (a.actionNo == "1") SecondWeightActionOne(a);
}
catch (Exception ex)
{
CMMLog.Error($"10010" + ex.Message, ex);
}
});
}
}
Thread.Sleep(3000);
}
#endregion
#region 10012-荆门变更
if (currActionModel.CN_N_ACTION_CODE == 10012)
{
PLCControl.changeSignal();
}
#endregion
#region 10013-ERP变更功能
if (currActionModel.CN_N_ACTION_CODE == 10013)
{
ERPService.SendERPTaskCompleteFunc();
}
#endregion
#region 10017-给电梯发送心跳指令
if (currActionModel.CN_N_ACTION_CODE == 10018)
{
lock (lock10017)
{
PLCControl.SendHeartBeat();
}
}
#endregion
#region 四钴车间
#region 10014-读取包装线信号-3楼包装取料--开发中
if (currActionModel.CN_N_ACTION_CODE == 10014)
{
lock (lock10014)
{
var list = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.deviceType == "21").ToList();
if (list.Count > 0)
{
list.ForEach(a =>
{
// 包装机启动时读取【翻页通道】,根据页号获取五条数据,然后写入包装机
//ERPService.WriteItemInfoTwo(a);
PLCControl.CheckPackingMachineTetracobalt(a);
});
}
Thread.Sleep(1500);
}
}
#endregion
#region 10015-ERP变更功能 将物料表数据插入中间表
if (currActionModel.CN_N_ACTION_CODE == 10015)
{
lock (lock10015)
{
//ERPService.insertMidTable();
}
}
#endregion
#region 10016-打包线下线-3楼打包下线--已完成(待测试)
//四钴车间打包下线物料信息需要从下线口获取
if (currActionModel.CN_N_ACTION_CODE == 10016)
{
var plc = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.deviceType == "26").FirstOrDefault();
if (plc != null)
{
if (CheckStartFree(plc.location))
{
//PLCControl.CheckPackingLineFullTwo(plc);
PLCControl.CheckPackingLineFullThree(plc);
}
}
//Thread.Sleep(500);
}
#endregion
#endregion
return bResult;
}
///
/// 触发回调处理
///
/// 当前动作模型
///
public bool EventCallbacks(TN_I_TASK_DTL_ACTION currActionModel) {
bool bResult = true;
//测试码检测
if (currActionModel.CN_N_ACTION_CODE == 99999) {
}
return bResult;
}
private DateTime GetTime(int hour, int min = 0,int second = 0) {
var now = DateTime.Today;
var dt = new DateTime(now.Year, now.Month, now.Day, hour, min, second);
Console.WriteLine(dt.ToString());
return dt;
}
public class Mongo_AGV_TASK_STATE
{
public ObjectId _id { get; set; }
public int State { get; set; }
public string TaskNo { get; set; }
public string ForkliftNo { get; set; }
public string LockNo { get; set; }
///
/// 扩展参数 可接收任何变量目前对应ext3
///
public string ExtData { get; set; }
}
}
}