Sic02-new/Modules/Mainframe/LLs/LoadLockBaseRoutine.cs

173 lines
4.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Aitex.Core.RT.Device;
using Aitex.Core.RT.Event;
using Aitex.Core.RT.Log;
using Aitex.Core.RT.Routine;
using MECF.Framework.Common.Equipment;
using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.LoadLocks;
using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.TMs;
using Mainframe.TMs;
namespace Mainframe.LLs
{
public class LoadLockBaseRoutine : ModuleRoutine, IRoutine
{
protected LoadLock LoadLockDevice
{
get { return _ll; }
}
protected TM TMDevice
{
get { return _tm; }
}
private LoadLock _ll = null;
private TM _tm = null;
private Stopwatch _stopWatch;
private int _maxRetryCounter = 3;
private int _retryCounter = 0;
public LoadLockBaseRoutine(ModuleName module)
{
Module = module.ToString();
_ll = DEVICE.GetDevice<SicLoadLock>($"{Module}.{Module}");
_tm = DEVICE.GetDevice<SicTM>($"{ModuleName.System}.{ModuleName.TM}");
_stopWatch = new Stopwatch();
//System.Diagnostics.Debug.Assert(_ll != null);
//System.Diagnostics.Debug.Assert(_tm != null);
}
public virtual Result Start(params object[] objs)
{
return Result.DONE;
}
public virtual Result Monitor()
{
return Result.DONE;
}
public virtual void Abort()
{
}
protected void OpenDoor(int id, LoadLock ll, bool isOpen, int timeout)
{
Tuple<bool, Result> ret = ExecuteAndWait(id, () =>
{
string name = isOpen ? "Open" : "Close";
Notify($"start {name}");
string reason;
if (!ll.SetDoor(!isOpen, out reason))
{
Stop(reason);
return false;
}
Thread.Sleep(1000);
if (!ll.SetDoor(isOpen, out reason))
{
Stop(reason);
return false;
}
_stopWatch.Restart();
_retryCounter = 1;
return true;
}, () =>
{
bool isFinished = isOpen ? ll.CheckDoorOpen() : ll.CheckDoorClose();
string name = isOpen ? "Open" : "Close";
if (!isFinished && (_retryCounter <= _maxRetryCounter)
&& _stopWatch.ElapsedMilliseconds > timeout * 1000 * _retryCounter / 4)
{
if (!ll.SetDoor(!isOpen, out string reason))
{
Stop(reason);
}
Thread.Sleep(1000);
if (!ll.SetDoor(isOpen, out reason))
{
Stop(reason);
}
LOG.Write($"Retry {name} {Module} {Name} for the {_retryCounter} / {_maxRetryCounter} time.");
_retryCounter++;
}
return isFinished;
}, timeout * 1000);
if (ret.Item1)
{
if (ret.Item2 == Result.FAIL)
{
throw (new RoutineFaildException());
}
else if (ret.Item2 == Result.TIMEOUT) //timeout
{
Stop($"can not open in {timeout} seconds");
throw (new RoutineFaildException());
}
else
throw (new RoutineBreakException());
}
}
public void VerifyAtmDoor(int id)
{
Tuple<bool, Result> ret = Execute(id, () =>
{
Notify($"verify atm door status {_ll.Name}");
if (_ll.CheckDoorOpen())
{
if (!_ll.SetDoor(true, out string reason))
{
EV.PostWarningLog(Module, $"Can not set {_ll.Name} door open, {reason}");
}
}
else if (_ll.CheckDoorClose())
{
if (!_ll.SetDoor(false, out string reason))
{
EV.PostWarningLog(Module, $"Can not set {_ll.Name} door close, {reason}");
}
}
Thread.Sleep(500);
return true;
});
if (ret.Item1)
{
throw (new RoutineBreakException());
}
}
}
}