using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Xml; using System.Xml.Serialization; using Aitex.Core.Common.DeviceData; using Aitex.Core.RT.DataCenter; using Aitex.Core.RT.Device; using Aitex.Core.RT.Device.Unit; 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 Venus_Core; namespace Venus_RT.Devices { //public enum LightType //{ // Red, // Yellow, // Green, // White, // Blue, // Buzzer1, // Buzzer2 //} public enum LightState { Off, On, Blink, } public class STEvents { [XmlElement(ElementName = "STEvent")] public List Events; } public class STEvent { [XmlAttribute(AttributeName = "name")] public string Name { get; set; } [XmlAttribute] public string Red { get; set; } [XmlAttribute] public string Yellow { get; set; } [XmlAttribute] public string Green { get; set; } [XmlAttribute] public string Blue { get; set; } [XmlAttribute] public string White { get; set; } [XmlAttribute] public string Buzzer1 { get; set; } [XmlAttribute] public string Buzzer2 { get; set; } } public class IoSignalTower : BaseDevice, IDevice { //device private IoSignalLight _red = null; private IoSignalLight _yellow = null; private IoSignalLight _green = null; private IoSignalLight _blue = null; private IoSignalLight _white = null; private IoSignalLight _buzzer1 = null; private IoSignalLight _buzzer2 = null; private PMState state; public bool HostControl { get; set; } private bool _buzzerSwitchOff = false; private bool _buzzerAndRed = false; private RfPowerBase _Generator; public DeviceTimer _timerBuzzerBlinking = new DeviceTimer(); public AITSignalTowerData DeviceData { get { AITSignalTowerData data = new AITSignalTowerData() { DeviceName = Name, DeviceSchematicId = DeviceID, DisplayName = Display, IsGreenLightOn = _green != null && _green.Value, IsRedLightOn = _red != null && _red.Value, IsYellowLightOn = _yellow != null && _yellow.Value, IsWhiteLightOn = _white != null && _white.Value, IsBlueLightOn = _blue != null && _blue.Value, IsBuzzerOn = (_buzzer1 != null && _buzzer1.Value) || (_buzzer2 != null && _buzzer2.Value), }; return data; } } private object _locker = new object(); private Dictionary> _config = new Dictionary>(); private Dictionary _cmdSetPoint = new Dictionary(); public IoSignalTower(string module, XmlElement node, string ioModule = "") { base.Module = module; base.Name = node.GetAttribute("id"); base.Display = node.GetAttribute("display"); base.DeviceID = node.GetAttribute("schematicId"); DOAccessor doRed = ParseDoNode("doRed", node, ioModule); if (doRed != null) { _red = new IoSignalLight(module, "SignalLightRed", "Red Light", "SignalLightRed", doRed); } DOAccessor doYellow = ParseDoNode("doYellow", node, ioModule); if (doYellow != null) { _yellow = new IoSignalLight(module, "SignalLightYellow", "Yellow Light", "SignalLightYellow", doYellow); } DOAccessor doGreen = ParseDoNode("doGreen", node, ioModule); if (doGreen != null) { _green = new IoSignalLight(module, "SignalLightGreen", "Green Light", "SignalLightGreen", doGreen); } DOAccessor doBlue = ParseDoNode("doBlue", node, ioModule); if (doBlue != null) { _blue = new IoSignalLight(module, "SignalLightBlue", "Blue Light", "SignalLightBlue", doBlue); } DOAccessor doWhite = ParseDoNode("doWhite", node, ioModule); if (doWhite != null) { _white = new IoSignalLight(module, "SignalLightWhite", "White Light", "SignalLightWhite", doWhite); } DOAccessor doBuzzer1 = ParseDoNode("doBuzzer1", node, ioModule); if (doBuzzer1 != null) { _buzzer1 = new IoSignalLight(module, "SignalLightBuzzer1", "Buzzer Light 1", "SignalLightBuzzer1", doBuzzer1); } DOAccessor doBuzzer2 = ParseDoNode("doBuzzer2", node, ioModule); if (doBuzzer2 != null) { _buzzer2 = new IoSignalLight(module, "SignalLightBuzzer2", "Buzzer Light 2", "SignalLightBuzzer2", doBuzzer2); } } public bool Initialize() { OP.Subscribe($"{Module}.{Name}.{AITSignalTowerOperation.SwitchOffBuzzer}", SwitchOffBuzzer); OP.Subscribe($"{Module}.{Name}.{AITSignalTowerOperation.SwitchOnBuzzerAndRed}", SwitchOnBuzzerAndRed); DATA.Subscribe($"{Module}.{Name}.DeviceData", () => DeviceData); DATA.Subscribe($"{Module}.{Name}.IsRedLightOn", () => _red != null && _red.Value); DATA.Subscribe($"{Module}.{Name}.IsYellowLightOn", () => _yellow != null && _yellow.Value); DATA.Subscribe($"{Module}.{Name}.IsGreenLightOn", () => _green != null && _green.Value); DATA.Subscribe($"{Module}.{Name}.IsBlueLightOn", () => _blue != null && _blue.Value); DATA.Subscribe($"{Module}.{Name}.IsBuzzerOn", () => (_buzzer1 != null && _buzzer1.Value) || (_buzzer2 != null && _buzzer2.Value)); return true; } public void SetLight(LightType lightType, LightState state, int blinkInterval = 500) { IoSignalLight light = null; switch (lightType) { case LightType.RED: light = _red; break; case LightType.GREEN: light = _green; break; case LightType.YELLOW: light = _yellow; break; case LightType.WHITE: light = _white; break; case LightType.BLUE: light = _blue; break; case LightType.BUZZER1: light = _buzzer1; break; //case LightType.buzz: // light = _buzzer2; // break; } if (light == null) { LOG.Write(eEvent.ERR_SIGNAL_TOWER_UNDEFINE, ModuleName.System, light.ToString()); return; } switch (state) { case LightState.On: _cmdSetPoint[light] = TowerLightStatus.On; light.StateSetPoint = TowerLightStatus.On; break; case LightState.Off: _cmdSetPoint[light] = TowerLightStatus.Off; light.StateSetPoint = TowerLightStatus.Off; break; case LightState.Blink: _cmdSetPoint[light] = TowerLightStatus.Blinking; light.Interval = blinkInterval; light.StateSetPoint = TowerLightStatus.Blinking; break; } } public bool CustomSignalTower(string configPathFile) { try { STEvents config = CustomXmlSerializer.Deserialize(new FileInfo(configPathFile)); lock (_locker) { foreach (STEvent e in config.Events) { if (!_config.ContainsKey(e.Name)) { _config[e.Name] = new Dictionary(); } if (_red != null) _config[e.Name][_red] = ParseLight(e.Red); if (_yellow != null) _config[e.Name][_yellow] = ParseLight(e.Yellow); if (_green != null) _config[e.Name][_green] = ParseLight(e.Green); if (_blue != null) _config[e.Name][_blue] = ParseLight(e.Blue); if (_white != null) _config[e.Name][_white] = ParseLight(e.White); if (_buzzer1 != null) _config[e.Name][_buzzer1] = ParseLight(e.Buzzer1); } } } catch (Exception e) { LOG.Write(eEvent.ERR_SIGNAL_TOWER_INVALID_CFG, ModuleName.System, e.Message); return false; } return true; } //响3声,通知工艺正常结束 public void BuzzerBlinking(double time) { _timerBuzzerBlinking.Start(time); } public bool SwitchOnBuzzerAndRed(string cmd, object[] objs) { return _buzzerAndRed = true; } public void Monitor() { if (DATA.Poll(Module, StateData.PMState.ToString()) != null) state = (PMState)DATA.Poll(Module, StateData.PMState.ToString()); List valves = DEVICE.GetDevice(); //if (DEVICE.GetDevice() != null) //{ // _Generator = DEVICE.GetDevice($"{Module}.{VenusDevice.Rf}"); //} //else if (DEVICE.GetDevice() != null) { _Generator = DEVICE.GetDevice($"{Module}.{VenusDevice.Rf}"); } else { _Generator = null; } //pumping valve开,vent purge valve开,Gas valve开,RF开,只要有其中之一处于开的状态,就是绿灯 if (state == PMState.Error || _buzzerAndRed) { SetLight(LightType.GREEN, LightState.Off); SetLight(LightType.RED, LightState.On); SetLight(LightType.YELLOW, LightState.Off); SetLight(LightType.BUZZER1, _buzzerSwitchOff ? LightState.Off : LightState.On); } else if ((_Generator != null && _Generator.IsPowerOn) || valves.Exists(v => v.Status)) { SetLight(LightType.GREEN, LightState.On); SetLight(LightType.RED, LightState.Off); SetLight(LightType.YELLOW, LightState.Off); SetLight(LightType.BUZZER1, LightState.Off); } else if (state == PMState.PreProcess || state == PMState.Processing || state == PMState.PostProcess) { SetLight(LightType.GREEN, LightState.On); SetLight(LightType.RED, LightState.Off); SetLight(LightType.YELLOW, LightState.Off); SetLight(LightType.BUZZER1, LightState.Off); } else { SetLight(LightType.GREEN, LightState.Off); SetLight(LightType.RED, LightState.Off); SetLight(LightType.YELLOW, LightState.On); SetLight(LightType.BUZZER1, LightState.Off); } if (!_timerBuzzerBlinking.IsIdle() && !_timerBuzzerBlinking.IsTimeout()) SetLight(LightType.BUZZER1, LightState.Blink); MonitorLight(_red); MonitorLight(_blue); MonitorLight(_yellow); MonitorLight(_green); MonitorLight(_white); MonitorLight(_buzzer1); MonitorLight(_buzzer2); } public void Reset() { ResetLight(_red); ResetLight(_blue); ResetLight(_yellow); ResetLight(_green); ResetLight(_white); ResetLight(_buzzer1); ResetLight(_buzzer2); _buzzerAndRed = false; _buzzerSwitchOff = false; } public bool SwitchOffBuzzer(string cmd, object[] objs) { if (cmd == $"{ Module}.{ Name}.{ AITSignalTowerOperation.SwitchOffBuzzer}" && _buzzer1 != null && _buzzer1.StateSetPoint != TowerLightStatus.Off) { _buzzerSwitchOff = true; } return true; } private void SetLight(IoSignalLight light, Dictionary state) { if (light != null) { light.StateSetPoint = state[light]; } } private void ResetLight(IoSignalLight light) { if (light != null) { light.Reset(); } } private void MonitorLight(IoSignalLight light) { if (light != null) { light.Monitor(); } } public void Terminate() { } private TowerLightStatus MergeCondition(TowerLightStatus newValue, TowerLightStatus oldValue) { if (newValue == TowerLightStatus.Blinking || oldValue == TowerLightStatus.Blinking) return TowerLightStatus.Blinking; if (newValue == TowerLightStatus.On || oldValue == TowerLightStatus.On) return TowerLightStatus.On; return TowerLightStatus.Off; } private TowerLightStatus ParseLight(string light) { if (string.IsNullOrEmpty(light)) return TowerLightStatus.Off; TowerLightStatus result = TowerLightStatus.Unknown; light = light.Trim().ToLower(); switch (light) { case "on": result = TowerLightStatus.On; break; case "off": result = TowerLightStatus.Off; break; case "blinking": result = TowerLightStatus.Blinking; break; default: LOG.Write(eEvent.ERR_SIGNAL_TOWER_INVALID_CFG, ModuleName.System, light); break; } return result; } } }