// /************************************************************************
// * @file         BaseRoutineWithDeviceLocker.cs
// * @author   Su Liang
// * @date       2022/11/18
// *
// * @copyright &copy Sicentury Inc.
// *
// * @brief
// *
// * @details
// *
// *
// * *****************************************************************************/

using System;
using Aitex.Core.RT.Event;
using Aitex.Core.RT.Routine;
using Sicentury.Core;

namespace MECF.Framework.RT.EquipmentLibrary.Core
{
    public class BaseRoutineWithDeviceLocker : ModuleRoutine
    {
        private readonly DeviceLocker _lockerPump2;

        public BaseRoutineWithDeviceLocker(string moduleName) :base(moduleName)
        {
            _lockerPump2 = DeviceLockerManager.Instance.GetLocker(DeviceLockerManager.LockerNames.Pump2);
        }

        public void LockPump2(out string reason, int timeoutMs = 30 * 60 * 1000)
        {
            reason = "";
            EV.PostInfoLog(Module, $"[{Module}] is locking the Pump2");
            var ret = _lockerPump2?.TryLock(Module, out reason, timeoutMs);
            if (ret.HasValue == false || ret == DeviceLocker.Results.Ok ||
                ret == DeviceLocker.Results.IHaveLocker) // 不需要锁定、已锁定成功、或已占有该锁
            {
                EV.PostInfoLog(Module, $"[{Module}] locked the Pump2, Result: {ret}");
                return;
            }

            // 无法锁定设备。
            EV.PostWarningLog(Module, $"[{Module}] was unable to lock Pump2, {reason}");
            throw new Exception($"unable to lock {_lockerPump2.LockerName} by {Module}, {reason}");
        }

        public void UnlockPump2()
        {
            EV.PostInfoLog(Module, $"[{Module}] is unlocking the Pump2");

            var reason = "";
            var ret = _lockerPump2?.TryUnlock(Module, out reason);
            if (ret.HasValue == false || ret == DeviceLocker.Results.Ok) // 不需要锁定、解锁成功
            {
                EV.PostInfoLog(Module, $"[{Module}] unlock the Pump2, Result: {ret}");
                return;
            }

            EV.PostWarningLog(Module, $"[{Module}] was unable to unlock pump2, {reason}");
        }

        public void ResetLocker()
        {
            _lockerPump2?.Reset();
            EV.PostInfoLog(Module, $"[{Module}] Pump2 Locker reset");
        }

        public override void Abort()
        {
            CancelLockWaiting();
            //UnlockPump2();
        }

        /// <summary>
        /// 取消等待锁定Pump2。
        /// </summary>
        public void CancelLockWaiting()
        {
            _lockerPump2?.CancelLockWaiting();
        }
    }
}