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.Linq;
using System.Threading;
using static Hanhe.iWCS.IndonesiaGLMProtocol.PLCControl;
using static Hanhe.iWCS.IndonesiaGLMProtocol.ProcessHelper;
namespace Hanhe.iWCS.IndonesiaGLMProtocol
{
///
/// 事件监听处理和触发事件回调
///
public class EnentListen : IEnentListen
{
private EquipmentCommandEQBLL CommandEQBLL = new EquipmentCommandEQBLL();
private ICallTaskProcessing iCallTask = new ICallTaskProcessing();
static object locker = new object();
static object lock10018 = 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)
{
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(3000);
}
#endregion
#region 3楼设备任务
#region 10001-读取包装线信号-3楼包装取料--已完成(待测试)
if (currActionModel.CN_N_ACTION_CODE == 10001)
{
var list = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.deviceType == "1").ToList();
if (list.Count > 0)
{
list.ForEach(a =>
{
CMMLog.Debug($"当前包装机号:{a.location},当前时间:{DateTime.Now},当前ip:{a.ip},当前端口:{a.port}");
PLCControl.CheckPackingMachine(a);
});
}
Thread.Sleep(3000);
}
#endregion
#region 10002-读取叠包机信号(三楼双层缓存架入叠盘机流程)-3楼缓存架入叠托--已完成(待测试)
if (currActionModel.CN_N_ACTION_CODE == 10002)
{
var list = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.deviceType == "3").ToList();
if (list.Count > 0)
{
list.ForEach(a =>
{
PLCControl.CacheStackingMouth(a);
Thread.Sleep(1000);
});
}
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 == "1").ToList();
if (list.Count > 0)
{
list.ForEach(a =>
{
PLCControl.PickUpBlank(a);
Thread.Sleep(1000);
});
}
Thread.Sleep(3000);
}
#endregion
#region 10004-打包线空托补料请求(3楼拆盘机补空托)-3楼拆盘补空--已完成(待测试)
if (currActionModel.CN_N_ACTION_CODE == 10004)
{
var plc = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.deviceType == "6").FirstOrDefault();
if (plc != null)
{
if (PickUpEndFree(plc.location))
{
PLCControl.CheckPackingLineEmpty(plc);
}
}
Thread.Sleep(3000);
}
#endregion
#region 10005-叠盘机满托下线(3楼叠盘下线)-3楼叠盘下线--已完成(待测试)
if (currActionModel.CN_N_ACTION_CODE == 10005)
{
var plc = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.deviceType == "15").FirstOrDefault();
if (plc != null)
{
if (ProcessHelper.PickUpStartFree(plc.location))
{
PLCControl.StackingLineEmpty(plc);
}
}
Thread.Sleep(3000);
}
#endregion
#region 10006-打包线下线-3楼打包下线--已完成(待测试)
if (currActionModel.CN_N_ACTION_CODE == 10006)
{
var plc = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.deviceType == "4").FirstOrDefault();
if (plc != null)
{
if (CheckStartFree(plc.location))
{
PLCControl.CheckPackingLineFull1(plc);
}
}
}
#endregion
#region 10007-缓存位入入料平台
if (currActionModel.CN_N_ACTION_CODE == 10007)
{
var plc = Settings.GetPlcInfo().Where(a => a.deviceType == "18" && a.enable == 1).ToList();
if(plc.Count > 0)
{
foreach(var a in plc)
{
PLCControl.InInlet(a);
}
}
Thread.Sleep(3000);
}
#endregion
#endregion
#region 2000-充电任务--无需改动
if (currActionModel.CN_N_ACTION_CODE == 2000)
{
ProcessHelper.AGVBatteryTime();
//Thread.Sleep(500);
}
#endregion
#region 2001-包装机写电量--已完成(待测试)
if (currActionModel.CN_N_ACTION_CODE == 2001)
{
PLCControl.WriteBattery();
Thread.Sleep(3000);
}
#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
{
CMMLog.Info("复称入缓存架任务,当前数据:"+ JsonConvert.SerializeObject(a));
if (a.actionNo == "1") SecondWeightActionOne(a);
}
catch (Exception ex)
{
CMMLog.Error($"10010" + ex.Message, ex);
}
});
}
}
Thread.Sleep(3000);
}
#endregion
#region 10011-设备合格率计算
if (currActionModel.CN_N_ACTION_CODE == 10011)
{
PLCControl.machine();
}
#endregion
#region 10012-荆门变更
if (currActionModel.CN_N_ACTION_CODE == 10012)
{
PLCControl.changeSignal();
Thread.Sleep(3000);
}
#endregion
#region 10013-删除变更中间表时间大于两周的任务
if (currActionModel.CN_N_ACTION_CODE == 10013)
{
PLCControl.deleteTableTask();
Thread.Sleep(30000);
}
#endregion
#region 10015-入库完成反馈ERP
if (currActionModel.CN_N_ACTION_CODE == 10014)
{
ERPService.SendERPTaskCompleteFunc();
}
#endregion
#region 10016-看板内容写入通道
//if (currActionModel.CN_N_ACTION_CODE == 10015)
//{
// PLCControl.writeQtt();
// Thread.Sleep(300000);
//}
#endregion
#region 10017-ERP变更功能 将物料表数据插入中间表 并写入plc
if (currActionModel.CN_N_ACTION_CODE == 10017)
{
lock (lock10018)
{
ERPService.insertMidTable();
}
}
#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; }
}
}
}