You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
3.9 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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);
}
}
}
}
}