Sic01/Modules/SicPM/Devices/IoConfinementRing.cs

452 lines
14 KiB
C#

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.RT.SCCore;
using Aitex.Core.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace SicPM.Devices
{
public enum ConfinementRingStateEnum
{
Unknown,
Up,
Down,
Error,
}
public partial class IoConfinementRing : BaseDevice, IDevice
{
public ConfinementRingStateEnum State
{
get
{
if (_diRingUpFaceback.Value &&_diRingDownFaceback.Value)
return ConfinementRingStateEnum.Error;
if (_diRingUpFaceback.Value && !_diRingDownFaceback.Value)
return ConfinementRingStateEnum.Up;
if (!_diRingUpFaceback.Value && _diRingDownFaceback.Value)
return ConfinementRingStateEnum.Down;
return ConfinementRingStateEnum.Unknown;
}
}
private DIAccessor _diRingDownFaceback = null;
private DIAccessor _diRingUpFaceback = null;
private DIAccessor _diRingResetFaceback = null;
private DOAccessor _doRingDownSetpoint = null;
private DOAccessor _doRingUpSetpoint = null;
private DOAccessor _doRingResetSetpoint = null;
enum DeviceState
{
Idle,
MovingUp,
MovingDown,
Error,
Reset,
}
#region DI
public bool RingDownFaceback
{
get
{
if (_diRingDownFaceback != null)
return _diRingDownFaceback.Value;
return false;
}
}
public bool RingUpFaceback
{
get
{
if (_diRingUpFaceback != null)
return _diRingUpFaceback.Value;
return false;
}
}
public bool RingResetFaceback
{
get
{
if (_diRingResetFaceback != null)
return _diRingResetFaceback.Value;
return false;
}
}
#endregion
#region DO
public bool RingDownSetpoint
{
get
{
if (_doRingDownSetpoint != null)
return _doRingDownSetpoint.Value;
return false;
}
set
{
if (_doRingDownSetpoint != null)
_doRingDownSetpoint.Value =value;
}
}
public bool RingUpSetpoint
{
get
{
if (_doRingUpSetpoint != null)
return _doRingUpSetpoint.Value;
return false;
}
set
{
if (_doRingUpSetpoint != null)
_doRingUpSetpoint.Value = value;
}
}
public bool RingResetSetpoint
{
get
{
if (_doRingResetSetpoint != null)
return _doRingResetSetpoint.Value;
return false;
}
}
#endregion
private DeviceState _state = DeviceState.Idle;
private DeviceTimer _timer = new DeviceTimer();
private DeviceTimer _timerWarning = new DeviceTimer();
private R_TRIG _trigDown = new R_TRIG();
private R_TRIG _trigUp = new R_TRIG();
private SCConfigItem _scDownWarnTime;
private R_TRIG _trigError = new R_TRIG();
public bool IsUp { get { return !_diRingDownFaceback.Value && _diRingUpFaceback.Value; } }
public bool IsDown { get { return _diRingDownFaceback.Value && !_diRingUpFaceback.Value; } }
public bool IsReset { get { return _diRingResetFaceback.Value; } }
public AITDeviceData DeviceData
{
get
{
AITDeviceData data = new AITDeviceData()
{
Module = Module,
DeviceName = Name,
DisplayName = Display,
DeviceSchematicId = DeviceID,
UniqueName = UniqueName,
};
data.AttrValue["RingUpFaceback"] = _diRingUpFaceback.Value;
data.AttrValue["RingDownFaceback"] = _diRingDownFaceback.Value;
data.AttrValue["RingResetFaceback"] = _diRingResetFaceback.Value;
data.AttrValue["RingUpSetpoint"] = _doRingUpSetpoint.Value;
data.AttrValue["RingDownSetpoint"] = _doRingDownSetpoint.Value;
data.AttrValue["RingResetSetpoint"] = _doRingResetSetpoint.Value;
//data.AttrValue["PV"] = _aiFeedBack.Value;
return data;
}
}
public IoConfinementRing(string module, XmlElement node, string ioModule = "")
{
var attrModule = node.GetAttribute("module");
base.Module = string.IsNullOrEmpty(attrModule) ? module : attrModule;
base.Name = node.GetAttribute("id");
base.Display = node.GetAttribute("display");
base.DeviceID = node.GetAttribute("schematicId");
_diRingDownFaceback = ParseDiNode("diRingDownFaceback", node, ioModule);
_diRingUpFaceback = ParseDiNode("diRingUpFaceback", node, ioModule);
_diRingResetFaceback = ParseDiNode("diRingResetFaceback", node, ioModule);
_doRingDownSetpoint = ParseDoNode("doRingDownSetpoint", node, ioModule);
_doRingUpSetpoint = ParseDoNode("doRingUpSetpoint", node, ioModule);
_doRingResetSetpoint = ParseDoNode("doRingResetSetpoint", node, ioModule);
_scDownWarnTime= ParseScNode("ConfinementRingDownWarnTime", node, "PM", $"PM.{Module}.ConfinementRingDownWarnTime");
}
public bool Initialize()
{
DATA.Subscribe($"{Module}.{Name}.DeviceData", () => DeviceData);
DATA.Subscribe($"{Module}.{Name}.RingDownFaceback", () => RingDownFaceback);
DATA.Subscribe($"{Module}.{Name}.RingUpFaceback", () => RingUpFaceback);
DATA.Subscribe($"{Module}.{Name}.RingDownSetpoint", () => RingDownSetpoint);
DATA.Subscribe($"{Module}.{Name}.RingUpSetpoint", () => RingUpSetpoint);
DATA.Subscribe($"{Module}.{Name}.RingResetSetpoint", () => RingResetSetpoint);
DATA.Subscribe($"{Module}.{Name}.RingResetFaceback", () => RingResetFaceback);
OP.Subscribe($"{Module}.{Name}.SetRingDownSetpoint", (function, args) =>
{
MoveDown(out reason);
return true;
});
OP.Subscribe($"{Module}.{Name}.SetRingUpSetpoint", (function, args) =>
{
MoveUp(out reason);
return true;
});
OP.Subscribe($"{Module}.{Name}.SetRingResetSetpoint", (function, args) =>
{
MoveUp(out reason);
return true;
});
return false;
}
string reason = string.Empty;
//public bool SetRingDownSetpoint(bool falg)
//{
// if (_doRingDownSetpoint.SetValue(falg, out reason))
// {
// _doRingUpSetpoint.SetValue(!falg, out reason);
// return true;
// }
// return false;
//}
//public bool SetRingUpSetpoint(bool falg)
//{
// if (_doRingUpSetpoint.SetValue(falg, out reason))
// {
// _doRingDownSetpoint.SetValue(!falg, out reason);
// return true;
// }
// return false;
//}
public bool MoveUp(out string reason)
{
//if (!_diMotionEnable.Value)
//{
// reason = "Motion enable interlock triggered";
// return false;
//}
if (State == ConfinementRingStateEnum.Up)
{
reason = "";
return true;
}
if (!_doRingDownSetpoint.Check(false, out reason))
{
EV.PostMessage(Module, EventEnum.DefaultWarning, reason);
return false;
}
if (!_doRingUpSetpoint.Check(true, out reason))
{
EV.PostMessage(Module, EventEnum.DefaultWarning, reason);
return false;
}
if (!_doRingDownSetpoint.SetValue(false, out reason))
return false;
if (!_doRingUpSetpoint.SetValue(true, out reason))
return false;
_timer.Start(20 * 1000);
_state = DeviceState.MovingUp;
return true;
}
public bool MoveDown(out string reason)
{
//if (!_diMotionEnable.Value)
//{
// reason = "Motion enable interlock triggered";
// return false;
//}
if (State == ConfinementRingStateEnum.Down)
{
reason = "";
return true;
}
if (!_doRingDownSetpoint.Check(true, out reason))
{
EV.PostMessage(Module, EventEnum.DefaultWarning, reason);
return false;
}
if (!_doRingUpSetpoint.Check(false, out reason))
{
EV.PostMessage(Module, EventEnum.DefaultWarning, reason);
return false;
}
if (!_doRingDownSetpoint.SetValue(true, out reason))
return false;
if (!_doRingUpSetpoint.SetValue(false, out reason))
return false;
_timer.Start(20 * 1000);
_state = DeviceState.MovingDown;
return true;
}
public bool Reset(out string reason)
{
//if (!_diMotionEnable.Value)
//{
// reason = "Motion enable interlock triggered";
// return false;
//}
if (!_doRingResetSetpoint.SetValue(true, out reason))
return false;
_timer.Start(20 * 1000);
_state = DeviceState.Reset;
return true;
}
public void Monitor()
{
_trigDown.CLK = _diRingDownFaceback.Value;
if (_trigDown.Q)
{
_trigUp.RST = true;
_timerWarning.Start(_scDownWarnTime.IntValue * 1000);
}
_trigUp.CLK = _diRingUpFaceback.Value;
if (_trigUp.Q)
{
_timerWarning.Stop();
_trigDown.RST = true;
}
if (_timerWarning.IsTimeout())
{
EV.PostAlarmLog(Module,$"Confinement in down position over {_scDownWarnTime.IntValue} s");
_timerWarning.Stop();
}
switch (_state)
{
case DeviceState.MovingUp:
if (IsUp)
{
if (!_doRingUpSetpoint.SetValue(false, out string reason))
{
LOG.Error($"{Module} reset DO failed, {reason}");
}
_state = DeviceState.Idle;
}
else if (_timer.IsTimeout())
{
_timer.Stop();
if (!_doRingUpSetpoint.SetValue(false, out string reason))
{
LOG.Error($"{Module} reset DO failed, {reason}");
}
EV.PostMessage(Module, EventEnum.DefaultWarning, $"{Module} {Name} Can not move up in {20} seconds");
_state = DeviceState.Error;
}
break;
case DeviceState.MovingDown:
if (IsDown)
{
if (!_doRingDownSetpoint.SetValue(false, out string reason))
{
LOG.Error($"{Module} reset DO failed, {reason}");
}
_state = DeviceState.Idle;
}
else if (_timer.IsTimeout())
{
_timer.Stop();
if (!_doRingDownSetpoint.SetValue(false, out string reason))
{
LOG.Error($"{Module} reset DO failed, {reason}");
}
EV.PostMessage(Module, EventEnum.DefaultWarning, $"{Module} {Name} Can not move down in {20} seconds");
_state = DeviceState.Error;
}
break;
case DeviceState.Reset:
{
if (IsUp)
{
if (!_doRingResetSetpoint.SetValue(false, out string reason))
{
LOG.Error($"{Module} reset DO failed, {reason}");
}
_state = DeviceState.Idle;
}else
if (_timer.IsTimeout())
{
_timer.Stop();
if (!_doRingResetSetpoint.SetValue(false, out string reason))
{
LOG.Error($"{Module} reset DO failed, {reason}");
}
EV.PostMessage(Module, EventEnum.DefaultWarning, $"{Module} {Name} Can not Rest Alarm in {20} seconds");
_state = DeviceState.Error;
}
}
break;
default:
break;
}
//_trigError.CLK = State == ConfinementRingStateEnum.Error;
//if (_trigError.Q)
//{
// EV.PostMessage(Module, EventEnum.DefaultWarning, "Chamber Confinement status is error");
//}
}
public void Reset()
{
_trigError.RST = true;
_trigDown.RST = true;
_trigUp.RST = true;
}
public void Terminate()
{
_doRingDownSetpoint.SetValue(false, out _);
_doRingUpSetpoint.SetValue(false, out _);
}
}
}