123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- using System;
- using System.Xml;
- using Aitex.Core.Common.DeviceData;
- using Aitex.Core.RT.DataCenter;
- using Aitex.Core.RT.Event;
- using Aitex.Core.RT.IOCore;
- using Aitex.Core.RT.Log;
- using Aitex.Core.UI.Control;
- using Aitex.Core.Util;
- namespace Aitex.Core.RT.Device.Unit
- {
- public class IoValve : BaseDevice, IDevice
- {
- public string GVName { get { return Name; } }
- public string GVDeviceID { get { return DeviceID; } }
- public bool GVIsDefaultOpen { get { return _isDefaultOpen; } }
- [Subscription(AITValveDataPropertyName.SetPoint)]
- public bool SetPoint //True:open| False:close
- {
- get
- {
- return _isNc ? _doOpen.Value : !_doOpen.Value;
- }
- }
- [Subscription(AITValveDataPropertyName.Status)]
- public bool Status //True:open | False:close
- {
- get
- {
- if (_diOpen != null)
- return _isNc ? _diOpen.Value : !_diOpen.Value;
- return _isNc ? _doOpen.Value : !_doOpen.Value;
- }
- }
- /// <summary>
- /// normal closed, 0 关闭,1打开
- /// </summary>
- public bool _isNc;
- /// <summary>
- /// default open
- /// </summary>
- public bool _isDefaultOpen;
- private DIAccessor _diOpenSensor;
- private DIAccessor _diCloseSensor;
- private DIAccessor _diOpen;
- private DOAccessor _doOpen;
- private bool _operation;
- private R_TRIG eventTrigger = new R_TRIG();
- DeviceTimer _timer = new DeviceTimer();
-
- public IoValve(string module, XmlElement node)
- {
- base.Module = module;
- base.Name = node.GetAttribute("id");
- base.Display = node.GetAttribute("display");
- base.DeviceID = node.GetAttribute("schematicId");
- _isNc = Convert.ToBoolean(node.GetAttribute("isNc"));
- _isDefaultOpen = Convert.ToBoolean(node.GetAttribute("isDefaultOpen"));
- _diOpenSensor = ParseDiNode("diOpenSensor", node);
- _diCloseSensor = ParseDiNode("diCloseSensor", node);
- _doOpen = ParseDoNode("doOpen", node);
- _diOpen = ParseDiNode("diOpen", node);
- }
- public bool Initialize()
- {
- DATA.Subscribe(string.Format("Device.{0}.{1}", Module , GVName), () =>
- {
- AITValveData data = new AITValveData()
- {
- DeviceName = GVName,
- DefaultValue = GVIsDefaultOpen,
- DeviceSchematicId = DeviceID,
- DisplayName = Display,
- Feedback = Status,
- SetPoint = SetPoint,
- };
- return data;
- }, SubscriptionAttribute.FLAG.IgnoreSaveDB);
- DEVICE.Register(String.Format("{0}.{1}", Name, AITValveOperation.GVTurnValve), (out string reason, int time, object[] param) =>
- {
- bool bOn = Convert.ToBoolean((string)param[0]);
- bool ret = TurnValve(bOn, out reason);
- if (ret)
- {
- reason = string.Format("Valve {0}{1}", Name, bOn ? "Open" : "Close");
- return true;
- }
- return false;
- });
- //for recipe
- DEVICE.Register(String.Format("{0}", Name), (out string reason, int time, object[] param) =>
- {
- bool bOn = Convert.ToBoolean((string)param[0]);
- bool ret = TurnValve(bOn, out reason);
- if (ret)
- {
- reason = string.Format("Valve {0}{1}", Name, bOn ? "Open" : "Close");
- return true;
- }
- return false;
- });
- return true;
- }
- public void Terminate()
- {
- string reason;
- TurnValve(_isDefaultOpen, out reason);
- }
- public void Monitor()
- {
- try
- {
- if (_timer.IsTimeout())
- {
- _timer.Stop();
- if (Status != _operation)
- {
- if (_operation)
- {
- string reason;
- if (!_doOpen.Check(_isNc ? true : false, out reason))
- EV.PostMessage(Module, EventEnum.ValveOperationFail, Module, Display, "Open", ":Failed for interlock " + reason);
- else
- EV.PostMessage(Module, EventEnum.ValveOperationFail, Module, Display, "Open", ":Valve keep closed ");
- }
- else
- {
- string reason;
- if (!_doOpen.Check(_isNc ? true : false, out reason))
- EV.PostMessage(Module, EventEnum.ValveOperationFail, Module, Display, "Close", ":Failed for interlock " + reason);
- else
- EV.PostMessage(Module, EventEnum.ValveOperationFail, Module, Display, "Close", ":Valve keep open");
- }
- }
- _operation = SetPoint;
- }
- else if (_timer.IsIdle())
- {
- eventTrigger.CLK = SetPoint != _operation; // fire event only check at first, SetPoint set by interlock
- if (eventTrigger.Q)
- {
- if (_operation)
- {
- string reason;
- if (!_doOpen.Check(_isNc ? true : false, out reason))
- EV.PostMessage(Module, EventEnum.SwInterlock, Module, string.Format("Valve {0} was {1},Reason:{2}", Display, "Close", reason));
- else
- EV.PostMessage(Module, EventEnum.SwInterlock, Module, string.Format("Valve {0} was {1},Reason {2}", Display, "Close", "PLC kept"));
- }
- else
- {
- string reason;
- if (!_doOpen.Check(_isNc ? true : false, out reason))
- EV.PostMessage(Module, EventEnum.SwInterlock, Module, string.Format("Valve {0} was {1},Reason:{2}", Display, "Open", reason));
- else
- EV.PostMessage(Module, EventEnum.SwInterlock, Module, string.Format("Valve {0} was {1},Reason {2}", Display, "Open", "PLC Kept"));
- }
- _operation = SetPoint;
- }
- }
- }
- catch (Exception ex)
- {
- LOG.Write(ex);
- throw ex;
- }
- }
- public bool TurnValve(bool bOperation, out string reason)
- {
- bool bValue = _isNc ? bOperation : !bOperation;
- if (!_doOpen.SetValue(bValue, out reason))
- return false;
- _operation = bOperation;
- _timer.Start(2000); //2 seconds to monitor
- return true;
- }
- public void Reset()
- {
- eventTrigger.RST = true;
- }
- }
- }
|