#define test
using BPA.Message;
using HBLConsole.Communication;
using HBLConsole.Factory;
using HBLConsole.Interface;
using HBLConsole.Model;
using HBLConsole.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using BPA.Message.Enum;
namespace HBLConsole.Business.Devices
{
public class MORKS : IBusiness
{
private volatile static MORKS _Instance;
public static MORKS GetInstance => _Instance ?? (_Instance = new MORKS());
private MORKS() { }
public GVL.MORKS mORKS { get; set; } = new GVL.MORKS();
///
/// 写入配方数据到 PLC
///
private void WriteRecipeBoms()
{
List recipeBoms = new List();
foreach (var item in Json.GetInstance.Base.recipeBoms.RecipeIds)
{
foreach (var rec in item.Recipes)
{
recipeBoms.Add((ushort)rec);
}
}
if (ModbusTcpHelper.GetInstance.Write(1100, WriteType.HoldingRegisters, recipeBoms.ToArray()))
{
MessageLog.GetInstance.Show("成功写入配方数据");
}
}
public void Init()
{
//Modbus TCP连接成功
ActionManagerment.GetInstance.Register(new Action(() =>
{
WriteRecipeBoms();
ReadPlcData();
DataParse();
Main();
//ResetProgram();
}), "ConnectOk");
//获取物料信息
SimpleFactory.GetInstance.GetBatchingInfo();
//Modbus Tcp 连接
ModbusTcpHelper.GetInstance.ModbusTcpConnect("192.168.1.11", 508);
//模拟订单
SimOrder();
Test();
Heartbeat();
MessageLog.GetInstance.Show("MORKS 设备初始化完成");
}
List batchingInfos = new List();
private void Heartbeat()
{
HeartbeatReport.GetInstance.GetMessage = new Action(() =>
{
HeartbeatReport.GetInstance.deviceStatus.BatchingInfo = batchingInfos;
#if test
HeartbeatReport.GetInstance.deviceStatus.Healthy = DeviceHealthy.UnHealth;
#else
HeartbeatReport.GetInstance.deviceStatus.Healthy = AllowRun ? DeviceHealthy.UnHealth : DeviceHealthy.Health;
#endif
});
}
///
/// 测试程序
///
private void Test()
{
#if test
ThreadManagerment.GetInstance.StartLong(new Action(() =>
{
while (Json.GetInstance.Base.morkOrderPushes.Count > 0)
{
var result = Json.GetInstance.Base.morkOrderPushes.ElementAt(0);
SimpleFactory.GetInstance.OrderChanged(result.OrderPush.SuborderId, ORDER_STATUS.COOKING);
MessageLog.GetInstance.Show($"{result.OrderPush.GoodsName},{ORDER_STATUS.COOKING}");
Thread.Sleep(5000);
SimpleFactory.GetInstance.OrderChanged(result.OrderPush.SuborderId, ORDER_STATUS.COMPLETED_COOK);
MessageLog.GetInstance.Show($"{result.OrderPush.GoodsName},{ORDER_STATUS.COMPLETED_COOK}");
Thread.Sleep(5000);
SimpleFactory.GetInstance.OrderChanged(result.OrderPush.SuborderId, ORDER_STATUS.COMPLETED_TAKE);
MessageLog.GetInstance.Show($"{result.OrderPush.GoodsName},{ORDER_STATUS.COMPLETED_TAKE}");
Thread.Sleep(5000);
Json.GetInstance.Base.morkOrderPushes.RemoveAt(0);
}
Thread.Sleep(500);
}), "test");
#endif
}
private void Readbool(ushort startAddress, ushort len, Action action)
{
object result;
result = ModbusTcpHelper.GetInstance.Read(startAddress, ReadType.Coils, len);
if (result != null)
{
if (result is bool[] bools)
{
if (bools.Length == len)
{
action(bools);
}
}
}
}
///
/// 复位程序
///
private void ResetProgram()
{
ThreadManagerment.GetInstance.StartLong(new Action(() =>
{
if (RTrig.GetInstance("ResetProgram").Start(mORKS.DeviceIniting))
{
ThreadManagerment.GetInstance.StopTask("MainTask", new Action(() =>
{
AllowRun = false;
TakeBowlId = string.Empty;
IngredientsCompleteId = string.Empty;
CookNodelId = new string[6] { string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, };
RobotTaskInterlock = false;
OutMealId = string.Empty;
TakeBowlInterlock = false;
TakeNoodleInterlock = false;
OutNoodleing = false;
Main();
}));
}
Thread.Sleep(10);
}), "ResetProgram");
}
///
/// 数据读取
///
private void ReadPlcData()
{
ThreadManagerment.GetInstance.StartLong(new Action(() =>
{
Readbool(323, 3, new Action((o) =>
{
mORKS.RobotTakeNoodle = o[0];
mORKS.RobotOutMeal = o[1];
mORKS.MoveTurntable = o[2];
}));
Readbool(1120, 16, new Action((bools) =>
{
mORKS.InitComplete = bools[0];
mORKS.TakeBowlIdle = bools[1];
mORKS.TemperatureReached = bools[2];
mORKS.AllowFallNoodle = bools[3];
mORKS.RbTakeNoodleComplete = bools[4];
mORKS.RbFallNoodleComplete = bools[5];
mORKS.RbOutMealComplete = bools[6];
mORKS.RobotIdle = bools[7];
mORKS.TakeMealDetect = bools[8];
mORKS.MissingBowl = bools[9];
mORKS.DeviceIniting = bools[10];
mORKS.TurntableLowerLimit = bools[11];
mORKS.MissingBowlSignal2 = bools[12];
mORKS.TurntableUpLimit = bools[13];
mORKS.TurntableMoveInPlace = bools[15];
}));
Readbool(1136, 6, new Action((bools) =>
{
for (int i = 0; i < 6; i++)
{
mORKS.NoodleCookerStatus[i] = bools[i];
}
}));
Readbool(1144, 6, new Action((bools) =>
{
for (int i = 0; i < 6; i++)
{
mORKS.CookNoodlesComplete[i] = bools[i];
}
}));
Thread.Sleep(500);
}), "ReadPLCData");
}
private void SimOrder()
{
ActionManagerment.GetInstance.Register(new Action(() =>
{
string subId = Guid.NewGuid().ToString();
ushort NoodleLoc = 1;//(ushort)(new Random().Next(1, 5));
ushort BolwLoc = (ushort)(new Random().Next(10, 11));
mORKS.RBTakeNoodleTask.Enqueue(new GVL.OrderLocInfo() { Loc = NoodleLoc, SuborderId = subId });
mORKS.TakeBowlTask.Enqueue(new GVL.OrderLocInfo() { Loc = BolwLoc, SuborderId = subId });
MessageLog.GetInstance.Show($"添加订单:面条位置【{NoodleLoc}】,碗位置【{BolwLoc}】");
}), "SimOrder");
}
///
/// 数据解析
///
private void DataParse()
{
ActionManagerment.GetInstance.Register(new Action