using BPASmartClient.AGV;
using BPASmartClient.AGV.Enums;
using BPASmartClient.AGV.Feedback;
using BPASmartClient.CustomResource.Pages.Model;
using BPA.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;
using BPASmartClient.CustomResource.UserControls.MessageShow;
using BPASmartClient.CustomResource.UserControls;
using Renci.SshNet.Messages;
namespace FryPot_DosingSystem.Control
{
internal class DosingLogicControl
{
public static DosingLogicControl _instance;
public static DosingLogicControl GetInstance => _instance ??= new DosingLogicControl();
public ushort[] RollerNum = new ushort[] { 101, 102, 103, 104, 105, 106, 107, 108, 201, 202, 203, 204, 205, 206, 207, 208, 301, 302, 303, 304, 305, 306, 307, 308, 401, 402, 403, 404, 405, 406, 407, 408, 501, 502, 503, 504, 505, 506, 507, 508 };
///
/// 桶净重
/// r
public ushort RollerNetweight = 19;//19公斤
///
/// 滚筒线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() { { "ZT11-21", 1 }, { "ZT11-22", 2 }, { "ZT11-23", 3 }, { "ZT11-24", 4 } };//agv小车的编号对AGV动画小车编号
private static readonly object Obj_Lock = new object();//3条滚筒线洗桶线程互斥锁
private static readonly object Pot_Lock = new object();//炒股工艺工艺下发锁
public int CleanNum = 0;//当前清洗台桶数
ushort LOneCurrentRollerNum = 0;//当前线体1的空桶桶号
ushort LTwoCurrentRollerNum = 0;//当前线体2的空桶桶号
ushort LThreeCurrentRollerNum = 0;//当前线体3的空桶桶号
public NewRecipeModel potOneCurrentRecipe = null;
public NewRecipeModel potTwoCurrentRecipe = null;
public NewRecipeModel potThreeCurrentRecipe = null;
public NewRecipeModel potFourCurrentRecipe = null;
public NewRecipeModel potFiveCurrentRecipe = null;
#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");
#endregion
//接口调试
//ActionManage.GetInstance.Register(new Action(() =>
//{
// string id = Guid.NewGuid().ToString("D");
// string errorCode = AGVHelper.GetInstance.AgvToLineOneLoadRoller(id);
// if (errorCode == "SUCCESS")
// {
// MessageNotify.GetInstance.ShowRunLog($"AGV任务下发成功");
// }
// else if (errorCode == "Analysis Error")
// {
// MessageNotify.GetInstance.ShowRunLog($"提示:AGV 调用API失败,请检查请求报文");
// }
// else
// {
// MessageNotify.GetInstance.ShowRunLog($"提示:AGV任务下发失败,错误码:{errorCode}");
// }
//}), "AgvDebug");
//ActionManage.GetInstance.Register(() =>
//{
// MessageNotify.GetInstance.ShowRunLog("清洗台卸桶完成");
//}, "AgvArriveCleanPlateLocCom");
//ActionManage.GetInstance.Register(() =>
//{
// MessageNotify.GetInstance.ShowRunLog("清洗台空桶在【4】号线卸桶完成");
// MessageNotify.GetInstance.ShowRunLog("【1】号线炒锅空桶在【4】号线卸桶完成");
// MessageNotify.GetInstance.ShowRunLog("【2】号线炒锅空桶在【4】号线卸桶完成");
// MessageNotify.GetInstance.ShowRunLog("【3】号线炒锅空桶在【4】号线卸桶完成");
//}, "AgvArriveLineFourLocCom");
//ActionManage.GetInstance.Register(() =>
//{
// MessageNotify.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