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() { }
GVL.MORKS mORKS = 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();
}), "ConnectOk");
//获取物料信息
SimpleFactory.GetInstance.GetBatchingInfo();
//Modbus Tcp 连接
ModbusTcpHelper.GetInstance.ModbusTcpConnect("127.0.0.1");
}
///
/// 数据读取
///
private void ReadPlcData()
{
ThreadManagerment.GetInstance.StartLong(new Action(() =>
{
object result;
result = ModbusTcpHelper.GetInstance.Read(1120, ReadType.Coils, 16);
if (result != null)
{
if (result is bool[] bools)
{
if (bools.Length == 16)
{
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.TurntableLowerLimit = bools[11];
}
}
}
//读取煮面栏状态
result = ModbusTcpHelper.GetInstance.Read(1136, ReadType.Coils, 6);
if (result != null)
{
if (result is bool[] bools)
{
if (bools.Length == 6)
{
for (int i = 0; i < 6; i++)
{
mORKS.NoodleCookerStatus[i] = bools[i];
}
}
}
}
//读取煮面炉完成信号
result = ModbusTcpHelper.GetInstance.Read(1144, ReadType.Coils, 6);
if (result != null)
{
if (result is bool[] bools)
{
if (bools.Length == 6)
{
for (int i = 0; i < 6; i++)
{
mORKS.CookNoodlesComplete[i] = bools[i];
}
}
}
}
Thread.Sleep(500);
}), "Read PLC Data");
}
///
/// 数据解析
///
private void DataParse()
{
ActionManagerment.GetInstance.Register(new Action