using Aitex.Common.Util; using Aitex.Core.Common.DeviceData; using Aitex.Core.RT.DataCenter; using Aitex.Core.RT.Device; using Aitex.Core.RT.Event; using Aitex.Core.RT.IOCore; using Aitex.Core.RT.Log; using Aitex.Core.RT.OperationCenter; using Aitex.Core.Util; using MECF.Framework.Common.Device.Bases; using MECF.Framework.Common.Equipment; using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml; namespace MECF.Framework.RT.EquipmentLibrary.Devices { public class IoSignalTower : BaseDevice, IDevice { private DOAccessor _redLight; private DOAccessor _yellowLight; private DOAccessor _greenLight; private DOAccessor _buzzer; private bool _switchBuzzerOff; private Dictionary> _signalTowerEvent; private Dictionary _signalLightDic = new Dictionary(); public AITSignalTowerData DeviceData => new AITSignalTowerData { DeviceName = base.Name, DeviceSchematicId = base.DeviceID, DisplayName = base.Display, IsGreenLightOn = _signalLightDic[LightType.Green].GetValue(), IsRedLightOn = _signalLightDic[LightType.Red].GetValue(), IsYellowLightOn = _signalLightDic[LightType.Yellow].GetValue(), IsBuzzerOn = _signalLightDic[LightType.Buzzer].GetValue() }; public IoSignalTower(string module, XmlElement node, string ioModule = "") { base.Module = string.IsNullOrEmpty(node.GetAttribute("module")) ? module : node.GetAttribute("module"); base.Name = node.GetAttribute("id"); base.Display = node.GetAttribute("display"); base.DeviceID = node.GetAttribute("schematicId"); _redLight = ParseDoNode("doRed", node, ioModule); _yellowLight = ParseDoNode("doYellow", node, ioModule); _greenLight = ParseDoNode("doGreen", node, ioModule); _buzzer = ParseDoNode("doBuzzer", node, ioModule); Debug.Assert(_redLight != null, "RedLight is not valid"); Debug.Assert(_yellowLight != null, "RedLight is not valid"); Debug.Assert(_greenLight != null, "RedLight is not valid"); Debug.Assert(_buzzer != null, "RedLight is not valid"); if (_redLight != null) { _signalLightDic.Add(LightType.Red, new IoSignalLight {lightType= LightType.Red, doLight = _redLight }); } if (_yellowLight != null) { _signalLightDic.Add(LightType.Yellow, new IoSignalLight {lightType = LightType.Yellow, doLight = _yellowLight }); } if (_greenLight != null) { _signalLightDic.Add(LightType.Green, new IoSignalLight {lightType = LightType.Green, doLight = _greenLight }); } if (_buzzer != null) { _signalLightDic.Add(LightType.Buzzer, new IoSignalLight {lightType = LightType.Buzzer, doLight = _buzzer }); } //解析三色灯Event ParseSignalTowerEvent(); } private bool ParseSignalTowerEvent() { try { string fileName = PathManager.GetCfgDir() + "_SignalTower.xml"; if (!File.Exists(fileName)) { fileName = PathManager.GetCfgDir() + "SignalTower.xml"; if (!File.Exists(fileName)) { return false; } } STEvents stEvents = CustomXmlSerializer.Deserialize(new FileInfo(fileName)); _signalTowerEvent = new Dictionary>(); foreach (STEvent _event in stEvents.Events) { if (!_signalTowerEvent.ContainsKey(_event.Name)) { _signalTowerEvent[_event.Name] = new List(); foreach (LightType type in Enum.GetValues(typeof(LightType))) { if (!_signalLightDic.ContainsKey(type)) { continue; } object obj = _event.GetType().GetProperty(type.ToString()).GetValue(_event); if (obj != null) { TowerLightStatus _action; string str = obj.ToString().ToLower(); if (str.Contains("on")) { _action = TowerLightStatus.On; } else if (str.Contains("off")) { _action = TowerLightStatus.Off; } else { _action = TowerLightStatus.Blinking; } _signalTowerEvent[_event.Name].Add(new SignalLightParam { lightType = type, action = _action }); } } } } return true; } catch (Exception ex) { LOG.Error(ex.ToString()); return false; } } public bool Initialize() { OP.Subscribe($"{base.Module}.{base.Name}.{AITSignalTowerOperation.SwitchOffBuzzer}", SwitchBuzzerOff); DATA.Subscribe(base.Module + "." + base.Name + ".DeviceData", () => DeviceData); return true; } public bool SwitchBuzzerOff(string arg1, object[] arg2) { _switchBuzzerOff = arg2[0] == null ? true : (bool)arg2[0]; return true; } public void Monitor() { var tempStatus = new Dictionary(); foreach (LightType lightType in _signalLightDic.Keys) { tempStatus[lightType] = TowerLightStatus.Off; } foreach (KeyValuePair> _event in _signalTowerEvent) { object obj = DATA.Poll(_event.Key); if (obj == null || !(obj is bool flag) || !flag) { continue; } foreach (SignalLightParam param in _event.Value) { TowerLightStatus aciton = MergeAction(tempStatus[param.lightType], param.action); if (_switchBuzzerOff && param.lightType == LightType.Buzzer) { aciton = TowerLightStatus.Off; } tempStatus[param.lightType] =aciton; } } foreach (KeyValuePair status in tempStatus) { _signalLightDic[status.Key].SetValue(status.Value); } foreach (IoSignalLight light in _signalLightDic.Values) { light?.Monitor(); } } private TowerLightStatus MergeAction(TowerLightStatus curAciton, TowerLightStatus setAction) { if (curAciton == TowerLightStatus.On || setAction == TowerLightStatus.On) { return TowerLightStatus.On; } else if (curAciton == TowerLightStatus.Blinking || setAction == TowerLightStatus.Blinking) { return TowerLightStatus.Blinking; } else { return TowerLightStatus.Off; } } public void Reset() { _switchBuzzerOff = false; foreach (IoSignalLight light in _signalLightDic.Values) { light.Reset(); } } public void Terminate() { foreach (IoSignalLight light in _signalLightDic.Values) { light.Terminate(); } } } }