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 SqlSugar;
using System;
using System.Linq;
using System.Threading;
using static Hanhe.iWCS.JingmenGEMTwoProtocol.PLCControl;
using static Hanhe.iWCS.JingmenGEMTwoProtocol.ProcessHelper;
using static Hanhe.iWCS.JingmenGEMTwoProtocol.ProtocolAnalysis;
namespace Hanhe.iWCS.JingmenGEMTwoProtocol
{
///
/// 事件监听处理和触发事件回调
///
public class EnentListen : IEnentListen
{
private EquipmentCommandEQBLL CommandEQBLL = new EquipmentCommandEQBLL();
private ICallTaskProcessing iCallTask = new ICallTaskProcessing();
static object locker = new object();
static object lock10015 = 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 =>
{
// 包装机启动时读取【翻页通道】,根据页号获取五条数据,然后写入包装机
//ERPService.WriteItemInfo(a);
//ERPService.WriteEmpInfo(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 == "22" || a.deviceType == "")).ToList(); //变更注释
var list = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.deviceType == "22").ToList();
if (list.Count > 0)
{
list.ForEach(a =>
{
// 包装机启动时读取【翻页通道】,根据页号获取五条数据,然后写入包装机
//ERPService.WriteItemInfo(a);
//ERPService.WriteEmpInfo(a);
CMMLog.Debug($"二期当前包装机号:{a.location},当前时间:{DateTime.Now},当前ip:{a.ip},当前端口:{a.port}");
PLCControl.CheckPackingMachineTwo(a);
CMMLog.Debug($"二期包装机取料任务结束,当前时间:{DateTime.Now},当前ip:{a.ip},当前端口:{a.port}");
});
}
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 == "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").ToList();
if (list.Count > 0)
{
list.ForEach(a =>
{
PLCControl.CacheStackingMouth(a);
Thread.Sleep(1000);
});
}
Thread.Sleep(3000);
}
#endregion
#region 10004-读包装机信号(包装机补空托流程)-3楼包装补空--已完成(待测试)
if (currActionModel.CN_N_ACTION_CODE == 10004)
{
var list = Settings.GetPlcInfo().Where(a => a.enable == 1 && (a.deviceType == "1" || a.deviceType == "22")).ToList();
if (list.Count > 0)
{
list.ForEach(a =>
{
PLCControl.PickUpBlank(a);
Thread.Sleep(1000);
});
}
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 == "6").FirstOrDefault();
if (plc != null)
{
if (PickUpEndFree(plc.location))
{
PLCControl.CheckPackingLineEmpty(plc);
}
}
Thread.Sleep(3000);
}
#endregion
#region 10006-叠盘机满托下线(3楼叠盘下线)-3楼叠盘下线--已完成(待测试)
if (currActionModel.CN_N_ACTION_CODE == 10006)
{
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 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);
//}
//测试用,直接生成任务
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.CheckPackingLineFull1(plc);
}
}
//Thread.Sleep(500);
}
#endregion
#region 10008-接驳位入立库 辅材流程
if (currActionModel.CN_N_ACTION_CODE == 10008)
{
var conInfo = Settings.GetConnectingbitsList().Where(a => a.enable == 1).ToList();
if(conInfo != null)
{
foreach(var a in conInfo)
{
CMMLog.Info(JsonConvert.SerializeObject(a));
PLCControl.InWarehouse(a.locCode);
}
}
}
#endregion
#region 10009-缓存位入入料平台
if (currActionModel.CN_N_ACTION_CODE == 10009)
{
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 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();
ProcessHelper.AGVBatteryTimeTwo();
//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.deleteTableTask();
Thread.Sleep(30000);
}
#endregion
#region 10013-ERP变更功能 插入简单生产入库表
if (currActionModel.CN_N_ACTION_CODE == 10013)
{
//ERPService.SendERPTaskCompleteFunc();
ERPService.SendERPTaskCompleteFuncTwo(); //2025-1-7 变更 增加分录ID 更改erp入库表
}
#endregion
#region 10014-车间看板内容写入通道
if (currActionModel.CN_N_ACTION_CODE == 10014)
{
var cjWare = Settings.GetwareInfoList().Where(a => a.wareName == "车间" && a.enable == 1).FirstOrDefault();
if (cjWare != null)
{
PLCControl.writeCJQtt(cjWare);
}
else CMMLog.Info("车间看板信息未配置");
Thread.Sleep(3000);
}
#endregion
#region 10015-ERP变更功能 将物料表数据插入中间表
if (currActionModel.CN_N_ACTION_CODE == 10015)
{
lock (lock10015)
{
ERPService.insertMidTable();
}
}
#endregion
#region 10016-MES入库反馈
if (currActionModel.CN_N_ACTION_CODE == 10016)
{
ERPService.SendMESTaskCompleteFunc();
}
#endregion
#region 10021-erp简单生产入库表反馈失败,重新反馈
if (currActionModel.CN_N_ACTION_CODE == 10021)
{
ERPService.SendERPTaskCompleteFuncThree();
}
#endregion
#region 电钴
#region 10017-A口
if (currActionModel.CN_N_ACTION_CODE == 10017)
{
var plc = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.deviceType == "41").FirstOrDefault();
if (plc != null)
{
PLCControl.AMachine(plc);
}
}
#endregion
#region 10018-C口
if (currActionModel.CN_N_ACTION_CODE == 10018)
{
var list = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.deviceType == "42").ToList();
if (list.Count > 0)
{
list.ForEach(a =>
{
PLCControl.CMachine(a);
});
}
Thread.Sleep(3000);
}
#endregion
#region 10019-D口
if (currActionModel.CN_N_ACTION_CODE == 10019)
{
var list = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.deviceType == "43").ToList();
if (list.Count > 0)
{
list.ForEach(a =>
{
PLCControl.DMachine(a);
});
}
Thread.Sleep(3000);
}
#endregion
#region 10020-电钴接驳位入立库
if (currActionModel.CN_N_ACTION_CODE == 10020)
{
var conInfo = Settings.GetDGConnectingbitsList().Where(a => a.enable == 1).ToList();
if (conInfo != null)
{
foreach (var a in conInfo)
{
CMMLog.Info(JsonConvert.SerializeObject(a));
PLCControl.DGInWarehouse(a.locCode);
}
}
}
#endregion
#region 10022-反馈erp 插入简单生产入库表
if (currActionModel.CN_N_ACTION_CODE == 10022)
{
ERPService.SendERPTaskCompleteFuncDG();
}
#endregion
#region 生产退库
#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; }
}
}
}