using BPASmartClient.AGV;
using BPASmartClient.AGV.Enums;
using BPASmartClient.AGV.Feedback;
using BPASmartClient.CustomResource.Pages.Model;
using BPASmartClient.Helper;
using BPASmartClient.HubHelper;
using FryPot_DosingSystem.Model;
using FryPot_DosingSystem.ViewModel;
using Newtonsoft.Json;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows;
using FryPot_DosingSystem.AGV;
using FryPot_DosingSystem.FryPotStatus;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Diagnostics;
namespace FryPot_DosingSystem.Control
{
internal class DosingLogicControl
{
public static DosingLogicControl _instance;
public static DosingLogicControl GetInstance => _instance ??= new DosingLogicControl();
///
/// 滚筒线PLC数据
///
public ConcurrentDictionary PlcReadData = new ConcurrentDictionary();
public ConcurrentDictionary FryOneData = new ConcurrentDictionary();
public ConcurrentDictionary FryTwoData = new ConcurrentDictionary();
public ConcurrentDictionary FryThreeData = new ConcurrentDictionary();
public ConcurrentDictionary FryFourData = new ConcurrentDictionary();
public ConcurrentDictionary FryFiveData = new ConcurrentDictionary();
///
/// 线体1配方队列
///
public ConcurrentQueue RecipeQuene = new ConcurrentQueue();
///
/// 线体2配方队列
///
public ConcurrentQueue LTwoRecipeQuene = new ConcurrentQueue();
///
/// 线体3配方队列
///
public ConcurrentQueue LThreeRecipeQuene = new ConcurrentQueue();
///
/// 线体1进料原料队列
///
public ConcurrentQueue InputMaterialQuene = new ConcurrentQueue();
///
/// 线体2进料原料队列
///
public ConcurrentQueue LTwoInputMaterialQuene = new ConcurrentQueue();
///
/// 线体3进料原料队列
///
public ConcurrentQueue LThreeInputMaterialQuene = new ConcurrentQueue();
///
/// 线体1出料原料队列
///
public ConcurrentQueue OutputMaterialQuene = new ConcurrentQueue();
///
/// 线体2出料原料队列
///
public ConcurrentQueue LTwoOutputMaterialQuene = new ConcurrentQueue();
///
/// 线体3出料原料队列
///
public ConcurrentQueue LThreeOutputMaterialQuene = new ConcurrentQueue();
///
/// 全局变量对象声明
///
GlobalVariable globalVar;
///
/// 线体状态对象声明
///
HardWareStatusViewModel hardWareStatusModel;
/////
///// 炒锅1状态
/////
//PotOneStatus fryOne;
/////
///// 炒锅2状态
/////
//PotTwoStatus fryTwo;
/////
///// 炒锅3状态
/////
//PotThreeStatus fryThree;
/////
///// 炒锅4状态
/////
//PotFourStatus fryFour;
/////
///// 炒锅5状态
/////
//PotFiveStatus fryFive;
///
/// 报警信息对象声明
///
#region 上位机内部变量
int FryPotAlarm = 0;//炒锅1滚筒故障信号 1:无故障 -1:故障
int FryPotTwoAlarm = 0;//炒锅2滚筒故障信号 1:无故障 -1:故障
int FryPotThreeAlarm = 0;//炒锅3滚筒故障信号 1:无故障 -1:故障
int FryPotFourAlarm = 0;//炒锅4滚筒故障信号 1:无故障 -1:故障
int FryPotFiveAlarm = 0;//炒锅5滚筒故障信号 1:无故障 -1:故障
int ReicpeNum = 0;//记录接收到的配方数
bool ErrorRecipe;//线体1错误配方标识
bool LTwoErrorRecipe;//线体2错误配方标识
bool LThreeErrorRecipe;//线体3错误配方标识
string fryOneRecipe = string.Empty;
string fryTwoRecipe = string.Empty;
string fryThreeRecipe = string.Empty;
string fryFourRecipe = string.Empty;
string fryFiveRecipe = string.Empty;
#endregion
#region agv临时变量
//bool agvArriveUpLoad = false;//agv是否到达线体1上料位置
//bool agvArriveLTwoUpLoad = false;//agv是否到达线体2上料位置
//bool agvArriveLThreeUpLoad = false;//agv是否到达线体3上料位置
//bool agvArriveUnLoad = false;//线体1 agv是否到达炒锅送料位置
//bool LTwoagvArriveUnLoad = false;//线体2 agv是否到达炒锅送料位置
//bool LThreeagvArriveUnLoad = false;//线体3 agv是否到达炒锅送料位置
//bool agvFryPotEmptyRollerArrive = false;// 线体1的agv是否拿到炒锅空桶
//bool LTwoagvFryPotEmptyRollerArrive = false; //线体2的agv是否拿到炒锅空桶
//bool LThreeagvFryPotEmptyRollerArrive = false;//线体3的agv是否拿到炒锅空桶
string LOnerobotJobId = string.Empty;//线体1当前上游系统任务号,全局唯一 从线体1到炒锅路径
string LTworobotJobId = string.Empty;//线体2当前上游系统任务号,全局唯一 从线体2到炒锅路径
string LThreerobotJobId = string.Empty;//线体3当前上游系统任务号,全局唯一 从线体3到炒锅路径
string LFourrobotJobId = string.Empty;//线体4当前上游系统任务号,全局唯一 从炒锅1、4到线体4路径
string LFiverobotJobId = string.Empty; //从炒锅2、5到线体4路径
string LSixrobotJobId = string.Empty; //从炒锅3到线体4路径
List LSevenrobotJobId = new List(); //从线体1到清洗台路径
List LEightrobotJobId = new List();//从线体2到清洗台路径
List LNinerobotJobId = new List(); //从线体3到清洗台路径
List LTenrobotJobId = new List(); //从清洗台到线体4路径
Dictionary agvCode = new Dictionary() { { "1", 1 }, { "2", 2 }, { "3", 3 }, { "4", 4 } };//agv小车的编号对AGV动画小车编号
public int CleanNum = 0;//当前清洗台桶数
#endregion
///
/// 调试命令注册
///
public void CommandRegist()
{
#region 线体空桶清洗及回收调试
ActionManage.GetInstance.Register(new Action(() =>
{
globalVar.agvArriveLineOneLoadEmptyRoller = true;
}), "AgvArriveLineOneEmptyRollerLoc");
ActionManage.GetInstance.Register(new Action(() =>
{
globalVar.agvArriveLineTwoLoadEmptyRoller = true;
}), "AgvArriveLineTwoEmptyRollerLoc");
ActionManage.GetInstance.Register(new Action(() =>
{
globalVar.agvArriveLineThreeLoadEmptyRoller = true;
}), "AgvArriveLineThreeEmptyRollerLoc");
ActionManage.GetInstance.Register(new Action(() =>
{
globalVar.CleanComplete = 1;
}), "CleanPlateCallAgv");
ActionManage.GetInstance.Register(new Action(() =>
{
globalVar.rollerLineOne.agvArriveCleanUnLoad = true;//清洗台空桶下料就位
globalVar.rollerLineTwo.agvArriveCleanUnLoad = true;
globalVar.rollerLineThree.agvArriveCleanUnLoad = true;
}), "AgvArriveCleanPlateLoc");
ActionManage.GetInstance.Register(new Action(() =>
{
globalVar.agvArriveCleanLoad = true;//清洗台空桶上料就位
}), "AgvArriveCleanPlateLocLoad");
ActionManage.GetInstance.Register(new Action(() =>
{
globalVar.agvArriveLineFour = true;
globalVar.rollerLineOne.agvArriveLineFour = true;
globalVar.rollerLineTwo.agvArriveLineFour = true;
globalVar.rollerLineThree.agvArriveLineFour = true;
}), "AgvArriveLineFourLoc");
ActionManage.GetInstance.Register(new Action(() =>
{
LineOneEmptyRollerCleanTaskRestart();
}), "EmptyRollerCleanTaskRestart");
ActionManage.GetInstance.Register(new Action(() =>
{
LineTwoEmptyRollerCleanTaskRestart();
}), "LineTwoEmptyRollerCleanTaskRestart");
ActionManage.GetInstance.Register(new Action(() =>
{
LineThreeEmptyRollerCleanTaskRestart();
}), "LineThreeEmptyRollerCleanTaskRestart");
#endregion
//接口调试
ActionManage.GetInstance.Register(new Action(() =>
{
string id = Guid.NewGuid().ToString();
string errorCode = AGVHelper._Instance.AgvToLineOneLoadRoller(id);
if (errorCode == "SUCCESS")
{
MessageLog.GetInstance.ShowRunLog($"AGV任务下发成功");
}
else if (errorCode == "Analysis Error")
{
MessageLog.GetInstance.ShowRunLog($"提示:AGV 调用API失败,请检查请求报文");
}
else
{
MessageLog.GetInstance.ShowRunLog($"提示:AGV任务下发失败,错误码:{errorCode}");
}
}), "AgvDebug");
ActionManage.GetInstance.Register(() =>
{
MessageLog.GetInstance.ShowRunLog("清洗台卸桶完成");
}, "AgvArriveCleanPlateLocCom");
ActionManage.GetInstance.Register(() =>
{
MessageLog.GetInstance.ShowRunLog("清洗台空桶在【4】号线卸桶完成");
MessageLog.GetInstance.ShowRunLog("【1】号线炒锅空桶在【4】号线卸桶完成");
MessageLog.GetInstance.ShowRunLog("【2】号线炒锅空桶在【4】号线卸桶完成");
MessageLog.GetInstance.ShowRunLog("【3】号线炒锅空桶在【4】号线卸桶完成");
}, "AgvArriveLineFourLocCom");
ActionManage.GetInstance.Register(() =>
{
MessageLog.GetInstance.ShowRunLog("清洗台空桶装载完成");
}, "AgvArriveCleanPlateLocLoadCom");
ActionManage.GetInstance.Register(() =>
{
globalVar.rollerLineOne.RecipeCompleteSingle = 1;
}, "LineOneRecipeComSignal");
ActionManage.GetInstance.Register(() =>
{
globalVar.rollerLineTwo.RecipeCompleteSingle = 1;
}, "LineTwoRecipeComSignal");
ActionManage.GetInstance.Register(() =>
{
globalVar.rollerLineThree.RecipeCompleteSingle = 1;
}, "LineThreeRecipeComSignal");
ActionManage.GetInstance.Register(new Action(() =>
{
globalVar.agvArriveLineOneLoadCom = true;
}), "AgvLineOneLoadEmptyCom");
ActionManage.GetInstance.Register(new Action(() =>
{
globalVar.agvArriveLineTwoLoadCom = true;
}), "AgvLineTwoLoadEmptyCom");
ActionManage.GetInstance.Register(new Action(() =>
{
globalVar.agvArriveLineThreeLoadCom = true;
}), "AgvLineThreeLoadEmptyCom");
ActionManage.GetInstance.Register(new Action(() =>
{
globalVar.rollerLineOne.CanRun = true;
}), "AGVLineOneLoadCom");
ActionManage.GetInstance.Register(new Action(() =>
{
globalVar.rollerLineTwo.CanRun = true;
}), "AGVLineTwoLoadCom");
ActionManage.GetInstance.Register(new Action(() =>
{
globalVar.rollerLineThree.CanRun = true;
}), "AGVLineThreeLoadCom");
ActionManage.GetInstance.Register(new Action(() =>
{
globalVar.rollerLineOne.OutMaterialingSingle = 1;
}), "RollerLineRunning");
ActionManage.GetInstance.Register(new Action