|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
|
using System.Text;
|
|
|
using Mesnac.Action.Base;
|
|
|
using System.Drawing;
|
|
|
using Mesnac.Equips;
|
|
|
using Mesnac.Action.Feeding.FinishBatch;
|
|
|
|
|
|
namespace Mesnac.Action.Feeding.ProducingPlan
|
|
|
{
|
|
|
/// <summary>
|
|
|
/// 初始化PLC设备状态
|
|
|
/// </summary>
|
|
|
public class InitPlcEquipState : FeedingAction, IAction
|
|
|
{
|
|
|
private static bool _isFirstRun = true; //是否首次执行
|
|
|
private static int count = 0; //计数器
|
|
|
public void Run(RuntimeParameter runtime)
|
|
|
{
|
|
|
base.RunIni(runtime);
|
|
|
base.LogDebug("初始化PLC设备状态...");
|
|
|
if (InitPlcEquipState._isFirstRun)
|
|
|
{
|
|
|
string plcEquipName = base.GetConfigValue("PLC.EquipName", "FeedingPLC"); //获取PLC设备名称
|
|
|
if (Mesnac.Equips.Factory.Instance.AllEquips.ContainsKey(plcEquipName))
|
|
|
{
|
|
|
Mesnac.Equips.Factory.Instance.AllEquips[plcEquipName].EquipReadData += ReadData;
|
|
|
}
|
|
|
InitPlcEquipState._isFirstRun = false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private void ReadData(object sender, Mesnac.Equips.ReadEventArgs e)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
count++;
|
|
|
if (count % 5 == 0) //设备读取5次刷新一次
|
|
|
{
|
|
|
List<Mesnac.Controls.Default.MCButton> buttons = this.GetTControls<Mesnac.Controls.Default.MCButton>();
|
|
|
foreach (Mesnac.Controls.Default.MCButton button in buttons)
|
|
|
{
|
|
|
if (button.MCKey!= null && button.MCKey.Trim().Equals("PLC.EquipState"))
|
|
|
{
|
|
|
if (button.InvokeRequired)
|
|
|
{
|
|
|
//button.BackColor = Color.Red;
|
|
|
BaseEquip.ReadDataHandler d = new BaseEquip.ReadDataHandler(RefreshPlcEquipState);
|
|
|
button.BeginInvoke(d, new object[] { button, e });
|
|
|
return;
|
|
|
}
|
|
|
//SetButtonBackColor(button);
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
count = 0;
|
|
|
}
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
ICSharpCode.Core.LoggingService.Error("刷新PLC设备状态失败:" + ex.Message);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private void RefreshPlcEquipState(object sender, Mesnac.Equips.ReadEventArgs e)
|
|
|
{
|
|
|
this.SetButtonBackColor(sender as Mesnac.Controls.Default.MCButton);
|
|
|
}
|
|
|
|
|
|
private void SetButtonBackColor(Mesnac.Controls.Default.MCButton button)
|
|
|
{
|
|
|
lock (String.Empty)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
if (button != null)
|
|
|
{
|
|
|
string plcEquipName = base.GetConfigValue("PLC.EquipName", "FeedingPLC"); //获取PLC设备名称
|
|
|
if (Mesnac.Equips.Factory.Instance.AllEquips.ContainsKey(plcEquipName))
|
|
|
{
|
|
|
//ICSharpCode.Core.LoggingService.Info("PLC设备状态:" + Mesnac.Equips.Factory.Instance.AllEquips[plcEquipName].State);
|
|
|
if (Mesnac.Equips.Factory.Instance.AllEquips[plcEquipName].State)
|
|
|
{
|
|
|
button.BackColor = Color.LightGreen;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
button.BackColor = Color.Red;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
ICSharpCode.Core.LoggingService.Error("刷新PLC设备状态(设备背景色)失败:" + ex.Message);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|