using BPA.Message;
using System;
using BPA.Message.Enum;
using BPASmartClient.Device;
using BPA.Helper;
using BPASmartClient.Model;
using BPASmartClient.Model.PLC;
using BPA.Models;
using BPASmartClient.Model.小炒机;
using BPASmartClient.MorkF.Model;
using System.Text;
using System.Collections.Concurrent;
using System.Windows.Documents;
using BPASmartClient.Business;
using Microsoft.CodeAnalysis;
using BPASmartClient.MorkF.ViewModel;
using BPASmartClient.ViewModel;
using System.Windows;
using static BPA.Helper.EventBus;
namespace BPASmartClient.MorkF
{
public class Control_MorkF : BaseDevice
{
#region 常数定义
///
/// while循环最大sleep次数
///
private const int sleepCount = 20;
///
/// while循环每次sleep时间
///
private const int sleepTime = 500;
///
/// 菜品库while循环最大sleep次数
///
private const int materialSleepCount = 600;
///
/// 菜品库while循环每次sleep时间
///
private const int materialSleepTime = 100;
///
/// 初始化炒锅数量
///
private int count = 2;
///
/// 菜品盒下移高度,相对于坐标系
///
//private const int materialBoxHigh = 4080;
////菜品库出餐高度
//private const int materialHigh = 33000;
////传感器到菜品库的高度(坐标系)
//private const int materialLaserHigh = 34500;
///
/// 菜品盒坐标系高度
///
private const int materialBoxHeight = 9700;
//传感器1到菜品库的高度(厘米)
//private const int materialLaserHighCM_1 = 22;
////传感器2到菜品库的高度(厘米)
//private const int materialLaserHighCM_2 = 27;
////传感器3到菜品库的高度(厘米)
//private const int materialLaserHighCM_3 = 31;
///
/// 传感器到隔板的距离(坐标系)
///
private const int clapboardLaserHigh = 10000;
////炒锅1倒菜的xy坐标
//private const int materialPutPositionXFry1 = 70000;
//private const int materialPutPositionYFry1 = 40000;
////炒锅2倒菜的xy坐标
//private const int materialPutPositionXFry2 = 70000;
//private const int materialPutPositionYFry2 = 40000;
//菜品库机械爪放置菜品距离
private const int materialToFryDistance = 10000;
//当前炒锅,默认为1号炒锅
private int fryIndex = 0;
#endregion
public override DeviceClientType DeviceType => DeviceClientType.MORKCS;
public AutoResetEvent minorReset = new AutoResetEvent(false);
public AutoResetEvent mainReset = new AutoResetEvent(false);
///
/// 等待炒制数量
///
private int StagingFryCount { get; set; }
///
/// 正在炒制数量
///
private int FryingCount { get; set; }
///
/// 炒锅编号与炒锅实例
///
Dictionary morkFs = new Dictionary();//全局对象声明
///
/// 菜品库对象
///
ML_MorkF ml_morkf = new ML_MorkF();
///
/// 小炒菜单集合
/// f
public static List LocalstirFryGoods = new List();
///
/// 待炒小炒队列
///
private ConcurrentQueue StirFryGoodsQuenes = new ConcurrentQueue();
///
/// 炒锅炒制线程名称
///
private const String striConst = "炒锅{0}炒制{1}线程";
///
/// 当前炒制菜品
///
private OrderLocInfo[] nowStirFryGood = new OrderLocInfo[2];
///
/// 待取菜队列
///
private ConcurrentQueue materialOperationQuenes = new ConcurrentQueue();
List resultorder = new List();//调试变量
///
/// 告警列表
///
private List alarmModel = new List();
///
///
///
private List statsModels = new List();
///
/// 暂存使用菜品量
///
private List stagingGoodBatchings = new List();
///
/// 是否为本地炒菜模式,默认为联网的
///
private bool isLocalFryingMode { get; set; } = true;
///
/// 入口
///
public override void DoMain()
{
IsHealth = true;
for (int i = 0; i < count; i++)
{
morkFs.Add(i, new GVL_MorkF());
}
DataParse();//数据解析
CommandRegist();//调试
ServerInit();
DeviceProcessLogShow("MORKF 设备初始化完成");
///初始化菜品库
//FoodLibInit();
Task2ReadMaterialData();
ScreenDataServer();
Json.Read();
//更新菜品余量
UpdateMaterialSurplus();
//读取坐标系
MaterialCoordinate materialCoordinate = ReadMaterialCoordinate();
if (materialCoordinate != null)
{
MaterialCoordinate.GetInstance().dicNoMaterialCoordinate = materialCoordinate.dicNoMaterialCoordinate;
MaterialCoordinate.GetInstance().MLCoordinateList = materialCoordinate.MLCoordinateList;
MaterialCoordinate.GetInstance().distance_1 = materialCoordinate.distance_1;
MaterialCoordinate.GetInstance().distance_2 = materialCoordinate.distance_2;
MaterialCoordinate.GetInstance().distance_3 = materialCoordinate.distance_3;
MaterialCoordinate.GetInstance().frying1 = materialCoordinate.frying1;
MaterialCoordinate.GetInstance().frying2 = materialCoordinate.frying2;
MaterialCoordinate.GetInstance().materialHeight = materialCoordinate.materialHeight;
MaterialCoordinate.GetInstance().MaterialOneX = materialCoordinate.MaterialOneX;
MaterialCoordinate.GetInstance().MaterialTwoX = materialCoordinate.MaterialTwoX;
MaterialCoordinate.GetInstance().MaterialThreeX = materialCoordinate.MaterialThreeX;
}
////菜品库编号与坐标系对象存储
//SaveMaterialCoordinate();
}
///
/// 主任务
///
public override void MainTask()
{
//炒锅炒制线程
MainProcessExcute();
//菜品库操作线程
MainProcessMaterial();
//MinorProcessExcute();
//SingleProcess();
}
private static object saveMaterialLock = new object();
private void SaveMaterialData()
{
lock (saveMaterialLock)
{
Json.Data = MaterialSurplusOperation.GetInstance().materialSurplus;
Json.Save();
}
}
private static object saveMaterialCoordinateLock = new object();
///
/// 保存当前的坐标系
///
public void SaveMaterialCoordinate()
{
lock (saveMaterialLock)
{
Json.Data = MaterialCoordinate.GetInstance();
Json.Save();
}
}
///
/// 读取坐标系
///
///
private MaterialCoordinate ReadMaterialCoordinate()
{
try
{
lock (saveMaterialLock)
{
Json.Read();
MaterialCoordinate materialCoordinate = Json.Data;
return materialCoordinate;
}
}
catch (Exception ex)
{
return null;
}
}
private void Task2ReadMaterialData()
{
TaskManage.GetInstance.StartLong(new Action(() =>
{
//获取定位到达状态
GetStatus("M10.0", new Action