using System;
using System.Collections.Generic;
using System.Text;
using WIDESEA_WCS.WCSClient;
using WIDESEA_Entity.DomainModels;
using WIDESEA_Services.Repositories;
using System.Linq;
using System.Threading;
using WIDESEA_Core.Utilities;
using System.Diagnostics;
using Quartz.Impl;
using WIDESEA_Common.CutomerModel;
using WIDESEA_Common;
using WIDESEA_Services.IRepositories;
using HslCommunication.WebSocket;
using HslCommunication;
using WIDESEA_Services;
using WIDESEA_Common.TaskEnum;
using WIDESEA_Core.EFDbContext;
using WIDESEA_Common.LogEnum;
using WIDESEA_Services.Services;
using WIDESEA_Core.Services;
using WIDESEA_Core.Enums;
namespace WIDESEA_WCS
{
public class WCSService
{
///
/// PLC连接集合
///
public static List Clients;
///
/// 调度中心
///
public static ISchedulerCenterServer Scheduler;
///
/// Job集合
///
public static List jobs = new List();
//WebServices推送监控数据
public static WebSocketServer webServer = new WebSocketServer();
///
/// 用来监控组盘工位的托盘RFID值
///
public static string RfidValue = "";
///
/// 当一个测量出库的任务穿梭车部分完成后,用此变量记录下该条任务的任务号
///
public static string MeasureTaskNumber = "";
//在入库工位处获取托盘码码,然后PDA定时获取,用于组盘,但是入库工位没有RFID读码器了,该函数暂时没用
public static WebResponseContent GetBoxingRfidValue()
{
RfidValue = "121212";
WebResponseContent content = new WebResponseContent();
content.OK(data: RfidValue);
return content;
}
///
/// 切换入库线体区的模式,正常模式和应急模式
///
///
///
public static WebResponseContent ChangeInboundLineModel(SaveModel saveModel)
{
WebResponseContent content = new WebResponseContent();
try
{
string modelValue = saveModel.MainData["model"].ToString();
PLCClient client = Clients.Find(r => r.PLCName == "LineDevice");
if (client.IsConnected)
{
int modelResult = int.Parse(client.ReadValue(CLineInfoDBName.R_System_Inline_Run_Model.ToString(), "System").ToString());
if (modelResult != 3)
return content.Error("当前非联机模式,不能切换模式.");
if (modelValue == "normal")
{
bool writeModelResult = client.WriteValue(CLineInfoDBName.W_System_Normal_Model.ToString(), 1);
bool writeModelResult1 = client.WriteValue(CLineInfoDBName.W_System_Emery_Model.ToString(), 0);
if (!writeModelResult || !writeModelResult1)
{
content.Error("切换到正常模式失败");
}
else
{
Thread.Sleep(666);
for (int i = 0; i < 5; i++)
{
bool logicResult = bool.Parse(client.ReadValue(CLineInfoDBName.R_System_Normal_Model.ToString(), "System").ToString());
if (logicResult)
return content.OK("切换到正常模式成功");
else
Thread.Sleep(88);
}
return content.Error("当前线体切换到正常模式失败");
}
}
else if (modelValue == "emerge")
{
bool writeModelResult = client.WriteValue(CLineInfoDBName.W_System_Emery_Model.ToString(), 1);
bool writeModelResult1 = client.WriteValue(CLineInfoDBName.W_System_Normal_Model.ToString(), 0);
if (!writeModelResult || !writeModelResult1)
content.Error("切换到应急模式失败");
else
{
Thread.Sleep(666);
for (int i = 0; i < 5; i++)
{
bool logicResult = bool.Parse(client.ReadValue(CLineInfoDBName.R_System_Emery_Model.ToString(), "System").ToString());
if (logicResult)
return content.OK("切换到应急模式成功");
else
Thread.Sleep(88);
}
return content.Error("当前线体切换到应急模式失败");
//bool logicResult = bool.Parse(client.ReadValue(CLineInfoDBName.R_System_Emery_Model.ToString(), "System").ToString());
////bool leftState = bool.Parse(client.ReadValue(CLineInfoDBName.R_System_LeftState.ToString(), "System").ToString());
////bool rightState = bool.Parse(client.ReadValue(CLineInfoDBName.R_System_RightState.ToString(), "System").ToString());
//if (logicResult)
//{
// content.OK("切换到应急模式成功");
//}
//else
// content.Error("当前线体不允许切换到应急模式");
}
}
}
else
{
content.Error("当前与PLC断开连接,请检查网络");
}
return content;
}
catch (Exception ex)
{
content.Error(ex.Message);
}
return content;
}
///
/// 获取入库线体当前模式状态
///
///
///
public static WebResponseContent GetInboundLineCurrentModel(SaveModel saveModel)
{
WebResponseContent content = new WebResponseContent();
Dictionary resultDic = new Dictionary();
try
{
PLCClient client = Clients.Find(r => r.PLCName == "LineDevice");
if (client.IsConnected)
{
bool modelResult1 = bool.Parse(client.ReadValue(CLineInfoDBName.R_System_Normal_Model.ToString(), "System").ToString());
if (modelResult1)
resultDic.Add("normal", "1");
else
resultDic.Add("normal", "0");
Thread.Sleep(300);
modelResult1 = bool.Parse(client.ReadValue(CLineInfoDBName.R_System_Emery_Model.ToString(), "System").ToString());
if (modelResult1)
{
resultDic.Add("emerge", "1");
//false是未停用
bool leftState = bool.Parse(client.ReadValue(CLineInfoDBName.R_System_LeftState.ToString(), "System").ToString());
if (!leftState)
{
resultDic.Add("hoisterState", "left");
}
else
{
bool rightState = bool.Parse(client.ReadValue(CLineInfoDBName.R_System_RightState.ToString(), "System").ToString());
if (!rightState)
resultDic.Add("hoisterState", "right");
else
resultDic.Add("hoisterState", "empty");
}
}
else
{
resultDic.Add("emerge", "0");
resultDic.Add("hoisterState", "empty");
}
Thread.Sleep(300);
int val = int.Parse(client.ReadValue(CLineInfoDBName.R_System_Inline_Run_Model.ToString(), "System").ToString());
if (val == 1)
resultDic.Add("controlModel", "手动控制");
else if (val == 2)
resultDic.Add("controlModel", "单机控制");
else if (val == 3)
resultDic.Add("controlModel", "联机控制");
else
resultDic.Add("controlModel", "未知状态");
}
else
{
content.Error("当前与PLC断开连接,请检查网络");
}
content.OK(data: resultDic);
}
catch (Exception ex)
{
content.Error(ex.Message);
}
return content;
}
///
/// 当校验托盘码不一致时,人工触发继续任务
///
///
///
public static WebResponseContent CheckBarcodeSameOk(SaveModel saveModel)
{
WebResponseContent content = new WebResponseContent();
Dictionary resultDic = new Dictionary();
try
{
PLCClient client = Clients.Find(r => r.PLCName == "LineDevice");
if (client.IsConnected)
{
string taskNumber = saveModel.MainData["taskNumber"].ToString();
string station = saveModel.MainData["station"].ToString();
string barcode = saveModel.MainData["systemRFID"].ToString();
if (string.IsNullOrEmpty(taskNumber) || string.IsNullOrEmpty(station) || string.IsNullOrEmpty(barcode))
{
content.Error("当前无需操作!");
return content;
}
Dt_TaskWCSinfo inWcsInfo = Dt_TaskWCSinfoRepository.Instance.FindFirst(x =>
x.wcstask_taskNumber == int.Parse(taskNumber) &&
x.wcstask_barcode == barcode);
if (inWcsInfo != null)
{
bool goOn = client.WriteValue(CLineInfoDBName.W_Line_RFID_Done.ToString(), station, 1);
if (goOn)
{
content.OK();
}
else
{
content.Error("给PLC下发继续任务指令失败,请重试");
}
}
else
{
content.Error($"系统检测没有检测到该工位:【{station}】对应的任务");
}
}
else
{
content.Error("当前与PLC断开连接,请检查网络");
}
}
catch (Exception ex)
{
content.Error(ex.Message);
}
return content;
}
///
/// 称重后,由人工继续任务
///
///
///
public static WebResponseContent WeightCheckOk(SaveModel saveModel)
{
WebResponseContent content = new WebResponseContent();
Dictionary resultDic = new Dictionary();
try
{
PLCClient client = Clients.Find(r => r.PLCName == "LineDevice");
if (client.IsConnected)
{
string barcode = saveModel.MainData["barcode"].ToString();
if (string.IsNullOrEmpty(barcode))
{
content.Error("当前无需操作!");
return content;
}
Dt_TaskWCSinfo inWcsInfo = Dt_TaskWCSinfoRepository.Instance.FindFirst(x =>
(x.wcstask_type == TaskType.TaskType_Empty_Pallet_Inbound.ToString() ||
x.wcstask_type == TaskType.TaskType_Box_Pallet_Inbound.ToString()) &&
x.wcstask_state == TaskState.TaskState_ConveyorLineExecuting.ToString() &&
x.wcstask_barcode == barcode);
if (inWcsInfo != null)
{
bool goOn = client.WriteValue(CLineInfoDBName.W_Line_Weight_OK.ToString(), "20201", 1);
if (goOn)
{
content.OK();
}
else
{
content.Error("给PLC下发继续任务指令失败,请重试");
}
}
else
{
content.Error($"系统检测没有检测到该工位:【20201】对应的任务");
}
}
else
{
content.Error("当前与PLC断开连接,请检查网络");
}
}
catch (Exception ex)
{
content.Error(ex.Message);
}
return content;
}
///
/// WCS接收RGV上报的任务状态
///
/// 任务信息
///
public static Dictionary WCSReceiveRGVData(Dictionary statusData)
{
WebResponseContent content = new WebResponseContent();
Dictionary resultDic = new Dictionary();
try
{
//任务编号
string taskId = statusData["taskId"]?.ToString();
//小车编号
string rgvId = statusData["rgvId"]?.ToString();
//任务状态
int taskStatus = int.Parse(statusData["status"].ToString());
LogRecord.WriteLog(LogEnum.RGV, $"接受到RGV上报的任务状态:任务号:{taskId},小车编号:{rgvId},任务状态:{taskStatus}");
DateTime now = DateTime.Now;
//在此根据上报的任务号,更新任务状态以及任务完成后的相关操作
Dt_TaskRGVinfo taskRGVinfo = Dt_TaskRGVinfoRepository.Instance.FindFirst(x => x.rgvtask_taskId == taskId);
if (taskRGVinfo == null)
{
resultDic.Add("Code", "0");
resultDic.Add("Message", "成功");
resultDic.Add("data ", null);
}
else
{
Dt_TaskWCSinfo taskWCSinfo = Dt_TaskWCSinfoRepository.Instance.FindFirst(x => x.wcstask_taskNumber == taskRGVinfo.rgvtask_wcsTaskNumber);
if (taskWCSinfo != null)
{
//RGV上报对应的任务正常完成
if (taskStatus == 1)
{
taskRGVinfo.rgvtask_backup_1 = rgvId;
content = CommonFunction.FinishedRgvTask(taskRGVinfo, rgvId);
if (content.Status)
{
if (taskRGVinfo.rgvtask_areaCode == "OutboundArea")
{
PLCClient client = WCSService.Clients.Find(r => r.PLCName == "LineDevice");
//写入AGV执行完成
Jobs.ConveyorLine.OutboundArea.OutboundAreaDispatch.WriteRGVState(client, true);
}
content.OK("RGV上报完成穿梭车任务成功");
}
else
content.Error($"RGV上报完成穿梭车任务出错:" + content.Message);
}//取放货完成
else if (taskStatus == 2)
{
taskRGVinfo.rgvtask_backup_2 = "2";
Dt_TaskRGVinfoRepository.Instance.Update(taskRGVinfo, r => r.rgvtask_backup_2, true);
content.OK("RGV上报取放货完成成功");
}
else
content.Error($"RGV上报了任务状态,但是状态不是1,表示非正常完成");
}
else
{
CommonFunction.AddRgvTaskToHistory(taskRGVinfo, Dt_TaskRGVinfoRepository.Instance, Dt_TaskRGVinfo_HtyRepository.Instance);
content.OK($"RGV上报了任务状态,但是在WCS任务表中没有找到对应的任务,任务号:【{taskId}】");
}
if (content.Status)
{
resultDic.Add("Code", "0");
resultDic.Add("Message", "成功");
resultDic.Add("data ", null);
}
else
{
resultDic.Add("Code", "-1");
resultDic.Add("Message", "失败");
resultDic.Add("data ", content.Message);
}
DateTime end = DateTime.Now;
// Console.Out.WriteLine($"状态:{taskStatus}耗时:{end - now}");
}
}
catch (Exception ex)
{
resultDic.Add("Code", "-1");
resultDic.Add("Message", "失败");
resultDic.Add("data ", ex.Message);
}
//Logger.AddLog(LoggerType.RGVUpload, statusData, resultDic, content);
return resultDic;
}
///
/// WCS查询提升机状态信息
///
/// 任务信息
///
public static Dictionary GetElevatorInfo()
{
Dictionary resultDic = new Dictionary();
try
{
//提升机层
PLCClient client = WCSService.Clients.Find(r => r.PLCName == "LineDevice");
string hoisterLayer = client.ReadValue(CLineInfoDBName.R_Line_Layer.ToString(), "90100").ToString();
resultDic.Add("code", "1");
resultDic.Add("layer", hoisterLayer);
resultDic.Add("status ", "提升机当前状态");
//Console.Out.WriteLine(DateTime.Now + "提升机当前状态" + hoisterLayer);
}
catch (Exception ex)
{
resultDic.Add("code", "0");
resultDic.Add("layer", "-1");
resultDic.Add("status ", "获取提升机状态失败" + ex.Message);
}
return resultDic;
}
}
}