| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585 | using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Text;using System.Threading.Tasks;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;namespace Aitex.Core.RT.Device.Unit{    public enum LightType    {        Red,        Yellow,        Green,        White,        Blue,        Buzzer,        Buzzer1,        Buzzer2,        Buzzer3,        Buzzer4,        Buzzer5,    }    public enum LightState    {        Off,        On,        Blink,    }    public class STEvents    {        [XmlElement(ElementName = "STEvent")]        public List<STEvent> 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 Buzzer { get; set; }        [XmlAttribute]        public string White { get; set; }        [XmlAttribute]        public string Buzzer1 { get; set; }        [XmlAttribute]        public string Buzzer2 { get; set; }        [XmlAttribute]        public string Buzzer3 { get; set; }        [XmlAttribute]        public string Buzzer4 { get; set; }        [XmlAttribute]        public string Buzzer5 { 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 _buzzer = null;        private IoSignalLight _buzzer1 = null;        private IoSignalLight _buzzer2 = null;        private IoSignalLight _buzzer3 = null;        private IoSignalLight _buzzer4 = null;        private IoSignalLight _buzzer5 = null;        public bool HostControl { get; set; }        private bool _buzzerSwitchOff = false;        private bool _buzzer1SwitchOff = false;        private bool _buzzer2SwitchOff = false;        private bool _buzzer3SwitchOff = false;        private bool _buzzer4SwitchOff = false;        private bool _buzzer5SwitchOff = false;        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 = _buzzer != null && _buzzer.Value,                    IsBuzzer1On = _buzzer1 != null && _buzzer1.Value,                    IsBuzzer2On = _buzzer2 != null && _buzzer2.Value,                    IsBuzzer3On = _buzzer3 != null && _buzzer3.Value,                    IsBuzzer4On = _buzzer4 != null && _buzzer4.Value,                    IsBuzzer5On = _buzzer5 != null && _buzzer5.Value,                 };                return data;            }        }        private object _locker = new object();         private Dictionary<string, Dictionary<IoSignalLight, TowerLightStatus>> _config =            new Dictionary<string, Dictionary<IoSignalLight, TowerLightStatus>>();        private Dictionary<IoSignalLight, TowerLightStatus> _cmdSetPoint = new Dictionary<IoSignalLight, TowerLightStatus>();        public IoSignalTower(string module, XmlElement node, string ioModule = "")        {            base.Module = node.GetAttribute("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", "Buzzer1 Light", "SignalLightBuzzer1", doBuzzer1);            }            DOAccessor doBuzzer2 = ParseDoNode("doBuzzer2", node, ioModule);            if (doBuzzer2 != null)            {                _buzzer2 = new IoSignalLight(module, "SignalLightBuzzer2", "Buzzer2 Light", "SignalLightBuzzer2", doBuzzer2);            }            DOAccessor doBuzzer3 = ParseDoNode("doBuzzer3", node, ioModule);            if (doBuzzer3 != null)            {                _buzzer3 = new IoSignalLight(module, "SignalLightBuzzer3", "Buzzer3 Light", "SignalLightBuzzer3", doBuzzer3);            }            DOAccessor doBuzzer4 = ParseDoNode("doBuzzer4", node, ioModule);            if (doBuzzer4 != null)            {                _buzzer4 = new IoSignalLight(module, "SignalLightBuzzer4", "Buzzer4 Light", "SignalLightBuzzer4", doBuzzer4);            }            DOAccessor doBuzzer5 = ParseDoNode("doBuzzer5", node, ioModule);            if (doBuzzer5 != null)            {                _buzzer5 = new IoSignalLight(module, "SignalLightBuzzer5", "Buzzer5 Light", "SignalLightBuzzer5", doBuzzer5);            }            IoSignalLight[] buzzers = new[] {_buzzer1, _buzzer2, _buzzer3, _buzzer4, _buzzer5};            _buzzer = Array.Find(buzzers, x => x != null);        }        public bool Initialize()        {            OP.Subscribe($"{Module}.{Name}.{AITSignalTowerOperation.SwitchOffBuzzer}", SwitchOffBuzzer);            OP.Subscribe($"{Module}.{Name}.{AITSignalTowerOperation.SwitchOffBuzzer1}", SwitchOffBuzzer);            OP.Subscribe($"{Module}.{Name}.{AITSignalTowerOperation.SwitchOffBuzzer2}", SwitchOffBuzzer);            OP.Subscribe($"{Module}.{Name}.{AITSignalTowerOperation.SwitchOffBuzzer3}", SwitchOffBuzzer);            OP.Subscribe($"{Module}.{Name}.{AITSignalTowerOperation.SwitchOffBuzzer4}", SwitchOffBuzzer);			//DATA.Subscribe($"{Module}.{Name}.DeviceData", () => DeviceData);            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.Buzzer:                    light = _buzzer;                    break;                case LightType.Buzzer1:                    light = _buzzer1;                    break;                case LightType.Buzzer2:                    light = _buzzer2;                    break;                case LightType.Buzzer3:                    light = _buzzer3;                    break;                case LightType.Buzzer4:                    light = _buzzer4;                    break;                case LightType.Buzzer5:                    light = _buzzer5;                    break;            }            if (light == null)            {                //LOG.Write($"Undefined light {lightType}");                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<STEvents>(new FileInfo(configPathFile));                lock (_locker)                {                    foreach (STEvent e in config.Events)                    {                        if (!_config.ContainsKey(e.Name))                        {                            _config[e.Name] = new Dictionary<IoSignalLight, TowerLightStatus>();                        }                        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 (_buzzer != null)                            _config[e.Name][_buzzer] = ParseLight(e.Buzzer);                        if (_buzzer1 != null)                            _config[e.Name][_buzzer1] = ParseLight(e.Buzzer1);                        if (_buzzer2 != null)                            _config[e.Name][_buzzer2] = ParseLight(e.Buzzer2);                        if (_buzzer3 != null)                            _config[e.Name][_buzzer3] = ParseLight(e.Buzzer3);                        if (_buzzer4 != null)                            _config[e.Name][_buzzer4] = ParseLight(e.Buzzer4);                        if (_buzzer5 != null)                            _config[e.Name][_buzzer5] = ParseLight(e.Buzzer5);                    }                }            }            catch (Exception e)            {                EV.PostWarningLog(Module, $"Signal tower config file is invalid, {e.Message}");                return false;            }            return true;        }        public void Monitor()        {            Dictionary<IoSignalLight, TowerLightStatus> dicState = new Dictionary<IoSignalLight, TowerLightStatus>();            if (_red != null)                dicState[_red] = TowerLightStatus.Off;            if (_yellow != null)                dicState[_yellow] = TowerLightStatus.Off;            if (_green != null)                dicState[_green] = TowerLightStatus.Off;            if (_blue != null)                dicState[_blue] = TowerLightStatus.Off;            if (_white != null)                dicState[_white] = TowerLightStatus.Off;            if (_buzzer != null)                dicState[_buzzer] = TowerLightStatus.Off;            if (_buzzer1 != null)                dicState[_buzzer1] = TowerLightStatus.Off;            if (_buzzer2 != null)	            dicState[_buzzer2] = TowerLightStatus.Off;            if (_buzzer3 != null)	            dicState[_buzzer3] = TowerLightStatus.Off;            if (_buzzer4 != null)	            dicState[_buzzer4] = TowerLightStatus.Off;            if (_buzzer5 != null)                dicState[_buzzer5] = TowerLightStatus.Off;            foreach (var trigCondition in _config)            {                var conditionValue = DATA.Poll(trigCondition.Key);                if (conditionValue == null)                    continue;                                bool isTrig = (bool)conditionValue;                if (isTrig)                {                    if (_red != null)                    {                        dicState[_red] = MergeCondition(dicState[_red], trigCondition.Value[_red]);                    }                    if (_yellow != null)                    {                        dicState[_yellow] = MergeCondition(dicState[_yellow], trigCondition.Value[_yellow]);                    }                    if (_green != null)                    {                        dicState[_green] = MergeCondition(dicState[_green], trigCondition.Value[_green]);                    }                    if (_white != null)                    {                        dicState[_white] = MergeCondition(dicState[_white], trigCondition.Value[_white]);                    }                    if (_blue != null)                    {                        dicState[_blue] = MergeCondition(dicState[_blue], trigCondition.Value[_blue]);                    }                    if (_buzzer != null)                    {                        dicState[_buzzer] = _buzzerSwitchOff ? TowerLightStatus.Off : MergeCondition(dicState[_buzzer], trigCondition.Value[_buzzer]);                    }                    if (_buzzer1 != null)                    {                        dicState[_buzzer1] = _buzzer1SwitchOff ? TowerLightStatus.Off : MergeCondition(dicState[_buzzer1], trigCondition.Value[_buzzer1]);                    }                    if (_buzzer2 != null)                    {	                    dicState[_buzzer2] = _buzzer2SwitchOff ? TowerLightStatus.Off : MergeCondition(dicState[_buzzer2], trigCondition.Value[_buzzer2]);					}					if (_buzzer3 != null)					{						dicState[_buzzer3] = _buzzer3SwitchOff ? TowerLightStatus.Off : MergeCondition(dicState[_buzzer3], trigCondition.Value[_buzzer3]);					}					if (_buzzer4 != null)					{						dicState[_buzzer4] = _buzzer4SwitchOff ? TowerLightStatus.Off : MergeCondition(dicState[_buzzer4], trigCondition.Value[_buzzer4]);					}                    if (_buzzer5 != null)                    {                        dicState[_buzzer5] = _buzzer5SwitchOff ? TowerLightStatus.Off : MergeCondition(dicState[_buzzer5], trigCondition.Value[_buzzer5]);                    }                }            }            if (_config.Count > 0 && !HostControl)            {                SetLight(_red, dicState);                SetLight(_blue, dicState);                SetLight(_yellow, dicState);                SetLight(_green, dicState);                SetLight(_white, dicState);                SetLight(_buzzer, dicState);                SetLight(_buzzer1, dicState);                SetLight(_buzzer2, dicState);                SetLight(_buzzer3, dicState);                SetLight(_buzzer4, dicState);                SetLight(_buzzer5, dicState);            }            MonitorLight(_red);            MonitorLight(_blue);            MonitorLight(_yellow);            MonitorLight(_green);            MonitorLight(_white);            MonitorLight(_buzzer);            MonitorLight(_buzzer1);            MonitorLight(_buzzer2);            MonitorLight(_buzzer3);            MonitorLight(_buzzer4);            MonitorLight(_buzzer5);        }        public void Reset()        {            ResetLight(_red );            ResetLight(_blue );            ResetLight(_yellow );            ResetLight(_green );            ResetLight(_white);            ResetLight(_buzzer );            _buzzerSwitchOff = false;            _buzzer1SwitchOff = false;            _buzzer2SwitchOff = false;            _buzzer3SwitchOff = false;            _buzzer4SwitchOff = false;            _buzzer5SwitchOff = false;		}        public bool SwitchOffBuzzer(string cmd, object[] objs)        {            if (cmd == $"{ Module}.{ Name}.{ AITSignalTowerOperation.SwitchOffBuzzer}" && _buzzer != null && _buzzer.StateSetPoint != TowerLightStatus.Off)            {                _buzzerSwitchOff = true;            }            if (cmd == $"{ Module}.{ Name}.{ AITSignalTowerOperation.SwitchOffBuzzer1}"&&_buzzer1 != null && _buzzer1.StateSetPoint != TowerLightStatus.Off)            {                _buzzer1SwitchOff = true;            }	        if (cmd == $"{ Module}.{ Name}.{ AITSignalTowerOperation.SwitchOffBuzzer2}" && _buzzer2 != null && _buzzer2.StateSetPoint != TowerLightStatus.Off)	        {		        _buzzer2SwitchOff = true;			}			if (cmd == $"{ Module}.{ Name}.{ AITSignalTowerOperation.SwitchOffBuzzer3}" && _buzzer3 != null && _buzzer3.StateSetPoint != TowerLightStatus.Off)			{				_buzzer3SwitchOff = true;			}			if (cmd == $"{ Module}.{ Name}.{ AITSignalTowerOperation.SwitchOffBuzzer4}" && _buzzer4 != null && _buzzer4.StateSetPoint != TowerLightStatus.Off)			{				_buzzer4SwitchOff = true;			}			return true;        }        private void SetLight(IoSignalLight light, Dictionary<IoSignalLight, TowerLightStatus> 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("signal tower config file has invalid set, " + light);                    break;            }            return result;        }    }}
 |