245 lines
9.5 KiB
C#
245 lines
9.5 KiB
C#
using Cowain.Bake.BLL;
|
||
using Cowain.Bake.Common.Core;
|
||
using Cowain.Bake.Common.Enums;
|
||
using Cowain.Bake.Communication.Interface;
|
||
using Cowain.Bake.Model;
|
||
using Cowain.Bake.Model.Models;
|
||
using HslCommunication;
|
||
using System.Linq;
|
||
using Unity;
|
||
|
||
namespace Cowain.Bake.Main.Common
|
||
{
|
||
public class CmdFactories
|
||
{
|
||
IUnityContainer _unityContainer { get; set; }
|
||
//IPLCDevice agv = null;
|
||
public CmdFactories(IUnityContainer unityContainer)
|
||
{
|
||
_unityContainer = unityContainer;
|
||
}
|
||
public bool StartBaking(IPLCDevice plc, int stationId, bool[] bakeEanble, bool manual = false)
|
||
{
|
||
if (!Write<bool[]>(plc, EStoveSignal.HeatEnableStep.ToString(), stationId, 1, bakeEanble).IsSuccess)
|
||
{
|
||
LogHelper.Instance.Error($"层烘烤使能失败!plc:{plc.Name}", true);
|
||
return false;
|
||
}
|
||
|
||
if (!Write<bool>(plc, EStoveSignal.BakingStart.ToString(), stationId, 1, true).IsSuccess)
|
||
{
|
||
LogHelper.Instance.GetCurrentClassError($"下发开始烘烤失败!plc:{plc.Name}", true);
|
||
return false;
|
||
}
|
||
|
||
if (manual)
|
||
{
|
||
LogHelper.Instance.Info($"下发开始烘烤成功!", true);
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
//public bool EndBaking(int stationId)
|
||
//{
|
||
// TDeviceConfig conf = _unityContainer.Resolve<DeviceConfigService>().GetConfig(stationId);
|
||
// IPLCDevice plc = _unityContainer.Resolve<IPLCDevice>(conf.Name);
|
||
// if (plc == null || !plc.IsConnect)
|
||
// {
|
||
// LogHelper.Instance.GetCurrentClassError($"连接PLC失败!plc:{plc.Name}", true);
|
||
// return false;
|
||
// }
|
||
|
||
// if (!Write<bool>(plc, EStoveSignal.BakingEnd.ToString(), stationId, true).IsSuccess)
|
||
// {
|
||
// LogHelper.Instance.GetCurrentClassError($"下发结束烘烤失败!plc:{plc.Name}", true);
|
||
// return false;
|
||
// }
|
||
|
||
// LogHelper.Instance.Info($"下发结束烘烤成功!", true);
|
||
// return true;
|
||
//}
|
||
|
||
//复位结束烘烤
|
||
public bool ResetEndBaking(int machineId)
|
||
{
|
||
TDeviceConfig conf = _unityContainer.Resolve<DeviceConfigService>().GetConfig(machineId);
|
||
IPLCDevice plc = _unityContainer.Resolve<IPLCDevice>(conf.Name);
|
||
if (plc == null || !plc.IsConnect)
|
||
{
|
||
LogHelper.Instance.GetCurrentClassError($"连接PLC失败!plc:{plc.Name}", true);
|
||
return false;
|
||
}
|
||
|
||
if (!Write<bool>(plc, EStoveSignal.BakingMark.ToString(), machineId, 1, false).IsSuccess)
|
||
{
|
||
LogHelper.Instance.GetCurrentClassError($"下发复位结束烘烤失败!plc:{plc.Name}", true);
|
||
return false;
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
public bool OpenDoor(int stationId, int layer)
|
||
{
|
||
TDeviceConfig fromDev = _unityContainer.Resolve<DeviceConfigService>().GetConfig(stationId);
|
||
IPLCDevice plc = _unityContainer.Resolve<IPLCDevice>(fromDev.Name);
|
||
if (plc == null || !plc.IsConnect)
|
||
{
|
||
LogHelper.Instance.GetCurrentClassError($"连接PLC失败!plc:{plc.Name},layer:{layer}", true);
|
||
return false;
|
||
}
|
||
|
||
if (!CommonFun.Instance.IsStoveQualified(plc, stationId, layer, false, true)) //不在工作中
|
||
{
|
||
return false;
|
||
}
|
||
|
||
if (Write<bool>(plc, EStoveSignal.OpenDoor.ToString(), stationId,1, true).IsSuccess)
|
||
{
|
||
HandyControl.Controls.MessageBox.Show($@"下发开门指令成功!", "操作提示");
|
||
return false;
|
||
}
|
||
else
|
||
{
|
||
HandyControl.Controls.MessageBox.Error($@"下发开门指令失败。", "操作提示");
|
||
LogHelper.Instance.GetCurrentClassError($"下发开门指令失败!plc:{plc.Name},layer:{layer}");
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public bool CloseDoor(int machineId, int layer)
|
||
{
|
||
TDeviceConfig fromDev = _unityContainer.Resolve<DeviceConfigService>().GetConfig(machineId);
|
||
IPLCDevice plc = _unityContainer.Resolve<IPLCDevice>(fromDev.Name);
|
||
|
||
if (plc == null || !plc.IsConnect)
|
||
{
|
||
LogHelper.Instance.GetCurrentClassError($"连接PLC失败!plc:{plc.Name},layer:{layer}", true);
|
||
return false;
|
||
}
|
||
|
||
if (!CommonFun.Instance.IsStoveQualified(plc, machineId, layer, false, true)) //不在工作中
|
||
{
|
||
return false;
|
||
}
|
||
|
||
if (Write<bool>(plc, EStoveSignal.CloseDoor.ToString(), machineId, 1, true).IsSuccess)
|
||
{
|
||
HandyControl.Controls.MessageBox.Show($@"下发关门指令成功!", "操作提示");
|
||
return true;
|
||
}
|
||
else
|
||
{
|
||
HandyControl.Controls.MessageBox.Error($@"下发关门指令失败。", "操作提示");
|
||
LogHelper.Instance.GetCurrentClassError($"下发关门指令失败!plc:{plc.Name},layer:{layer}");
|
||
return false;
|
||
}
|
||
}
|
||
|
||
public bool InitStove(int stationId, int layer)
|
||
{
|
||
TDeviceConfig dev = _unityContainer.Resolve<DeviceConfigService>().GetConfig(stationId);
|
||
IPLCDevice plc = _unityContainer.Resolve<IPLCDevice>(dev.Name);
|
||
|
||
if (plc == null || !plc.IsConnect)
|
||
{
|
||
LogHelper.Instance.GetCurrentClassError($"连接PLC失败!plc:{plc.Name}");
|
||
return false;
|
||
}
|
||
|
||
if (!CommonFun.Instance.IsStoveQualified(plc, stationId, layer, false, true)) //不在工作中
|
||
{
|
||
return false;
|
||
}
|
||
|
||
if (Write<bool>(plc, EStoveSignal.Initial.ToString(), stationId, 1, true).IsSuccess)
|
||
{
|
||
HandyControl.Controls.MessageBox.Show($@"下发初始化请求指令成功!", "操作提示");
|
||
return true;
|
||
}
|
||
else
|
||
{
|
||
HandyControl.Controls.MessageBox.Error($@"下发初始化请求指令失败。", "操作提示");
|
||
LogHelper.Instance.GetCurrentClassError($"下发初始化请求指令失败!plc:{plc.Name}");
|
||
return false;
|
||
}
|
||
}
|
||
//public bool NotifyPLCHavePallet(int machineId, int layer, int number)
|
||
//{
|
||
// TDeviceConfig dev = _unityContainer.Resolve<DeviceConfigService>().GetConfig(machineId);
|
||
// IPLCDevice plc = _unityContainer.Resolve<IPLCDevice>(dev.Name);
|
||
// if (plc == null || !plc.IsConnect)
|
||
// {
|
||
// LogHelper.Instance.GetCurrentClassError($"连接PLC失败!plc:{plc.Name},layer:{layer}");
|
||
// return false;
|
||
// }
|
||
// if (!CommonFun.Instance.IsStoveQualified(plc, machineId, layer))
|
||
// {
|
||
// return false;
|
||
// }
|
||
// string Tray = number == 1 ? EStoveSignal.Tray1.ToString() : EStoveSignal.Tray2.ToString();
|
||
// if (Write<bool>(plc, Tray, machineId,layer,true).IsSuccess)
|
||
// {
|
||
// HandyControl.Controls.MessageBox.Show($@"下发托盘记忆成功!", "操作提示");
|
||
// return true;
|
||
// }
|
||
// else
|
||
// {
|
||
// HandyControl.Controls.MessageBox.Error($@"下发托盘记忆失败。", "操作提示");
|
||
// LogHelper.Instance.GetCurrentClassError($"下发托盘记忆失败!plc:{plc.Name},layer:{layer}");
|
||
// return false;
|
||
// }
|
||
//}
|
||
//public bool NotifyPLCDontHavePallet(int machineId, int layer, int number)
|
||
//{
|
||
// TDeviceConfig dev = _unityContainer.Resolve<DeviceConfigService>().GetConfig(machineId);
|
||
// IPLCDevice plc = _unityContainer.Resolve<IPLCDevice>(dev.Name);
|
||
// if (plc == null || !plc.IsConnect)
|
||
// {
|
||
// LogHelper.Instance.GetCurrentClassError($"连接PLC失败!plc:{plc.Name},layer:{layer}");
|
||
// return false;
|
||
// }
|
||
// if (!CommonFun.Instance.IsStoveQualified(plc, machineId, layer))
|
||
// {
|
||
// return false;
|
||
// }
|
||
// string Tray = number == 1 ? EStoveSignal.Tray1.ToString() : EStoveSignal.Tray2.ToString();
|
||
// if (Write<bool>(plc, Tray, machineId, layer, false).IsSuccess)
|
||
// {
|
||
// HandyControl.Controls.MessageBox.Show($@"下发托盘记忆成功!", "操作提示");
|
||
// return true;
|
||
// }
|
||
// else
|
||
// {
|
||
// HandyControl.Controls.MessageBox.Error($@"下发清除托盘记忆失败。", "操作提示");
|
||
// LogHelper.Instance.GetCurrentClassError($"下发清除托盘记忆失败!plc:{plc.Name},layer:{layer}");
|
||
// return false;
|
||
// }
|
||
//}
|
||
|
||
public OperateResult Write<T>(IPLCDevice plc, string paramName, int machineId, int layer, T data)
|
||
{
|
||
string nodeAddr = "";
|
||
OperateResult result = new OperateResult()
|
||
{
|
||
IsSuccess = false,
|
||
};
|
||
|
||
Variable node = null;
|
||
node = (from storage in plc.Storages
|
||
from item in storage.VariableList
|
||
where storage.StationId == machineId && item.ParamName == paramName && item.Number == layer
|
||
select item).FirstOrDefault();
|
||
|
||
if (null == node)
|
||
{
|
||
return result;
|
||
}
|
||
|
||
nodeAddr = $"{node.Address}{node.VarName}";
|
||
return plc.Write<T>(nodeAddr, data);
|
||
}
|
||
}
|
||
}
|