| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 | using System;using System.Xml;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;namespace Venus_RT.Devices.IODevices{    public class IoLid : BaseDevice, IDevice    {        private DIAccessor _diOFF;        private DOAccessor _doON;        private DOAccessor _doOFF;        private CylinderState _operation;        private DeviceTimer _timer = new DeviceTimer();        private R_TRIG _trigReset = new R_TRIG();        private R_TRIG _trigError = new R_TRIG();        public int SetPoint        {            get            {                return (int)CylinderState.Unknown;                /*                if (_doON.Value && _doOFF.Value) return (int)CylinderState.Error;                if (_doON.Value && !_doOFF.Value) return (int)CylinderState.Open;                if (!_doON.Value && _doOFF.Value) return (int)CylinderState.Close;                if (!_doON.Value && !_doOFF.Value) return (int)CylinderState.Unknown;                */            }        }        public CylinderState State        {            get            {                return _diOFF.Value ? CylinderState.Close : CylinderState.Open;            }        }        public bool OFFFeedback        {            get { return _diOFF.Value; }        }        public bool ONSetPoint        {            get            {                return _doON != null ? _doON.Value : false;            }            set            {                if (_doON != null && _doON.Check(value, out _))                    _doON.Value = value;            }        }        public bool OFFSetPoint        {            get            {                return _doOFF != null ? _doON.Value : true;            }            set            {                if (_doOFF != null && _doOFF.Check(value, out _))                    _doOFF.Value = value;            }        }        private AITCylinderData DeviceData        {            get            {                AITCylinderData deviceData = new AITCylinderData                {                    Module = Module,                    DeviceName = Name,                    DeviceSchematicId = DeviceID,                    DisplayName = Display,                    CloseFeedback = OFFFeedback,                };                return deviceData;            }        }        public IoLid(string module, XmlElement node, string ioModule = "")        {            base.Module = module;            base.Name = node.GetAttribute("id");            base.Display = node.GetAttribute("display");            base.DeviceID = node.GetAttribute("schematicId");            _diOFF = ParseDiNode("diOFF", node, ioModule);            _doON = ParseDoNode("doON", node, ioModule);            _doOFF = ParseDoNode("doOFF", node, ioModule);        }        public bool Initialize()        {            //DATA.Subscribe($"{Module}.{Name}.DeviceData", () => DeviceData);            DATA.Subscribe($"{Module}.{Name}.IsClosed",   () => OFFFeedback);            OP.Subscribe($"{Module}.{Name}.{AITCylinderOperation.Open}", InvokeOpenCylinder);            OP.Subscribe($"{Module}.{Name}.{AITCylinderOperation.Close}", InvokeCloseCylinder);            DEVICE.Register($"{Module}.{Name}.{AITCylinderOperation.Open}", (out string reason, int time, object[] param) =>            {                bool ret = SetCylinder(true, out reason);                if (ret)                {                    reason = $"Open Cylinder {Name}";                    return true;                }                return false;            });            DEVICE.Register($"{Module}.{Name}.{AITCylinderOperation.Close}", (out string reason, int time, object[] param) =>            {                bool ret = SetCylinder(false, out reason);                if (ret)                {                    reason = $"Close Cylinder {Name}";                    return true;                }                return false;            });            return true;        }        private bool InvokeOpenCylinder(string arg1, object[] arg2)        {            if (!SetCylinder(true, out var reason))            {                EV.PostWarningLog(Module, $"Can not open cylinder {Module}.{Name}, {reason}");                return false;            }            LOG.Write(eEvent.EV_DEVICE_INFO, Module, $"Open cylinder {Module}.{Name}");            return true;        }        private bool InvokeCloseCylinder(string arg1, object[] arg2)        {            if (!SetCylinder(false, out var reason))            {                EV.PostWarningLog(Module, $"Can not close cylinder {Module}.{Name}, {reason}");                return false;            }            LOG.Write(eEvent.EV_DEVICE_INFO, Module, $"Close cylinder {Module}.{Name}");            return true;        }        public void Terminate()        {            OFFSetPoint = false;            ONSetPoint = false;        }        public bool SetCylinder(bool isOpen, out string reason)        {            reason = "";            ONSetPoint = isOpen;            OFFSetPoint = !isOpen;            _operation = isOpen ? CylinderState.Open : CylinderState.Close;            _timer.Start(1000 * 60 * 3);            return true;        }        public void Monitor()        {            try            {                if (_timer.IsTimeout())                {                    _timer.Stop();                    if (State != _operation)                    {                        if (_operation == CylinderState.Open)                        {                            if (!_doON.Check(true, out var reason))                                LOG.Write(eEvent.ERR_DEVICE_INFO, Module, "Open Cylinder Failed for interlock, " + reason);                            else                                LOG.Write(eEvent.ERR_DEVICE_INFO, Module, "Cylinder hold close status");                        }                        else                        {                            if (!_doON.Check(false, out var reason))                                LOG.Write(eEvent.ERR_DEVICE_INFO, Module, "Close Cylinder Failed for interlock, " + reason);                            else                                LOG.Write(eEvent.ERR_DEVICE_INFO, Module, "Cylinder hold open status");                        }                    }                    _operation = (CylinderState)SetPoint;                }                else if (_timer.IsIdle())                {                    return;                }                _trigError.CLK = State == CylinderState.Error;                if (_trigError.Q)                {                    LOG.Write(eEvent.ERR_DEVICE_INFO, Module, "Cylinder in error status");                }                if ((SetPoint == (int)State) && (SetPoint == (int)CylinderState.Open || SetPoint == (int)CylinderState.Close))                {                    OFFSetPoint = false;                    ONSetPoint = false;                }            }            catch (Exception ex)            {                LOG.WriteExeption(ex);            }        }        public void Reset()        {            _trigReset.RST = true;            _trigError.RST = true;        }    }}
 |