Sic.Framework/MECF.Framework.Common/MECF/Framework/Common/Device/Bases/STEvents.cs

103 lines
3.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Aitex.Core.RT.Log;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Serialization;
namespace MECF.Framework.Common.Device.Bases
{
public class STEvents
{
#region Properties
/// <summary>
/// 返回工作模式模板。
/// </summary>
[XmlArray("STPatterns")]
[XmlArrayItem("STPattern")]
public List<STPatternSetting> PatternsSettings { get; set; }
/// <summary>
/// 返回预定义的事件。
/// </summary>
[XmlArray("STEvents")]
public List<STEvent> Events { get; set; }
#endregion
#region Methods
/// <summary>
/// 解析配置文件中定义的事件。
/// </summary>
/// <param name="events">
/// 返回事件字典。
/// <remarks>
/// 事件字典的Key为事件名称名称为RT中注册的Bool型数据通过DATA.Poll访问。
/// <br/>
/// 事件动作为<see cref="STEventAction"/>动作列表表示当上述事件为True时指定的信号塔组件执行的动作。
/// </remarks>
/// </param>
public void ParseEvents(out Dictionary<string, List<STEventAction>> events)
{
events = new Dictionary<string, List<STEventAction>>();
foreach (var stEvent in Events)
{
// 如果相同的事件已经添加,则不再重复添加
if (events.ContainsKey(stEvent.Name))
continue;
// 字典中创建一个事件项目
events[stEvent.Name] = new List<STEventAction>();
// 解析当前事件执行的动作
foreach (LightType light in Enum.GetValues(typeof(LightType)))
{
var strStatus = stEvent.GetType().GetProperty(light.ToString())?.GetValue(stEvent)?.ToString().ToLower();
if (!string.IsNullOrEmpty(strStatus))
{
var status = TowerLightStatus.Unknown;
STBlinkPattern blinkPattern = null;
if (strStatus.Contains(TowerLightStatus.On.ToString().ToLower()))
{
status = TowerLightStatus.On;
}
else if (strStatus.Contains(TowerLightStatus.Off.ToString().ToLower()))
{
status = TowerLightStatus.Off;
}
else
{
// 使用工作模式配置中的项目
var pattSetting = PatternsSettings.FirstOrDefault(x =>
string.Compare(x.Name, strStatus, StringComparison.OrdinalIgnoreCase) == 0);
if (pattSetting != null)
blinkPattern = new STBlinkPattern(pattSetting.Pattern, pattSetting.Priority, pattSetting.Cycles);
else
{
LOG.Warning($"Unable to find the STPatternSetting {strStatus} from STEvents config file.");
blinkPattern = STBlinkPattern.GetDefaultPattern();
}
status = TowerLightStatus.Customized;
}
events[stEvent.Name].Add(new STEventAction(light, status, blinkPattern));
}
else
{
LOG.Warning($"Unable to find the status of light {light} from STEvents config file.");
}
}
}
}
#endregion
}
}